@lesomnus/grpc-dgram 0.0.1 → 0.1.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/CHANGELOG.md +43 -0
- package/README.md +61 -21
- package/dist/{conn-DOmx4nbt.mjs → conn-CmYPHTPR.mjs} +327 -23
- package/dist/conn-Dc57BC-h.d.mts +775 -0
- package/dist/index.d.mts +4 -3
- package/dist/index.mjs +238 -48
- package/dist/{protocol-K4Zy8MuQ.mjs → protocol-CTxSUZQF.mjs} +60 -5
- package/dist/transport/connect.d.mts +1 -1
- package/dist/transport/node-udp.d.mts +5 -4
- package/dist/transport/node-udp.mjs +16 -10
- package/dist/transport/port.d.mts +5 -4
- package/dist/transport/port.mjs +15 -8
- package/dist/transport/webrtc.d.mts +5 -4
- package/dist/transport/webrtc.mjs +13 -7
- package/dist/transport/websocket.d.mts +5 -4
- package/dist/transport/websocket.mjs +14 -8
- package/dist/transport/webtransport.d.mts +53 -0
- package/dist/transport/webtransport.mjs +144 -0
- package/dist/wasm/worker.mjs +20 -5
- package/dist/wasm.d.mts +7 -3
- package/dist/wasm.mjs +21 -9
- package/dist/{wire-BR8KiyRg.mjs → wire-DqHx0oUs.mjs} +211 -39
- package/package.json +6 -3
- package/dist/conn-DTWG9vIx.d.mts +0 -331
- package/dist/server-xIr1mwqq.d.mts +0 -112
|
@@ -9,16 +9,17 @@ const DefaultWasmExec = "/wasm_exec.js";
|
|
|
9
9
|
async function startInstance(app, opts = {}) {
|
|
10
10
|
const name = opts.entryPoint ?? "drpcServe";
|
|
11
11
|
const entry = claimEntryPoint(name);
|
|
12
|
+
const readyTimeoutMs = opts.readyTimeoutMs ?? 1e4;
|
|
12
13
|
try {
|
|
13
14
|
const go = opts.go ?? await newGo(String(opts.wasmExec ?? "/wasm_exec.js"));
|
|
14
15
|
const instance = await instantiate(app, go.importObject);
|
|
15
16
|
const run = go.run(instance);
|
|
16
|
-
return live(name, go, await awaitPublish(entry.published, run, name,
|
|
17
|
+
return live(name, go, await awaitPublish(entry.published, run, name, readyTimeoutMs), run, readyTimeoutMs);
|
|
17
18
|
} finally {
|
|
18
19
|
entry.release();
|
|
19
20
|
}
|
|
20
21
|
}
|
|
21
|
-
function live(name, go, serve, run) {
|
|
22
|
+
function live(name, go, serve, run, timeoutMs) {
|
|
22
23
|
let dead;
|
|
23
24
|
let announce;
|
|
24
25
|
const exited = new Promise((res) => {
|
|
@@ -34,18 +35,29 @@ function live(name, go, serve, run) {
|
|
|
34
35
|
return {
|
|
35
36
|
entryPoint: name,
|
|
36
37
|
exited,
|
|
37
|
-
serve(port) {
|
|
38
|
+
serve(port, entryPoint, readyTimeoutMs) {
|
|
38
39
|
if (dead !== void 0) throw new Error(`wasm: the instance behind globalThis.${name} has exited${dead.cause instanceof Error ? `: ${dead.cause.message}` : ""}`);
|
|
39
|
-
|
|
40
|
+
if (entryPoint === void 0 || entryPoint === name) {
|
|
41
|
+
serve(port);
|
|
42
|
+
return Promise.resolve();
|
|
43
|
+
}
|
|
44
|
+
return entryPointNamed(entryPoint, readyTimeoutMs ?? timeoutMs, exited).then((fn) => {
|
|
45
|
+
if (dead !== void 0) throw new Error(`wasm: the instance behind globalThis.${name} exited while waiting for globalThis.${entryPoint}`);
|
|
46
|
+
fn(port);
|
|
47
|
+
});
|
|
40
48
|
}
|
|
41
49
|
};
|
|
42
50
|
}
|
|
43
51
|
function makeInert(go) {
|
|
44
52
|
const inner = go;
|
|
45
53
|
if (typeof inner._resume === "function") inner._resume = noop;
|
|
54
|
+
if (inner._scheduledTimeouts instanceof Map) {
|
|
55
|
+
for (const t of inner._scheduledTimeouts.values()) clearTimeout(t);
|
|
56
|
+
inner._scheduledTimeouts.clear();
|
|
57
|
+
}
|
|
46
58
|
}
|
|
47
59
|
const awaiting = /* @__PURE__ */ new Set();
|
|
48
|
-
function claimEntryPoint(name) {
|
|
60
|
+
function claimEntryPoint(name, after = "restore") {
|
|
49
61
|
if (awaiting.has(name)) throw new Error(`wasm: another instance is already waiting for globalThis.${name}; give one of them its own entryPoint`);
|
|
50
62
|
const g = globalThis;
|
|
51
63
|
const prev = Object.getOwnPropertyDescriptor(g, name);
|
|
@@ -70,11 +82,54 @@ function claimEntryPoint(name) {
|
|
|
70
82
|
if (released) return;
|
|
71
83
|
released = true;
|
|
72
84
|
awaiting.delete(name);
|
|
85
|
+
if (after === "keep" && value !== void 0) {
|
|
86
|
+
Object.defineProperty(g, name, {
|
|
87
|
+
configurable: true,
|
|
88
|
+
writable: true,
|
|
89
|
+
enumerable: true,
|
|
90
|
+
value
|
|
91
|
+
});
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
73
94
|
if (prev === void 0) Reflect.deleteProperty(g, name);
|
|
74
95
|
else Object.defineProperty(g, name, prev);
|
|
75
96
|
}
|
|
76
97
|
};
|
|
77
98
|
}
|
|
99
|
+
const waits = /* @__PURE__ */ new Map();
|
|
100
|
+
function entryPointNamed(name, timeoutMs, exited) {
|
|
101
|
+
const g = globalThis;
|
|
102
|
+
const v = Object.prototype.hasOwnProperty.call(g, name) ? g[name] : void 0;
|
|
103
|
+
if (typeof v === "function") return Promise.resolve(v);
|
|
104
|
+
if (v !== void 0) return Promise.reject(/* @__PURE__ */ new Error(`wasm: globalThis.${name} is a ${typeof v}, not a function taking one port`));
|
|
105
|
+
const pending = waits.get(name);
|
|
106
|
+
if (pending !== void 0) return pending;
|
|
107
|
+
if (awaiting.has(name)) return Promise.reject(/* @__PURE__ */ new Error(`wasm: an instance is starting on globalThis.${name}; it cannot be dialled as a second entry point at the same time`));
|
|
108
|
+
const w = watchEntryPoint(name, timeoutMs, exited);
|
|
109
|
+
waits.set(name, w);
|
|
110
|
+
w.catch(noop).then(() => {
|
|
111
|
+
if (waits.get(name) === w) waits.delete(name);
|
|
112
|
+
});
|
|
113
|
+
return w;
|
|
114
|
+
}
|
|
115
|
+
function watchEntryPoint(name, timeoutMs, exited) {
|
|
116
|
+
const entry = claimEntryPoint(name, "keep");
|
|
117
|
+
let timer;
|
|
118
|
+
const arms = [entry.published, exited.then((cause) => {
|
|
119
|
+
throw new Error(`wasm: the instance exited before publishing globalThis.${name}`, { cause });
|
|
120
|
+
})];
|
|
121
|
+
if (timeoutMs > 0) arms.push(new Promise((_, rej) => {
|
|
122
|
+
timer = setTimeout(() => rej(/* @__PURE__ */ new Error(`wasm: no globalThis.${name} after ${timeoutMs} ms — is that the name the second gateway serves under, and does it reach Serve?`)), timeoutMs);
|
|
123
|
+
unrefTimer(timer);
|
|
124
|
+
}));
|
|
125
|
+
return Promise.race(arms).then((fn) => {
|
|
126
|
+
if (typeof fn !== "function") throw new Error(`wasm: globalThis.${name} was set to a ${typeof fn}, not a function taking one port`);
|
|
127
|
+
return fn;
|
|
128
|
+
}).finally(() => {
|
|
129
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
130
|
+
entry.release();
|
|
131
|
+
});
|
|
132
|
+
}
|
|
78
133
|
async function awaitPublish(published, run, name, timeoutMs) {
|
|
79
134
|
let timer;
|
|
80
135
|
const arms = [published, run.then(() => {
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { r as Server } from "../server-xIr1mwqq.mjs";
|
|
1
|
+
import { F as EnvelopeSender, I as FrameContext, L as FrameHandler, P as ConnAttacher, R as TransportInfo, Y as Frame, b as Server, i as ConnOptions, r as Conn } from "../conn-Dc57BC-h.mjs";
|
|
3
2
|
import { Socket } from "node:dgram";
|
|
4
3
|
//#region src/transport/node-udp/index.d.ts
|
|
5
4
|
declare const DefaultMaxMessageSize = 1200;
|
|
6
|
-
declare class UdpTransport implements FrameHandler, TransportInfo, ConnAttacher {
|
|
5
|
+
declare class UdpTransport implements FrameHandler, TransportInfo, ConnAttacher, EnvelopeSender {
|
|
7
6
|
private readonly socket;
|
|
8
7
|
private readonly max;
|
|
9
8
|
private conn;
|
|
@@ -13,10 +12,11 @@ declare class UdpTransport implements FrameHandler, TransportInfo, ConnAttacher
|
|
|
13
12
|
});
|
|
14
13
|
reliable(): boolean;
|
|
15
14
|
attachConn(conn: Conn): void;
|
|
15
|
+
sendFrames(frames: readonly Frame[]): Promise<void>;
|
|
16
16
|
handle(f: Frame): Promise<void>;
|
|
17
17
|
close(err?: unknown): void;
|
|
18
18
|
}
|
|
19
|
-
declare class UdpGateway implements FrameHandler {
|
|
19
|
+
declare class UdpGateway implements FrameHandler, EnvelopeSender {
|
|
20
20
|
private readonly socket;
|
|
21
21
|
private readonly max;
|
|
22
22
|
private readonly peers;
|
|
@@ -26,6 +26,7 @@ declare class UdpGateway implements FrameHandler {
|
|
|
26
26
|
maxMessageSize?: number;
|
|
27
27
|
});
|
|
28
28
|
serve(server: Server): Promise<void>;
|
|
29
|
+
sendFrames(frames: readonly Frame[], ctx?: FrameContext): Promise<void>;
|
|
29
30
|
handle(f: Frame, ctx?: FrameContext): Promise<void>;
|
|
30
31
|
close(): void;
|
|
31
32
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as Conn } from "../conn-CmYPHTPR.mjs";
|
|
2
|
+
import { c as decodeEnvelope, it as unpack, u as encodeEnvelope } from "../wire-DqHx0oUs.mjs";
|
|
2
3
|
import { n as MessageTooLargeError } from "../status-DZwMDWIn.mjs";
|
|
3
|
-
import { n as Conn } from "../conn-DOmx4nbt.mjs";
|
|
4
4
|
import { createSocket } from "node:dgram";
|
|
5
5
|
//#region src/transport/node-udp/index.ts
|
|
6
6
|
const DefaultMaxMessageSize = 1200;
|
|
@@ -36,7 +36,7 @@ var UdpTransport = class {
|
|
|
36
36
|
this.socket.on("message", (data) => {
|
|
37
37
|
let frames;
|
|
38
38
|
try {
|
|
39
|
-
frames =
|
|
39
|
+
frames = decodeEnvelope(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
|
|
40
40
|
} catch {
|
|
41
41
|
return;
|
|
42
42
|
}
|
|
@@ -48,11 +48,14 @@ var UdpTransport = class {
|
|
|
48
48
|
});
|
|
49
49
|
this.socket.on("close", () => this.conn?.close());
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
const data =
|
|
53
|
-
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`node-udp: ${data.length}-byte
|
|
51
|
+
sendFrames(frames) {
|
|
52
|
+
const data = encodeEnvelope(frames);
|
|
53
|
+
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`node-udp: ${data.length}-byte envelope over the ${this.max}-byte limit`);
|
|
54
54
|
return sendDatagram(this.socket, data);
|
|
55
55
|
}
|
|
56
|
+
handle(f) {
|
|
57
|
+
return this.sendFrames([f]);
|
|
58
|
+
}
|
|
56
59
|
close(err) {
|
|
57
60
|
if (this.closed) return;
|
|
58
61
|
this.closed = true;
|
|
@@ -87,7 +90,7 @@ var UdpGateway = class {
|
|
|
87
90
|
});
|
|
88
91
|
let frames;
|
|
89
92
|
try {
|
|
90
|
-
frames =
|
|
93
|
+
frames = decodeEnvelope(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
|
|
91
94
|
} catch {
|
|
92
95
|
return;
|
|
93
96
|
}
|
|
@@ -100,15 +103,18 @@ var UdpGateway = class {
|
|
|
100
103
|
this.socket.on("error", () => this.close());
|
|
101
104
|
});
|
|
102
105
|
}
|
|
103
|
-
|
|
106
|
+
sendFrames(frames, ctx = {}) {
|
|
104
107
|
const key = ctx.peer;
|
|
105
108
|
if (typeof key !== "string") return Promise.reject(/* @__PURE__ */ new Error(`node-udp: no gateway peer in context (got ${String(key)})`));
|
|
106
109
|
const target = this.peers.get(key);
|
|
107
110
|
if (target === void 0) return Promise.reject(/* @__PURE__ */ new Error(`node-udp: peer ${key} is unknown`));
|
|
108
|
-
const data =
|
|
109
|
-
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`node-udp: ${data.length}-byte
|
|
111
|
+
const data = encodeEnvelope(frames);
|
|
112
|
+
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`node-udp: ${data.length}-byte envelope over the ${this.max}-byte limit`);
|
|
110
113
|
return sendDatagram(this.socket, data, target);
|
|
111
114
|
}
|
|
115
|
+
handle(f, ctx = {}) {
|
|
116
|
+
return this.sendFrames([f], ctx);
|
|
117
|
+
}
|
|
112
118
|
close() {
|
|
113
119
|
if (this.closed) return;
|
|
114
120
|
this.closed = true;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { r as Server } from "../server-xIr1mwqq.mjs";
|
|
1
|
+
import { F as EnvelopeSender, I as FrameContext, L as FrameHandler, P as ConnAttacher, R as TransportInfo, Y as Frame, b as Server, i as ConnOptions, r as Conn } from "../conn-Dc57BC-h.mjs";
|
|
3
2
|
//#region src/transport/port/index.d.ts
|
|
4
3
|
declare const DefaultMaxMessageSize = 0;
|
|
5
4
|
interface PortLike {
|
|
@@ -16,12 +15,13 @@ interface PortOptions {
|
|
|
16
15
|
maxMessageSize?: number;
|
|
17
16
|
transfer?: boolean;
|
|
18
17
|
}
|
|
19
|
-
declare class PortTransport implements FrameHandler, TransportInfo, ConnAttacher {
|
|
18
|
+
declare class PortTransport implements FrameHandler, TransportInfo, ConnAttacher, EnvelopeSender {
|
|
20
19
|
private readonly pt;
|
|
21
20
|
private attached;
|
|
22
21
|
constructor(port: PortLike, opts?: PortOptions);
|
|
23
22
|
reliable(): boolean;
|
|
24
23
|
attachConn(conn: Conn): void;
|
|
24
|
+
sendFrames(frames: readonly Frame[]): Promise<void>;
|
|
25
25
|
handle(f: Frame): Promise<void>;
|
|
26
26
|
close(cause?: unknown): void;
|
|
27
27
|
}
|
|
@@ -32,7 +32,7 @@ interface WorkerDialOptions extends PortOptions {
|
|
|
32
32
|
message?: unknown;
|
|
33
33
|
}
|
|
34
34
|
declare function dialWorker(worker: WorkerLike, opts?: ConnOptions & WorkerDialOptions): Conn;
|
|
35
|
-
declare class PortGateway implements FrameHandler, TransportInfo {
|
|
35
|
+
declare class PortGateway implements FrameHandler, TransportInfo, EnvelopeSender {
|
|
36
36
|
private readonly o;
|
|
37
37
|
private next;
|
|
38
38
|
private readonly ports;
|
|
@@ -45,6 +45,7 @@ declare class PortGateway implements FrameHandler, TransportInfo {
|
|
|
45
45
|
servePeer(server: Server, port: PortLike, opts?: {
|
|
46
46
|
signal?: AbortSignal;
|
|
47
47
|
}): Promise<unknown>;
|
|
48
|
+
sendFrames(frames: readonly Frame[], ctx?: FrameContext): Promise<void>;
|
|
48
49
|
handle(f: Frame, ctx?: FrameContext): Promise<void>;
|
|
49
50
|
close(): void;
|
|
50
51
|
}
|
package/dist/transport/port.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as Conn } from "../conn-CmYPHTPR.mjs";
|
|
2
|
+
import { F as Latch, V as abortListener, Y as noop, c as decodeEnvelope, it as unpack, u as encodeEnvelope } from "../wire-DqHx0oUs.mjs";
|
|
2
3
|
import { n as MessageTooLargeError, r as StatusError } from "../status-DZwMDWIn.mjs";
|
|
3
|
-
import { n as Conn } from "../conn-DOmx4nbt.mjs";
|
|
4
4
|
//#region src/transport/port/index.ts
|
|
5
5
|
const DefaultMaxMessageSize = 0;
|
|
6
6
|
const GOODBYE = /* @__PURE__ */ new Uint8Array(0);
|
|
@@ -66,8 +66,9 @@ var Port = class {
|
|
|
66
66
|
wakeAll(this.rxWaiters);
|
|
67
67
|
}
|
|
68
68
|
async send(frames) {
|
|
69
|
-
|
|
70
|
-
|
|
69
|
+
if (frames.length === 0) return;
|
|
70
|
+
const data = encodeEnvelope(frames);
|
|
71
|
+
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`port: ${data.length}-byte envelope over the ${this.max}-byte limit`);
|
|
71
72
|
if (this.dead.tripped) throw this.closedErr();
|
|
72
73
|
try {
|
|
73
74
|
this.post(data);
|
|
@@ -101,7 +102,7 @@ var Port = class {
|
|
|
101
102
|
}
|
|
102
103
|
let frames;
|
|
103
104
|
try {
|
|
104
|
-
frames =
|
|
105
|
+
frames = decodeEnvelope(data);
|
|
105
106
|
} catch {
|
|
106
107
|
continue;
|
|
107
108
|
}
|
|
@@ -152,8 +153,11 @@ var PortTransport = class {
|
|
|
152
153
|
this.close();
|
|
153
154
|
})();
|
|
154
155
|
}
|
|
156
|
+
sendFrames(frames) {
|
|
157
|
+
return this.pt.send(frames);
|
|
158
|
+
}
|
|
155
159
|
handle(f) {
|
|
156
|
-
return this.
|
|
160
|
+
return this.sendFrames([f]);
|
|
157
161
|
}
|
|
158
162
|
close(cause) {
|
|
159
163
|
this.pt.close(cause);
|
|
@@ -225,12 +229,15 @@ var PortGateway = class {
|
|
|
225
229
|
this.drop(port, b);
|
|
226
230
|
}
|
|
227
231
|
}
|
|
228
|
-
|
|
232
|
+
sendFrames(frames, ctx = {}) {
|
|
229
233
|
const key = ctx.peer;
|
|
230
234
|
if (typeof key !== "number") return Promise.reject(/* @__PURE__ */ new Error(`port: no gateway peer in context (got ${String(key)})`));
|
|
231
235
|
const pt = this.peers.get(key);
|
|
232
236
|
if (pt === void 0) return Promise.reject(/* @__PURE__ */ new Error(`port: peer ${key} is disconnected`));
|
|
233
|
-
return pt.send(
|
|
237
|
+
return pt.send(frames);
|
|
238
|
+
}
|
|
239
|
+
handle(f, ctx = {}) {
|
|
240
|
+
return this.sendFrames([f], ctx);
|
|
234
241
|
}
|
|
235
242
|
close() {
|
|
236
243
|
for (const b of [...this.ports.values()]) b.pt.close();
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { r as Server } from "../server-xIr1mwqq.mjs";
|
|
1
|
+
import { F as EnvelopeSender, I as FrameContext, L as FrameHandler, Y as Frame, b as Server, r as Conn } from "../conn-Dc57BC-h.mjs";
|
|
3
2
|
//#region src/transport/webrtc/index.d.ts
|
|
4
3
|
declare const DefaultMaxMessageSizeUnreliable = 1200;
|
|
5
4
|
declare const DefaultMaxMessageSizeReliable: number;
|
|
@@ -28,17 +27,18 @@ interface DataChannelOptions {
|
|
|
28
27
|
sendStallTimeoutMs?: number;
|
|
29
28
|
}
|
|
30
29
|
declare function channelReliable(dc: DataChannelLike): boolean;
|
|
31
|
-
declare class DataChannelTransport {
|
|
30
|
+
declare class DataChannelTransport implements FrameHandler, EnvelopeSender {
|
|
32
31
|
private readonly ch;
|
|
33
32
|
private attached;
|
|
34
33
|
private closed;
|
|
35
34
|
constructor(dc: DataChannelLike, opts?: DataChannelOptions);
|
|
36
35
|
reliable(): boolean;
|
|
37
36
|
attachConn(conn: Conn): void;
|
|
37
|
+
sendFrames(frames: readonly Frame[], ctx?: FrameContext): Promise<void>;
|
|
38
38
|
handle(f: Frame, ctx?: FrameContext): Promise<void>;
|
|
39
39
|
close(): void;
|
|
40
40
|
}
|
|
41
|
-
declare class DataChannelGateway {
|
|
41
|
+
declare class DataChannelGateway implements FrameHandler, EnvelopeSender {
|
|
42
42
|
private readonly o;
|
|
43
43
|
private next;
|
|
44
44
|
private readonly chans;
|
|
@@ -50,6 +50,7 @@ declare class DataChannelGateway {
|
|
|
50
50
|
servePeer(server: Server, dc: DataChannelLike, opts?: {
|
|
51
51
|
signal?: AbortSignal;
|
|
52
52
|
}): Promise<unknown>;
|
|
53
|
+
sendFrames(frames: readonly Frame[], ctx?: FrameContext): Promise<void>;
|
|
53
54
|
handle(f: Frame, ctx?: FrameContext): Promise<void>;
|
|
54
55
|
}
|
|
55
56
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { F as Latch, V as abortListener, Y as noop, c as decodeEnvelope, it as unpack, tt as unrefTimer, u as encodeEnvelope } from "../wire-DqHx0oUs.mjs";
|
|
2
2
|
import { n as MessageTooLargeError } from "../status-DZwMDWIn.mjs";
|
|
3
3
|
//#region src/transport/webrtc/index.ts
|
|
4
4
|
const DefaultMaxMessageSizeUnreliable = 1200;
|
|
@@ -64,8 +64,8 @@ var Channel = class {
|
|
|
64
64
|
wakeAll(this.rxWaiters);
|
|
65
65
|
}
|
|
66
66
|
async send(frames, signal) {
|
|
67
|
-
const data =
|
|
68
|
-
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`webrtc: ${data.length}-byte
|
|
67
|
+
const data = encodeEnvelope(frames);
|
|
68
|
+
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`webrtc: ${data.length}-byte envelope over the ${this.max}-byte limit`);
|
|
69
69
|
const stalled = new Latch();
|
|
70
70
|
let stallTimer;
|
|
71
71
|
if (this.stallMs > 0) {
|
|
@@ -129,7 +129,7 @@ var Channel = class {
|
|
|
129
129
|
if (data !== void 0) {
|
|
130
130
|
let frames;
|
|
131
131
|
try {
|
|
132
|
-
frames =
|
|
132
|
+
frames = decodeEnvelope(data);
|
|
133
133
|
} catch {
|
|
134
134
|
continue;
|
|
135
135
|
}
|
|
@@ -168,8 +168,11 @@ var DataChannelTransport = class {
|
|
|
168
168
|
conn.close(err);
|
|
169
169
|
})();
|
|
170
170
|
}
|
|
171
|
+
sendFrames(frames, ctx = {}) {
|
|
172
|
+
return this.ch.send(frames, ctx.signal);
|
|
173
|
+
}
|
|
171
174
|
handle(f, ctx = {}) {
|
|
172
|
-
return this.
|
|
175
|
+
return this.sendFrames([f], ctx);
|
|
173
176
|
}
|
|
174
177
|
close() {
|
|
175
178
|
if (this.closed) return;
|
|
@@ -228,12 +231,15 @@ var DataChannelGateway = class {
|
|
|
228
231
|
this.drop(dc, b);
|
|
229
232
|
}
|
|
230
233
|
}
|
|
231
|
-
|
|
234
|
+
sendFrames(frames, ctx = {}) {
|
|
232
235
|
const key = ctx.peer;
|
|
233
236
|
if (typeof key !== "number") return Promise.reject(/* @__PURE__ */ new Error(`webrtc: no gateway peer in context (got ${String(key)})`));
|
|
234
237
|
const ch = this.peers.get(key);
|
|
235
238
|
if (ch === void 0) return Promise.reject(/* @__PURE__ */ new Error(`webrtc: peer ${key} is gone`));
|
|
236
|
-
return ch.send(
|
|
239
|
+
return ch.send(frames, ctx.signal);
|
|
240
|
+
}
|
|
241
|
+
handle(f, ctx = {}) {
|
|
242
|
+
return this.sendFrames([f], ctx);
|
|
237
243
|
}
|
|
238
244
|
};
|
|
239
245
|
//#endregion
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { r as Server } from "../server-xIr1mwqq.mjs";
|
|
1
|
+
import { F as EnvelopeSender, I as FrameContext, L as FrameHandler, P as ConnAttacher, R as TransportInfo, Y as Frame, b as Server, i as ConnOptions, r as Conn } from "../conn-Dc57BC-h.mjs";
|
|
3
2
|
//#region src/transport/websocket/index.d.ts
|
|
4
3
|
declare const DefaultMaxMessageSize = 0;
|
|
5
4
|
declare const DefaultMaxBufferedAmount: number;
|
|
@@ -26,17 +25,18 @@ interface WebSocketOptions {
|
|
|
26
25
|
keepaliveIntervalMs?: number;
|
|
27
26
|
keepaliveTimeoutMs?: number;
|
|
28
27
|
}
|
|
29
|
-
declare class WebSocketTransport implements FrameHandler, TransportInfo, ConnAttacher {
|
|
28
|
+
declare class WebSocketTransport implements FrameHandler, TransportInfo, ConnAttacher, EnvelopeSender {
|
|
30
29
|
private readonly sock;
|
|
31
30
|
private attached;
|
|
32
31
|
private closed;
|
|
33
32
|
constructor(ws: WebSocketLike, opts?: WebSocketOptions);
|
|
34
33
|
reliable(): boolean;
|
|
35
34
|
attachConn(conn: Conn): void;
|
|
35
|
+
sendFrames(frames: readonly Frame[], ctx?: FrameContext): Promise<void>;
|
|
36
36
|
handle(f: Frame, ctx?: FrameContext): Promise<void>;
|
|
37
37
|
close(): void;
|
|
38
38
|
}
|
|
39
|
-
declare class WebSocketGateway implements FrameHandler, TransportInfo {
|
|
39
|
+
declare class WebSocketGateway implements FrameHandler, TransportInfo, EnvelopeSender {
|
|
40
40
|
private readonly o;
|
|
41
41
|
private next;
|
|
42
42
|
private readonly socks;
|
|
@@ -49,6 +49,7 @@ declare class WebSocketGateway implements FrameHandler, TransportInfo {
|
|
|
49
49
|
servePeer(server: Server, ws: WebSocketLike, opts?: {
|
|
50
50
|
signal?: AbortSignal;
|
|
51
51
|
}): Promise<unknown>;
|
|
52
|
+
sendFrames(frames: readonly Frame[], ctx?: FrameContext): Promise<void>;
|
|
52
53
|
handle(f: Frame, ctx?: FrameContext): Promise<void>;
|
|
53
54
|
close(): void;
|
|
54
55
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as Conn } from "../conn-CmYPHTPR.mjs";
|
|
2
|
+
import { F as Latch, V as abortListener, Y as noop, c as decodeEnvelope, it as unpack, tt as unrefTimer, u as encodeEnvelope } from "../wire-DqHx0oUs.mjs";
|
|
2
3
|
import { n as MessageTooLargeError, r as StatusError } from "../status-DZwMDWIn.mjs";
|
|
3
|
-
import { n as Conn } from "../conn-DOmx4nbt.mjs";
|
|
4
4
|
//#region src/transport/websocket/index.ts
|
|
5
5
|
const CONNECTING = 0;
|
|
6
6
|
const OPEN = 1;
|
|
@@ -129,8 +129,8 @@ var Socket = class {
|
|
|
129
129
|
wakeAll(this.rxWaiters);
|
|
130
130
|
}
|
|
131
131
|
async send(frames, signal) {
|
|
132
|
-
const data =
|
|
133
|
-
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`websocket: ${data.length}-byte
|
|
132
|
+
const data = encodeEnvelope(frames);
|
|
133
|
+
if (this.max > 0 && data.length > this.max) throw new MessageTooLargeError(`websocket: ${data.length}-byte envelope over the ${this.max}-byte limit`);
|
|
134
134
|
const stalled = new Latch();
|
|
135
135
|
let stallTimer;
|
|
136
136
|
if (this.stallMs > 0) {
|
|
@@ -198,7 +198,7 @@ var Socket = class {
|
|
|
198
198
|
if (data !== void 0) {
|
|
199
199
|
let frames;
|
|
200
200
|
try {
|
|
201
|
-
frames =
|
|
201
|
+
frames = decodeEnvelope(data);
|
|
202
202
|
} catch {
|
|
203
203
|
continue;
|
|
204
204
|
}
|
|
@@ -244,8 +244,11 @@ var WebSocketTransport = class {
|
|
|
244
244
|
this.close();
|
|
245
245
|
})();
|
|
246
246
|
}
|
|
247
|
+
sendFrames(frames, ctx = {}) {
|
|
248
|
+
return this.sock.send(frames, ctx.signal);
|
|
249
|
+
}
|
|
247
250
|
handle(f, ctx = {}) {
|
|
248
|
-
return this.
|
|
251
|
+
return this.sendFrames([f], ctx);
|
|
249
252
|
}
|
|
250
253
|
close() {
|
|
251
254
|
if (this.closed) return;
|
|
@@ -304,12 +307,15 @@ var WebSocketGateway = class {
|
|
|
304
307
|
this.drop(ws, b);
|
|
305
308
|
}
|
|
306
309
|
}
|
|
307
|
-
|
|
310
|
+
sendFrames(frames, ctx = {}) {
|
|
308
311
|
const key = ctx.peer;
|
|
309
312
|
if (typeof key !== "number") return Promise.reject(/* @__PURE__ */ new Error(`websocket: no gateway peer in context (got ${String(key)})`));
|
|
310
313
|
const sock = this.peers.get(key);
|
|
311
314
|
if (sock === void 0) return Promise.reject(/* @__PURE__ */ new Error(`websocket: peer ${key} is disconnected`));
|
|
312
|
-
return sock.send(
|
|
315
|
+
return sock.send(frames, ctx.signal);
|
|
316
|
+
}
|
|
317
|
+
handle(f, ctx = {}) {
|
|
318
|
+
return this.sendFrames([f], ctx);
|
|
313
319
|
}
|
|
314
320
|
close() {
|
|
315
321
|
for (const b of [...this.socks.values()]) b.sock.close();
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { F as EnvelopeSender, L as FrameHandler, P as ConnAttacher, R as TransportInfo, Y as Frame, i as ConnOptions, r as Conn } from "../conn-Dc57BC-h.mjs";
|
|
2
|
+
//#region src/transport/webtransport/index.d.ts
|
|
3
|
+
declare const DefaultMaxMessageSize = 1200;
|
|
4
|
+
interface WebTransportLike {
|
|
5
|
+
readonly ready: Promise<void>;
|
|
6
|
+
readonly closed: Promise<unknown>;
|
|
7
|
+
readonly datagrams: {
|
|
8
|
+
readonly readable: ReadableStream<Uint8Array>;
|
|
9
|
+
readonly writable?: WritableStream<Uint8Array>;
|
|
10
|
+
createWritable?(): WritableStream<Uint8Array>;
|
|
11
|
+
readonly maxDatagramSize?: number;
|
|
12
|
+
};
|
|
13
|
+
close(info?: unknown): void;
|
|
14
|
+
}
|
|
15
|
+
interface WebTransportDatagramOptions {
|
|
16
|
+
maxMessageSize?: number;
|
|
17
|
+
}
|
|
18
|
+
interface WebTransportDialOptions {
|
|
19
|
+
allowPooling?: boolean;
|
|
20
|
+
congestionControl?: 'default' | 'throughput' | 'low-latency';
|
|
21
|
+
protocols?: string[];
|
|
22
|
+
requireUnreliable?: boolean;
|
|
23
|
+
serverCertificateHashes?: {
|
|
24
|
+
algorithm: string;
|
|
25
|
+
value: ArrayBuffer | ArrayBufferView<ArrayBuffer>;
|
|
26
|
+
}[];
|
|
27
|
+
}
|
|
28
|
+
declare class WebTransportDatagramTransport implements FrameHandler, TransportInfo, ConnAttacher, EnvelopeSender {
|
|
29
|
+
private readonly wt;
|
|
30
|
+
private readonly max;
|
|
31
|
+
private readonly writer;
|
|
32
|
+
private reader;
|
|
33
|
+
private readonly opened;
|
|
34
|
+
private readonly dead;
|
|
35
|
+
private err;
|
|
36
|
+
private attached;
|
|
37
|
+
private closed;
|
|
38
|
+
constructor(wt: WebTransportLike, opts?: WebTransportDatagramOptions);
|
|
39
|
+
reliable(): boolean;
|
|
40
|
+
attachConn(conn: Conn): void;
|
|
41
|
+
private pump;
|
|
42
|
+
private limit;
|
|
43
|
+
private check;
|
|
44
|
+
sendFrames(frames: readonly Frame[]): Promise<void>;
|
|
45
|
+
handle(f: Frame): Promise<void>;
|
|
46
|
+
private send;
|
|
47
|
+
private fail;
|
|
48
|
+
private closedErr;
|
|
49
|
+
close(): void;
|
|
50
|
+
}
|
|
51
|
+
declare function dialWebTransport(url: string, opts?: ConnOptions & WebTransportDatagramOptions & WebTransportDialOptions): Conn;
|
|
52
|
+
//#endregion
|
|
53
|
+
export { DefaultMaxMessageSize, WebTransportDatagramOptions, WebTransportDatagramTransport, WebTransportDialOptions, WebTransportLike, dialWebTransport };
|