@statewalker/webrun-streams-signaling 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +113 -0
- package/dist/byte-channel.d.ts +23 -0
- package/dist/byte-channel.d.ts.map +1 -0
- package/dist/byte-queue.d.ts +15 -0
- package/dist/byte-queue.d.ts.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +708 -0
- package/dist/peer-connection.d.ts +72 -0
- package/dist/peer-connection.d.ts.map +1 -0
- package/dist/peer-manager.d.ts +30 -0
- package/dist/peer-manager.d.ts.map +1 -0
- package/dist/qr-signaling.d.ts +60 -0
- package/dist/qr-signaling.d.ts.map +1 -0
- package/dist/room-manager.d.ts +32 -0
- package/dist/room-manager.d.ts.map +1 -0
- package/dist/types.d.ts +159 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +53 -0
- package/src/byte-channel.ts +99 -0
- package/src/byte-queue.ts +68 -0
- package/src/index.ts +30 -0
- package/src/peer-connection.ts +355 -0
- package/src/peer-manager.ts +74 -0
- package/src/qr-signaling.ts +227 -0
- package/src/room-manager.ts +114 -0
- package/src/types.ts +177 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2026 statewalker
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# @statewalker/webrun-streams-signaling
|
|
2
|
+
|
|
3
|
+
Generic P2P signaling / connection-setup helpers — `PeerManager`, `QrSignaling`, `RoomManager` — that each yield a webrun-streams `ByteChannel`.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Signaling here is **transport-setup only**: it runs the handshake (WebRTC offer/answer/ICE, or LiveKit room membership) that *establishes* a peer connection, then hands back an established `ByteChannel`. The byte transport and multiplexing on top of that channel is `@statewalker/webrun-streams`. These helpers were relocated out of the retired `@statewalker/vcs-port-*` packages and neutralised — nothing here is Git/VCS-specific — so a generic P2P concern lives in the webrun ecosystem. Vendor libraries (`livekit-client`) are optional peer-deps injected via a factory; native WebRTC is likewise injectable, so tests need no vendor install.
|
|
8
|
+
|
|
9
|
+
**Out of scope:** the byte transport / multiplexing (`@statewalker/webrun-streams`), and the signaling *server* (STUN/TURN/rendezvous) — a deployment concern; these are clients.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pnpm add @statewalker/webrun-streams-signaling
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
`PeerManager` dials a peer over a `SignalingTransport` (your websocket / rendezvous side-channel) and resolves with a `ByteChannel`:
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { PeerManager } from "@statewalker/webrun-streams-signaling";
|
|
23
|
+
|
|
24
|
+
// `signaling` is your SignalingTransport (localId + send + onMessage).
|
|
25
|
+
// In production `PeerConnection` defaults to `new RTCPeerConnection(config)`;
|
|
26
|
+
// pass `{ rtc }` only to inject a factory (tests, or a non-browser stack).
|
|
27
|
+
const a = new PeerManager(signalingA);
|
|
28
|
+
const b = new PeerManager(signalingB);
|
|
29
|
+
|
|
30
|
+
b.onConnection((peerId, ch) => {
|
|
31
|
+
ch.send(new Uint8Array([1, 2, 3]));
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const ch = await a.connect("B"); // Promise<ByteChannel>
|
|
35
|
+
ch.send(new Uint8Array([9, 8]));
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`QrSignaling` pairs two devices serverless, exchanging compact QR strings:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { QrSignaling } from "@statewalker/webrun-streams-signaling";
|
|
42
|
+
|
|
43
|
+
const deviceA = new QrSignaling();
|
|
44
|
+
const deviceB = new QrSignaling();
|
|
45
|
+
|
|
46
|
+
const { qr: offerQr, accept } = await deviceA.offer(); // display offerQr
|
|
47
|
+
const { qr: answerQr, channel: chB } = await deviceB.answer(offerQr); // scan + reply
|
|
48
|
+
const chA = await accept(answerQr); // both sides now have a ByteChannel
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`RoomManager` joins a LiveKit-style room and opens a `ByteChannel` per participant:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { RoomManager } from "@statewalker/webrun-streams-signaling";
|
|
55
|
+
import { Room } from "livekit-client";
|
|
56
|
+
|
|
57
|
+
const room = new RoomManager({
|
|
58
|
+
url: "wss://my-livekit-host",
|
|
59
|
+
getToken: (name) => fetchToken(name),
|
|
60
|
+
roomFactory: () => new Room(),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
await room.join("room1");
|
|
64
|
+
for await (const peerId of room.participants()) {
|
|
65
|
+
const ch = await room.channelTo(peerId);
|
|
66
|
+
ch.send(new Uint8Array([5, 5]));
|
|
67
|
+
}
|
|
68
|
+
await room.leave();
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## API
|
|
72
|
+
|
|
73
|
+
### Managers
|
|
74
|
+
|
|
75
|
+
- **`class PeerManager`** — `new PeerManager(signaling: SignalingTransport, options?: PeerManagerOptions)`. WebRTC peer discovery + connection over a signaling transport.
|
|
76
|
+
- `connect(peerId: string): Promise<ByteChannel>` — dial a peer; resolves with the established channel.
|
|
77
|
+
- `onConnection(handler: (peerId: string, ch: ByteChannel) => void): () => void` — accept inbound dials; returns an unsubscribe fn.
|
|
78
|
+
- `close(): void` — tear down all connections and stop listening.
|
|
79
|
+
- **`class QrSignaling`** — `new QrSignaling(options?: QrSignalingOptions)`. Serverless offer/answer exchange via QR strings.
|
|
80
|
+
- `offer(): Promise<{ qr: string; accept(answerQr: string): Promise<ByteChannel> }>` — create an offer QR; `accept` finishes the handshake.
|
|
81
|
+
- `answer(offerQr: string): Promise<{ qr: string; channel: ByteChannel }>` — consume an offer QR, produce an answer QR + the channel.
|
|
82
|
+
- `getSessionId(): string`.
|
|
83
|
+
- **`class RoomManager`** — `new RoomManager(options: RoomManagerOptions)`. LiveKit room membership + per-participant channels.
|
|
84
|
+
- `join(room: string): Promise<void>`, `participants(): AsyncIterable<string>`, `channelTo(peerId: string): Promise<ByteChannel>`, `leave(): Promise<void>`.
|
|
85
|
+
|
|
86
|
+
### ByteChannel adapters
|
|
87
|
+
|
|
88
|
+
- **`byteChannelFromDataChannel(channel: RTCDataChannel): ByteChannel`** — wrap an established `RTCDataChannel`.
|
|
89
|
+
- **`byteChannelFromRoom(room: RoomLike, peerId: string): ByteChannel`** — per-participant channel over a LiveKit-style `Room`.
|
|
90
|
+
|
|
91
|
+
### Low-level
|
|
92
|
+
|
|
93
|
+
- **`class PeerConnection`** — single WebRTC connection lifecycle (offer/answer/ICE) that `PeerManager` and `QrSignaling` build on.
|
|
94
|
+
|
|
95
|
+
### QR pure functions
|
|
96
|
+
|
|
97
|
+
- **`generateSessionId(): string`** — short random session id.
|
|
98
|
+
- **`createCompressedSignal(sessionId, role, description, candidates): CompressedSignal`** — build a compact QR payload from SDP + ICE.
|
|
99
|
+
- **`parseCompressedSignal(signal): { sessionId, role, description, candidates }`** — reverse of the above.
|
|
100
|
+
- **`encodeSignal(signal): string`** / **`decodeSignal(encoded): CompressedSignal`** — URL-safe base64 (de)serialisation for QR strings.
|
|
101
|
+
|
|
102
|
+
Types (`ByteChannel`-adjacent handshake shapes) are re-exported from `./types` — `SignalingTransport`, `SignalingMessage`, `RoomLike`, `RoomManagerOptions`, `WebRtcConnectionOptions`, `CompressedSignal`, `DEFAULT_ICE_SERVERS`, and more.
|
|
103
|
+
|
|
104
|
+
## Notes
|
|
105
|
+
|
|
106
|
+
- Each helper yields a webrun-streams **`ByteChannel`** (`send` / `recv` / `closed` / `close`), not a `MessagePort` — the byte transport and `emulateMux` run on top via `@statewalker/webrun-streams`.
|
|
107
|
+
- **Vendor coupling stays behind optional peer-deps.** `livekit-client` is injected via `RoomManagerOptions.roomFactory` (the module keeps only a structural `RoomLike` view), and native WebRTC is injected via the optional `rtc` factory (defaulting to `new RTCPeerConnection`). So the tests use in-memory mocks and require no vendor install.
|
|
108
|
+
- **`peerjs` is not a dependency.** These helpers use native WebRTC; the peerjs byte transport lives in `@statewalker/webrun-streams-peerjs`.
|
|
109
|
+
- Built red/green TDD.
|
|
110
|
+
|
|
111
|
+
## License
|
|
112
|
+
|
|
113
|
+
MIT
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ByteChannel adapters — the MessagePort→ByteChannel-style shims the relocation
|
|
3
|
+
* requires. The original `vcs-port-*` helpers surfaced an established
|
|
4
|
+
* connection as a Web `MessagePort` (via the out-of-scope `datachannel-port` /
|
|
5
|
+
* `livekit-port` byte bridges); here the established primitive is wrapped
|
|
6
|
+
* *directly* as a webrun-streams {@link ByteChannel} so the byte transport +
|
|
7
|
+
* multiplexing (`emulateMux`) can run on top.
|
|
8
|
+
*/
|
|
9
|
+
import type { ByteChannel } from "@statewalker/webrun-streams";
|
|
10
|
+
import type { RoomLike } from "./types.js";
|
|
11
|
+
/**
|
|
12
|
+
* Wrap an established `RTCDataChannel` as a {@link ByteChannel}. Outbound bytes
|
|
13
|
+
* go through `channel.send`; inbound `message` events feed `recv`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function byteChannelFromDataChannel(channel: RTCDataChannel): ByteChannel;
|
|
16
|
+
/**
|
|
17
|
+
* Build a {@link ByteChannel} to a single room participant over a LiveKit-style
|
|
18
|
+
* `Room`. Outbound bytes are published to `peerId`; inbound `dataReceived`
|
|
19
|
+
* events from `peerId` feed `recv`. This is the per-participant filtering the
|
|
20
|
+
* retired `vcs-port-livekit` `livekit-port` performed, adapted to ByteChannel.
|
|
21
|
+
*/
|
|
22
|
+
export declare function byteChannelFromRoom(room: RoomLike, peerId: string): ByteChannel;
|
|
23
|
+
//# sourceMappingURL=byte-channel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"byte-channel.d.ts","sourceRoot":"","sources":["../src/byte-channel.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAC;AAE/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,cAAc,GAAG,WAAW,CAyC/E;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,CAgC/E"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal eager push/pull byte queue backing the `recv` side of the
|
|
3
|
+
* {@link ByteChannel} adapters. "Eager" = bytes pushed before the consumer
|
|
4
|
+
* starts iterating are buffered (not dropped), which the signaling adapters
|
|
5
|
+
* need because a channel can receive data the instant it opens, before the
|
|
6
|
+
* caller wires up its `for await`.
|
|
7
|
+
*/
|
|
8
|
+
export declare function makeByteQueue(): {
|
|
9
|
+
recv: AsyncIterable<Uint8Array>;
|
|
10
|
+
push: (bytes: Uint8Array) => void;
|
|
11
|
+
done: () => void;
|
|
12
|
+
};
|
|
13
|
+
/** Coerce a message payload (Uint8Array / ArrayBuffer / view) to a Uint8Array copy. */
|
|
14
|
+
export declare function toBytes(data: unknown): Uint8Array;
|
|
15
|
+
//# sourceMappingURL=byte-queue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"byte-queue.d.ts","sourceRoot":"","sources":["../src/byte-queue.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,aAAa,IAAI;IAC/B,IAAI,EAAE,aAAa,CAAC,UAAU,CAAC,CAAC;IAChC,IAAI,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,CAAC;IAClC,IAAI,EAAE,MAAM,IAAI,CAAC;CAClB,CA6CA;AAED,uFAAuF;AACvF,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,CAQjD"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @statewalker/webrun-streams-signaling
|
|
3
|
+
*
|
|
4
|
+
* Generic P2P signaling / connection-setup helpers, relocated from the retiring
|
|
5
|
+
* `@statewalker/vcs-port-{webrtc,livekit}` packages and adapted to yield
|
|
6
|
+
* webrun-streams {@link ByteChannel}s. Signaling here is *transport-setup only*:
|
|
7
|
+
* it establishes a peer connection and hands back an established channel; the
|
|
8
|
+
* byte transport + multiplexing is `@statewalker/webrun-streams`.
|
|
9
|
+
*
|
|
10
|
+
* - {@link PeerManager} — WebRTC peer discovery + connection over a signaling transport.
|
|
11
|
+
* - {@link QrSignaling} — serverless offer/answer exchange via QR strings.
|
|
12
|
+
* - {@link RoomManager} — LiveKit room membership + per-participant channels.
|
|
13
|
+
*
|
|
14
|
+
* @packageDocumentation
|
|
15
|
+
*/
|
|
16
|
+
export { byteChannelFromDataChannel, byteChannelFromRoom } from "./byte-channel.js";
|
|
17
|
+
export { PeerConnection } from "./peer-connection.js";
|
|
18
|
+
export { PeerManager, type PeerManagerOptions } from "./peer-manager.js";
|
|
19
|
+
export { createCompressedSignal, decodeSignal, encodeSignal, generateSessionId, parseCompressedSignal, QrSignaling, type QrSignalingOptions, } from "./qr-signaling.js";
|
|
20
|
+
export { RoomManager } from "./room-manager.js";
|
|
21
|
+
export * from "./types.js";
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,0BAA0B,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACpF,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACzE,OAAO,EACL,sBAAsB,EACtB,YAAY,EACZ,YAAY,EACZ,iBAAiB,EACjB,qBAAqB,EACrB,WAAW,EACX,KAAK,kBAAkB,GACxB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,cAAc,YAAY,CAAC"}
|