@statewalker/webrun-msgpack 0.4.1 → 0.6.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/dist/index.js +19 -10
- package/dist/port-codec.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/port-codec.ts +35 -13
package/dist/index.js
CHANGED
|
@@ -611,7 +611,7 @@ function isEnvelope(value) {
|
|
|
611
611
|
const candidate = value;
|
|
612
612
|
if (typeof candidate.id !== "number") return false;
|
|
613
613
|
if (!Number.isInteger(candidate.id) || candidate.id < 0) return false;
|
|
614
|
-
return candidate.type === "open" || candidate.type === "message" || candidate.type === "close";
|
|
614
|
+
return candidate.type === "open" || candidate.type === "message" || candidate.type === "close" || candidate.type === "credit" || candidate.type === "frame" || candidate.type === "ping" || candidate.type === "pong";
|
|
615
615
|
}
|
|
616
616
|
/** Accept whatever byte shape a transport pump hands over. */
|
|
617
617
|
function toBytes(data) {
|
|
@@ -622,6 +622,17 @@ function toBytes(data) {
|
|
|
622
622
|
return new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
623
623
|
}
|
|
624
624
|
}
|
|
625
|
+
function decodeEnvelope(data) {
|
|
626
|
+
const bytes = toBytes(data);
|
|
627
|
+
if (!bytes || bytes.byteLength === 0) return void 0;
|
|
628
|
+
let decoded;
|
|
629
|
+
try {
|
|
630
|
+
decoded = deserialize(bytes);
|
|
631
|
+
} catch {
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
return isEnvelope(decoded) ? decoded : void 0;
|
|
635
|
+
}
|
|
625
636
|
/**
|
|
626
637
|
* For ports whose messages are bytes — a WebSocket, a WebRTC data channel, a
|
|
627
638
|
* LiveKit data packet.
|
|
@@ -645,16 +656,14 @@ const msgpackCodec = {
|
|
|
645
656
|
post(port, envelope) {
|
|
646
657
|
port.postMessage(serialize(envelope));
|
|
647
658
|
},
|
|
659
|
+
encode(envelope) {
|
|
660
|
+
return serialize(envelope);
|
|
661
|
+
},
|
|
662
|
+
decode(data) {
|
|
663
|
+
return decodeEnvelope(data);
|
|
664
|
+
},
|
|
648
665
|
read(event) {
|
|
649
|
-
|
|
650
|
-
if (!bytes || bytes.byteLength === 0) return void 0;
|
|
651
|
-
let decoded;
|
|
652
|
-
try {
|
|
653
|
-
decoded = deserialize(bytes);
|
|
654
|
-
} catch {
|
|
655
|
-
return;
|
|
656
|
-
}
|
|
657
|
-
return isEnvelope(decoded) ? decoded : void 0;
|
|
666
|
+
return decodeEnvelope(event.data);
|
|
658
667
|
}
|
|
659
668
|
};
|
|
660
669
|
//#endregion
|
package/dist/port-codec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"port-codec.d.ts","sourceRoot":"","sources":["../src/port-codec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAgB,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"port-codec.d.ts","sourceRoot":"","sources":["../src/port-codec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAgB,MAAM,yBAAyB,CAAC;AAkDvE;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,YAAY,EAAE,SAkB1B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/webrun-msgpack",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "MessagePack for the wire, with no runtime dependencies: serialize/deserialize (a TypeScript port of msgpack.js), a length-prefixed stream codec for iterables (values and Float32Arrays), and msgpackCodec, a PortCodec that carries webrun-rpc port envelopes over a byte transport.",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"src"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@statewalker/webrun-rpc": "^0.
|
|
33
|
+
"@statewalker/webrun-rpc": "^0.7.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@biomejs/biome": "^2.5.15",
|
package/src/port-codec.ts
CHANGED
|
@@ -11,7 +11,15 @@ function isEnvelope(value: unknown): value is PortEnvelope {
|
|
|
11
11
|
const candidate = value as { type?: unknown; id?: unknown };
|
|
12
12
|
if (typeof candidate.id !== "number") return false;
|
|
13
13
|
if (!Number.isInteger(candidate.id) || candidate.id < 0) return false;
|
|
14
|
-
return
|
|
14
|
+
return (
|
|
15
|
+
candidate.type === "open" ||
|
|
16
|
+
candidate.type === "message" ||
|
|
17
|
+
candidate.type === "close" ||
|
|
18
|
+
candidate.type === "credit" ||
|
|
19
|
+
candidate.type === "frame" ||
|
|
20
|
+
candidate.type === "ping" ||
|
|
21
|
+
candidate.type === "pong"
|
|
22
|
+
);
|
|
15
23
|
}
|
|
16
24
|
|
|
17
25
|
/** Accept whatever byte shape a transport pump hands over. */
|
|
@@ -25,6 +33,21 @@ function toBytes(data: unknown): Uint8Array | undefined {
|
|
|
25
33
|
return undefined;
|
|
26
34
|
}
|
|
27
35
|
|
|
36
|
+
function decodeEnvelope(data: unknown): PortEnvelope | undefined {
|
|
37
|
+
const bytes = toBytes(data);
|
|
38
|
+
if (!bytes || bytes.byteLength === 0) return undefined;
|
|
39
|
+
let decoded: unknown;
|
|
40
|
+
try {
|
|
41
|
+
decoded = deserialize(bytes);
|
|
42
|
+
} catch {
|
|
43
|
+
// A malformed frame is a peer bug or hostile traffic. Dropping it keeps
|
|
44
|
+
// layer 1's drop-never-queue posture; throwing here would escape into
|
|
45
|
+
// the raw port's own listener, outside any consumer's reach.
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
return isEnvelope(decoded) ? decoded : undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
28
51
|
/**
|
|
29
52
|
* For ports whose messages are bytes — a WebSocket, a WebRTC data channel, a
|
|
30
53
|
* LiveKit data packet.
|
|
@@ -49,18 +72,17 @@ export const msgpackCodec: PortCodec = {
|
|
|
49
72
|
port.postMessage(serialize(envelope));
|
|
50
73
|
},
|
|
51
74
|
|
|
75
|
+
// A port with a window counts the frame's real length, posts these very
|
|
76
|
+
// bytes (encoding once) and, with `frameSize`, splits them.
|
|
77
|
+
encode(envelope) {
|
|
78
|
+
return serialize(envelope);
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
decode(data) {
|
|
82
|
+
return decodeEnvelope(data);
|
|
83
|
+
},
|
|
84
|
+
|
|
52
85
|
read(event) {
|
|
53
|
-
|
|
54
|
-
if (!bytes || bytes.byteLength === 0) return undefined;
|
|
55
|
-
let decoded: unknown;
|
|
56
|
-
try {
|
|
57
|
-
decoded = deserialize(bytes);
|
|
58
|
-
} catch {
|
|
59
|
-
// A malformed frame is a peer bug or hostile traffic. Dropping it keeps
|
|
60
|
-
// layer 1's drop-never-queue posture; throwing here would escape into
|
|
61
|
-
// the raw port's own listener, outside any consumer's reach.
|
|
62
|
-
return undefined;
|
|
63
|
-
}
|
|
64
|
-
return isEnvelope(decoded) ? decoded : undefined;
|
|
86
|
+
return decodeEnvelope(event.data);
|
|
65
87
|
},
|
|
66
88
|
};
|