smoke

2025-12-07 0 493

Smoke

Run Web Servers in Web Browsers over WebRTC

Example

Smoke enables Browsers run micro Web Servers over WebRTC

import { Network } from \'@sinclair/smoke\'

// ------------------------------------------------------------------
//
// Create a Virtual Network
//
// ------------------------------------------------------------------

const { Http } = new Network()

// ------------------------------------------------------------------
//
// Create a Http Listener on a Virtual Port
//
// ------------------------------------------------------------------

Http.listen({ port: 5000 }, request => new Response(\'hello webrtc\'))

// ------------------------------------------------------------------
//
// Fetch data over WebRTC
//
// ------------------------------------------------------------------

const text = Http.fetch(\'http://**loca*lhost:5000\').then(r => r.text())

Install

$ npm install @sinclair/smoke

Overview

Smoke is an experimental browser networking and storage framework that provides Http, Tcp, and WebSocket emulation over WebRTC, as well as large file storage using IndexedDB. It is designed as a foundation for developing peer-to-peer web services directly in the browser, with each browser accessible through an application-controlled virtual network.

Smoke reshapes WebRTC into standard Http compatible interfaces enabling traditional web server applications to be made portable between server and browser environments. It is developed in support of alternative software architectures where user centric services can be moved away from the cloud and run peer to peer in the browser.

Licence MIT

Contents

  • Network
    • Private
    • Public
  • Http
    • Listen
    • Fetch
    • Upgrade
    • Connect
  • Net
    • Listen
    • Connect
  • Media
    • Listen
    • Send
    • Audio
    • Video
    • Pattern
  • Proxy
    • Listen
    • Worker
  • FileSystem
    • Open
    • Stat
    • Exists
    • Mkdir
    • Readdir
    • Blob
    • Read
    • Write
    • Delete
    • Rename
    • Copy
    • Move
    • Watch
  • Contribute

Network

Smoke networking API\’s are provided by way of Network objects. A Network object represents an active connection to a shared signalling Hub and exposes the Http, Net and Media functionality used to communicate with other Network objects connected to the same Hub.

import { Network, Hubs } from \'@sinclair/smoke\'

const { Http, Net, Media, Hub } = new Network({ hub: new Hubs.Private() })

const address = await Hub.address() // The address of this Network object.

Private

A Private hub is an in-memory relay that forwards WebRTC ICE messages by way of the browser\’s BroadcastChannel API. A private hub can only relay messages to the page and other tabs running within the same browser process. Because private hubs cannot facilitate connections made outside the current page, it is considered private. This Hub is the default.

import { Network, Hubs } from \'@sinclair/smoke\'

const { Http } = new Network({ hub: new Hubs.Private() })

Public

The implementation of this hub is currently pending.

import { Network, Hubs } from \'@sinclair/smoke\'

const { Http } = new Network({ hub: new Hubs.Public(\'ws://server/hub\') })

Http

The Http API supports Http listen and fetch over WebRTC. It also provides WebSocket emulation.

const { Http } = new Network()

Listen

Use the listen function to receive Http requests from remote peers.

Http.listen({ port: 5000 }, request => new Response(\'hello\'))

Fetch

Use the fetch function to make a Http request to remote peers.

const response = await Http.fetch(\'http://**loca*lhost:5000\')

const message = await response.text()

Upgrade

Use the upgrade function to convert a Http request into a WebSocket

Http.listen({ port: 5000 }, request => Http.upgrade(request, (socket) => socket.send(\'hello\')))

Connect

Use the connect function to connect to a remote WebSocket server.

const socket = await Http.connect(\'ws://localhost:5000\')

socket.on(\'message\', (event) => console.log(event.data))

socket.on(\'error\', (event) => console.log(event))

socket.on(\'close\', (event) => console.log(event))

Net

The Net API provides Tcp emulation over RTCDataChannel

const { Net } = new Network()

Listen

Use the listen function to accept an incoming socket.

Net.listen({ port: 5000 }, async socket => {

  const data = await socket.read()

  await socket.write(data)

  await socket.close()
})

Connect

Use the connect function to establish a Net connection to a remote listener.

const socket = await Net.connect({ hostname: \'localhost\', port: 5000 })

await socket.write(new Uint8Array(1000))

const data = await socket.read() // Uint8Array()

const end = await socket.read() // null

Media

The Media API provides functionality to send and receive MediaStream objects over WebRTC.

const { Media } = new Network()

Listen

Use the listen function to listen for incoming MediaStream objects

Media.listen({ port: 6000 }, (receiver) => {
  
  const video = document.createElement(\'video\')
  
  video.srcObject = receiver.mediastream
  
  video.play()

  document.body.appendChild(video)

  receiver.on(\'close\', () => document.removeChild(video))
})

Send

Use the send function to send a MediaStream to a listener

const sender = await Media.send({ hostname: \'localhost\', port: 6000 }, new MediaStream([...]))

sender.close() // stop sending live media

Audio

Use the audio function to create a streamable AudioSource.

const audio = Media.audio({ src: \'./audio.mp3\' })

const sender = Media.send({ hostname: \'localhost\', port: 6000 }, audio.mediastream)

Video

Use the video function to create a streamable VideoSource.

const video = Media.video({ src: \'./video.mp4\' })

const sender = Media.send({ hostname: \'localhost\', port: 6000 }, video.mediastream)

Pattern

Use the pattern function to generate a MediaStream test pattern. This function can be useful for testing live media streaming without web cameras or other media sources.

const pattern = Media.pattern()

const sender = Media.send({ port: 5000 }, pattern.mediastream)

Proxy

A Smoke Proxy enables a web page to intercept outbound HTTP requests. It uses a Service Worker to redirect these requests back to the calling page, allowing the page to handle its own requests. This functionality supports both fetch requests and referenced assets embedded in HTML. Currently, the Smoke Proxy is supported only in Chromium-based browsers.

Listen

Use the listen function to intercept Http requests made to a given path.

import { Proxy } from \'@sinclair/smoke\'

Proxy.listen({ path: \'/some-path\', workerPath: \'worker.js\' }, request => {

  return new Response(\'hello world\')
})

// ...

const result = await fetch(\'/some-path/foo\').then(res => res.text())

Worker

The Proxy requires a Service Worker script to be loaded at the root path of the website. Smoke provides a prebuilt worker script that you can copy into the website\’s root directory.

# Copy this JavaScript file to the website root.

node_modules/@sinclair/smoke/worker.js

FileSystem

Smoke provides a hierarchical file system able to store large files within the browser. The file system is backed by IndexedDB and has support for streaming read and write, directory enumeration, copy, move, rename as well as file and directory watch events. It is designed to act as a static file store for network services but can be used as a general purpose file system for applications needing to store large files in the browser.

Open

Use the open function to open a file system with the given database name. If the database does not exist it is created.

import { FileSystem } from \'@sinclair/smoke\'

const Fs = await FileSystem.open(\'<database-name>\')

Stat

Use the stat function to return information about a file or directory.

const stat = await Fs.write(\'/path/file.txt\')

Exists

Use the exists function to check a path exists.

const exists = await Fs.exists(\'/path/file.txt\')

Mkdir

Use the mkdir function to create a directory.

await Fs.mkdir(\'/media/videos\')

Readdir

Use the readdir function to return stat objects for the given directory path.

const stats = await Fs.readdir(\'/media/videos\')

Blob

Use the blob function to return a Blob object to a file path.

const blob = await Fs.readdir(\'/video.mp4\')

const url = URL.createObjectUrl(blob)

Write

Use the write and writeText functions to write file content.

await Fs.write(\'/path/file.dat\', new Uint8Array([1, 2, 3, 4]))

await Fs.writeText(\'/path/file.txt\', \'hello world\')

Read

Use the read and readText functions will read content from a file.

const buffer = await fs.read(\'/path/file.dat\')

const content = await Fs.readText(\'/path/file.txt\')

Delete

Use the delete function to delete a file or directory.

await Fs.delete(\'/path/file.txt\')

Rename

Use the rename function to rename a file or directory.

await Fs.writeText(\'/path/fileA.txt\', \'...\')

await Fs.rename(\'/path/fileA.txt\', \'fileB.txt\')

Copy

Use the copy function to copy a file or directory into a target directory.

await Fs.writeText(\'/path/fileA.txt\', \'...\')

await Fs.copy(\'/path/fileA.txt\', \'/backup\')

Move

Use the move function to move a file or directory into a target directory.

await Fs.writeText(\'/path/fileA.txt\', \'...\')

await Fs.move(\'/path/fileA.txt\', \'/backup\')

Watch

Use the watch function to watch for file and directory events.

Fs.watch(\'/dir\', event => console.log(event))

Contribute

Smoke is open to community contribution. Please ensure you submit an open issue before submitting your pull request. The Smoke project prefers open community discussion before accepting new features.

下载源码

通过命令行克隆项目:

git clone https://github.com/sinclairzx81/smoke.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 smoke https://www.zuozi.net/31513.html

dada mail
上一篇: dada mail
webapplib
下一篇: webapplib
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务