@statewalker/webrun-streams-livekit 0.3.0 → 0.4.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 +3 -3
- package/dist/index.js +43 -74
- package/dist/message-target.d.ts +8 -16
- package/dist/message-target.d.ts.map +1 -1
- package/package.json +9 -5
- package/src/message-target.ts +58 -103
package/README.md
CHANGED
|
@@ -189,9 +189,9 @@ contract.
|
|
|
189
189
|
### Dependencies
|
|
190
190
|
|
|
191
191
|
- `livekit-client` (peer): you supply and own the `Room`. `src/` imports only its `Room` type.
|
|
192
|
-
-
|
|
193
|
-
`@statewalker/webrun-rpc
|
|
194
|
-
|
|
192
|
+
- `@statewalker/webrun-streams` at runtime, for `newMessagePipe`: the pipe module every transport adapter shares (listeners, delivery, life, the byte check, the frame ceiling, drain, close), so a pipe rule is fixed once for all of them (#151). The target is a `MessageTarget` by shape and carries its own `closeSignal` and `maxMessageSize`, so `overPipe(target, { codec, side })` needs nothing else.
|
|
193
|
+
`@statewalker/webrun-rpc` and `@statewalker/webrun-msgpack` are dev dependencies of the tests;
|
|
194
|
+
consumers supply their own `webrun-rpc` and codec.
|
|
195
195
|
- Dev only: `livekit-server-sdk` for test tokens, `@vitest/browser-playwright` and `playwright`
|
|
196
196
|
for the browser run, `@statewalker/webrun-streams-conformance` for the suite.
|
|
197
197
|
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { DEFAULT_DRAIN_LOW_WATER, newMessagePipe } from "@statewalker/webrun-streams";
|
|
1
2
|
//#region src/message-target.ts
|
|
2
3
|
/**
|
|
3
4
|
* The largest data packet livekit-client (2.x) publishes: `RTCEngine` refuses
|
|
@@ -22,7 +23,7 @@ const DEFAULT_MAX_MESSAGE_SIZE = 12288;
|
|
|
22
23
|
* `whenDrained` resolves once the bytes whose publish has not settled are at
|
|
23
24
|
* most this: the room can take more.
|
|
24
25
|
*/
|
|
25
|
-
const DRAIN_LOW_WATER =
|
|
26
|
+
const DRAIN_LOW_WATER = DEFAULT_DRAIN_LOW_WATER;
|
|
26
27
|
/**
|
|
27
28
|
* One remote participant of a connected `Room` as a pipe of messages, for a
|
|
28
29
|
* multiplexer to run over:
|
|
@@ -48,100 +49,68 @@ function messageTargetOverLiveKit(room, peerIdentity, options = {}) {
|
|
|
48
49
|
const closeTopic = `${topic}/close`;
|
|
49
50
|
const remotes = room.remoteParticipants;
|
|
50
51
|
if (!(remotes ? remotes.has(peerIdentity) || [...remotes.values()].some((p) => p.identity === peerIdentity) : true)) throw new Error(`messageTargetOverLiveKit: "${peerIdentity}" is not in the room; wait for it to join`);
|
|
51
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
52
|
-
const life = new AbortController();
|
|
53
52
|
const roomApi = room;
|
|
54
53
|
const publisher = room.localParticipant;
|
|
55
54
|
let inFlight = 0;
|
|
56
55
|
const drainWaiters = /* @__PURE__ */ new Set();
|
|
57
56
|
const wakeDrainWaiters = () => {
|
|
58
|
-
if (inFlight >
|
|
57
|
+
if (inFlight > DRAIN_LOW_WATER) return;
|
|
59
58
|
for (const w of [...drainWaiters]) w();
|
|
60
59
|
drainWaiters.clear();
|
|
61
60
|
};
|
|
62
61
|
const onData = (...args) => {
|
|
63
62
|
const [payload, participant, , packetTopic] = args;
|
|
64
|
-
if (
|
|
63
|
+
if (participant?.identity !== peerIdentity) return;
|
|
65
64
|
if (packetTopic === closeTopic) {
|
|
66
|
-
end(/* @__PURE__ */ new Error(`webrun-streams-livekit: "${peerIdentity}" closed the pipe`)
|
|
65
|
+
control.end(/* @__PURE__ */ new Error(`webrun-streams-livekit: "${peerIdentity}" closed the pipe`));
|
|
67
66
|
return;
|
|
68
67
|
}
|
|
69
68
|
if (packetTopic !== topic) return;
|
|
70
|
-
|
|
71
|
-
for (const listener of [...listeners]) try {
|
|
72
|
-
Promise.resolve(listener(event)).catch(() => void 0);
|
|
73
|
-
} catch {}
|
|
69
|
+
control.deliver(payload);
|
|
74
70
|
};
|
|
75
|
-
const onDisconnected = () => end(/* @__PURE__ */ new Error("webrun-streams-livekit: the room disconnected")
|
|
71
|
+
const onDisconnected = () => control.end(/* @__PURE__ */ new Error("webrun-streams-livekit: the room disconnected"));
|
|
76
72
|
const onParticipantDisconnected = (...args) => {
|
|
77
73
|
if (args[0]?.identity !== peerIdentity) return;
|
|
78
|
-
end(/* @__PURE__ */ new Error(`webrun-streams-livekit: "${peerIdentity}" left the room`)
|
|
79
|
-
};
|
|
80
|
-
/** `tellPeer`: the pipe ended on this side, and the room is still there to say so. */
|
|
81
|
-
const end = (reason, tellPeer = true) => {
|
|
82
|
-
if (life.signal.aborted) return;
|
|
83
|
-
roomApi.off("dataReceived", onData);
|
|
84
|
-
roomApi.off("disconnected", onDisconnected);
|
|
85
|
-
roomApi.off("participantDisconnected", onParticipantDisconnected);
|
|
86
|
-
life.abort(reason);
|
|
87
|
-
wakeDrainWaiters();
|
|
88
|
-
if (tellPeer) publisher.publishData(/* @__PURE__ */ new Uint8Array(0), {
|
|
89
|
-
reliable: true,
|
|
90
|
-
destinationIdentities: [peerIdentity],
|
|
91
|
-
topic: closeTopic
|
|
92
|
-
}).catch(() => void 0);
|
|
74
|
+
control.end(/* @__PURE__ */ new Error(`webrun-streams-livekit: "${peerIdentity}" left the room`));
|
|
93
75
|
};
|
|
76
|
+
const control = newMessagePipe({
|
|
77
|
+
send: (bytes) => {
|
|
78
|
+
const size = bytes.byteLength;
|
|
79
|
+
inFlight += size;
|
|
80
|
+
publisher.publishData(bytes, {
|
|
81
|
+
reliable: true,
|
|
82
|
+
destinationIdentities: [peerIdentity],
|
|
83
|
+
topic
|
|
84
|
+
}).then(() => void 0, (err) => control.end(new Error(`webrun-streams-livekit: a reliable publish failed: ${err?.message ?? String(err)}`, { cause: err }), true)).finally(() => {
|
|
85
|
+
inFlight -= size;
|
|
86
|
+
wakeDrainWaiters();
|
|
87
|
+
});
|
|
88
|
+
},
|
|
89
|
+
bufferedAmount: () => inFlight,
|
|
90
|
+
whenDrained: () => new Promise((resolve) => drainWaiters.add(resolve)),
|
|
91
|
+
/** `local`: the pipe ended on this side, and the room is still there to say so. */
|
|
92
|
+
close: (local) => {
|
|
93
|
+
roomApi.off("dataReceived", onData);
|
|
94
|
+
roomApi.off("disconnected", onDisconnected);
|
|
95
|
+
roomApi.off("participantDisconnected", onParticipantDisconnected);
|
|
96
|
+
drainWaiters.clear();
|
|
97
|
+
if (local) publisher.publishData(/* @__PURE__ */ new Uint8Array(0), {
|
|
98
|
+
reliable: true,
|
|
99
|
+
destinationIdentities: [peerIdentity],
|
|
100
|
+
topic: closeTopic
|
|
101
|
+
}).catch(() => void 0);
|
|
102
|
+
}
|
|
103
|
+
}, {
|
|
104
|
+
name: "webrun-streams-livekit",
|
|
105
|
+
maxFrameBytes,
|
|
106
|
+
maxMessageSize,
|
|
107
|
+
copyOnSend: true,
|
|
108
|
+
copyOnDeliver: true
|
|
109
|
+
});
|
|
94
110
|
roomApi.on("dataReceived", onData);
|
|
95
111
|
roomApi.on("disconnected", onDisconnected);
|
|
96
112
|
roomApi.on("participantDisconnected", onParticipantDisconnected);
|
|
97
|
-
return
|
|
98
|
-
signal: life.signal,
|
|
99
|
-
maxMessageSize,
|
|
100
|
-
target: {
|
|
101
|
-
addEventListener: (_type, listener) => {
|
|
102
|
-
listeners.add(listener);
|
|
103
|
-
},
|
|
104
|
-
removeEventListener: (_type, listener) => {
|
|
105
|
-
listeners.delete(listener);
|
|
106
|
-
},
|
|
107
|
-
postMessage(message) {
|
|
108
|
-
const bytes = toBytes(message);
|
|
109
|
-
if (!bytes) throw new TypeError(`webrun-streams-livekit: a byte pipe carries Uint8Array, ArrayBuffer or a view, got ${Object.prototype.toString.call(message)}`);
|
|
110
|
-
if (life.signal.aborted) return;
|
|
111
|
-
if (bytes.byteLength > maxFrameBytes) {
|
|
112
|
-
const err = /* @__PURE__ */ new RangeError(`webrun-streams-livekit: a ${bytes.byteLength}-byte packet exceeds maxFrameBytes (${maxFrameBytes})`);
|
|
113
|
-
end(err);
|
|
114
|
-
throw err;
|
|
115
|
-
}
|
|
116
|
-
const size = bytes.byteLength;
|
|
117
|
-
inFlight += size;
|
|
118
|
-
publisher.publishData(bytes.slice(), {
|
|
119
|
-
reliable: true,
|
|
120
|
-
destinationIdentities: [peerIdentity],
|
|
121
|
-
topic
|
|
122
|
-
}).then(() => void 0, (err) => end(new Error(`webrun-streams-livekit: a reliable publish failed: ${err?.message ?? String(err)}`, { cause: err }))).finally(() => {
|
|
123
|
-
inFlight -= size;
|
|
124
|
-
wakeDrainWaiters();
|
|
125
|
-
});
|
|
126
|
-
},
|
|
127
|
-
async close() {
|
|
128
|
-
listeners.clear();
|
|
129
|
-
end(/* @__PURE__ */ new Error("webrun-streams-livekit: closed locally"));
|
|
130
|
-
},
|
|
131
|
-
get bufferedAmount() {
|
|
132
|
-
return inFlight;
|
|
133
|
-
},
|
|
134
|
-
whenDrained() {
|
|
135
|
-
if (inFlight <= 262144 || life.signal.aborted) return Promise.resolve();
|
|
136
|
-
return new Promise((resolve) => drainWaiters.add(resolve));
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
function toBytes(data) {
|
|
142
|
-
if (data instanceof Uint8Array) return data;
|
|
143
|
-
if (data instanceof ArrayBuffer) return new Uint8Array(data);
|
|
144
|
-
if (ArrayBuffer.isView(data)) return new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
113
|
+
return control.pipe;
|
|
145
114
|
}
|
|
146
115
|
//#endregion
|
|
147
116
|
export { CLOSE_TOPIC, DEFAULT_MAX_FRAME_BYTES, DEFAULT_MAX_MESSAGE_SIZE, DEFAULT_TOPIC, DRAIN_LOW_WATER, messageTargetOverLiveKit };
|
package/dist/message-target.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { type MessagePipe, type MessagePipeTarget } from "@statewalker/webrun-streams";
|
|
1
2
|
import type { Room } from "livekit-client";
|
|
2
|
-
type Listener = (event: MessageEvent) => void | Promise<void>;
|
|
3
3
|
/**
|
|
4
4
|
* The largest data packet livekit-client (2.x) publishes: `RTCEngine` refuses
|
|
5
5
|
* a larger one with a `PublishDataError`. A post past it ends the pipe with a
|
|
@@ -37,23 +37,16 @@ export interface MessageTargetOverLiveKitOptions {
|
|
|
37
37
|
topic?: string;
|
|
38
38
|
}
|
|
39
39
|
/** What `messageTargetOverLiveKit` gives: a pipe of bytes to one participant, its life, and its payload ceiling. */
|
|
40
|
-
export interface LiveKitMessageTarget {
|
|
40
|
+
export interface LiveKitMessageTarget extends MessagePipe {
|
|
41
41
|
/**
|
|
42
42
|
* A `MessageTarget` (as `@statewalker/webrun-rpc` defines it) whose messages
|
|
43
|
-
* are `Uint8Array`s, one reliable data packet each.
|
|
43
|
+
* are `Uint8Array`s, one reliable data packet each. A post past
|
|
44
|
+
* `maxFrameBytes` throws `RangeError` and ends the pipe; `close()` detaches
|
|
45
|
+
* from the room, which stays connected (it is the caller's);
|
|
46
|
+
* `bufferedAmount` counts bytes whose `publishData` has not settled, and
|
|
47
|
+
* `whenDrained` waits for it to reach {@link DRAIN_LOW_WATER}.
|
|
44
48
|
*/
|
|
45
|
-
target:
|
|
46
|
-
addEventListener(type: "message", listener: Listener): void;
|
|
47
|
-
removeEventListener(type: "message", listener: Listener): void;
|
|
48
|
-
/** Publishes one packet. `TypeError` for non-bytes; `RangeError`, ending the pipe, past `maxFrameBytes`. */
|
|
49
|
-
postMessage(message: unknown): void;
|
|
50
|
-
/** Detaches from the room. The room itself stays connected: it is the caller's. */
|
|
51
|
-
close(): Promise<void>;
|
|
52
|
-
/** Bytes published whose `publishData` has not settled. */
|
|
53
|
-
readonly bufferedAmount: number;
|
|
54
|
-
/** Resolves once `bufferedAmount` is at most {@link DRAIN_LOW_WATER}, or the pipe ended. */
|
|
55
|
-
whenDrained(): Promise<void>;
|
|
56
|
-
};
|
|
49
|
+
target: MessagePipeTarget;
|
|
57
50
|
/**
|
|
58
51
|
* Aborts when the room disconnects, the participant leaves, a publish fails
|
|
59
52
|
* (the stream above lost a message), or a post exceeds `maxFrameBytes`.
|
|
@@ -82,5 +75,4 @@ export interface LiveKitMessageTarget {
|
|
|
82
75
|
* publish — a goodbye on the close topic ends the peer's pipe too.
|
|
83
76
|
*/
|
|
84
77
|
export declare function messageTargetOverLiveKit(room: Room, peerIdentity: string, options?: MessageTargetOverLiveKitOptions): LiveKitMessageTarget;
|
|
85
|
-
export {};
|
|
86
78
|
//# sourceMappingURL=message-target.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"message-target.d.ts","sourceRoot":"","sources":["../src/message-target.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"message-target.d.ts","sourceRoot":"","sources":["../src/message-target.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,iBAAiB,EAEvB,MAAM,6BAA6B,CAAC;AACrC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE3C;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,QAAS,CAAC;AAE9C,uFAAuF;AACvF,eAAO,MAAM,aAAa,sBAAsB,CAAC;AAEjD;;;GAGG;AACH,eAAO,MAAM,WAAW,4BAA4B,CAAC;AAErD;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,QAAY,CAAC;AAElD;;;GAGG;AACH,eAAO,MAAM,eAAe,QAA0B,CAAC;AAEvD,MAAM,WAAW,+BAA+B;IAC9C,mHAAmH;IACnH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,+EAA+E;IAC/E,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,oHAAoH;AACpH,MAAM,WAAW,oBAAqB,SAAQ,WAAW;IACvD;;;;;;;OAOG;IACH,MAAM,EAAE,iBAAiB,CAAC;IAC1B;;;;OAIG;IACH,MAAM,EAAE,WAAW,CAAC;IACpB,2FAA2F;IAC3F,cAAc,EAAE,MAAM,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,wBAAwB,CACtC,IAAI,EAAE,IAAI,EACV,YAAY,EAAE,MAAM,EACpB,OAAO,GAAE,+BAAoC,GAC5C,oBAAoB,CAkHtB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/webrun-streams-livekit",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "LiveKit-backed Connect/Serve adapter in the webrun-streams-* family",
|
|
@@ -32,6 +32,9 @@
|
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"livekit-client": "^2.18.3"
|
|
34
34
|
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@statewalker/webrun-streams": "^0.5.0"
|
|
37
|
+
},
|
|
35
38
|
"devDependencies": {
|
|
36
39
|
"@biomejs/biome": "^2.5.15",
|
|
37
40
|
"@types/node": "^26.6.4",
|
|
@@ -44,10 +47,9 @@
|
|
|
44
47
|
"testcontainers": "^12.2.0",
|
|
45
48
|
"typescript": "^7.0.2",
|
|
46
49
|
"vitest": "^5.0.5",
|
|
47
|
-
"@statewalker/webrun-
|
|
48
|
-
"@statewalker/webrun-streams-conformance": "^0.
|
|
49
|
-
"@statewalker/webrun-
|
|
50
|
-
"@statewalker/webrun-msgpack": "^0.10.0"
|
|
50
|
+
"@statewalker/webrun-msgpack": "^0.12.0",
|
|
51
|
+
"@statewalker/webrun-streams-conformance": "^0.6.0",
|
|
52
|
+
"@statewalker/webrun-rpc": "^0.13.0"
|
|
51
53
|
},
|
|
52
54
|
"sideEffects": false,
|
|
53
55
|
"publishConfig": {
|
|
@@ -58,6 +60,8 @@
|
|
|
58
60
|
"build": "rimraf dist && rolldown -c && tsc --emitDeclarationOnly --declaration",
|
|
59
61
|
"test": "vitest run",
|
|
60
62
|
"test:browser": "vitest run --config vitest.browser.config.ts",
|
|
63
|
+
"typecheck": "tsc --noEmit",
|
|
64
|
+
"typecheck:tests": "tsc -p tsconfig.tests.json",
|
|
61
65
|
"lint": "biome check src tests"
|
|
62
66
|
}
|
|
63
67
|
}
|
package/src/message-target.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_DRAIN_LOW_WATER,
|
|
3
|
+
type MessagePipe,
|
|
4
|
+
type MessagePipeTarget,
|
|
5
|
+
newMessagePipe,
|
|
6
|
+
} from "@statewalker/webrun-streams";
|
|
1
7
|
import type { Room } from "livekit-client";
|
|
2
8
|
|
|
3
|
-
type Listener = (event: MessageEvent) => void | Promise<void>;
|
|
4
|
-
|
|
5
9
|
/**
|
|
6
10
|
* The largest data packet livekit-client (2.x) publishes: `RTCEngine` refuses
|
|
7
11
|
* a larger one with a `PublishDataError`. A post past it ends the pipe with a
|
|
@@ -29,7 +33,7 @@ export const DEFAULT_MAX_MESSAGE_SIZE = 12 * 1024;
|
|
|
29
33
|
* `whenDrained` resolves once the bytes whose publish has not settled are at
|
|
30
34
|
* most this: the room can take more.
|
|
31
35
|
*/
|
|
32
|
-
export const DRAIN_LOW_WATER =
|
|
36
|
+
export const DRAIN_LOW_WATER = DEFAULT_DRAIN_LOW_WATER;
|
|
33
37
|
|
|
34
38
|
export interface MessageTargetOverLiveKitOptions {
|
|
35
39
|
/** The largest packet posted; past it the pipe ends with `RangeError`. Default {@link DEFAULT_MAX_FRAME_BYTES}. */
|
|
@@ -45,23 +49,16 @@ export interface MessageTargetOverLiveKitOptions {
|
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
/** What `messageTargetOverLiveKit` gives: a pipe of bytes to one participant, its life, and its payload ceiling. */
|
|
48
|
-
export interface LiveKitMessageTarget {
|
|
52
|
+
export interface LiveKitMessageTarget extends MessagePipe {
|
|
49
53
|
/**
|
|
50
54
|
* A `MessageTarget` (as `@statewalker/webrun-rpc` defines it) whose messages
|
|
51
|
-
* are `Uint8Array`s, one reliable data packet each.
|
|
55
|
+
* are `Uint8Array`s, one reliable data packet each. A post past
|
|
56
|
+
* `maxFrameBytes` throws `RangeError` and ends the pipe; `close()` detaches
|
|
57
|
+
* from the room, which stays connected (it is the caller's);
|
|
58
|
+
* `bufferedAmount` counts bytes whose `publishData` has not settled, and
|
|
59
|
+
* `whenDrained` waits for it to reach {@link DRAIN_LOW_WATER}.
|
|
52
60
|
*/
|
|
53
|
-
target:
|
|
54
|
-
addEventListener(type: "message", listener: Listener): void;
|
|
55
|
-
removeEventListener(type: "message", listener: Listener): void;
|
|
56
|
-
/** Publishes one packet. `TypeError` for non-bytes; `RangeError`, ending the pipe, past `maxFrameBytes`. */
|
|
57
|
-
postMessage(message: unknown): void;
|
|
58
|
-
/** Detaches from the room. The room itself stays connected: it is the caller's. */
|
|
59
|
-
close(): Promise<void>;
|
|
60
|
-
/** Bytes published whose `publishData` has not settled. */
|
|
61
|
-
readonly bufferedAmount: number;
|
|
62
|
-
/** Resolves once `bufferedAmount` is at most {@link DRAIN_LOW_WATER}, or the pipe ended. */
|
|
63
|
-
whenDrained(): Promise<void>;
|
|
64
|
-
};
|
|
61
|
+
target: MessagePipeTarget;
|
|
65
62
|
/**
|
|
66
63
|
* Aborts when the room disconnects, the participant leaves, a publish fails
|
|
67
64
|
* (the stream above lost a message), or a post exceeds `maxFrameBytes`.
|
|
@@ -112,8 +109,6 @@ export function messageTargetOverLiveKit(
|
|
|
112
109
|
`messageTargetOverLiveKit: "${peerIdentity}" is not in the room; wait for it to join`,
|
|
113
110
|
);
|
|
114
111
|
}
|
|
115
|
-
const listeners = new Set<Listener>();
|
|
116
|
-
const life = new AbortController();
|
|
117
112
|
const roomApi = room as unknown as {
|
|
118
113
|
on(ev: string, fn: (...args: unknown[]) => void): void;
|
|
119
114
|
off(ev: string, fn: (...args: unknown[]) => void): void;
|
|
@@ -127,7 +122,7 @@ export function messageTargetOverLiveKit(
|
|
|
127
122
|
let inFlight = 0;
|
|
128
123
|
const drainWaiters = new Set<() => void>();
|
|
129
124
|
const wakeDrainWaiters = (): void => {
|
|
130
|
-
if (inFlight > DRAIN_LOW_WATER
|
|
125
|
+
if (inFlight > DRAIN_LOW_WATER) return;
|
|
131
126
|
for (const w of [...drainWaiters]) w();
|
|
132
127
|
drainWaiters.clear();
|
|
133
128
|
};
|
|
@@ -139,89 +134,37 @@ export function messageTargetOverLiveKit(
|
|
|
139
134
|
unknown,
|
|
140
135
|
string | undefined,
|
|
141
136
|
];
|
|
142
|
-
if (
|
|
137
|
+
if (participant?.identity !== peerIdentity) return;
|
|
143
138
|
if (packetTopic === closeTopic) {
|
|
144
|
-
end(new Error(`webrun-streams-livekit: "${peerIdentity}" closed the pipe`)
|
|
139
|
+
control.end(new Error(`webrun-streams-livekit: "${peerIdentity}" closed the pipe`));
|
|
145
140
|
return;
|
|
146
141
|
}
|
|
147
142
|
if (packetTopic !== topic) return;
|
|
148
|
-
|
|
149
|
-
for (const listener of [...listeners]) {
|
|
150
|
-
try {
|
|
151
|
-
Promise.resolve(listener(event)).catch(() => undefined);
|
|
152
|
-
} catch {}
|
|
153
|
-
}
|
|
143
|
+
control.deliver(payload);
|
|
154
144
|
};
|
|
155
145
|
const onDisconnected = (): void =>
|
|
156
|
-
end(new Error("webrun-streams-livekit: the room disconnected")
|
|
146
|
+
control.end(new Error("webrun-streams-livekit: the room disconnected"));
|
|
157
147
|
const onParticipantDisconnected = (...args: unknown[]): void => {
|
|
158
148
|
if ((args[0] as { identity?: string } | undefined)?.identity !== peerIdentity) return;
|
|
159
|
-
end(new Error(`webrun-streams-livekit: "${peerIdentity}" left the room`)
|
|
149
|
+
control.end(new Error(`webrun-streams-livekit: "${peerIdentity}" left the room`));
|
|
160
150
|
};
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
roomApi.off("dataReceived", onData);
|
|
165
|
-
roomApi.off("disconnected", onDisconnected);
|
|
166
|
-
roomApi.off("participantDisconnected", onParticipantDisconnected);
|
|
167
|
-
life.abort(reason);
|
|
168
|
-
wakeDrainWaiters();
|
|
169
|
-
if (tellPeer) {
|
|
170
|
-
publisher
|
|
171
|
-
.publishData(new Uint8Array(0), {
|
|
172
|
-
reliable: true,
|
|
173
|
-
destinationIdentities: [peerIdentity],
|
|
174
|
-
topic: closeTopic,
|
|
175
|
-
})
|
|
176
|
-
.catch(() => undefined);
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
roomApi.on("dataReceived", onData);
|
|
180
|
-
roomApi.on("disconnected", onDisconnected);
|
|
181
|
-
roomApi.on("participantDisconnected", onParticipantDisconnected);
|
|
182
|
-
|
|
183
|
-
return {
|
|
184
|
-
signal: life.signal,
|
|
185
|
-
maxMessageSize,
|
|
186
|
-
target: {
|
|
187
|
-
addEventListener: (_type, listener) => {
|
|
188
|
-
listeners.add(listener);
|
|
189
|
-
},
|
|
190
|
-
removeEventListener: (_type, listener) => {
|
|
191
|
-
listeners.delete(listener);
|
|
192
|
-
},
|
|
193
|
-
postMessage(message) {
|
|
194
|
-
const bytes = toBytes(message);
|
|
195
|
-
if (!bytes) {
|
|
196
|
-
throw new TypeError(
|
|
197
|
-
`webrun-streams-livekit: a byte pipe carries Uint8Array, ArrayBuffer or a view, got ${Object.prototype.toString.call(message)}`,
|
|
198
|
-
);
|
|
199
|
-
}
|
|
200
|
-
if (life.signal.aborted) return; // `signal` says so
|
|
201
|
-
if (bytes.byteLength > maxFrameBytes) {
|
|
202
|
-
const err = new RangeError(
|
|
203
|
-
`webrun-streams-livekit: a ${bytes.byteLength}-byte packet exceeds maxFrameBytes (${maxFrameBytes})`,
|
|
204
|
-
);
|
|
205
|
-
end(err);
|
|
206
|
-
throw err;
|
|
207
|
-
}
|
|
151
|
+
const control = newMessagePipe(
|
|
152
|
+
{
|
|
153
|
+
send: (bytes) => {
|
|
208
154
|
const size = bytes.byteLength;
|
|
209
155
|
inFlight += size;
|
|
210
156
|
publisher
|
|
211
|
-
|
|
212
|
-
.publishData(bytes.slice(), {
|
|
213
|
-
reliable: true,
|
|
214
|
-
destinationIdentities: [peerIdentity],
|
|
215
|
-
topic,
|
|
216
|
-
})
|
|
157
|
+
.publishData(bytes, { reliable: true, destinationIdentities: [peerIdentity], topic })
|
|
217
158
|
.then(
|
|
218
159
|
() => undefined,
|
|
160
|
+
// The stream above lost a message: end, and tell the peer.
|
|
219
161
|
(err: unknown) =>
|
|
220
|
-
end(
|
|
162
|
+
control.end(
|
|
221
163
|
new Error(
|
|
222
164
|
`webrun-streams-livekit: a reliable publish failed: ${(err as Error)?.message ?? String(err)}`,
|
|
223
165
|
{ cause: err },
|
|
224
166
|
),
|
|
167
|
+
true,
|
|
225
168
|
),
|
|
226
169
|
)
|
|
227
170
|
.finally(() => {
|
|
@@ -229,25 +172,37 @@ export function messageTargetOverLiveKit(
|
|
|
229
172
|
wakeDrainWaiters();
|
|
230
173
|
});
|
|
231
174
|
},
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (
|
|
241
|
-
|
|
175
|
+
bufferedAmount: () => inFlight,
|
|
176
|
+
whenDrained: () => new Promise<void>((resolve) => drainWaiters.add(resolve)),
|
|
177
|
+
/** `local`: the pipe ended on this side, and the room is still there to say so. */
|
|
178
|
+
close: (local) => {
|
|
179
|
+
roomApi.off("dataReceived", onData);
|
|
180
|
+
roomApi.off("disconnected", onDisconnected);
|
|
181
|
+
roomApi.off("participantDisconnected", onParticipantDisconnected);
|
|
182
|
+
drainWaiters.clear();
|
|
183
|
+
if (local) {
|
|
184
|
+
publisher
|
|
185
|
+
.publishData(new Uint8Array(0), {
|
|
186
|
+
reliable: true,
|
|
187
|
+
destinationIdentities: [peerIdentity],
|
|
188
|
+
topic: closeTopic,
|
|
189
|
+
})
|
|
190
|
+
.catch(() => undefined);
|
|
191
|
+
}
|
|
242
192
|
},
|
|
243
193
|
},
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
194
|
+
{
|
|
195
|
+
name: "webrun-streams-livekit",
|
|
196
|
+
maxFrameBytes,
|
|
197
|
+
maxMessageSize,
|
|
198
|
+
// livekit-client serializes the packet after an await, and may reuse
|
|
199
|
+
// the payload buffer it hands out.
|
|
200
|
+
copyOnSend: true,
|
|
201
|
+
copyOnDeliver: true,
|
|
202
|
+
},
|
|
203
|
+
);
|
|
204
|
+
roomApi.on("dataReceived", onData);
|
|
205
|
+
roomApi.on("disconnected", onDisconnected);
|
|
206
|
+
roomApi.on("participantDisconnected", onParticipantDisconnected);
|
|
207
|
+
return control.pipe as LiveKitMessageTarget;
|
|
253
208
|
}
|