@statewalker/webrun-streams-signaling 0.1.6 → 0.2.0
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/README.md +29 -14
- package/dist/byte-channel.d.ts +3 -4
- package/dist/byte-channel.d.ts.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +150 -8
- package/dist/message-target.d.ts +48 -0
- package/dist/message-target.d.ts.map +1 -0
- package/dist/peer-connection.d.ts +2 -3
- package/dist/peer-connection.d.ts.map +1 -1
- package/dist/peer-manager.d.ts +2 -3
- package/dist/peer-manager.d.ts.map +1 -1
- package/dist/qr-signaling.d.ts +2 -3
- package/dist/qr-signaling.d.ts.map +1 -1
- package/dist/room-manager.d.ts +2 -3
- package/dist/room-manager.d.ts.map +1 -1
- package/dist/types.d.ts +15 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/byte-channel.ts +27 -13
- package/src/index.ts +3 -2
- package/src/message-target.ts +112 -0
- package/src/peer-connection.ts +2 -2
- package/src/peer-manager.ts +7 -3
- package/src/qr-signaling.ts +2 -2
- package/src/room-manager.ts +2 -3
- package/src/types.ts +16 -2
package/README.md
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
## What it is
|
|
4
4
|
|
|
5
5
|
Connection-setup helpers for peer-to-peer links. Each one runs a handshake and hands back an
|
|
6
|
-
established
|
|
6
|
+
established `ByteChannel` (`send` / `recv` / `closed` / `close`, declared by this package).
|
|
7
7
|
`PeerManager` runs WebRTC offer/answer/ICE over a signaling transport you supply; `QrSignaling`
|
|
8
8
|
pairs two devices without a server by exchanging compact QR strings; `RoomManager` joins a
|
|
9
9
|
LiveKit-style room and opens a channel per participant. What runs on the channel afterwards —
|
|
10
|
-
typically `webrun-
|
|
10
|
+
typically `webrun-rpc`'s port stack, through `messageTargetOverByteChannel` — is up to you.
|
|
11
11
|
|
|
12
12
|
## Why it exists
|
|
13
13
|
|
|
@@ -43,7 +43,9 @@ pass an `rtc` factory from a WebRTC implementation.
|
|
|
43
43
|
| `new RoomManager(options: RoomManagerOptions)` | Room membership. `join(room)`, `participants(): AsyncIterable<string>`, `channelTo(peerId): Promise<ByteChannel>`, `leave()`. |
|
|
44
44
|
| `new PeerConnection(role, options?)` | One WebRTC connection's lifecycle, which `PeerManager` and `QrSignaling` build on. `connect()`, `handleSignal(msg)`, `open(): Promise<ByteChannel>`, `waitForIceGathering()`, `getLocalDescription()`, `getCollectedCandidates()`, `getState()`, `getRole()`, `getDataChannel()`, `on` / `off`, `close()`. |
|
|
45
45
|
| `byteChannelFromDataChannel(dc)` | Wrap an `RTCDataChannel` as a `ByteChannel`. |
|
|
46
|
-
| `byteChannelFromRoom(room, peerId)` | A `ByteChannel` to one participant of a `RoomLike`. |
|
|
46
|
+
| `byteChannelFromRoom(room, peerId)` | A `ByteChannel` to one participant of a `RoomLike`. It closes when the room disconnects, the participant leaves, or a reliable publish fails, so whatever runs on it learns the other end is gone. |
|
|
47
|
+
| `messageTargetOverByteChannel(channel, options?)` | A `ByteChannel` as `{ target, signal, maxMessageSize }` for `webrun-rpc`'s `overPipe`: a `MessageTarget` of bytes, a signal aborted when the channel closes, and the payload ceiling (`DEFAULT_MAX_MESSAGE_SIZE`, 16 KiB − 256, what every browser pair delivers over a data channel; pass `maxMessageSize: 12 * 1024` for a `byteChannelFromRoom` channel). It must be the channel's only reader. |
|
|
48
|
+
| `ByteChannel` | The channel type. It used to come from `@statewalker/webrun-streams`; it is declared here since `emulateMux` was removed. |
|
|
47
49
|
| `generateSessionId()`, `createCompressedSignal(...)`, `parseCompressedSignal(signal)`, `encodeSignal(signal)`, `decodeSignal(text)` | The pure QR payload functions. |
|
|
48
50
|
|
|
49
51
|
Types and constants: `SignalingTransport` (`localId`, `send(to, msg)`,
|
|
@@ -75,8 +77,10 @@ The `SignalingTransport` below is an in-memory bus, so both peers run in one pag
|
|
|
75
77
|
application it wraps your WebSocket or other rendezvous channel.
|
|
76
78
|
|
|
77
79
|
```ts
|
|
78
|
-
import {
|
|
80
|
+
import { msgpackCodec } from "@statewalker/webrun-msgpack";
|
|
81
|
+
import { connect, overPipe, serve } from "@statewalker/webrun-rpc";
|
|
79
82
|
import {
|
|
83
|
+
messageTargetOverByteChannel,
|
|
80
84
|
PeerManager,
|
|
81
85
|
type SignalingMessage,
|
|
82
86
|
type SignalingTransport,
|
|
@@ -99,17 +103,28 @@ const a = new PeerManager(bus("A"));
|
|
|
99
103
|
const b = new PeerManager(bus("B"));
|
|
100
104
|
|
|
101
105
|
b.onConnection((peerId, channel) => {
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
+
const { target, signal, maxMessageSize } = messageTargetOverByteChannel(channel);
|
|
107
|
+
void serve(
|
|
108
|
+
{ mux: overPipe(target, { codec: msgpackCodec, side: "responder", signal, maxMessageSize }) },
|
|
109
|
+
async function* echo(input) {
|
|
110
|
+
for await (const chunk of input) yield chunk;
|
|
111
|
+
},
|
|
112
|
+
);
|
|
106
113
|
});
|
|
107
114
|
|
|
108
|
-
const
|
|
109
|
-
const
|
|
110
|
-
|
|
115
|
+
const pipe = messageTargetOverByteChannel(await a.connect("B"));
|
|
116
|
+
const { call, close } = await connect({
|
|
117
|
+
mux: overPipe(pipe.target, {
|
|
118
|
+
codec: msgpackCodec,
|
|
119
|
+
side: "initiator",
|
|
120
|
+
signal: pipe.signal,
|
|
121
|
+
maxMessageSize: pipe.maxMessageSize,
|
|
122
|
+
}),
|
|
123
|
+
});
|
|
124
|
+
for await (const chunk of call([new TextEncoder().encode("ping")])) {
|
|
111
125
|
console.log(new TextDecoder().decode(chunk)); // "ping"
|
|
112
126
|
}
|
|
127
|
+
await close();
|
|
113
128
|
|
|
114
129
|
a.close();
|
|
115
130
|
b.close();
|
|
@@ -167,7 +182,7 @@ for await (const peerId of rooms.participants()) {
|
|
|
167
182
|
ByteChannel ── send / recv / closed / close
|
|
168
183
|
│
|
|
169
184
|
▼
|
|
170
|
-
|
|
185
|
+
the port stack (messageTargetOverByteChannel) or any byte protocol (you run)
|
|
171
186
|
```
|
|
172
187
|
|
|
173
188
|
### How `PeerManager` routes signals
|
|
@@ -237,8 +252,8 @@ that joins, and ends at `leave()`. Departures are not reported. Only the most re
|
|
|
237
252
|
|
|
238
253
|
### Dependencies
|
|
239
254
|
|
|
240
|
-
Zero runtime
|
|
241
|
-
|
|
255
|
+
Zero runtime dependencies: the bundle imports nothing, and `ByteChannel` is declared here.
|
|
256
|
+
`@statewalker/webrun-rpc`, `-msgpack` and `-streams` are dev dependencies of the tests. `livekit-client` is an optional peer, used only through the
|
|
242
257
|
`RoomLike` shape you inject. A neutrality test (`tests/neutrality.test.ts`) keeps the source from
|
|
243
258
|
importing anything else.
|
|
244
259
|
|
package/dist/byte-channel.d.ts
CHANGED
|
@@ -3,11 +3,10 @@
|
|
|
3
3
|
* requires. The original `vcs-port-*` helpers surfaced an established
|
|
4
4
|
* connection as a Web `MessagePort` (via the out-of-scope `datachannel-port` /
|
|
5
5
|
* `livekit-port` byte bridges); here the established primitive is wrapped
|
|
6
|
-
* *directly* as a
|
|
7
|
-
* multiplexing (`
|
|
6
|
+
* *directly* as a {@link ByteChannel} so the byte transport +
|
|
7
|
+
* multiplexing (`webrun-rpc`'s port stack, via `messageTargetOverByteChannel`) can run on top.
|
|
8
8
|
*/
|
|
9
|
-
import type { ByteChannel } from "
|
|
10
|
-
import type { RoomLike } from "./types.js";
|
|
9
|
+
import type { ByteChannel, RoomLike } from "./types.js";
|
|
11
10
|
/**
|
|
12
11
|
* Wrap an established `RTCDataChannel` as a {@link ByteChannel}. Outbound bytes
|
|
13
12
|
* go through `channel.send`; inbound `message` events feed `recv`.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"byte-channel.d.ts","sourceRoot":"","sources":["../src/byte-channel.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;
|
|
1
|
+
{"version":3,"file":"byte-channel.d.ts","sourceRoot":"","sources":["../src/byte-channel.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGxD;;;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,CA8C/E"}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Generic P2P signaling / connection-setup helpers, relocated from the retiring
|
|
5
5
|
* `@statewalker/vcs-port-{webrtc,livekit}` packages and adapted to yield
|
|
6
|
-
*
|
|
6
|
+
* {@link ByteChannel}s. Signaling here is *transport-setup only*:
|
|
7
7
|
* it establishes a peer connection and hands back an established channel; the
|
|
8
|
-
* byte transport + multiplexing is `@statewalker/webrun-
|
|
8
|
+
* byte transport + multiplexing is `@statewalker/webrun-rpc`'s port stack.
|
|
9
9
|
*
|
|
10
10
|
* - {@link PeerManager} — WebRTC peer discovery + connection over a signaling transport.
|
|
11
11
|
* - {@link QrSignaling} — serverless offer/answer exchange via QR strings.
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
* @packageDocumentation
|
|
15
15
|
*/
|
|
16
16
|
export { byteChannelFromDataChannel, byteChannelFromRoom } from "./byte-channel.js";
|
|
17
|
+
export * from "./message-target.js";
|
|
17
18
|
export { PeerConnection } from "./peer-connection.js";
|
|
18
19
|
export { PeerManager, type PeerManagerOptions } from "./peer-manager.js";
|
|
19
20
|
export { createCompressedSignal, decodeSignal, encodeSignal, generateSessionId, parseCompressedSignal, QrSignaling, type QrSignalingOptions, } from "./qr-signaling.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +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"}
|
|
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,cAAc,qBAAqB,CAAC;AACpC,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"}
|
package/dist/index.js
CHANGED
|
@@ -71,11 +71,20 @@ const DEFAULT_ICE_SERVERS = [{ urls: "stun:stun.l.google.com:19302" }, { urls: "
|
|
|
71
71
|
const ROOM_EVENT = {
|
|
72
72
|
participantConnected: "participantConnected",
|
|
73
73
|
participantDisconnected: "participantDisconnected",
|
|
74
|
+
disconnected: "disconnected",
|
|
74
75
|
dataReceived: "dataReceived"
|
|
75
76
|
};
|
|
76
77
|
//#endregion
|
|
77
78
|
//#region src/byte-channel.ts
|
|
78
79
|
/**
|
|
80
|
+
* ByteChannel adapters — the MessagePort→ByteChannel-style shims the relocation
|
|
81
|
+
* requires. The original `vcs-port-*` helpers surfaced an established
|
|
82
|
+
* connection as a Web `MessagePort` (via the out-of-scope `datachannel-port` /
|
|
83
|
+
* `livekit-port` byte bridges); here the established primitive is wrapped
|
|
84
|
+
* *directly* as a {@link ByteChannel} so the byte transport +
|
|
85
|
+
* multiplexing (`webrun-rpc`'s port stack, via `messageTargetOverByteChannel`) can run on top.
|
|
86
|
+
*/
|
|
87
|
+
/**
|
|
79
88
|
* Wrap an established `RTCDataChannel` as a {@link ByteChannel}. Outbound bytes
|
|
80
89
|
* go through `channel.send`; inbound `message` events feed `recv`.
|
|
81
90
|
*/
|
|
@@ -124,31 +133,130 @@ function byteChannelFromRoom(room, peerId) {
|
|
|
124
133
|
const closed = new Promise((r) => {
|
|
125
134
|
closedResolve = r;
|
|
126
135
|
});
|
|
136
|
+
let ended = false;
|
|
127
137
|
const onData = (...args) => {
|
|
128
138
|
const payload = args[0];
|
|
129
|
-
|
|
130
|
-
if (participant && participant.identity !== peerId) return;
|
|
139
|
+
if (args[1]?.identity !== peerId) return;
|
|
131
140
|
if (payload != null) queue.push(toBytes(payload));
|
|
132
141
|
};
|
|
142
|
+
const onParticipantDisconnected = (...args) => {
|
|
143
|
+
if (args[0]?.identity === peerId) end();
|
|
144
|
+
};
|
|
145
|
+
const end = () => {
|
|
146
|
+
if (ended) return;
|
|
147
|
+
ended = true;
|
|
148
|
+
room.off(ROOM_EVENT.dataReceived, onData);
|
|
149
|
+
room.off(ROOM_EVENT.participantDisconnected, onParticipantDisconnected);
|
|
150
|
+
room.off(ROOM_EVENT.disconnected, end);
|
|
151
|
+
queue.done();
|
|
152
|
+
closedResolve();
|
|
153
|
+
};
|
|
133
154
|
room.on(ROOM_EVENT.dataReceived, onData);
|
|
155
|
+
room.on(ROOM_EVENT.participantDisconnected, onParticipantDisconnected);
|
|
156
|
+
room.on(ROOM_EVENT.disconnected, end);
|
|
134
157
|
return {
|
|
135
158
|
send(bytes) {
|
|
159
|
+
if (ended) return;
|
|
136
160
|
room.localParticipant.publishData(bytes, {
|
|
137
161
|
reliable: true,
|
|
138
162
|
destinationIdentities: [peerId]
|
|
139
|
-
}).catch(
|
|
163
|
+
}).catch(end);
|
|
140
164
|
},
|
|
141
165
|
recv: queue.recv,
|
|
142
166
|
closed,
|
|
143
|
-
close
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
167
|
+
close: end
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/message-target.ts
|
|
172
|
+
/**
|
|
173
|
+
* The payload ceiling for a channel over an `RTCDataChannel` (`PeerManager`,
|
|
174
|
+
* `QrSignaling`, `PeerConnection`): 16 KiB, the message size every browser
|
|
175
|
+
* pair delivers, less the 256 bytes the port stack's framing needs (spec D10).
|
|
176
|
+
* For a `byteChannelFromRoom` channel pass `maxMessageSize: 12 * 1024`, as
|
|
177
|
+
* `webrun-streams-livekit` uses: an older LiveKit client dropped larger
|
|
178
|
+
* reliable packets silently.
|
|
179
|
+
*/
|
|
180
|
+
const DEFAULT_MAX_MESSAGE_SIZE = 16128;
|
|
181
|
+
/**
|
|
182
|
+
* A `ByteChannel` this package hands back (`PeerManager`, `QrSignaling`,
|
|
183
|
+
* `RoomManager`) as a pipe for `webrun-rpc`'s port stack:
|
|
184
|
+
*
|
|
185
|
+
* ```ts
|
|
186
|
+
* const { target, signal, maxMessageSize } = messageTargetOverByteChannel(channel);
|
|
187
|
+
* const { call } = await connect({
|
|
188
|
+
* mux: overPipe(target, { codec: msgpackCodec, side: "initiator", signal, maxMessageSize }),
|
|
189
|
+
* });
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* It starts reading the channel at once, so attach listeners in the same tick;
|
|
193
|
+
* a message with no listener is dropped, as on a `MessagePort`. It must be the
|
|
194
|
+
* channel's only reader: a second reader of `recv` would take messages from it.
|
|
195
|
+
* Messages already received when the channel closes are still delivered.
|
|
196
|
+
*/
|
|
197
|
+
function messageTargetOverByteChannel(channel, options = {}) {
|
|
198
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
199
|
+
const life = new AbortController();
|
|
200
|
+
let closedHere = false;
|
|
201
|
+
const end = (reason) => {
|
|
202
|
+
if (!life.signal.aborted) life.abort(reason);
|
|
203
|
+
};
|
|
204
|
+
channel.closed.then(() => end(/* @__PURE__ */ new Error("webrun-streams-signaling: the channel closed")));
|
|
205
|
+
(async () => {
|
|
206
|
+
for await (const bytes of channel.recv) {
|
|
207
|
+
if (closedHere) break;
|
|
208
|
+
const event = new MessageEvent("message", { data: bytes.slice() });
|
|
209
|
+
for (const listener of [...listeners]) try {
|
|
210
|
+
Promise.resolve(listener(event)).catch(() => void 0);
|
|
211
|
+
} catch {}
|
|
212
|
+
}
|
|
213
|
+
end(/* @__PURE__ */ new Error("webrun-streams-signaling: the channel closed"));
|
|
214
|
+
})().catch((err) => end(err instanceof Error ? err : new Error(String(err), { cause: err })));
|
|
215
|
+
return {
|
|
216
|
+
signal: life.signal,
|
|
217
|
+
maxMessageSize: options.maxMessageSize ?? 16128,
|
|
218
|
+
target: {
|
|
219
|
+
addEventListener: (_type, listener) => {
|
|
220
|
+
listeners.add(listener);
|
|
221
|
+
},
|
|
222
|
+
removeEventListener: (_type, listener) => {
|
|
223
|
+
listeners.delete(listener);
|
|
224
|
+
},
|
|
225
|
+
postMessage(message) {
|
|
226
|
+
const bytes = message instanceof Uint8Array ? message : message instanceof ArrayBuffer ? new Uint8Array(message) : ArrayBuffer.isView(message) ? new Uint8Array(message.buffer, message.byteOffset, message.byteLength) : void 0;
|
|
227
|
+
if (!bytes) throw new TypeError(`webrun-streams-signaling: a byte pipe carries Uint8Array, ArrayBuffer or a view, got ${Object.prototype.toString.call(message)}`);
|
|
228
|
+
if (life.signal.aborted) return;
|
|
229
|
+
channel.send(bytes.slice());
|
|
230
|
+
},
|
|
231
|
+
async close() {
|
|
232
|
+
listeners.clear();
|
|
233
|
+
closedHere = true;
|
|
234
|
+
end(/* @__PURE__ */ new Error("webrun-streams-signaling: closed locally"));
|
|
235
|
+
try {
|
|
236
|
+
channel.close();
|
|
237
|
+
} catch {}
|
|
238
|
+
}
|
|
147
239
|
}
|
|
148
240
|
};
|
|
149
241
|
}
|
|
150
242
|
//#endregion
|
|
151
243
|
//#region src/peer-connection.ts
|
|
244
|
+
/**
|
|
245
|
+
* WebRTC peer connection lifecycle (single connection).
|
|
246
|
+
*
|
|
247
|
+
* Relocated from `@statewalker/vcs-port-webrtc`'s `PeerManager` and neutralised
|
|
248
|
+
* (no Git/VCS coupling). Handles signaling, ICE candidate gathering and data
|
|
249
|
+
* channel creation for one peer, in either "initiator" (creates offer) or
|
|
250
|
+
* "responder" (creates answer) role.
|
|
251
|
+
*
|
|
252
|
+
* Adaptations vs. the original:
|
|
253
|
+
* - `new RTCPeerConnection(...)` is behind an injectable {@link RtcPeerConnectionFactory}
|
|
254
|
+
* so tests run without a real WebRTC stack.
|
|
255
|
+
* - remote ICE candidates are added as plain `RTCIceCandidateInit` (accepted by
|
|
256
|
+
* `addIceCandidate`) instead of constructing a global `RTCIceCandidate`,
|
|
257
|
+
* which does not exist outside a browser.
|
|
258
|
+
* - `open()` yields a {@link ByteChannel} rather than a `MessagePort`.
|
|
259
|
+
*/
|
|
152
260
|
const DEFAULT_CONNECTION_TIMEOUT = 3e4;
|
|
153
261
|
const DEFAULT_ICE_GATHERING_TIMEOUT = 5e3;
|
|
154
262
|
const DEFAULT_CHANNEL_LABEL = "webrun-data";
|
|
@@ -418,6 +526,16 @@ var PeerConnection = class {
|
|
|
418
526
|
};
|
|
419
527
|
//#endregion
|
|
420
528
|
//#region src/peer-manager.ts
|
|
529
|
+
/**
|
|
530
|
+
* Peer discovery + connection manager over a {@link SignalingTransport}.
|
|
531
|
+
*
|
|
532
|
+
* Wraps the low-level {@link PeerConnection} (relocated from
|
|
533
|
+
* `@statewalker/vcs-port-webrtc`) with a multi-peer, id-keyed surface: dial a
|
|
534
|
+
* peer with `connect(peerId)` or accept inbound dials via `onConnection`. Each
|
|
535
|
+
* established connection yields a {@link ByteChannel}. The WebRTC
|
|
536
|
+
* offer/answer/ICE routing is the original logic; only the routing-by-peer-id
|
|
537
|
+
* and the ByteChannel result are new.
|
|
538
|
+
*/
|
|
421
539
|
var PeerManager = class {
|
|
422
540
|
signaling;
|
|
423
541
|
options;
|
|
@@ -469,6 +587,18 @@ var PeerManager = class {
|
|
|
469
587
|
};
|
|
470
588
|
//#endregion
|
|
471
589
|
//#region src/qr-signaling.ts
|
|
590
|
+
/**
|
|
591
|
+
* QR code signaling for serverless WebRTC connection.
|
|
592
|
+
*
|
|
593
|
+
* Relocated from `@statewalker/vcs-port-webrtc`'s `signaling.ts` (neutralised).
|
|
594
|
+
* Compresses WebRTC signaling data (SDP + ICE candidates) into a compact,
|
|
595
|
+
* base64 form suitable for QR codes, enabling out-of-band manual pairing where
|
|
596
|
+
* peers exchange QR strings instead of using a live signaling server.
|
|
597
|
+
*
|
|
598
|
+
* The compress/expand/encode/decode functions are pure and unchanged; the
|
|
599
|
+
* {@link QrSignaling} class is adapted to the target API and yields
|
|
600
|
+
* {@link ByteChannel}s.
|
|
601
|
+
*/
|
|
472
602
|
/** Current protocol version */
|
|
473
603
|
const PROTOCOL_VERSION = 1;
|
|
474
604
|
/** Generate a short random session ID. */
|
|
@@ -617,6 +747,18 @@ var QrSignaling = class {
|
|
|
617
747
|
};
|
|
618
748
|
//#endregion
|
|
619
749
|
//#region src/room-manager.ts
|
|
750
|
+
/**
|
|
751
|
+
* Room membership manager over a LiveKit-style `Room`.
|
|
752
|
+
*
|
|
753
|
+
* Relocated from `@statewalker/vcs-port-livekit`'s `RoomManager` and adapted to
|
|
754
|
+
* the target API: `join`/`participants`/`channelTo`/`leave`. Participant
|
|
755
|
+
* tracking is the original event-wiring; `channelTo` yields a per-participant
|
|
756
|
+
* {@link ByteChannel} (see {@link byteChannelFromRoom}).
|
|
757
|
+
*
|
|
758
|
+
* The vendor (`livekit-client`) stays an optional peerDependency: the `Room` is
|
|
759
|
+
* injected via {@link RoomManagerOptions.roomFactory} (production:
|
|
760
|
+
* `() => new Room()`), so this module never hard-imports it.
|
|
761
|
+
*/
|
|
620
762
|
var RoomManager = class {
|
|
621
763
|
options;
|
|
622
764
|
room = null;
|
|
@@ -705,4 +847,4 @@ var RoomManager = class {
|
|
|
705
847
|
}
|
|
706
848
|
};
|
|
707
849
|
//#endregion
|
|
708
|
-
export { DEFAULT_ICE_SERVERS, PeerConnection, PeerManager, QrSignaling, ROOM_EVENT, RoomManager, byteChannelFromDataChannel, byteChannelFromRoom, createCompressedSignal, decodeSignal, encodeSignal, generateSessionId, parseCompressedSignal };
|
|
850
|
+
export { DEFAULT_ICE_SERVERS, DEFAULT_MAX_MESSAGE_SIZE, PeerConnection, PeerManager, QrSignaling, ROOM_EVENT, RoomManager, byteChannelFromDataChannel, byteChannelFromRoom, createCompressedSignal, decodeSignal, encodeSignal, generateSessionId, messageTargetOverByteChannel, parseCompressedSignal };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { ByteChannel } from "./types.js";
|
|
2
|
+
type Listener = (event: MessageEvent) => void | Promise<void>;
|
|
3
|
+
/**
|
|
4
|
+
* The payload ceiling for a channel over an `RTCDataChannel` (`PeerManager`,
|
|
5
|
+
* `QrSignaling`, `PeerConnection`): 16 KiB, the message size every browser
|
|
6
|
+
* pair delivers, less the 256 bytes the port stack's framing needs (spec D10).
|
|
7
|
+
* For a `byteChannelFromRoom` channel pass `maxMessageSize: 12 * 1024`, as
|
|
8
|
+
* `webrun-streams-livekit` uses: an older LiveKit client dropped larger
|
|
9
|
+
* reliable packets silently.
|
|
10
|
+
*/
|
|
11
|
+
export declare const DEFAULT_MAX_MESSAGE_SIZE: number;
|
|
12
|
+
/** What `messageTargetOverByteChannel` gives: a pipe of bytes, its life, and its payload ceiling. */
|
|
13
|
+
export interface ByteChannelMessageTarget {
|
|
14
|
+
/** A `MessageTarget` (as `@statewalker/webrun-rpc` defines it) of bytes, one channel message each. */
|
|
15
|
+
target: {
|
|
16
|
+
addEventListener(type: "message", listener: Listener): void;
|
|
17
|
+
removeEventListener(type: "message", listener: Listener): void;
|
|
18
|
+
/** Sends one message; `TypeError` for anything but bytes. */
|
|
19
|
+
postMessage(message: unknown): void;
|
|
20
|
+
/** Closes the channel. */
|
|
21
|
+
close(): Promise<void>;
|
|
22
|
+
};
|
|
23
|
+
/** Aborts when the channel closes. Pass it as `overPipe`'s `signal`. */
|
|
24
|
+
signal: AbortSignal;
|
|
25
|
+
/** Pass as `overPipe`'s `maxMessageSize`. */
|
|
26
|
+
maxMessageSize: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A `ByteChannel` this package hands back (`PeerManager`, `QrSignaling`,
|
|
30
|
+
* `RoomManager`) as a pipe for `webrun-rpc`'s port stack:
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* const { target, signal, maxMessageSize } = messageTargetOverByteChannel(channel);
|
|
34
|
+
* const { call } = await connect({
|
|
35
|
+
* mux: overPipe(target, { codec: msgpackCodec, side: "initiator", signal, maxMessageSize }),
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* It starts reading the channel at once, so attach listeners in the same tick;
|
|
40
|
+
* a message with no listener is dropped, as on a `MessagePort`. It must be the
|
|
41
|
+
* channel's only reader: a second reader of `recv` would take messages from it.
|
|
42
|
+
* Messages already received when the channel closes are still delivered.
|
|
43
|
+
*/
|
|
44
|
+
export declare function messageTargetOverByteChannel(channel: ByteChannel, options?: {
|
|
45
|
+
maxMessageSize?: number;
|
|
46
|
+
}): ByteChannelMessageTarget;
|
|
47
|
+
export {};
|
|
48
|
+
//# sourceMappingURL=message-target.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-target.d.ts","sourceRoot":"","sources":["../src/message-target.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAE9C,KAAK,QAAQ,GAAG,CAAC,KAAK,EAAE,YAAY,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;AAE9D;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,QAAkB,CAAC;AAExD,qGAAqG;AACrG,MAAM,WAAW,wBAAwB;IACvC,sGAAsG;IACtG,MAAM,EAAE;QACN,gBAAgB,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;QAC5D,mBAAmB,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,GAAG,IAAI,CAAC;QAC/D,6DAA6D;QAC7D,WAAW,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;QACpC,0BAA0B;QAC1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;KACxB,CAAC;IACF,wEAAwE;IACxE,MAAM,EAAE,WAAW,CAAC;IACpB,6CAA6C;IAC7C,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,4BAA4B,CAC1C,OAAO,EAAE,WAAW,EACpB,OAAO,GAAE;IAAE,cAAc,CAAC,EAAE,MAAM,CAAA;CAAO,GACxC,wBAAwB,CA6D1B"}
|
|
@@ -12,10 +12,9 @@
|
|
|
12
12
|
* - remote ICE candidates are added as plain `RTCIceCandidateInit` (accepted by
|
|
13
13
|
* `addIceCandidate`) instead of constructing a global `RTCIceCandidate`,
|
|
14
14
|
* which does not exist outside a browser.
|
|
15
|
-
* - `open()` yields a
|
|
15
|
+
* - `open()` yields a {@link ByteChannel} rather than a `MessagePort`.
|
|
16
16
|
*/
|
|
17
|
-
import type { ByteChannel } from "
|
|
18
|
-
import type { ConnectionState, IceCandidate, PeerConnectionEvents, PeerRole, SessionDescription, SignalingMessage, WebRtcConnectionOptions } from "./types.js";
|
|
17
|
+
import type { ByteChannel, ConnectionState, IceCandidate, PeerConnectionEvents, PeerRole, SessionDescription, SignalingMessage, WebRtcConnectionOptions } from "./types.js";
|
|
19
18
|
/**
|
|
20
19
|
* Manages a single WebRTC peer connection lifecycle.
|
|
21
20
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer-connection.d.ts","sourceRoot":"","sources":["../src/peer-connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;
|
|
1
|
+
{"version":3,"file":"peer-connection.d.ts","sourceRoot":"","sources":["../src/peer-connection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAGH,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,YAAY,EACZ,oBAAoB,EACpB,QAAQ,EACR,kBAAkB,EAClB,gBAAgB,EAChB,uBAAuB,EACxB,MAAM,YAAY,CAAC;AASpB;;GAEG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAW;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA0B;IAClD,OAAO,CAAC,EAAE,CAAkC;IAC5C,OAAO,CAAC,WAAW,CAA+B;IAClD,OAAO,CAAC,KAAK,CAA0B;IAEvC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAGZ;IAEd,OAAO,CAAC,iBAAiB,CAA6B;IACtD,OAAO,CAAC,oBAAoB,CAAS;IAErC,OAAO,CAAC,mBAAmB,CAAsB;IACjD,OAAO,CAAC,oBAAoB,CAAS;IAErC,YAAY,IAAI,EAAE,QAAQ,EAAE,OAAO,GAAE,uBAA4B,EAGhE;IAED,QAAQ,IAAI,eAAe,CAE1B;IAED,OAAO,IAAI,QAAQ,CAElB;IAED,cAAc,IAAI,cAAc,GAAG,IAAI,CAEtC;IAED,EAAE,CAAC,CAAC,SAAS,MAAM,oBAAoB,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAAG,IAAI,CAQ1F;IAED,GAAG,CAAC,CAAC,SAAS,MAAM,oBAAoB,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAAG,IAAI,CAG3F;IAED,OAAO,CAAC,IAAI;IAYZ,OAAO,CAAC,SAAS;IAKjB,OAAO,CAAC,cAAc;IA0DtB,OAAO,CAAC,gBAAgB;IAWxB,OAAO,CAAC,QAAQ;IAOhB;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAkB7B;IAED,uDAAuD;IACjD,YAAY,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAe3D;YAEa,WAAW;YAeX,YAAY;YASZ,eAAe;YAaf,wBAAwB;IAOtC;;;OAGG;IACG,mBAAmB,IAAI,OAAO,CAAC,IAAI,CAAC,CAiBzC;IAED,mDAAmD;IACnD,mBAAmB,IAAI,kBAAkB,GAAG,IAAI,CAM/C;IAED,qDAAqD;IACrD,sBAAsB,IAAI,YAAY,EAAE,CAEvC;IAED;;;OAGG;IACH,IAAI,IAAI,OAAO,CAAC,WAAW,CAAC,CA8B3B;IAED,kDAAkD;IAClD,KAAK,IAAI,IAAI,CAWZ;CACF"}
|
package/dist/peer-manager.d.ts
CHANGED
|
@@ -4,12 +4,11 @@
|
|
|
4
4
|
* Wraps the low-level {@link PeerConnection} (relocated from
|
|
5
5
|
* `@statewalker/vcs-port-webrtc`) with a multi-peer, id-keyed surface: dial a
|
|
6
6
|
* peer with `connect(peerId)` or accept inbound dials via `onConnection`. Each
|
|
7
|
-
* established connection yields a
|
|
7
|
+
* established connection yields a {@link ByteChannel}. The WebRTC
|
|
8
8
|
* offer/answer/ICE routing is the original logic; only the routing-by-peer-id
|
|
9
9
|
* and the ByteChannel result are new.
|
|
10
10
|
*/
|
|
11
|
-
import type { ByteChannel } from "
|
|
12
|
-
import type { SignalingTransport, WebRtcConnectionOptions } from "./types.js";
|
|
11
|
+
import type { ByteChannel, SignalingTransport, WebRtcConnectionOptions } from "./types.js";
|
|
13
12
|
export type PeerManagerOptions = WebRtcConnectionOptions;
|
|
14
13
|
export declare class PeerManager {
|
|
15
14
|
private readonly signaling;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"peer-manager.d.ts","sourceRoot":"","sources":["../src/peer-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;
|
|
1
|
+
{"version":3,"file":"peer-manager.d.ts","sourceRoot":"","sources":["../src/peer-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAGH,OAAO,KAAK,EACV,WAAW,EAEX,kBAAkB,EAClB,uBAAuB,EACxB,MAAM,YAAY,CAAC;AAEpB,MAAM,MAAM,kBAAkB,GAAG,uBAAuB,CAAC;AAEzD,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAC/C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAC7C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAqC;IACjE,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAwD;IAC3F,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;IAEzC,YAAY,SAAS,EAAE,kBAAkB,EAAE,OAAO,GAAE,kBAAuB,EAI1E;IAED,8DAA8D;IACxD,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAKlD;IAED,6EAA6E;IAC7E,YAAY,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,WAAW,KAAK,IAAI,GAAG,MAAM,IAAI,CAG3E;IAED,kEAAkE;IAClE,KAAK,IAAI,IAAI,CAKZ;IAED,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,QAAQ;CAajB"}
|
package/dist/qr-signaling.d.ts
CHANGED
|
@@ -8,10 +8,9 @@
|
|
|
8
8
|
*
|
|
9
9
|
* The compress/expand/encode/decode functions are pure and unchanged; the
|
|
10
10
|
* {@link QrSignaling} class is adapted to the target API and yields
|
|
11
|
-
*
|
|
11
|
+
* {@link ByteChannel}s.
|
|
12
12
|
*/
|
|
13
|
-
import type { ByteChannel } from "
|
|
14
|
-
import type { CompressedSignal, IceCandidate, PeerRole, SessionDescription, WebRtcConnectionOptions } from "./types.js";
|
|
13
|
+
import type { ByteChannel, CompressedSignal, IceCandidate, PeerRole, SessionDescription, WebRtcConnectionOptions } from "./types.js";
|
|
15
14
|
/** Generate a short random session ID. */
|
|
16
15
|
export declare function generateSessionId(): string;
|
|
17
16
|
/** Create a compressed signal for QR code exchange. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"qr-signaling.d.ts","sourceRoot":"","sources":["../src/qr-signaling.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;
|
|
1
|
+
{"version":3,"file":"qr-signaling.d.ts","sourceRoot":"","sources":["../src/qr-signaling.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EACV,WAAW,EACX,gBAAgB,EAChB,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,uBAAuB,EACxB,MAAM,YAAY,CAAC;AAKpB,0CAA0C;AAC1C,wBAAgB,iBAAiB,IAAI,MAAM,CAO1C;AAsED,uDAAuD;AACvD,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,QAAQ,EACd,WAAW,EAAE,kBAAkB,EAC/B,UAAU,EAAE,YAAY,EAAE,GACzB,gBAAgB,CAQlB;AAED,iCAAiC;AACjC,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,gBAAgB,GAAG;IAC/D,SAAS,EAAE,MAAM,CAAC;IAClB,IAAI,EAAE,QAAQ,CAAC;IACf,WAAW,EAAE,kBAAkB,CAAC;IAChC,UAAU,EAAE,YAAY,EAAE,CAAC;CAC5B,CAWA;AAED,4EAA4E;AAC5E,wBAAgB,YAAY,CAAC,MAAM,EAAE,gBAAgB,GAAG,MAAM,CAG7D;AAED,6CAA6C;AAC7C,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,gBAAgB,CAI9D;AAED,uCAAuC;AACvC,MAAM,WAAW,kBAAkB;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,uBAAuB,CAAC;CACtC;AAED;;;;;;;;GAQG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA0B;IAErD,YAAY,OAAO,GAAE,kBAAuB,EAG3C;IAED,YAAY,IAAI,MAAM,CAErB;IAED,qEAAqE;IAC/D,KAAK,IAAI,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAA;KAAE,CAAC,CAuBrF;IAED,8EAA8E;IACxE,MAAM,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,WAAW,CAAA;KAAE,CAAC,CAuB3E;CACF"}
|
package/dist/room-manager.d.ts
CHANGED
|
@@ -4,14 +4,13 @@
|
|
|
4
4
|
* Relocated from `@statewalker/vcs-port-livekit`'s `RoomManager` and adapted to
|
|
5
5
|
* the target API: `join`/`participants`/`channelTo`/`leave`. Participant
|
|
6
6
|
* tracking is the original event-wiring; `channelTo` yields a per-participant
|
|
7
|
-
*
|
|
7
|
+
* {@link ByteChannel} (see {@link byteChannelFromRoom}).
|
|
8
8
|
*
|
|
9
9
|
* The vendor (`livekit-client`) stays an optional peerDependency: the `Room` is
|
|
10
10
|
* injected via {@link RoomManagerOptions.roomFactory} (production:
|
|
11
11
|
* `() => new Room()`), so this module never hard-imports it.
|
|
12
12
|
*/
|
|
13
|
-
import type { ByteChannel } from "
|
|
14
|
-
import type { RoomManagerOptions } from "./types.js";
|
|
13
|
+
import type { ByteChannel, RoomManagerOptions } from "./types.js";
|
|
15
14
|
export declare class RoomManager {
|
|
16
15
|
private readonly options;
|
|
17
16
|
private room;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"room-manager.d.ts","sourceRoot":"","sources":["../src/room-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;
|
|
1
|
+
{"version":3,"file":"room-manager.d.ts","sourceRoot":"","sources":["../src/room-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,EAAE,WAAW,EAAY,kBAAkB,EAAuB,MAAM,YAAY,CAAC;AAGjG,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAC7C,OAAO,CAAC,IAAI,CAAyB;IAErC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAgB;IACrC,OAAO,CAAC,IAAI,CAA6C;IACzD,OAAO,CAAC,eAAe,CAA6B;IACpD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAyB;IAElD,YAAY,OAAO,EAAE,kBAAkB,EAEtC;IAED,sEAAsE;IAChE,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAmBtC;IAED,2EAA2E;IAC3E,YAAY,IAAI,aAAa,CAAC,MAAM,CAAC,CAyCpC;IAED,4DAA4D;IACtD,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAGpD;IAED,kDAAkD;IAC5C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAQ3B;CACF"}
|
package/dist/types.d.ts
CHANGED
|
@@ -4,9 +4,21 @@
|
|
|
4
4
|
* Relocated from the retiring `@statewalker/vcs-port-{webrtc,livekit}` packages
|
|
5
5
|
* and neutralised: nothing here is VCS/Git-specific. These describe the generic
|
|
6
6
|
* P2P handshake (WebRTC offer/answer/ICE, QR payloads, LiveKit room membership)
|
|
7
|
-
* used to *establish* a channel
|
|
8
|
-
*
|
|
7
|
+
* used to *establish* a channel, and the `ByteChannel` an established data
|
|
8
|
+
* channel is handed back as.
|
|
9
9
|
*/
|
|
10
|
+
/**
|
|
11
|
+
* An established connection's bytes: inbound as an async iterable, outbound by
|
|
12
|
+
* `send`, message-oriented (each `send` arrives as one item). This package
|
|
13
|
+
* owns the type: it used to come from `@statewalker/webrun-streams`, whose
|
|
14
|
+
* `emulateMux` (its only other consumer) is gone.
|
|
15
|
+
*/
|
|
16
|
+
export type ByteChannel = {
|
|
17
|
+
send(bytes: Uint8Array): void;
|
|
18
|
+
recv: AsyncIterable<Uint8Array>;
|
|
19
|
+
closed: Promise<void>;
|
|
20
|
+
close(): void;
|
|
21
|
+
};
|
|
10
22
|
/** WebRTC peer role in the connection. */
|
|
11
23
|
export type PeerRole = "initiator" | "responder";
|
|
12
24
|
/** Connection state for a WebRTC peer. */
|
|
@@ -154,6 +166,7 @@ export interface RoomManagerOptions {
|
|
|
154
166
|
export declare const ROOM_EVENT: {
|
|
155
167
|
readonly participantConnected: "participantConnected";
|
|
156
168
|
readonly participantDisconnected: "participantDisconnected";
|
|
169
|
+
readonly disconnected: "disconnected";
|
|
157
170
|
readonly dataReceived: "dataReceived";
|
|
158
171
|
};
|
|
159
172
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,0CAA0C;AAC1C,MAAM,MAAM,QAAQ,GAAG,WAAW,GAAG,WAAW,CAAC;AAEjD,0CAA0C;AAC1C,MAAM,MAAM,eAAe,GACvB,KAAK,GACL,YAAY,GACZ,WAAW,GACX,cAAc,GACd,QAAQ,GACR,QAAQ,CAAC;AAEb,kDAAkD;A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kFAAkF;IAClF,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAC5C,0EAA0E;IAC1E,WAAW,EAAE,MAAM,QAAQ,CAAC;CAC7B;AAED,oFAAoF;AACpF,eAAO,MAAM,UAAU;aACrB,oBAAoB,EAAE,sBAAsB;aAC5C,uBAAuB,EAAE,yBAAyB;aAClD,YAAY,EAAE,cAAc;aAC5B,YAAY,EAAE,cAAc;CACpB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/webrun-streams-signaling",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
|
-
"description": "Generic P2P signaling/connection-setup (PeerManager, QrSignaling, RoomManager) yielding
|
|
6
|
+
"description": "Generic P2P signaling/connection-setup (PeerManager, QrSignaling, RoomManager) yielding ByteChannels",
|
|
7
7
|
"bugs": {
|
|
8
8
|
"url": "https://github.com/statewalker/webrun-wire/issues"
|
|
9
9
|
},
|
|
@@ -29,9 +29,6 @@
|
|
|
29
29
|
"dist",
|
|
30
30
|
"src"
|
|
31
31
|
],
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"@statewalker/webrun-streams": "^0.2.3"
|
|
34
|
-
},
|
|
35
32
|
"peerDependencies": {
|
|
36
33
|
"livekit-client": "^2.18.3"
|
|
37
34
|
},
|
|
@@ -47,7 +44,10 @@
|
|
|
47
44
|
"rimraf": "^6.1.3",
|
|
48
45
|
"rolldown": "^1.2.12",
|
|
49
46
|
"typescript": "^7.0.2",
|
|
50
|
-
"vitest": "^5.0.
|
|
47
|
+
"vitest": "^5.0.5",
|
|
48
|
+
"@statewalker/webrun-msgpack": "^0.10.0",
|
|
49
|
+
"@statewalker/webrun-streams": "^0.3.0",
|
|
50
|
+
"@statewalker/webrun-rpc": "^0.11.0"
|
|
51
51
|
},
|
|
52
52
|
"sideEffects": false,
|
|
53
53
|
"publishConfig": {
|
package/src/byte-channel.ts
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
* requires. The original `vcs-port-*` helpers surfaced an established
|
|
4
4
|
* connection as a Web `MessagePort` (via the out-of-scope `datachannel-port` /
|
|
5
5
|
* `livekit-port` byte bridges); here the established primitive is wrapped
|
|
6
|
-
* *directly* as a
|
|
7
|
-
* multiplexing (`
|
|
6
|
+
* *directly* as a {@link ByteChannel} so the byte transport +
|
|
7
|
+
* multiplexing (`webrun-rpc`'s port stack, via `messageTargetOverByteChannel`) can run on top.
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
import { makeByteQueue, toBytes } from "./byte-queue.js";
|
|
11
|
-
import type { RoomLike } from "./types.js";
|
|
11
|
+
import type { ByteChannel, RoomLike } from "./types.js";
|
|
12
12
|
import { ROOM_EVENT } from "./types.js";
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -70,30 +70,44 @@ export function byteChannelFromRoom(room: RoomLike, peerId: string): ByteChannel
|
|
|
70
70
|
const closed = new Promise<void>((r) => {
|
|
71
71
|
closedResolve = r;
|
|
72
72
|
});
|
|
73
|
+
let ended = false;
|
|
73
74
|
|
|
74
75
|
const onData = (...args: unknown[]): void => {
|
|
75
76
|
const payload = args[0];
|
|
76
77
|
const participant = args[1] as { identity?: string } | undefined;
|
|
77
|
-
|
|
78
|
+
// A packet with no participant is the server's, not the peer's.
|
|
79
|
+
if (participant?.identity !== peerId) return;
|
|
78
80
|
if (payload != null) queue.push(toBytes(payload));
|
|
79
81
|
};
|
|
82
|
+
const onParticipantDisconnected = (...args: unknown[]): void => {
|
|
83
|
+
if ((args[0] as { identity?: string } | undefined)?.identity === peerId) end();
|
|
84
|
+
};
|
|
85
|
+
// The channel ends with the room or with the peer: otherwise nothing above
|
|
86
|
+
// it would ever learn the other end is gone.
|
|
87
|
+
const end = (): void => {
|
|
88
|
+
if (ended) return;
|
|
89
|
+
ended = true;
|
|
90
|
+
room.off(ROOM_EVENT.dataReceived, onData);
|
|
91
|
+
room.off(ROOM_EVENT.participantDisconnected, onParticipantDisconnected);
|
|
92
|
+
room.off(ROOM_EVENT.disconnected, end);
|
|
93
|
+
queue.done();
|
|
94
|
+
closedResolve();
|
|
95
|
+
};
|
|
80
96
|
|
|
81
97
|
room.on(ROOM_EVENT.dataReceived, onData);
|
|
98
|
+
room.on(ROOM_EVENT.participantDisconnected, onParticipantDisconnected);
|
|
99
|
+
room.on(ROOM_EVENT.disconnected, end);
|
|
82
100
|
|
|
83
101
|
return {
|
|
84
102
|
send(bytes) {
|
|
103
|
+
if (ended) return;
|
|
85
104
|
void room.localParticipant
|
|
86
105
|
.publishData(bytes, { reliable: true, destinationIdentities: [peerId] })
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
});
|
|
106
|
+
// A reliable publish that fails lost a message: the stream above is broken.
|
|
107
|
+
.catch(end);
|
|
90
108
|
},
|
|
91
109
|
recv: queue.recv,
|
|
92
110
|
closed,
|
|
93
|
-
close
|
|
94
|
-
room.off(ROOM_EVENT.dataReceived, onData);
|
|
95
|
-
queue.done();
|
|
96
|
-
closedResolve();
|
|
97
|
-
},
|
|
111
|
+
close: end,
|
|
98
112
|
};
|
|
99
113
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Generic P2P signaling / connection-setup helpers, relocated from the retiring
|
|
5
5
|
* `@statewalker/vcs-port-{webrtc,livekit}` packages and adapted to yield
|
|
6
|
-
*
|
|
6
|
+
* {@link ByteChannel}s. Signaling here is *transport-setup only*:
|
|
7
7
|
* it establishes a peer connection and hands back an established channel; the
|
|
8
|
-
* byte transport + multiplexing is `@statewalker/webrun-
|
|
8
|
+
* byte transport + multiplexing is `@statewalker/webrun-rpc`'s port stack.
|
|
9
9
|
*
|
|
10
10
|
* - {@link PeerManager} — WebRTC peer discovery + connection over a signaling transport.
|
|
11
11
|
* - {@link QrSignaling} — serverless offer/answer exchange via QR strings.
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
export { byteChannelFromDataChannel, byteChannelFromRoom } from "./byte-channel.js";
|
|
18
|
+
export * from "./message-target.js";
|
|
18
19
|
export { PeerConnection } from "./peer-connection.js";
|
|
19
20
|
export { PeerManager, type PeerManagerOptions } from "./peer-manager.js";
|
|
20
21
|
export {
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import type { ByteChannel } from "./types.js";
|
|
2
|
+
|
|
3
|
+
type Listener = (event: MessageEvent) => void | Promise<void>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The payload ceiling for a channel over an `RTCDataChannel` (`PeerManager`,
|
|
7
|
+
* `QrSignaling`, `PeerConnection`): 16 KiB, the message size every browser
|
|
8
|
+
* pair delivers, less the 256 bytes the port stack's framing needs (spec D10).
|
|
9
|
+
* For a `byteChannelFromRoom` channel pass `maxMessageSize: 12 * 1024`, as
|
|
10
|
+
* `webrun-streams-livekit` uses: an older LiveKit client dropped larger
|
|
11
|
+
* reliable packets silently.
|
|
12
|
+
*/
|
|
13
|
+
export const DEFAULT_MAX_MESSAGE_SIZE = 16 * 1024 - 256;
|
|
14
|
+
|
|
15
|
+
/** What `messageTargetOverByteChannel` gives: a pipe of bytes, its life, and its payload ceiling. */
|
|
16
|
+
export interface ByteChannelMessageTarget {
|
|
17
|
+
/** A `MessageTarget` (as `@statewalker/webrun-rpc` defines it) of bytes, one channel message each. */
|
|
18
|
+
target: {
|
|
19
|
+
addEventListener(type: "message", listener: Listener): void;
|
|
20
|
+
removeEventListener(type: "message", listener: Listener): void;
|
|
21
|
+
/** Sends one message; `TypeError` for anything but bytes. */
|
|
22
|
+
postMessage(message: unknown): void;
|
|
23
|
+
/** Closes the channel. */
|
|
24
|
+
close(): Promise<void>;
|
|
25
|
+
};
|
|
26
|
+
/** Aborts when the channel closes. Pass it as `overPipe`'s `signal`. */
|
|
27
|
+
signal: AbortSignal;
|
|
28
|
+
/** Pass as `overPipe`'s `maxMessageSize`. */
|
|
29
|
+
maxMessageSize: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A `ByteChannel` this package hands back (`PeerManager`, `QrSignaling`,
|
|
34
|
+
* `RoomManager`) as a pipe for `webrun-rpc`'s port stack:
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* const { target, signal, maxMessageSize } = messageTargetOverByteChannel(channel);
|
|
38
|
+
* const { call } = await connect({
|
|
39
|
+
* mux: overPipe(target, { codec: msgpackCodec, side: "initiator", signal, maxMessageSize }),
|
|
40
|
+
* });
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* It starts reading the channel at once, so attach listeners in the same tick;
|
|
44
|
+
* a message with no listener is dropped, as on a `MessagePort`. It must be the
|
|
45
|
+
* channel's only reader: a second reader of `recv` would take messages from it.
|
|
46
|
+
* Messages already received when the channel closes are still delivered.
|
|
47
|
+
*/
|
|
48
|
+
export function messageTargetOverByteChannel(
|
|
49
|
+
channel: ByteChannel,
|
|
50
|
+
options: { maxMessageSize?: number } = {},
|
|
51
|
+
): ByteChannelMessageTarget {
|
|
52
|
+
const listeners = new Set<Listener>();
|
|
53
|
+
const life = new AbortController();
|
|
54
|
+
let closedHere = false;
|
|
55
|
+
const end = (reason: Error): void => {
|
|
56
|
+
if (!life.signal.aborted) life.abort(reason);
|
|
57
|
+
};
|
|
58
|
+
void channel.closed.then(() => end(new Error("webrun-streams-signaling: the channel closed")));
|
|
59
|
+
void (async () => {
|
|
60
|
+
for await (const bytes of channel.recv) {
|
|
61
|
+
// The channel closing ends `recv` after what it already holds; only a
|
|
62
|
+
// close on this side stops delivery at once.
|
|
63
|
+
if (closedHere) break;
|
|
64
|
+
const event = new MessageEvent("message", { data: bytes.slice() });
|
|
65
|
+
for (const listener of [...listeners]) {
|
|
66
|
+
try {
|
|
67
|
+
Promise.resolve(listener(event)).catch(() => undefined);
|
|
68
|
+
} catch {}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
end(new Error("webrun-streams-signaling: the channel closed"));
|
|
72
|
+
})().catch((err: unknown) =>
|
|
73
|
+
end(err instanceof Error ? err : new Error(String(err), { cause: err })),
|
|
74
|
+
);
|
|
75
|
+
return {
|
|
76
|
+
signal: life.signal,
|
|
77
|
+
maxMessageSize: options.maxMessageSize ?? DEFAULT_MAX_MESSAGE_SIZE,
|
|
78
|
+
target: {
|
|
79
|
+
addEventListener: (_type, listener) => {
|
|
80
|
+
listeners.add(listener);
|
|
81
|
+
},
|
|
82
|
+
removeEventListener: (_type, listener) => {
|
|
83
|
+
listeners.delete(listener);
|
|
84
|
+
},
|
|
85
|
+
postMessage(message) {
|
|
86
|
+
const bytes =
|
|
87
|
+
message instanceof Uint8Array
|
|
88
|
+
? message
|
|
89
|
+
: message instanceof ArrayBuffer
|
|
90
|
+
? new Uint8Array(message)
|
|
91
|
+
: ArrayBuffer.isView(message)
|
|
92
|
+
? new Uint8Array(message.buffer, message.byteOffset, message.byteLength)
|
|
93
|
+
: undefined;
|
|
94
|
+
if (!bytes) {
|
|
95
|
+
throw new TypeError(
|
|
96
|
+
`webrun-streams-signaling: a byte pipe carries Uint8Array, ArrayBuffer or a view, got ${Object.prototype.toString.call(message)}`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
if (life.signal.aborted) return; // `signal` says so
|
|
100
|
+
channel.send(bytes.slice());
|
|
101
|
+
},
|
|
102
|
+
async close() {
|
|
103
|
+
listeners.clear();
|
|
104
|
+
closedHere = true;
|
|
105
|
+
end(new Error("webrun-streams-signaling: closed locally"));
|
|
106
|
+
try {
|
|
107
|
+
channel.close();
|
|
108
|
+
} catch {}
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
}
|
package/src/peer-connection.ts
CHANGED
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
* - remote ICE candidates are added as plain `RTCIceCandidateInit` (accepted by
|
|
13
13
|
* `addIceCandidate`) instead of constructing a global `RTCIceCandidate`,
|
|
14
14
|
* which does not exist outside a browser.
|
|
15
|
-
* - `open()` yields a
|
|
15
|
+
* - `open()` yields a {@link ByteChannel} rather than a `MessagePort`.
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
|
-
import type { ByteChannel } from "@statewalker/webrun-streams";
|
|
19
18
|
import { byteChannelFromDataChannel } from "./byte-channel.js";
|
|
20
19
|
import type {
|
|
20
|
+
ByteChannel,
|
|
21
21
|
ConnectionState,
|
|
22
22
|
IceCandidate,
|
|
23
23
|
PeerConnectionEvents,
|
package/src/peer-manager.ts
CHANGED
|
@@ -4,14 +4,18 @@
|
|
|
4
4
|
* Wraps the low-level {@link PeerConnection} (relocated from
|
|
5
5
|
* `@statewalker/vcs-port-webrtc`) with a multi-peer, id-keyed surface: dial a
|
|
6
6
|
* peer with `connect(peerId)` or accept inbound dials via `onConnection`. Each
|
|
7
|
-
* established connection yields a
|
|
7
|
+
* established connection yields a {@link ByteChannel}. The WebRTC
|
|
8
8
|
* offer/answer/ICE routing is the original logic; only the routing-by-peer-id
|
|
9
9
|
* and the ByteChannel result are new.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import type { ByteChannel } from "@statewalker/webrun-streams";
|
|
13
12
|
import { PeerConnection } from "./peer-connection.js";
|
|
14
|
-
import type {
|
|
13
|
+
import type {
|
|
14
|
+
ByteChannel,
|
|
15
|
+
SignalingMessage,
|
|
16
|
+
SignalingTransport,
|
|
17
|
+
WebRtcConnectionOptions,
|
|
18
|
+
} from "./types.js";
|
|
15
19
|
|
|
16
20
|
export type PeerManagerOptions = WebRtcConnectionOptions;
|
|
17
21
|
|
package/src/qr-signaling.ts
CHANGED
|
@@ -8,12 +8,12 @@
|
|
|
8
8
|
*
|
|
9
9
|
* The compress/expand/encode/decode functions are pure and unchanged; the
|
|
10
10
|
* {@link QrSignaling} class is adapted to the target API and yields
|
|
11
|
-
*
|
|
11
|
+
* {@link ByteChannel}s.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import type { ByteChannel } from "@statewalker/webrun-streams";
|
|
15
14
|
import { PeerConnection } from "./peer-connection.js";
|
|
16
15
|
import type {
|
|
16
|
+
ByteChannel,
|
|
17
17
|
CompressedSignal,
|
|
18
18
|
IceCandidate,
|
|
19
19
|
PeerRole,
|
package/src/room-manager.ts
CHANGED
|
@@ -4,16 +4,15 @@
|
|
|
4
4
|
* Relocated from `@statewalker/vcs-port-livekit`'s `RoomManager` and adapted to
|
|
5
5
|
* the target API: `join`/`participants`/`channelTo`/`leave`. Participant
|
|
6
6
|
* tracking is the original event-wiring; `channelTo` yields a per-participant
|
|
7
|
-
*
|
|
7
|
+
* {@link ByteChannel} (see {@link byteChannelFromRoom}).
|
|
8
8
|
*
|
|
9
9
|
* The vendor (`livekit-client`) stays an optional peerDependency: the `Room` is
|
|
10
10
|
* injected via {@link RoomManagerOptions.roomFactory} (production:
|
|
11
11
|
* `() => new Room()`), so this module never hard-imports it.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import type { ByteChannel } from "@statewalker/webrun-streams";
|
|
15
14
|
import { byteChannelFromRoom } from "./byte-channel.js";
|
|
16
|
-
import type { RoomLike, RoomManagerOptions, RoomParticipantLike } from "./types.js";
|
|
15
|
+
import type { ByteChannel, RoomLike, RoomManagerOptions, RoomParticipantLike } from "./types.js";
|
|
17
16
|
import { ROOM_EVENT } from "./types.js";
|
|
18
17
|
|
|
19
18
|
export class RoomManager {
|
package/src/types.ts
CHANGED
|
@@ -4,10 +4,23 @@
|
|
|
4
4
|
* Relocated from the retiring `@statewalker/vcs-port-{webrtc,livekit}` packages
|
|
5
5
|
* and neutralised: nothing here is VCS/Git-specific. These describe the generic
|
|
6
6
|
* P2P handshake (WebRTC offer/answer/ICE, QR payloads, LiveKit room membership)
|
|
7
|
-
* used to *establish* a channel
|
|
8
|
-
*
|
|
7
|
+
* used to *establish* a channel, and the `ByteChannel` an established data
|
|
8
|
+
* channel is handed back as.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* An established connection's bytes: inbound as an async iterable, outbound by
|
|
13
|
+
* `send`, message-oriented (each `send` arrives as one item). This package
|
|
14
|
+
* owns the type: it used to come from `@statewalker/webrun-streams`, whose
|
|
15
|
+
* `emulateMux` (its only other consumer) is gone.
|
|
16
|
+
*/
|
|
17
|
+
export type ByteChannel = {
|
|
18
|
+
send(bytes: Uint8Array): void;
|
|
19
|
+
recv: AsyncIterable<Uint8Array>;
|
|
20
|
+
closed: Promise<void>;
|
|
21
|
+
close(): void;
|
|
22
|
+
};
|
|
23
|
+
|
|
11
24
|
/** WebRTC peer role in the connection. */
|
|
12
25
|
export type PeerRole = "initiator" | "responder";
|
|
13
26
|
|
|
@@ -173,5 +186,6 @@ export interface RoomManagerOptions {
|
|
|
173
186
|
export const ROOM_EVENT = {
|
|
174
187
|
participantConnected: "participantConnected",
|
|
175
188
|
participantDisconnected: "participantDisconnected",
|
|
189
|
+
disconnected: "disconnected",
|
|
176
190
|
dataReceived: "dataReceived",
|
|
177
191
|
} as const;
|