@statewalker/webrun-streams-signaling 0.1.6 → 0.3.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 +83 -36
- package/dist/byte-channel.d.ts +49 -9
- package/dist/byte-channel.d.ts.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +406 -56
- package/dist/message-target.d.ts +55 -0
- package/dist/message-target.d.ts.map +1 -0
- package/dist/peer-connection.d.ts +4 -3
- package/dist/peer-connection.d.ts.map +1 -1
- package/dist/peer-manager.d.ts +15 -4
- 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 +5 -5
- package/dist/room-manager.d.ts.map +1 -1
- package/dist/types.d.ts +23 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -6
- package/src/byte-channel.ts +188 -35
- package/src/index.ts +11 -3
- package/src/message-target.ts +137 -0
- package/src/peer-connection.ts +15 -2
- package/src/peer-manager.ts +71 -13
- package/src/qr-signaling.ts +56 -14
- package/src/room-manager.ts +6 -7
- package/src/types.ts +24 -3
|
@@ -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;QACvB,8DAA8D;QAC9D,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAC;QAChC;;;WAGG;QACH,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;KAC9B,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,CA+E1B"}
|
|
@@ -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
|
*/
|
|
@@ -33,6 +32,8 @@ export declare class PeerConnection {
|
|
|
33
32
|
constructor(role: PeerRole, options?: WebRtcConnectionOptions);
|
|
34
33
|
getState(): ConnectionState;
|
|
35
34
|
getRole(): PeerRole;
|
|
35
|
+
/** Whether the remote description (the peer's offer or answer) has been applied. */
|
|
36
|
+
hasRemoteDescription(): boolean;
|
|
36
37
|
getDataChannel(): RTCDataChannel | null;
|
|
37
38
|
on<K extends keyof PeerConnectionEvents>(event: K, listener: PeerConnectionEvents[K]): this;
|
|
38
39
|
off<K extends keyof PeerConnectionEvents>(event: K, listener: PeerConnectionEvents[K]): this;
|
|
@@ -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,oFAAoF;IACpF,oBAAoB,IAAI,OAAO,CAE9B;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,CAsC3B;IAED,kDAAkD;IAClD,KAAK,IAAI,IAAI,CAWZ;CACF"}
|
package/dist/peer-manager.d.ts
CHANGED
|
@@ -4,13 +4,21 @@
|
|
|
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
|
-
|
|
13
|
-
|
|
11
|
+
import type { ByteChannel, SignalingTransport, WebRtcConnectionOptions } from "./types.js";
|
|
12
|
+
export type PeerManagerOptions = WebRtcConnectionOptions & {
|
|
13
|
+
/**
|
|
14
|
+
* Called when a connection fails with no caller to tell: a signal it
|
|
15
|
+
* rejects (an offer during glare, malformed SDP or candidates), or an
|
|
16
|
+
* inbound connection whose channel never opens. The connection has already
|
|
17
|
+
* been closed and forgotten, so the peer can dial again. Errors are dropped
|
|
18
|
+
* when unset. A failed `connect()` rejects instead.
|
|
19
|
+
*/
|
|
20
|
+
onError?: (error: Error, peerId: string) => void;
|
|
21
|
+
};
|
|
14
22
|
export declare class PeerManager {
|
|
15
23
|
private readonly signaling;
|
|
16
24
|
private readonly options;
|
|
@@ -25,6 +33,9 @@ export declare class PeerManager {
|
|
|
25
33
|
/** Tear down all connections and stop listening for signaling. */
|
|
26
34
|
close(): void;
|
|
27
35
|
private makeConnection;
|
|
36
|
+
/** Close `pc` and drop it, unless a newer connection to `peerId` replaced it. */
|
|
37
|
+
private forget;
|
|
38
|
+
private fail;
|
|
28
39
|
private onSignal;
|
|
29
40
|
}
|
|
30
41
|
//# sourceMappingURL=peer-manager.d.ts.map
|
|
@@ -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,GAAG;IACzD;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CAClD,CAAC;AAEF,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,CAclD;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,CAMZ;IAED,OAAO,CAAC,cAAc;IAatB,iFAAiF;IACjF,OAAO,CAAC,MAAM;IAMd,OAAO,CAAC,IAAI;IAUZ,OAAO,CAAC,QAAQ;CA0BjB"}
|
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;AAwGD,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,CA+BrF;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,14 @@
|
|
|
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
|
|
14
|
-
import type { RoomManagerOptions } from "./types.js";
|
|
13
|
+
import { type ByteChannelFromRoomOptions } from "./byte-channel.js";
|
|
14
|
+
import type { ByteChannel, RoomManagerOptions } from "./types.js";
|
|
15
15
|
export declare class RoomManager {
|
|
16
16
|
private readonly options;
|
|
17
17
|
private room;
|
|
@@ -24,8 +24,8 @@ export declare class RoomManager {
|
|
|
24
24
|
join(room: string): Promise<void>;
|
|
25
25
|
/** Observe participant identities: those already present, then joiners. */
|
|
26
26
|
participants(): AsyncIterable<string>;
|
|
27
|
-
/** Open a {@link ByteChannel} to a specific participant. */
|
|
28
|
-
channelTo(peerId: string): Promise<ByteChannel>;
|
|
27
|
+
/** Open a {@link ByteChannel} to a specific participant (see {@link byteChannelFromRoom}). */
|
|
28
|
+
channelTo(peerId: string, options?: ByteChannelFromRoomOptions): Promise<ByteChannel>;
|
|
29
29
|
/** Leave the room: unsubscribe and disconnect. */
|
|
30
30
|
leave(): Promise<void>;
|
|
31
31
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"room-manager.d.ts","sourceRoot":"","sources":["../src/room-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"room-manager.d.ts","sourceRoot":"","sources":["../src/room-manager.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAEH,OAAO,EAAE,KAAK,0BAA0B,EAAuB,MAAM,mBAAmB,CAAC;AACzF,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,8FAA8F;IACxF,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,0BAA0B,GAAG,OAAO,CAAC,WAAW,CAAC,CAG1F;IAED,kDAAkD;IAC5C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAQ3B;CACF"}
|
package/dist/types.d.ts
CHANGED
|
@@ -4,9 +4,28 @@
|
|
|
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
|
+
/** Bytes sent and not yet written out, where the transport can tell. */
|
|
22
|
+
readonly bufferedAmount?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Resolves once the transport can take more (`bufferedAmount` at most a
|
|
25
|
+
* low-water mark) or the channel closed. Absent where it cannot tell.
|
|
26
|
+
*/
|
|
27
|
+
whenDrained?(): Promise<void>;
|
|
28
|
+
};
|
|
10
29
|
/** WebRTC peer role in the connection. */
|
|
11
30
|
export type PeerRole = "initiator" | "responder";
|
|
12
31
|
/** Connection state for a WebRTC peer. */
|
|
@@ -131,6 +150,7 @@ export interface RoomLocalParticipantLike {
|
|
|
131
150
|
publishData(data: Uint8Array, opts?: {
|
|
132
151
|
reliable?: boolean;
|
|
133
152
|
destinationIdentities?: string[];
|
|
153
|
+
topic?: string;
|
|
134
154
|
}): Promise<void>;
|
|
135
155
|
}
|
|
136
156
|
export interface RoomLike {
|
|
@@ -154,6 +174,7 @@ export interface RoomManagerOptions {
|
|
|
154
174
|
export declare const ROOM_EVENT: {
|
|
155
175
|
readonly participantConnected: "participantConnected";
|
|
156
176
|
readonly participantDisconnected: "participantDisconnected";
|
|
177
|
+
readonly disconnected: "disconnected";
|
|
157
178
|
readonly dataReceived: "dataReceived";
|
|
158
179
|
};
|
|
159
180
|
//# 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;AAClD,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED,gDAAgD;AAChD,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,OAAO,GAAG,QAAQ,CAAC;IACzB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,iDAAiD;AACjD,MAAM,MAAM,gBAAgB,GACxB;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAC/B;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,SAAS,EAAE,YAAY,CAAA;CAAE,GAC9C;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC;AAEtB;;;;;GAKG;AACH,MAAM,WA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F,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.3.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,11 @@
|
|
|
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.11.0",
|
|
49
|
+
"@statewalker/webrun-streams": "^0.4.0",
|
|
50
|
+
"@statewalker/webrun-rpc": "^0.12.0",
|
|
51
|
+
"@statewalker/webrun-streams-conformance": "^0.5.0"
|
|
51
52
|
},
|
|
52
53
|
"sideEffects": false,
|
|
53
54
|
"publishConfig": {
|
|
@@ -57,6 +58,8 @@
|
|
|
57
58
|
"scripts": {
|
|
58
59
|
"build": "rimraf dist && rolldown -c && tsc --emitDeclarationOnly --declaration",
|
|
59
60
|
"test": "vitest run",
|
|
61
|
+
"typecheck": "tsc --noEmit",
|
|
62
|
+
"typecheck:tests": "tsc -p tsconfig.tests.json",
|
|
60
63
|
"lint": "biome check src tests"
|
|
61
64
|
}
|
|
62
65
|
}
|
package/src/byte-channel.ts
CHANGED
|
@@ -3,97 +3,250 @@
|
|
|
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
|
+
/**
|
|
15
|
+
* `whenDrained` resolves once the bytes sent and not yet written out are at
|
|
16
|
+
* most this: the transport can take more. The same mark the ws, peerjs,
|
|
17
|
+
* livekit and webrtc adapters use.
|
|
18
|
+
*/
|
|
19
|
+
export const DRAIN_LOW_WATER = 256 * 1024;
|
|
20
|
+
|
|
14
21
|
/**
|
|
15
22
|
* Wrap an established `RTCDataChannel` as a {@link ByteChannel}. Outbound bytes
|
|
16
|
-
* go through `channel.send`; inbound `message` events feed `recv`.
|
|
23
|
+
* go through `channel.send`; inbound `message` events feed `recv`. A message
|
|
24
|
+
* that is not bytes, or a `send` that fails (a closed channel, Chrome's full
|
|
25
|
+
* send queue), ends the channel: the stream above it lost a message.
|
|
26
|
+
* `bufferedAmount` is the data channel's; `whenDrained` waits for its
|
|
27
|
+
* `bufferedamountlow`.
|
|
17
28
|
*/
|
|
18
29
|
export function byteChannelFromDataChannel(channel: RTCDataChannel): ByteChannel {
|
|
19
30
|
const queue = makeByteQueue();
|
|
20
31
|
let closedResolve!: () => void;
|
|
32
|
+
let ended = false;
|
|
21
33
|
const closed = new Promise<void>((r) => {
|
|
22
34
|
closedResolve = r;
|
|
23
35
|
});
|
|
36
|
+
const drainWaiters = new Set<() => void>();
|
|
24
37
|
|
|
25
|
-
const onMessage = (ev: MessageEvent): void =>
|
|
26
|
-
|
|
38
|
+
const onMessage = (ev: MessageEvent): void => {
|
|
39
|
+
if (ended) return;
|
|
40
|
+
let bytes: Uint8Array;
|
|
41
|
+
try {
|
|
42
|
+
bytes = toBytes(ev.data);
|
|
43
|
+
} catch {
|
|
44
|
+
close();
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
queue.push(bytes);
|
|
48
|
+
};
|
|
49
|
+
const onDrained = (): void => {
|
|
50
|
+
for (const w of [...drainWaiters]) w();
|
|
51
|
+
drainWaiters.clear();
|
|
52
|
+
};
|
|
53
|
+
const end = (): void => {
|
|
54
|
+
if (ended) return;
|
|
55
|
+
ended = true;
|
|
56
|
+
channel.removeEventListener("message", onMessage as EventListener);
|
|
57
|
+
channel.removeEventListener("close", end);
|
|
58
|
+
channel.removeEventListener("bufferedamountlow", onDrained);
|
|
27
59
|
queue.done();
|
|
28
60
|
closedResolve();
|
|
61
|
+
onDrained();
|
|
62
|
+
};
|
|
63
|
+
const close = (): void => {
|
|
64
|
+
end();
|
|
65
|
+
try {
|
|
66
|
+
channel.close();
|
|
67
|
+
} catch {
|
|
68
|
+
/* ignore */
|
|
69
|
+
}
|
|
29
70
|
};
|
|
30
71
|
|
|
31
72
|
channel.binaryType = "arraybuffer";
|
|
73
|
+
channel.bufferedAmountLowThreshold = DRAIN_LOW_WATER;
|
|
32
74
|
channel.addEventListener("message", onMessage as EventListener);
|
|
33
|
-
channel.addEventListener("close",
|
|
75
|
+
channel.addEventListener("close", end);
|
|
76
|
+
channel.addEventListener("bufferedamountlow", onDrained);
|
|
34
77
|
|
|
35
78
|
return {
|
|
36
79
|
send(bytes) {
|
|
80
|
+
if (ended) return;
|
|
37
81
|
try {
|
|
38
82
|
// Cast around the DOM lib's `ArrayBufferView<ArrayBuffer>` overload:
|
|
39
83
|
// a plain `Uint8Array` is `ArrayBufferLike`-backed at the type level.
|
|
40
84
|
channel.send(bytes as unknown as ArrayBufferView<ArrayBuffer>);
|
|
41
85
|
} catch {
|
|
42
|
-
|
|
86
|
+
close();
|
|
43
87
|
}
|
|
44
88
|
},
|
|
45
89
|
recv: queue.recv,
|
|
46
90
|
closed,
|
|
47
|
-
close
|
|
48
|
-
|
|
49
|
-
channel.
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
queue.done();
|
|
56
|
-
closedResolve();
|
|
91
|
+
close,
|
|
92
|
+
get bufferedAmount() {
|
|
93
|
+
return channel.bufferedAmount ?? 0;
|
|
94
|
+
},
|
|
95
|
+
whenDrained() {
|
|
96
|
+
if (ended || (channel.bufferedAmount ?? 0) <= DRAIN_LOW_WATER) return Promise.resolve();
|
|
97
|
+
return new Promise<void>((resolve) => drainWaiters.add(resolve));
|
|
57
98
|
},
|
|
58
99
|
};
|
|
59
100
|
}
|
|
60
101
|
|
|
102
|
+
/**
|
|
103
|
+
* The topic a {@link byteChannelFromRoom} channel's packets carry, the same
|
|
104
|
+
* default as `webrun-streams-livekit`'s `messageTargetOverLiveKit`; packets on
|
|
105
|
+
* other topics are someone else's. The goodbye goes on this topic with
|
|
106
|
+
* `/close` appended, as there.
|
|
107
|
+
*/
|
|
108
|
+
export const DEFAULT_ROOM_TOPIC = "webrun/port-stack";
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The largest packet a {@link byteChannelFromRoom} channel publishes, as in
|
|
112
|
+
* `webrun-streams-livekit`: livekit-client (2.x) refuses a larger one.
|
|
113
|
+
*/
|
|
114
|
+
export const DEFAULT_ROOM_MAX_FRAME_BYTES = 64_000;
|
|
115
|
+
|
|
116
|
+
/** Options for {@link byteChannelFromRoom}. */
|
|
117
|
+
export interface ByteChannelFromRoomOptions {
|
|
118
|
+
/**
|
|
119
|
+
* The data topic, default {@link DEFAULT_ROOM_TOPIC}; the goodbye uses it
|
|
120
|
+
* with `/close` appended. Both ends must agree. Use another to run two
|
|
121
|
+
* channels to the same participant.
|
|
122
|
+
*/
|
|
123
|
+
topic?: string;
|
|
124
|
+
/** The largest packet sent; past it `send` ends the channel and throws `RangeError`. Default {@link DEFAULT_ROOM_MAX_FRAME_BYTES}. */
|
|
125
|
+
maxFrameBytes?: number;
|
|
126
|
+
}
|
|
127
|
+
|
|
61
128
|
/**
|
|
62
129
|
* Build a {@link ByteChannel} to a single room participant over a LiveKit-style
|
|
63
|
-
* `Room`. Outbound bytes are published to `peerId
|
|
64
|
-
* events from `peerId`
|
|
65
|
-
*
|
|
130
|
+
* `Room`. Outbound bytes are published reliably to `peerId` on the channel's
|
|
131
|
+
* topic; inbound `dataReceived` events from `peerId` on that topic feed
|
|
132
|
+
* `recv`. The packets and the goodbye follow `webrun-streams-livekit`'s
|
|
133
|
+
* conventions, so the port stack over this channel talks to one over
|
|
134
|
+
* `messageTargetOverLiveKit` with the same topic.
|
|
135
|
+
*
|
|
136
|
+
* The channel ends when the room disconnects, the participant leaves, the
|
|
137
|
+
* peer's goodbye arrives, a reliable publish fails, or a `send` exceeds
|
|
138
|
+
* `maxFrameBytes`. When it ends on this side, a goodbye on the close topic
|
|
139
|
+
* ends the peer's channel too: the room stays connected, so nothing else would.
|
|
140
|
+
* `bufferedAmount` counts bytes whose publish has not settled.
|
|
66
141
|
*/
|
|
67
|
-
export function byteChannelFromRoom(
|
|
142
|
+
export function byteChannelFromRoom(
|
|
143
|
+
room: RoomLike,
|
|
144
|
+
peerId: string,
|
|
145
|
+
options: ByteChannelFromRoomOptions = {},
|
|
146
|
+
): ByteChannel {
|
|
147
|
+
const topic = options.topic ?? DEFAULT_ROOM_TOPIC;
|
|
148
|
+
const closeTopic = `${topic}/close`;
|
|
149
|
+
const maxFrameBytes = options.maxFrameBytes ?? DEFAULT_ROOM_MAX_FRAME_BYTES;
|
|
68
150
|
const queue = makeByteQueue();
|
|
69
151
|
let closedResolve!: () => void;
|
|
70
152
|
const closed = new Promise<void>((r) => {
|
|
71
153
|
closedResolve = r;
|
|
72
154
|
});
|
|
155
|
+
let ended = false;
|
|
156
|
+
let inFlight = 0;
|
|
157
|
+
const drainWaiters = new Set<() => void>();
|
|
158
|
+
const wakeDrainWaiters = (): void => {
|
|
159
|
+
if (inFlight > DRAIN_LOW_WATER && !ended) return;
|
|
160
|
+
for (const w of [...drainWaiters]) w();
|
|
161
|
+
drainWaiters.clear();
|
|
162
|
+
};
|
|
73
163
|
|
|
74
164
|
const onData = (...args: unknown[]): void => {
|
|
75
|
-
const payload = args[
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
165
|
+
const [payload, participant, , packetTopic] = args as [
|
|
166
|
+
unknown,
|
|
167
|
+
{ identity?: string } | undefined,
|
|
168
|
+
unknown,
|
|
169
|
+
string | undefined,
|
|
170
|
+
];
|
|
171
|
+
// A packet with no participant is the server's, not the peer's.
|
|
172
|
+
if (ended || participant?.identity !== peerId) return;
|
|
173
|
+
if (packetTopic === closeTopic) {
|
|
174
|
+
end(false);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (packetTopic !== topic || payload == null) return;
|
|
178
|
+
let bytes: Uint8Array;
|
|
179
|
+
try {
|
|
180
|
+
bytes = toBytes(payload);
|
|
181
|
+
} catch {
|
|
182
|
+
end(true);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
queue.push(bytes);
|
|
186
|
+
};
|
|
187
|
+
const onParticipantDisconnected = (...args: unknown[]): void => {
|
|
188
|
+
if ((args[0] as { identity?: string } | undefined)?.identity === peerId) end(false);
|
|
189
|
+
};
|
|
190
|
+
const onDisconnected = (): void => end(false);
|
|
191
|
+
// The channel ends with the room, with the peer, or with the peer's
|
|
192
|
+
// goodbye: otherwise nothing above it would ever learn the other end is gone.
|
|
193
|
+
/** `tellPeer`: it ended on this side, and the room is still there to say so. */
|
|
194
|
+
const end = (tellPeer: boolean): void => {
|
|
195
|
+
if (ended) return;
|
|
196
|
+
ended = true;
|
|
197
|
+
room.off(ROOM_EVENT.dataReceived, onData);
|
|
198
|
+
room.off(ROOM_EVENT.participantDisconnected, onParticipantDisconnected);
|
|
199
|
+
room.off(ROOM_EVENT.disconnected, onDisconnected);
|
|
200
|
+
queue.done();
|
|
201
|
+
closedResolve();
|
|
202
|
+
wakeDrainWaiters();
|
|
203
|
+
if (tellPeer) {
|
|
204
|
+
room.localParticipant
|
|
205
|
+
.publishData(new Uint8Array(0), {
|
|
206
|
+
reliable: true,
|
|
207
|
+
destinationIdentities: [peerId],
|
|
208
|
+
topic: closeTopic,
|
|
209
|
+
})
|
|
210
|
+
.catch(() => undefined);
|
|
211
|
+
}
|
|
79
212
|
};
|
|
80
213
|
|
|
81
214
|
room.on(ROOM_EVENT.dataReceived, onData);
|
|
215
|
+
room.on(ROOM_EVENT.participantDisconnected, onParticipantDisconnected);
|
|
216
|
+
room.on(ROOM_EVENT.disconnected, onDisconnected);
|
|
82
217
|
|
|
83
218
|
return {
|
|
84
219
|
send(bytes) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
220
|
+
if (ended) return;
|
|
221
|
+
if (bytes.byteLength > maxFrameBytes) {
|
|
222
|
+
end(true);
|
|
223
|
+
throw new RangeError(
|
|
224
|
+
`webrun-streams-signaling: a ${bytes.byteLength}-byte packet exceeds maxFrameBytes (${maxFrameBytes})`,
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
const size = bytes.byteLength;
|
|
228
|
+
inFlight += size;
|
|
229
|
+
room.localParticipant
|
|
230
|
+
.publishData(bytes, { reliable: true, destinationIdentities: [peerId], topic })
|
|
231
|
+
// A reliable publish that fails lost a message: the stream above is broken.
|
|
232
|
+
.then(
|
|
233
|
+
() => undefined,
|
|
234
|
+
() => end(true),
|
|
235
|
+
)
|
|
236
|
+
.finally(() => {
|
|
237
|
+
inFlight -= size;
|
|
238
|
+
wakeDrainWaiters();
|
|
89
239
|
});
|
|
90
240
|
},
|
|
91
241
|
recv: queue.recv,
|
|
92
242
|
closed,
|
|
93
|
-
close()
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
243
|
+
close: () => end(true),
|
|
244
|
+
get bufferedAmount() {
|
|
245
|
+
return inFlight;
|
|
246
|
+
},
|
|
247
|
+
whenDrained() {
|
|
248
|
+
if (ended || inFlight <= DRAIN_LOW_WATER) return Promise.resolve();
|
|
249
|
+
return new Promise<void>((resolve) => drainWaiters.add(resolve));
|
|
97
250
|
},
|
|
98
251
|
};
|
|
99
252
|
}
|
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.
|
|
@@ -14,7 +14,15 @@
|
|
|
14
14
|
* @packageDocumentation
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
-
export {
|
|
17
|
+
export {
|
|
18
|
+
type ByteChannelFromRoomOptions,
|
|
19
|
+
byteChannelFromDataChannel,
|
|
20
|
+
byteChannelFromRoom,
|
|
21
|
+
DEFAULT_ROOM_MAX_FRAME_BYTES,
|
|
22
|
+
DEFAULT_ROOM_TOPIC,
|
|
23
|
+
DRAIN_LOW_WATER,
|
|
24
|
+
} from "./byte-channel.js";
|
|
25
|
+
export * from "./message-target.js";
|
|
18
26
|
export { PeerConnection } from "./peer-connection.js";
|
|
19
27
|
export { PeerManager, type PeerManagerOptions } from "./peer-manager.js";
|
|
20
28
|
export {
|