@statewalker/webrun-streams-conformance 0.6.0 → 0.7.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 +1 -0
- package/dist/describe-port-window.d.ts +34 -0
- package/dist/describe-port-window.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +271 -27
- package/package.json +2 -2
- package/src/describe-port-window.ts +295 -0
- package/src/index.ts +7 -0
package/README.md
CHANGED
|
@@ -41,6 +41,7 @@ peer dependencies. One entry point, `.`.
|
|
|
41
41
|
| `measureRunAhead(makePair, direction)` | function | L6's run-ahead measurement, for one direction: resolves `{ bound, ahead, complete, intact }`. |
|
|
42
42
|
| `RunAheadMeasurement` | type | What `measureRunAhead` resolves. |
|
|
43
43
|
| `DescribeDuplexAdapterOptions` | type | `{ concurrency?: number; skipHugeBody?: boolean }`. |
|
|
44
|
+
| `describePortWindow(name, make)` | function | The per-port window of native ports (`window` on a native port mux, statewalker/webrun-wire#153): agreement, a stalled consumer holding the sender to the window, credit coming back, a message larger than the window, a close sending what the window held, and both mixed cases against a mux without a window. `make(codec, { opener?, server? })` builds two connected ends with those windows (`PortWindowHarness`). |
|
|
44
45
|
|
|
45
46
|
| Option | Default | Effect |
|
|
46
47
|
| --- | --- | --- |
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type NativePort, type NativePortMux, type ValueCodec } from "@statewalker/webrun-streams";
|
|
2
|
+
/** The windows each end of a {@link PortWindowHarness} grants; unset is none (an older peer). */
|
|
3
|
+
export interface PortWindows {
|
|
4
|
+
opener?: number;
|
|
5
|
+
server?: number;
|
|
6
|
+
}
|
|
7
|
+
/** Two ends of one connection whose muxes may have a window (`window` on the transport's mux). */
|
|
8
|
+
export interface PortWindowHarness {
|
|
9
|
+
/** A mux that opens ports and accepts none, with `windows.opener`. */
|
|
10
|
+
opener: NativePortMux;
|
|
11
|
+
/** A mux on the other end, with `windows.server`, handing each port to `onPort`. */
|
|
12
|
+
serve(onPort: (port: NativePort) => boolean | undefined): Promise<NativePortMux>;
|
|
13
|
+
/** Releases the connection. */
|
|
14
|
+
dispose(): void | Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export interface DescribePortWindowOptions {
|
|
17
|
+
/**
|
|
18
|
+
* The largest encoded message the transport carries (WebRTC: 16 KiB). The
|
|
19
|
+
* case of a message larger than the window is skipped below 64 KiB.
|
|
20
|
+
*/
|
|
21
|
+
maxFrameBytes?: number;
|
|
22
|
+
}
|
|
23
|
+
/** Builds two connected ends whose muxes use `codec` and the given windows. */
|
|
24
|
+
export type MakePortWindowHarness = (codec: ValueCodec, windows: PortWindows) => Promise<PortWindowHarness>;
|
|
25
|
+
/**
|
|
26
|
+
* Per-port flow control on native ports (statewalker/webrun-wire#153): a mux
|
|
27
|
+
* given `window` grants its peer at most that many bytes of a port's messages
|
|
28
|
+
* delivered and not yet consumed, where both ends speak it, and runs as before
|
|
29
|
+
* against one that does not. The module (`newNativePortMux` in
|
|
30
|
+
* `@statewalker/webrun-streams`) implements it once; each transport runs this
|
|
31
|
+
* suite with its own negotiation.
|
|
32
|
+
*/
|
|
33
|
+
export declare function describePortWindow(name: string, make: MakePortWindowHarness, options?: DescribePortWindowOptions): void;
|
|
34
|
+
//# sourceMappingURL=describe-port-window.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"describe-port-window.d.ts","sourceRoot":"","sources":["../src/describe-port-window.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,UAAU,EACf,KAAK,aAAa,EAElB,KAAK,UAAU,EAChB,MAAM,6BAA6B,CAAC;AAGrC,iGAAiG;AACjG,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,kGAAkG;AAClG,MAAM,WAAW,iBAAiB;IAChC,sEAAsE;IACtE,MAAM,EAAE,aAAa,CAAC;IACtB,oFAAoF;IACpF,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,OAAO,GAAG,SAAS,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IACjF,+BAA+B;IAC/B,OAAO,IAAI,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACjC;AAED,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,+EAA+E;AAC/E,MAAM,MAAM,qBAAqB,GAAG,CAClC,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,WAAW,KACjB,OAAO,CAAC,iBAAiB,CAAC,CAAC;AA6DhC;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,qBAAqB,EAC3B,OAAO,GAAE,yBAA8B,GACtC,IAAI,CAwLN"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { type DescribeDuplexAdapterOptions, describeDuplexAdapter, } from "./describe-duplex-adapter.js";
|
|
2
2
|
export { type DescribeMessagePipeOptions, describeMessagePipe, type MessagePipeHarness, } from "./describe-message-pipe.js";
|
|
3
3
|
export { describePortMux, type MakePortMuxHarness, type PortMuxHarness, } from "./describe-port-mux.js";
|
|
4
|
+
export { type DescribePortWindowOptions, describePortWindow, type MakePortWindowHarness, type PortWindowHarness, type PortWindows, } from "./describe-port-window.js";
|
|
4
5
|
export { type ConnectServePair, type MakePair, makeLoopbackPair, type PairTuning, } from "./loopback.js";
|
|
5
6
|
export { measureRunAhead, type RunAheadMeasurement } from "./run-ahead.js";
|
|
6
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,4BAA4B,EACjC,qBAAqB,GACtB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,KAAK,0BAA0B,EAC/B,mBAAmB,EACnB,KAAK,kBAAkB,GACxB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,eAAe,EACf,KAAK,kBAAkB,EACvB,KAAK,cAAc,GACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,QAAQ,EACb,gBAAgB,EAChB,KAAK,UAAU,GAChB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,4BAA4B,EACjC,qBAAqB,GACtB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,KAAK,0BAA0B,EAC/B,mBAAmB,EACnB,KAAK,kBAAkB,GACxB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,eAAe,EACf,KAAK,kBAAkB,EACvB,KAAK,cAAc,GACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,KAAK,yBAAyB,EAC9B,kBAAkB,EAClB,KAAK,qBAAqB,EAC1B,KAAK,iBAAiB,EACtB,KAAK,WAAW,GACjB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EACL,KAAK,gBAAgB,EACrB,KAAK,QAAQ,EACb,gBAAgB,EAChB,KAAK,UAAU,GAChB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,KAAK,mBAAmB,EAAE,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { collectBytes } from "@statewalker/webrun-streams";
|
|
1
|
+
import { MIN_PORT_WINDOW, collectBytes, portFlow } from "@statewalker/webrun-streams";
|
|
2
2
|
import { describe, expect, it } from "vitest";
|
|
3
3
|
//#region src/run-ahead.ts
|
|
4
4
|
const CHUNK = 4096;
|
|
@@ -597,15 +597,15 @@ const contractCodec = {
|
|
|
597
597
|
return JSON.parse(new TextDecoder().decode(bytes));
|
|
598
598
|
}
|
|
599
599
|
};
|
|
600
|
-
const tick = (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
601
|
-
async function until(predicate, label, ms = 5e3) {
|
|
600
|
+
const tick$1 = (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
601
|
+
async function until$1(predicate, label, ms = 5e3) {
|
|
602
602
|
const deadline = Date.now() + ms;
|
|
603
603
|
while (!predicate()) {
|
|
604
604
|
if (Date.now() > deadline) throw new Error(`timed out waiting for: ${label}`);
|
|
605
|
-
await tick(5);
|
|
605
|
+
await tick$1(5);
|
|
606
606
|
}
|
|
607
607
|
}
|
|
608
|
-
function received(port) {
|
|
608
|
+
function received$1(port) {
|
|
609
609
|
const got = [];
|
|
610
610
|
port.addEventListener("message", (e) => {
|
|
611
611
|
got.push(e.data);
|
|
@@ -634,17 +634,17 @@ function describePortMux(name, make) {
|
|
|
634
634
|
describe(`${name}: the native port mux contract`, () => {
|
|
635
635
|
it("a port opened on one end arrives on the other; values cross both ways, encoded", () => withPair(async (h, inbound) => {
|
|
636
636
|
const port = await h.opener.openPort();
|
|
637
|
-
const fromServer = received(port);
|
|
637
|
+
const fromServer = received$1(port);
|
|
638
638
|
port.postMessage({
|
|
639
639
|
n: 1,
|
|
640
640
|
s: "a"
|
|
641
641
|
});
|
|
642
|
-
await until(() => inbound.length === 1, "the inbound port");
|
|
642
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
643
643
|
const server = inbound[0];
|
|
644
|
-
const fromClient = received(server);
|
|
645
|
-
await until(() => fromClient.length === 1, "the first message");
|
|
644
|
+
const fromClient = received$1(server);
|
|
645
|
+
await until$1(() => fromClient.length === 1, "the first message");
|
|
646
646
|
server.postMessage([2, "b"]);
|
|
647
|
-
await until(() => fromServer.length === 1, "the answer");
|
|
647
|
+
await until$1(() => fromServer.length === 1, "the answer");
|
|
648
648
|
expect(fromClient).toEqual([{
|
|
649
649
|
n: 1,
|
|
650
650
|
s: "a"
|
|
@@ -656,12 +656,12 @@ function describePortMux(name, make) {
|
|
|
656
656
|
port.postMessage(1);
|
|
657
657
|
port.postMessage(2);
|
|
658
658
|
port.postMessage(3);
|
|
659
|
-
await until(() => inbound.length === 1, "the inbound port");
|
|
660
|
-
await tick(50);
|
|
659
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
660
|
+
await tick$1(50);
|
|
661
661
|
const server = inbound[0];
|
|
662
|
-
const first = received(server);
|
|
663
|
-
const second = received(server);
|
|
664
|
-
await until(() => first.length === 3 && second.length === 3, "the held messages");
|
|
662
|
+
const first = received$1(server);
|
|
663
|
+
const second = received$1(server);
|
|
664
|
+
await until$1(() => first.length === 3 && second.length === 3, "the held messages");
|
|
665
665
|
expect(first).toEqual([
|
|
666
666
|
1,
|
|
667
667
|
2,
|
|
@@ -676,7 +676,7 @@ function describePortMux(name, make) {
|
|
|
676
676
|
it("removeEventListener stops delivery to that listener", () => withPair(async (h, inbound) => {
|
|
677
677
|
const port = await h.opener.openPort();
|
|
678
678
|
port.postMessage("one");
|
|
679
|
-
await until(() => inbound.length === 1, "the inbound port");
|
|
679
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
680
680
|
const server = inbound[0];
|
|
681
681
|
const kept = [];
|
|
682
682
|
const dropped = [];
|
|
@@ -687,41 +687,41 @@ function describePortMux(name, make) {
|
|
|
687
687
|
kept.push(e.data);
|
|
688
688
|
});
|
|
689
689
|
server.addEventListener("message", drop);
|
|
690
|
-
await until(() => kept.length === 1, "the first message");
|
|
690
|
+
await until$1(() => kept.length === 1, "the first message");
|
|
691
691
|
server.removeEventListener("message", drop);
|
|
692
692
|
port.postMessage("two");
|
|
693
|
-
await until(() => kept.length === 2, "the second message");
|
|
693
|
+
await until$1(() => kept.length === 2, "the second message");
|
|
694
694
|
expect(dropped).toEqual(["one"]);
|
|
695
695
|
}));
|
|
696
696
|
it("a port the serving end refuses fails: the opener's close signal aborts", () => withPair(async (h) => {
|
|
697
697
|
const port = await h.opener.openPort();
|
|
698
698
|
port.postMessage("hello");
|
|
699
|
-
await until(() => port.closeSignal.aborted, "the refusal");
|
|
699
|
+
await until$1(() => port.closeSignal.aborted, "the refusal");
|
|
700
700
|
}, () => false));
|
|
701
701
|
it("an onPort that throws refuses the port", () => withPair(async (h) => {
|
|
702
702
|
const port = await h.opener.openPort();
|
|
703
703
|
port.postMessage("hello");
|
|
704
|
-
await until(() => port.closeSignal.aborted, "the refusal");
|
|
704
|
+
await until$1(() => port.closeSignal.aborted, "the refusal");
|
|
705
705
|
}, () => {
|
|
706
706
|
throw new Error("onPort failed");
|
|
707
707
|
}));
|
|
708
708
|
it("a frame the codec cannot decode fails the receiving port", () => withPair(async (h, inbound) => {
|
|
709
709
|
const port = await h.opener.openPort();
|
|
710
710
|
port.postMessage("fine");
|
|
711
|
-
await until(() => inbound.length === 1, "the inbound port");
|
|
711
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
712
712
|
const server = inbound[0];
|
|
713
|
-
received(server);
|
|
713
|
+
received$1(server);
|
|
714
714
|
port.postMessage(POISON);
|
|
715
|
-
await until(() => server.closeSignal.aborted, "the receiving port failed");
|
|
715
|
+
await until$1(() => server.closeSignal.aborted, "the receiving port failed");
|
|
716
716
|
expect(String(server.closeSignal.reason?.message)).toMatch(/decode/);
|
|
717
717
|
}));
|
|
718
718
|
it("closing a port ends it on both ends", () => withPair(async (h, inbound) => {
|
|
719
719
|
const port = await h.opener.openPort();
|
|
720
720
|
port.postMessage("hello");
|
|
721
|
-
await until(() => inbound.length === 1, "the inbound port");
|
|
721
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
722
722
|
await port.close();
|
|
723
723
|
expect(port.closeSignal.aborted).toBe(true);
|
|
724
|
-
await until(() => inbound[0].closeSignal.aborted, "the peer's port closed");
|
|
724
|
+
await until$1(() => inbound[0].closeSignal.aborted, "the peer's port closed");
|
|
725
725
|
}));
|
|
726
726
|
it("close() closes the ports the mux opened, and openPort rejects afterwards", () => withPair(async (h) => {
|
|
727
727
|
const a = await h.opener.openPort();
|
|
@@ -739,7 +739,7 @@ function describePortMux(name, make) {
|
|
|
739
739
|
const port = await h.opener.openPort(void 0, { signal: later.signal });
|
|
740
740
|
expect(port.closeSignal.aborted).toBe(false);
|
|
741
741
|
later.abort(/* @__PURE__ */ new Error("done with it"));
|
|
742
|
-
await until(() => port.closeSignal.aborted, "the port closed");
|
|
742
|
+
await until$1(() => port.closeSignal.aborted, "the port closed");
|
|
743
743
|
}));
|
|
744
744
|
it("reports its maxMessageSize as a positive number", () => withPair(async (h) => {
|
|
745
745
|
expect(h.opener.maxMessageSize).toBeGreaterThan(0);
|
|
@@ -747,6 +747,250 @@ function describePortMux(name, make) {
|
|
|
747
747
|
});
|
|
748
748
|
}
|
|
749
749
|
//#endregion
|
|
750
|
+
//#region src/describe-port-window.ts
|
|
751
|
+
const encoder = new TextEncoder();
|
|
752
|
+
const decoder = new TextDecoder();
|
|
753
|
+
/** JSON: a string of n characters encodes to n + 2 bytes. */
|
|
754
|
+
const jsonCodec = {
|
|
755
|
+
encode: (value) => encoder.encode(JSON.stringify(value)),
|
|
756
|
+
decode: (bytes) => JSON.parse(decoder.decode(bytes))
|
|
757
|
+
};
|
|
758
|
+
/**
|
|
759
|
+
* JSON behind one leading byte: 0xC1, the byte a windowed channel's control
|
|
760
|
+
* frames start with, for a value marked `{ c1: true }`, else 0x00. A codec
|
|
761
|
+
* whose frames start with that byte must still have them arrive as values.
|
|
762
|
+
*/
|
|
763
|
+
const leadingByteCodec = {
|
|
764
|
+
encode: (value) => {
|
|
765
|
+
const json = encoder.encode(JSON.stringify(value));
|
|
766
|
+
const out = new Uint8Array(json.byteLength + 1);
|
|
767
|
+
out[0] = value?.c1 ? 193 : 0;
|
|
768
|
+
out.set(json, 1);
|
|
769
|
+
return out;
|
|
770
|
+
},
|
|
771
|
+
decode: (bytes) => JSON.parse(decoder.decode(bytes.subarray(1)))
|
|
772
|
+
};
|
|
773
|
+
const tick = (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
774
|
+
async function until(predicate, label, ms = 5e3) {
|
|
775
|
+
const deadline = Date.now() + ms;
|
|
776
|
+
while (!predicate()) {
|
|
777
|
+
if (Date.now() > deadline) throw new Error(`timed out waiting for: ${label}`);
|
|
778
|
+
await tick(5);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
function received(port) {
|
|
782
|
+
const got = [];
|
|
783
|
+
port.addEventListener("message", (e) => {
|
|
784
|
+
got.push(e.data);
|
|
785
|
+
});
|
|
786
|
+
return got;
|
|
787
|
+
}
|
|
788
|
+
/** A listener that holds every message unconsumed until `release()`. */
|
|
789
|
+
function stalled(port) {
|
|
790
|
+
const got = [];
|
|
791
|
+
let release;
|
|
792
|
+
const gate = new Promise((r) => {
|
|
793
|
+
release = r;
|
|
794
|
+
});
|
|
795
|
+
port.addEventListener("message", (e) => {
|
|
796
|
+
got.push(e.data);
|
|
797
|
+
return gate;
|
|
798
|
+
});
|
|
799
|
+
return {
|
|
800
|
+
got,
|
|
801
|
+
release
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
const agreed = (port) => !!portFlow(port)?.agreement()?.enforced;
|
|
805
|
+
/**
|
|
806
|
+
* Per-port flow control on native ports (statewalker/webrun-wire#153): a mux
|
|
807
|
+
* given `window` grants its peer at most that many bytes of a port's messages
|
|
808
|
+
* delivered and not yet consumed, where both ends speak it, and runs as before
|
|
809
|
+
* against one that does not. The module (`newNativePortMux` in
|
|
810
|
+
* `@statewalker/webrun-streams`) implements it once; each transport runs this
|
|
811
|
+
* suite with its own negotiation.
|
|
812
|
+
*/
|
|
813
|
+
function describePortWindow(name, make, options = {}) {
|
|
814
|
+
const { maxFrameBytes = Number.POSITIVE_INFINITY } = options;
|
|
815
|
+
async function withPair(windows, body, codec = jsonCodec) {
|
|
816
|
+
const h = await make(codec, windows);
|
|
817
|
+
const inbound = [];
|
|
818
|
+
try {
|
|
819
|
+
await h.serve((port) => {
|
|
820
|
+
inbound.push(port);
|
|
821
|
+
return true;
|
|
822
|
+
});
|
|
823
|
+
await body(h, inbound);
|
|
824
|
+
} finally {
|
|
825
|
+
await h.opener.close().catch(() => void 0);
|
|
826
|
+
await h.dispose();
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
/** A port both ends have agreed a window on, and its inbound end. */
|
|
830
|
+
async function agreedPort(h, inbound) {
|
|
831
|
+
const port = await h.opener.openPort();
|
|
832
|
+
port.postMessage("hello");
|
|
833
|
+
await until(() => inbound.length === 1, "the inbound port");
|
|
834
|
+
const server = inbound[0];
|
|
835
|
+
await until(() => agreed(port) && agreed(server), "the window agreed on both ends");
|
|
836
|
+
return {
|
|
837
|
+
port,
|
|
838
|
+
server
|
|
839
|
+
};
|
|
840
|
+
}
|
|
841
|
+
describe(`${name}: per-port windows`, () => {
|
|
842
|
+
it("both ends with a window agree it: each end reports pipeline and its own window", () => withPair({
|
|
843
|
+
opener: 131072,
|
|
844
|
+
server: 65536
|
|
845
|
+
}, async (h, inbound) => {
|
|
846
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
847
|
+
const mine = portFlow(port)?.agreement();
|
|
848
|
+
const theirs = portFlow(server)?.agreement();
|
|
849
|
+
expect(mine?.caps.has("pipeline")).toBe(true);
|
|
850
|
+
expect(theirs?.caps.has("pipeline")).toBe(true);
|
|
851
|
+
expect(mine?.window).toBe(131072);
|
|
852
|
+
expect(theirs?.window).toBe(65536);
|
|
853
|
+
}));
|
|
854
|
+
it("control frames never reach listeners; values cross both ways intact", () => withPair({
|
|
855
|
+
opener: 65536,
|
|
856
|
+
server: 65536
|
|
857
|
+
}, async (h, inbound) => {
|
|
858
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
859
|
+
const fromServer = received(port);
|
|
860
|
+
const fromClient = received(server);
|
|
861
|
+
for (let i = 0; i < 20; i++) port.postMessage({ i });
|
|
862
|
+
for (let i = 0; i < 20; i++) server.postMessage([i]);
|
|
863
|
+
await until(() => fromClient.length === 21 && fromServer.length === 20, "every message, both ways");
|
|
864
|
+
expect(fromClient).toEqual(["hello", ...Array.from({ length: 20 }, (_, i) => ({ i }))]);
|
|
865
|
+
expect(fromServer).toEqual(Array.from({ length: 20 }, (_, i) => [i]));
|
|
866
|
+
}));
|
|
867
|
+
it("a frame whose encoding starts with the control byte still arrives as a value", () => withPair({
|
|
868
|
+
opener: 65536,
|
|
869
|
+
server: 65536
|
|
870
|
+
}, async (h, inbound) => {
|
|
871
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
872
|
+
const fromServer = received(port);
|
|
873
|
+
const fromClient = received(server);
|
|
874
|
+
port.postMessage({
|
|
875
|
+
c1: true,
|
|
876
|
+
n: 1
|
|
877
|
+
});
|
|
878
|
+
server.postMessage({
|
|
879
|
+
c1: true,
|
|
880
|
+
n: 2
|
|
881
|
+
});
|
|
882
|
+
await until(() => fromClient.length === 2 && fromServer.length === 1, "both values");
|
|
883
|
+
expect(fromClient[1]).toEqual({
|
|
884
|
+
c1: true,
|
|
885
|
+
n: 1
|
|
886
|
+
});
|
|
887
|
+
expect(fromServer[0]).toEqual({
|
|
888
|
+
c1: true,
|
|
889
|
+
n: 2
|
|
890
|
+
});
|
|
891
|
+
}, leadingByteCodec));
|
|
892
|
+
it("a stalled consumer holds the sender to the window; consuming returns the credit", () => withPair({
|
|
893
|
+
opener: 65536,
|
|
894
|
+
server: 65536
|
|
895
|
+
}, async (h, inbound) => {
|
|
896
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
897
|
+
const consumer = stalled(server);
|
|
898
|
+
await until(() => consumer.got.length === 1, "the held hello");
|
|
899
|
+
consumer.got.length = 0;
|
|
900
|
+
const body = "x".repeat(8e3);
|
|
901
|
+
for (let i = 0; i < 30; i++) port.postMessage(body);
|
|
902
|
+
await tick(200);
|
|
903
|
+
const flow = portFlow(port);
|
|
904
|
+
expect(consumer.got.length * 8002).toBeLessThanOrEqual(65536);
|
|
905
|
+
expect(consumer.got.length).toBeGreaterThan(0);
|
|
906
|
+
expect(flow?.bufferedAmount()).toBeGreaterThan(0);
|
|
907
|
+
let drained = false;
|
|
908
|
+
flow?.ready().then(() => {
|
|
909
|
+
drained = true;
|
|
910
|
+
});
|
|
911
|
+
consumer.release();
|
|
912
|
+
await until(() => consumer.got.length === 30, "every message once consumed");
|
|
913
|
+
await until(() => drained, "the sender's queue drained");
|
|
914
|
+
expect(flow?.bufferedAmount()).toBe(0);
|
|
915
|
+
}));
|
|
916
|
+
it("a port opened once the mux has heard a peer's hello is agreed at once, holding back by the floor", () => withPair({
|
|
917
|
+
opener: 1048576,
|
|
918
|
+
server: 1048576
|
|
919
|
+
}, async (h, inbound) => {
|
|
920
|
+
await agreedPort(h, inbound);
|
|
921
|
+
const second = await h.opener.openPort();
|
|
922
|
+
expect(agreed(second)).toBe(true);
|
|
923
|
+
expect(portFlow(second)?.stats().peerWindow).toBeGreaterThanOrEqual(MIN_PORT_WINDOW);
|
|
924
|
+
const body = "p".repeat(15e3);
|
|
925
|
+
for (let i = 0; i < 6; i++) second.postMessage(body);
|
|
926
|
+
expect(portFlow(second)?.bufferedAmount()).toBeGreaterThan(0);
|
|
927
|
+
await until(() => inbound.length === 2, "the second inbound port");
|
|
928
|
+
const got = received(inbound[1]);
|
|
929
|
+
await until(() => got.length === 6, "every message, once the hello raised the window");
|
|
930
|
+
expect(portFlow(second)?.stats().peerWindow).toBe(1048576);
|
|
931
|
+
}));
|
|
932
|
+
it.skipIf(maxFrameBytes < 131072)("a message larger than the window goes once nothing is outstanding", () => withPair({
|
|
933
|
+
opener: 65536,
|
|
934
|
+
server: 65536
|
|
935
|
+
}, async (h, inbound) => {
|
|
936
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
937
|
+
const got = received(server);
|
|
938
|
+
await until(() => got.length === 1, "the hello");
|
|
939
|
+
const big = "y".repeat(204800);
|
|
940
|
+
port.postMessage(big);
|
|
941
|
+
port.postMessage(big);
|
|
942
|
+
await until(() => got.length === 3, "both large messages");
|
|
943
|
+
expect(got.slice(1)).toEqual([big, big]);
|
|
944
|
+
}));
|
|
945
|
+
it("closing a port sends what the window held back before the close", () => withPair({
|
|
946
|
+
opener: 65536,
|
|
947
|
+
server: 65536
|
|
948
|
+
}, async (h, inbound) => {
|
|
949
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
950
|
+
const consumer = stalled(server);
|
|
951
|
+
const body = "z".repeat(8e3);
|
|
952
|
+
for (let i = 0; i < 20; i++) port.postMessage(body);
|
|
953
|
+
await tick(50);
|
|
954
|
+
expect(portFlow(port)?.bufferedAmount()).toBeGreaterThan(0);
|
|
955
|
+
await port.close();
|
|
956
|
+
consumer.release();
|
|
957
|
+
await until(() => consumer.got.length === 21, "every message, the held ones included");
|
|
958
|
+
}));
|
|
959
|
+
it("an opener with a window and a serving end without: no agreement, messages flow as before", () => withPair({ opener: 65536 }, async (h, inbound) => {
|
|
960
|
+
const port = await h.opener.openPort();
|
|
961
|
+
port.postMessage("hello");
|
|
962
|
+
await until(() => inbound.length === 1, "the inbound port");
|
|
963
|
+
const server = inbound[0];
|
|
964
|
+
const consumer = stalled(server);
|
|
965
|
+
const fromServer = received(port);
|
|
966
|
+
const body = "w".repeat(1e3);
|
|
967
|
+
for (let i = 0; i < 10; i++) port.postMessage(body);
|
|
968
|
+
server.postMessage("answer");
|
|
969
|
+
await until(() => consumer.got.length === 11, "every message, unwindowed");
|
|
970
|
+
await until(() => fromServer.length === 1, "the answer");
|
|
971
|
+
expect(fromServer).toEqual(["answer"]);
|
|
972
|
+
expect(agreed(port)).toBe(false);
|
|
973
|
+
expect(agreed(server)).toBe(false);
|
|
974
|
+
expect(portFlow(port)?.bufferedAmount() ?? 0).toBe(0);
|
|
975
|
+
}));
|
|
976
|
+
it("a serving end with a window and an opener without: no agreement, messages flow as before", () => withPair({ server: 65536 }, async (h, inbound) => {
|
|
977
|
+
const port = await h.opener.openPort();
|
|
978
|
+
const fromServer = received(port);
|
|
979
|
+
port.postMessage("hello");
|
|
980
|
+
await until(() => inbound.length === 1, "the inbound port");
|
|
981
|
+
const server = inbound[0];
|
|
982
|
+
const fromClient = received(server);
|
|
983
|
+
const body = "v".repeat(1e3);
|
|
984
|
+
for (let i = 0; i < 10; i++) server.postMessage(body);
|
|
985
|
+
await until(() => fromServer.length === 10, "every message, unwindowed");
|
|
986
|
+
await until(() => fromClient.length === 1, "the hello");
|
|
987
|
+
expect(fromServer.every((m) => m === body)).toBe(true);
|
|
988
|
+
expect(agreed(port)).toBe(false);
|
|
989
|
+
expect(agreed(server)).toBe(false);
|
|
990
|
+
}));
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
//#endregion
|
|
750
994
|
//#region src/loopback.ts
|
|
751
995
|
/**
|
|
752
996
|
* Loopback pair: `call` invokes the registered `handler` directly, no
|
|
@@ -787,4 +1031,4 @@ const makeLoopbackPair = async () => {
|
|
|
787
1031
|
};
|
|
788
1032
|
};
|
|
789
1033
|
//#endregion
|
|
790
|
-
export { describeDuplexAdapter, describeMessagePipe, describePortMux, makeLoopbackPair, measureRunAhead };
|
|
1034
|
+
export { describeDuplexAdapter, describeMessagePipe, describePortMux, describePortWindow, makeLoopbackPair, measureRunAhead };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/webrun-streams-conformance",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Conformance test suite for Duplex/Connect/Serve adapters in the webrun-streams-* family",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
33
|
"vitest": "^5.0.5",
|
|
34
|
-
"@statewalker/webrun-streams": "^0.
|
|
34
|
+
"@statewalker/webrun-streams": "^0.6.0"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@biomejs/biome": "^2.5.15",
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MIN_PORT_WINDOW,
|
|
3
|
+
type NativePort,
|
|
4
|
+
type NativePortMux,
|
|
5
|
+
portFlow,
|
|
6
|
+
type ValueCodec,
|
|
7
|
+
} from "@statewalker/webrun-streams";
|
|
8
|
+
import { describe, expect, it } from "vitest";
|
|
9
|
+
|
|
10
|
+
/** The windows each end of a {@link PortWindowHarness} grants; unset is none (an older peer). */
|
|
11
|
+
export interface PortWindows {
|
|
12
|
+
opener?: number;
|
|
13
|
+
server?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Two ends of one connection whose muxes may have a window (`window` on the transport's mux). */
|
|
17
|
+
export interface PortWindowHarness {
|
|
18
|
+
/** A mux that opens ports and accepts none, with `windows.opener`. */
|
|
19
|
+
opener: NativePortMux;
|
|
20
|
+
/** A mux on the other end, with `windows.server`, handing each port to `onPort`. */
|
|
21
|
+
serve(onPort: (port: NativePort) => boolean | undefined): Promise<NativePortMux>;
|
|
22
|
+
/** Releases the connection. */
|
|
23
|
+
dispose(): void | Promise<void>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface DescribePortWindowOptions {
|
|
27
|
+
/**
|
|
28
|
+
* The largest encoded message the transport carries (WebRTC: 16 KiB). The
|
|
29
|
+
* case of a message larger than the window is skipped below 64 KiB.
|
|
30
|
+
*/
|
|
31
|
+
maxFrameBytes?: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Builds two connected ends whose muxes use `codec` and the given windows. */
|
|
35
|
+
export type MakePortWindowHarness = (
|
|
36
|
+
codec: ValueCodec,
|
|
37
|
+
windows: PortWindows,
|
|
38
|
+
) => Promise<PortWindowHarness>;
|
|
39
|
+
|
|
40
|
+
const encoder = new TextEncoder();
|
|
41
|
+
const decoder = new TextDecoder();
|
|
42
|
+
|
|
43
|
+
/** JSON: a string of n characters encodes to n + 2 bytes. */
|
|
44
|
+
const jsonCodec: ValueCodec = {
|
|
45
|
+
encode: (value) => encoder.encode(JSON.stringify(value)),
|
|
46
|
+
decode: (bytes) => JSON.parse(decoder.decode(bytes)),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* JSON behind one leading byte: 0xC1, the byte a windowed channel's control
|
|
51
|
+
* frames start with, for a value marked `{ c1: true }`, else 0x00. A codec
|
|
52
|
+
* whose frames start with that byte must still have them arrive as values.
|
|
53
|
+
*/
|
|
54
|
+
const leadingByteCodec: ValueCodec = {
|
|
55
|
+
encode: (value) => {
|
|
56
|
+
const json = encoder.encode(JSON.stringify(value));
|
|
57
|
+
const out = new Uint8Array(json.byteLength + 1);
|
|
58
|
+
out[0] = (value as { c1?: boolean } | null)?.c1 ? 0xc1 : 0x00;
|
|
59
|
+
out.set(json, 1);
|
|
60
|
+
return out;
|
|
61
|
+
},
|
|
62
|
+
decode: (bytes) => JSON.parse(decoder.decode(bytes.subarray(1))),
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const tick = (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
66
|
+
|
|
67
|
+
async function until(predicate: () => boolean, label: string, ms = 5000): Promise<void> {
|
|
68
|
+
const deadline = Date.now() + ms;
|
|
69
|
+
while (!predicate()) {
|
|
70
|
+
if (Date.now() > deadline) throw new Error(`timed out waiting for: ${label}`);
|
|
71
|
+
await tick(5);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function received(port: NativePort): unknown[] {
|
|
76
|
+
const got: unknown[] = [];
|
|
77
|
+
port.addEventListener("message", (e) => {
|
|
78
|
+
got.push(e.data);
|
|
79
|
+
});
|
|
80
|
+
return got;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A listener that holds every message unconsumed until `release()`. */
|
|
84
|
+
function stalled(port: NativePort): { got: unknown[]; release(): void } {
|
|
85
|
+
const got: unknown[] = [];
|
|
86
|
+
let release!: () => void;
|
|
87
|
+
const gate = new Promise<void>((r) => {
|
|
88
|
+
release = r;
|
|
89
|
+
});
|
|
90
|
+
port.addEventListener("message", (e) => {
|
|
91
|
+
got.push(e.data);
|
|
92
|
+
return gate;
|
|
93
|
+
});
|
|
94
|
+
return { got, release };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const agreed = (port: NativePort) => !!portFlow(port)?.agreement()?.enforced;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Per-port flow control on native ports (statewalker/webrun-wire#153): a mux
|
|
101
|
+
* given `window` grants its peer at most that many bytes of a port's messages
|
|
102
|
+
* delivered and not yet consumed, where both ends speak it, and runs as before
|
|
103
|
+
* against one that does not. The module (`newNativePortMux` in
|
|
104
|
+
* `@statewalker/webrun-streams`) implements it once; each transport runs this
|
|
105
|
+
* suite with its own negotiation.
|
|
106
|
+
*/
|
|
107
|
+
export function describePortWindow(
|
|
108
|
+
name: string,
|
|
109
|
+
make: MakePortWindowHarness,
|
|
110
|
+
options: DescribePortWindowOptions = {},
|
|
111
|
+
): void {
|
|
112
|
+
const { maxFrameBytes = Number.POSITIVE_INFINITY } = options;
|
|
113
|
+
async function withPair(
|
|
114
|
+
windows: PortWindows,
|
|
115
|
+
body: (h: PortWindowHarness, inbound: NativePort[]) => Promise<void>,
|
|
116
|
+
codec: ValueCodec = jsonCodec,
|
|
117
|
+
): Promise<void> {
|
|
118
|
+
const h = await make(codec, windows);
|
|
119
|
+
const inbound: NativePort[] = [];
|
|
120
|
+
try {
|
|
121
|
+
await h.serve((port) => {
|
|
122
|
+
inbound.push(port);
|
|
123
|
+
return true;
|
|
124
|
+
});
|
|
125
|
+
await body(h, inbound);
|
|
126
|
+
} finally {
|
|
127
|
+
await h.opener.close().catch(() => undefined);
|
|
128
|
+
await h.dispose();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** A port both ends have agreed a window on, and its inbound end. */
|
|
133
|
+
async function agreedPort(
|
|
134
|
+
h: PortWindowHarness,
|
|
135
|
+
inbound: NativePort[],
|
|
136
|
+
): Promise<{ port: NativePort; server: NativePort }> {
|
|
137
|
+
const port = await h.opener.openPort();
|
|
138
|
+
port.postMessage("hello");
|
|
139
|
+
await until(() => inbound.length === 1, "the inbound port");
|
|
140
|
+
const server = inbound[0] as NativePort;
|
|
141
|
+
await until(() => agreed(port) && agreed(server), "the window agreed on both ends");
|
|
142
|
+
return { port, server };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
describe(`${name}: per-port windows`, () => {
|
|
146
|
+
it("both ends with a window agree it: each end reports pipeline and its own window", () =>
|
|
147
|
+
withPair({ opener: 128 * 1024, server: 64 * 1024 }, async (h, inbound) => {
|
|
148
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
149
|
+
const mine = portFlow(port)?.agreement();
|
|
150
|
+
const theirs = portFlow(server)?.agreement();
|
|
151
|
+
expect(mine?.caps.has("pipeline")).toBe(true);
|
|
152
|
+
expect(theirs?.caps.has("pipeline")).toBe(true);
|
|
153
|
+
// The window an end polices is the one it grants.
|
|
154
|
+
expect(mine?.window).toBe(128 * 1024);
|
|
155
|
+
expect(theirs?.window).toBe(64 * 1024);
|
|
156
|
+
}));
|
|
157
|
+
|
|
158
|
+
it("control frames never reach listeners; values cross both ways intact", () =>
|
|
159
|
+
withPair({ opener: 64 * 1024, server: 64 * 1024 }, async (h, inbound) => {
|
|
160
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
161
|
+
const fromServer = received(port);
|
|
162
|
+
const fromClient = received(server);
|
|
163
|
+
for (let i = 0; i < 20; i++) port.postMessage({ i });
|
|
164
|
+
for (let i = 0; i < 20; i++) server.postMessage([i]);
|
|
165
|
+
await until(
|
|
166
|
+
() => fromClient.length === 21 && fromServer.length === 20,
|
|
167
|
+
"every message, both ways",
|
|
168
|
+
);
|
|
169
|
+
expect(fromClient).toEqual(["hello", ...Array.from({ length: 20 }, (_, i) => ({ i }))]);
|
|
170
|
+
expect(fromServer).toEqual(Array.from({ length: 20 }, (_, i) => [i]));
|
|
171
|
+
}));
|
|
172
|
+
|
|
173
|
+
it("a frame whose encoding starts with the control byte still arrives as a value", () =>
|
|
174
|
+
withPair(
|
|
175
|
+
{ opener: 64 * 1024, server: 64 * 1024 },
|
|
176
|
+
async (h, inbound) => {
|
|
177
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
178
|
+
const fromServer = received(port);
|
|
179
|
+
const fromClient = received(server);
|
|
180
|
+
port.postMessage({ c1: true, n: 1 });
|
|
181
|
+
server.postMessage({ c1: true, n: 2 });
|
|
182
|
+
await until(() => fromClient.length === 2 && fromServer.length === 1, "both values");
|
|
183
|
+
expect(fromClient[1]).toEqual({ c1: true, n: 1 });
|
|
184
|
+
expect(fromServer[0]).toEqual({ c1: true, n: 2 });
|
|
185
|
+
},
|
|
186
|
+
leadingByteCodec,
|
|
187
|
+
));
|
|
188
|
+
|
|
189
|
+
it("a stalled consumer holds the sender to the window; consuming returns the credit", () =>
|
|
190
|
+
withPair({ opener: 64 * 1024, server: 64 * 1024 }, async (h, inbound) => {
|
|
191
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
192
|
+
const consumer = stalled(server);
|
|
193
|
+
// "hello" was consumed before the listener existed: nothing is outstanding.
|
|
194
|
+
await until(() => consumer.got.length === 1, "the held hello");
|
|
195
|
+
consumer.got.length = 0;
|
|
196
|
+
const body = "x".repeat(8000); // 8002 bytes in JSON
|
|
197
|
+
for (let i = 0; i < 30; i++) port.postMessage(body);
|
|
198
|
+
await tick(200);
|
|
199
|
+
const flow = portFlow(port);
|
|
200
|
+
// 8 messages fit 64 KiB; the rest wait in the sender's queue.
|
|
201
|
+
expect(consumer.got.length * 8002).toBeLessThanOrEqual(64 * 1024);
|
|
202
|
+
expect(consumer.got.length).toBeGreaterThan(0);
|
|
203
|
+
expect(flow?.bufferedAmount()).toBeGreaterThan(0);
|
|
204
|
+
let drained = false;
|
|
205
|
+
void flow?.ready().then(() => {
|
|
206
|
+
drained = true;
|
|
207
|
+
});
|
|
208
|
+
consumer.release();
|
|
209
|
+
await until(() => consumer.got.length === 30, "every message once consumed");
|
|
210
|
+
await until(() => drained, "the sender's queue drained");
|
|
211
|
+
expect(flow?.bufferedAmount()).toBe(0);
|
|
212
|
+
}));
|
|
213
|
+
|
|
214
|
+
it("a port opened once the mux has heard a peer's hello is agreed at once, holding back by the floor", () =>
|
|
215
|
+
withPair({ opener: 1024 * 1024, server: 1024 * 1024 }, async (h, inbound) => {
|
|
216
|
+
await agreedPort(h, inbound);
|
|
217
|
+
const second = await h.opener.openPort();
|
|
218
|
+
// No round trip: the peer's mux speaks the window, and grants at least the floor.
|
|
219
|
+
expect(agreed(second)).toBe(true);
|
|
220
|
+
expect(portFlow(second)?.stats().peerWindow).toBeGreaterThanOrEqual(MIN_PORT_WINDOW);
|
|
221
|
+
const body = "p".repeat(15000);
|
|
222
|
+
for (let i = 0; i < 6; i++) second.postMessage(body);
|
|
223
|
+
// Before the peer's hello, only the floor goes out: four of six.
|
|
224
|
+
expect(portFlow(second)?.bufferedAmount()).toBeGreaterThan(0);
|
|
225
|
+
await until(() => inbound.length === 2, "the second inbound port");
|
|
226
|
+
const got = received(inbound[1] as NativePort);
|
|
227
|
+
await until(() => got.length === 6, "every message, once the hello raised the window");
|
|
228
|
+
expect(portFlow(second)?.stats().peerWindow).toBe(1024 * 1024);
|
|
229
|
+
}));
|
|
230
|
+
|
|
231
|
+
it.skipIf(maxFrameBytes < 128 * 1024)(
|
|
232
|
+
"a message larger than the window goes once nothing is outstanding",
|
|
233
|
+
() =>
|
|
234
|
+
withPair({ opener: 64 * 1024, server: 64 * 1024 }, async (h, inbound) => {
|
|
235
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
236
|
+
const got = received(server);
|
|
237
|
+
await until(() => got.length === 1, "the hello");
|
|
238
|
+
const big = "y".repeat(200 * 1024);
|
|
239
|
+
port.postMessage(big);
|
|
240
|
+
port.postMessage(big);
|
|
241
|
+
await until(() => got.length === 3, "both large messages");
|
|
242
|
+
expect(got.slice(1)).toEqual([big, big]);
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
245
|
+
|
|
246
|
+
it("closing a port sends what the window held back before the close", () =>
|
|
247
|
+
withPair({ opener: 64 * 1024, server: 64 * 1024 }, async (h, inbound) => {
|
|
248
|
+
const { port, server } = await agreedPort(h, inbound);
|
|
249
|
+
const consumer = stalled(server);
|
|
250
|
+
const body = "z".repeat(8000);
|
|
251
|
+
for (let i = 0; i < 20; i++) port.postMessage(body);
|
|
252
|
+
await tick(50);
|
|
253
|
+
expect(portFlow(port)?.bufferedAmount()).toBeGreaterThan(0);
|
|
254
|
+
await port.close();
|
|
255
|
+
consumer.release();
|
|
256
|
+
await until(() => consumer.got.length === 21, "every message, the held ones included");
|
|
257
|
+
}));
|
|
258
|
+
|
|
259
|
+
it("an opener with a window and a serving end without: no agreement, messages flow as before", () =>
|
|
260
|
+
withPair({ opener: 64 * 1024 }, async (h, inbound) => {
|
|
261
|
+
const port = await h.opener.openPort();
|
|
262
|
+
port.postMessage("hello");
|
|
263
|
+
await until(() => inbound.length === 1, "the inbound port");
|
|
264
|
+
const server = inbound[0] as NativePort;
|
|
265
|
+
const consumer = stalled(server);
|
|
266
|
+
const fromServer = received(port);
|
|
267
|
+
const body = "w".repeat(1000);
|
|
268
|
+
for (let i = 0; i < 10; i++) port.postMessage(body);
|
|
269
|
+
server.postMessage("answer");
|
|
270
|
+
await until(() => consumer.got.length === 11, "every message, unwindowed");
|
|
271
|
+
await until(() => fromServer.length === 1, "the answer");
|
|
272
|
+
expect(fromServer).toEqual(["answer"]);
|
|
273
|
+
expect(agreed(port)).toBe(false);
|
|
274
|
+
expect(agreed(server)).toBe(false);
|
|
275
|
+
expect(portFlow(port)?.bufferedAmount() ?? 0).toBe(0);
|
|
276
|
+
}));
|
|
277
|
+
|
|
278
|
+
it("a serving end with a window and an opener without: no agreement, messages flow as before", () =>
|
|
279
|
+
withPair({ server: 64 * 1024 }, async (h, inbound) => {
|
|
280
|
+
const port = await h.opener.openPort();
|
|
281
|
+
const fromServer = received(port);
|
|
282
|
+
port.postMessage("hello");
|
|
283
|
+
await until(() => inbound.length === 1, "the inbound port");
|
|
284
|
+
const server = inbound[0] as NativePort;
|
|
285
|
+
const fromClient = received(server);
|
|
286
|
+
const body = "v".repeat(1000);
|
|
287
|
+
for (let i = 0; i < 10; i++) server.postMessage(body);
|
|
288
|
+
await until(() => fromServer.length === 10, "every message, unwindowed");
|
|
289
|
+
await until(() => fromClient.length === 1, "the hello");
|
|
290
|
+
expect(fromServer.every((m) => m === body)).toBe(true);
|
|
291
|
+
expect(agreed(port)).toBe(false);
|
|
292
|
+
expect(agreed(server)).toBe(false);
|
|
293
|
+
}));
|
|
294
|
+
});
|
|
295
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -12,6 +12,13 @@ export {
|
|
|
12
12
|
type MakePortMuxHarness,
|
|
13
13
|
type PortMuxHarness,
|
|
14
14
|
} from "./describe-port-mux.js";
|
|
15
|
+
export {
|
|
16
|
+
type DescribePortWindowOptions,
|
|
17
|
+
describePortWindow,
|
|
18
|
+
type MakePortWindowHarness,
|
|
19
|
+
type PortWindowHarness,
|
|
20
|
+
type PortWindows,
|
|
21
|
+
} from "./describe-port-window.js";
|
|
15
22
|
export {
|
|
16
23
|
type ConnectServePair,
|
|
17
24
|
type MakePair,
|