@statewalker/webrun-streams-conformance 0.5.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-message-pipe.d.ts +35 -0
- package/dist/describe-message-pipe.d.ts.map +1 -0
- package/dist/describe-port-mux.d.ts +19 -0
- package/dist/describe-port-mux.d.ts.map +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 +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +544 -2
- package/package.json +2 -2
- package/src/describe-message-pipe.ts +181 -0
- package/src/describe-port-mux.ts +199 -0
- package/src/describe-port-window.ts +295 -0
- package/src/index.ts +17 -0
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;
|
|
@@ -449,6 +449,548 @@ function concat(chunks) {
|
|
|
449
449
|
return out;
|
|
450
450
|
}
|
|
451
451
|
//#endregion
|
|
452
|
+
//#region src/describe-message-pipe.ts
|
|
453
|
+
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
454
|
+
/**
|
|
455
|
+
* The contract every pipe of byte messages keeps (statewalker/webrun-wire#151),
|
|
456
|
+
* run against an adapter through its harness. The pipe module
|
|
457
|
+
* (`newMessagePipe` in `@statewalker/webrun-streams`) implements it once; each
|
|
458
|
+
* `webrun-streams-*` adapter runs this suite to show it still does.
|
|
459
|
+
*/
|
|
460
|
+
function describeMessagePipe(name, make, options = {}) {
|
|
461
|
+
const lowWater = options.drainLowWater ?? 262144;
|
|
462
|
+
async function withHarness(build, body) {
|
|
463
|
+
const h = await build();
|
|
464
|
+
try {
|
|
465
|
+
await body(h);
|
|
466
|
+
} finally {
|
|
467
|
+
await h.dispose?.();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
describe(`${name}: the message pipe contract`, () => {
|
|
471
|
+
it("carries its own life and ceiling: target.closeSignal is the signal, target.maxMessageSize the ceiling", () => withHarness(make, async ({ pipe }) => {
|
|
472
|
+
expect(pipe.target.closeSignal).toBe(pipe.signal);
|
|
473
|
+
expect(pipe.target.maxMessageSize).toBe(pipe.maxMessageSize);
|
|
474
|
+
}));
|
|
475
|
+
it("delivers inbound bytes to every listener as Uint8Array, in order, past a faulty listener", () => withHarness(make, async (h) => {
|
|
476
|
+
const got = [];
|
|
477
|
+
const other = [];
|
|
478
|
+
h.pipe.target.addEventListener("message", () => {
|
|
479
|
+
throw new Error("a faulty listener");
|
|
480
|
+
});
|
|
481
|
+
h.pipe.target.addEventListener("message", async () => {
|
|
482
|
+
throw new Error("a rejecting listener");
|
|
483
|
+
});
|
|
484
|
+
h.pipe.target.addEventListener("message", (e) => {
|
|
485
|
+
expect(e.data).toBeInstanceOf(Uint8Array);
|
|
486
|
+
got.push([...e.data]);
|
|
487
|
+
});
|
|
488
|
+
const removed = (e) => {
|
|
489
|
+
other.push([...e.data]);
|
|
490
|
+
};
|
|
491
|
+
h.pipe.target.addEventListener("message", removed);
|
|
492
|
+
h.pipe.target.removeEventListener("message", removed);
|
|
493
|
+
await h.inject(new Uint8Array([1, 2]));
|
|
494
|
+
await h.inject(new Uint8Array([3]).buffer);
|
|
495
|
+
await h.inject(new Uint8Array([
|
|
496
|
+
9,
|
|
497
|
+
4,
|
|
498
|
+
9
|
|
499
|
+
]).subarray(1, 2));
|
|
500
|
+
await flush();
|
|
501
|
+
expect(got).toEqual([
|
|
502
|
+
[1, 2],
|
|
503
|
+
[3],
|
|
504
|
+
[4]
|
|
505
|
+
]);
|
|
506
|
+
expect(other).toEqual([]);
|
|
507
|
+
expect(h.pipe.signal.aborted).toBe(false);
|
|
508
|
+
}));
|
|
509
|
+
it("sends bytes one message each, and refuses anything else with TypeError", () => withHarness(make, async (h) => {
|
|
510
|
+
h.pipe.target.postMessage(new Uint8Array([
|
|
511
|
+
1,
|
|
512
|
+
2,
|
|
513
|
+
3
|
|
514
|
+
]));
|
|
515
|
+
h.pipe.target.postMessage(new Uint8Array([4, 5]).buffer);
|
|
516
|
+
expect(() => h.pipe.target.postMessage("text")).toThrow(TypeError);
|
|
517
|
+
expect(() => h.pipe.target.postMessage({ a: 1 })).toThrow(TypeError);
|
|
518
|
+
await flush();
|
|
519
|
+
expect((await h.sent()).map((b) => [...b])).toEqual([[
|
|
520
|
+
1,
|
|
521
|
+
2,
|
|
522
|
+
3
|
|
523
|
+
], [4, 5]]);
|
|
524
|
+
}));
|
|
525
|
+
it("ends when the transport ends: the signal aborts with an Error, and a post is a no-op", () => withHarness(make, async (h) => {
|
|
526
|
+
await h.endRemotely();
|
|
527
|
+
await flush();
|
|
528
|
+
expect(h.pipe.signal.aborted).toBe(true);
|
|
529
|
+
expect(h.pipe.signal.reason).toBeInstanceOf(Error);
|
|
530
|
+
const before = (await h.sent()).length;
|
|
531
|
+
h.pipe.target.postMessage(new Uint8Array([1]));
|
|
532
|
+
await flush();
|
|
533
|
+
expect((await h.sent()).length).toBe(before);
|
|
534
|
+
}));
|
|
535
|
+
it("close() ends the pipe: the signal aborts, and nothing more is delivered", () => withHarness(make, async (h) => {
|
|
536
|
+
const got = [];
|
|
537
|
+
h.pipe.target.addEventListener("message", (e) => {
|
|
538
|
+
got.push(e.data);
|
|
539
|
+
});
|
|
540
|
+
await h.pipe.target.close();
|
|
541
|
+
expect(h.pipe.signal.aborted).toBe(true);
|
|
542
|
+
await h.inject(new Uint8Array([1]));
|
|
543
|
+
await flush();
|
|
544
|
+
expect(got).toEqual([]);
|
|
545
|
+
await h.pipe.target.close();
|
|
546
|
+
}));
|
|
547
|
+
if (options.withMaxFrameBytes) {
|
|
548
|
+
const build = () => options.withMaxFrameBytes?.(100);
|
|
549
|
+
it("a message of exactly maxFrameBytes is sent; one byte past it ends the pipe with RangeError", () => withHarness(build, async (h) => {
|
|
550
|
+
h.pipe.target.postMessage(/* @__PURE__ */ new Uint8Array(100));
|
|
551
|
+
expect(h.pipe.signal.aborted).toBe(false);
|
|
552
|
+
expect(() => h.pipe.target.postMessage(/* @__PURE__ */ new Uint8Array(101))).toThrow(RangeError);
|
|
553
|
+
expect(h.pipe.signal.aborted).toBe(true);
|
|
554
|
+
expect(h.pipe.signal.reason).toBeInstanceOf(RangeError);
|
|
555
|
+
await flush();
|
|
556
|
+
expect((await h.sent()).map((b) => b.byteLength)).toEqual([100]);
|
|
557
|
+
}));
|
|
558
|
+
}
|
|
559
|
+
it("whenDrained resolves at once on an idle pipe, and once the pipe ended", () => withHarness(make, async (h) => {
|
|
560
|
+
await h.pipe.target.whenDrained();
|
|
561
|
+
await h.endRemotely();
|
|
562
|
+
await h.pipe.target.whenDrained();
|
|
563
|
+
}));
|
|
564
|
+
it("a pending whenDrained resolves when the buffer drains to the low-water mark", () => withHarness(make, async (h) => {
|
|
565
|
+
if (!h.setBuffered) return;
|
|
566
|
+
h.setBuffered(lowWater + 1);
|
|
567
|
+
expect(h.pipe.target.bufferedAmount).toBe(lowWater + 1);
|
|
568
|
+
let drained = false;
|
|
569
|
+
const wait = h.pipe.target.whenDrained().then(() => {
|
|
570
|
+
drained = true;
|
|
571
|
+
});
|
|
572
|
+
await new Promise((resolve) => setTimeout(resolve, 20));
|
|
573
|
+
expect(drained).toBe(false);
|
|
574
|
+
h.setBuffered(lowWater);
|
|
575
|
+
await h.settle?.();
|
|
576
|
+
await wait;
|
|
577
|
+
expect(drained).toBe(true);
|
|
578
|
+
}));
|
|
579
|
+
it("a pending whenDrained resolves when the pipe ends", () => withHarness(make, async (h) => {
|
|
580
|
+
if (!h.setBuffered) return;
|
|
581
|
+
h.setBuffered(lowWater + 1);
|
|
582
|
+
const wait = h.pipe.target.whenDrained();
|
|
583
|
+
await h.endRemotely();
|
|
584
|
+
await wait;
|
|
585
|
+
}));
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
//#endregion
|
|
589
|
+
//#region src/describe-port-mux.ts
|
|
590
|
+
/** A value whose encoding the contract's codec refuses to decode. */
|
|
591
|
+
const POISON = "\0poison";
|
|
592
|
+
/** JSON, except that {@link POISON} encodes to a frame `decode` rejects. */
|
|
593
|
+
const contractCodec = {
|
|
594
|
+
encode: (value) => value === POISON ? new Uint8Array([255]) : new TextEncoder().encode(JSON.stringify(value)),
|
|
595
|
+
decode: (bytes) => {
|
|
596
|
+
if (bytes.byteLength === 1 && bytes[0] === 255) throw new Error("undecodable");
|
|
597
|
+
return JSON.parse(new TextDecoder().decode(bytes));
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
const tick$1 = (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
601
|
+
async function until$1(predicate, label, ms = 5e3) {
|
|
602
|
+
const deadline = Date.now() + ms;
|
|
603
|
+
while (!predicate()) {
|
|
604
|
+
if (Date.now() > deadline) throw new Error(`timed out waiting for: ${label}`);
|
|
605
|
+
await tick$1(5);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
function received$1(port) {
|
|
609
|
+
const got = [];
|
|
610
|
+
port.addEventListener("message", (e) => {
|
|
611
|
+
got.push(e.data);
|
|
612
|
+
});
|
|
613
|
+
return got;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* The lifecycle every native port mux keeps (statewalker/webrun-wire#152): the
|
|
617
|
+
* module (`newNativePortMux` in `@statewalker/webrun-streams`) implements it
|
|
618
|
+
* once, and each transport (an in-memory one, WebRTC, libp2p) runs this suite.
|
|
619
|
+
*/
|
|
620
|
+
function describePortMux(name, make) {
|
|
621
|
+
async function withPair(body, accept = () => true) {
|
|
622
|
+
const h = await make(contractCodec);
|
|
623
|
+
const inbound = [];
|
|
624
|
+
try {
|
|
625
|
+
await body(h, inbound, await h.serve((port) => {
|
|
626
|
+
inbound.push(port);
|
|
627
|
+
return accept(port);
|
|
628
|
+
}));
|
|
629
|
+
} finally {
|
|
630
|
+
await h.opener.close().catch(() => void 0);
|
|
631
|
+
await h.dispose();
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
describe(`${name}: the native port mux contract`, () => {
|
|
635
|
+
it("a port opened on one end arrives on the other; values cross both ways, encoded", () => withPair(async (h, inbound) => {
|
|
636
|
+
const port = await h.opener.openPort();
|
|
637
|
+
const fromServer = received$1(port);
|
|
638
|
+
port.postMessage({
|
|
639
|
+
n: 1,
|
|
640
|
+
s: "a"
|
|
641
|
+
});
|
|
642
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
643
|
+
const server = inbound[0];
|
|
644
|
+
const fromClient = received$1(server);
|
|
645
|
+
await until$1(() => fromClient.length === 1, "the first message");
|
|
646
|
+
server.postMessage([2, "b"]);
|
|
647
|
+
await until$1(() => fromServer.length === 1, "the answer");
|
|
648
|
+
expect(fromClient).toEqual([{
|
|
649
|
+
n: 1,
|
|
650
|
+
s: "a"
|
|
651
|
+
}]);
|
|
652
|
+
expect(fromServer).toEqual([[2, "b"]]);
|
|
653
|
+
}));
|
|
654
|
+
it("messages that arrive before a port's first listener are held, for every listener added in that tick", () => withPair(async (h, inbound) => {
|
|
655
|
+
const port = await h.opener.openPort();
|
|
656
|
+
port.postMessage(1);
|
|
657
|
+
port.postMessage(2);
|
|
658
|
+
port.postMessage(3);
|
|
659
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
660
|
+
await tick$1(50);
|
|
661
|
+
const server = inbound[0];
|
|
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
|
+
expect(first).toEqual([
|
|
666
|
+
1,
|
|
667
|
+
2,
|
|
668
|
+
3
|
|
669
|
+
]);
|
|
670
|
+
expect(second).toEqual([
|
|
671
|
+
1,
|
|
672
|
+
2,
|
|
673
|
+
3
|
|
674
|
+
]);
|
|
675
|
+
}));
|
|
676
|
+
it("removeEventListener stops delivery to that listener", () => withPair(async (h, inbound) => {
|
|
677
|
+
const port = await h.opener.openPort();
|
|
678
|
+
port.postMessage("one");
|
|
679
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
680
|
+
const server = inbound[0];
|
|
681
|
+
const kept = [];
|
|
682
|
+
const dropped = [];
|
|
683
|
+
const drop = (e) => {
|
|
684
|
+
dropped.push(e.data);
|
|
685
|
+
};
|
|
686
|
+
server.addEventListener("message", (e) => {
|
|
687
|
+
kept.push(e.data);
|
|
688
|
+
});
|
|
689
|
+
server.addEventListener("message", drop);
|
|
690
|
+
await until$1(() => kept.length === 1, "the first message");
|
|
691
|
+
server.removeEventListener("message", drop);
|
|
692
|
+
port.postMessage("two");
|
|
693
|
+
await until$1(() => kept.length === 2, "the second message");
|
|
694
|
+
expect(dropped).toEqual(["one"]);
|
|
695
|
+
}));
|
|
696
|
+
it("a port the serving end refuses fails: the opener's close signal aborts", () => withPair(async (h) => {
|
|
697
|
+
const port = await h.opener.openPort();
|
|
698
|
+
port.postMessage("hello");
|
|
699
|
+
await until$1(() => port.closeSignal.aborted, "the refusal");
|
|
700
|
+
}, () => false));
|
|
701
|
+
it("an onPort that throws refuses the port", () => withPair(async (h) => {
|
|
702
|
+
const port = await h.opener.openPort();
|
|
703
|
+
port.postMessage("hello");
|
|
704
|
+
await until$1(() => port.closeSignal.aborted, "the refusal");
|
|
705
|
+
}, () => {
|
|
706
|
+
throw new Error("onPort failed");
|
|
707
|
+
}));
|
|
708
|
+
it("a frame the codec cannot decode fails the receiving port", () => withPair(async (h, inbound) => {
|
|
709
|
+
const port = await h.opener.openPort();
|
|
710
|
+
port.postMessage("fine");
|
|
711
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
712
|
+
const server = inbound[0];
|
|
713
|
+
received$1(server);
|
|
714
|
+
port.postMessage(POISON);
|
|
715
|
+
await until$1(() => server.closeSignal.aborted, "the receiving port failed");
|
|
716
|
+
expect(String(server.closeSignal.reason?.message)).toMatch(/decode/);
|
|
717
|
+
}));
|
|
718
|
+
it("closing a port ends it on both ends", () => withPair(async (h, inbound) => {
|
|
719
|
+
const port = await h.opener.openPort();
|
|
720
|
+
port.postMessage("hello");
|
|
721
|
+
await until$1(() => inbound.length === 1, "the inbound port");
|
|
722
|
+
await port.close();
|
|
723
|
+
expect(port.closeSignal.aborted).toBe(true);
|
|
724
|
+
await until$1(() => inbound[0].closeSignal.aborted, "the peer's port closed");
|
|
725
|
+
}));
|
|
726
|
+
it("close() closes the ports the mux opened, and openPort rejects afterwards", () => withPair(async (h) => {
|
|
727
|
+
const a = await h.opener.openPort();
|
|
728
|
+
const b = await h.opener.openPort();
|
|
729
|
+
await h.opener.close();
|
|
730
|
+
expect(a.closeSignal.aborted).toBe(true);
|
|
731
|
+
expect(b.closeSignal.aborted).toBe(true);
|
|
732
|
+
await expect(h.opener.openPort()).rejects.toThrow(/closed/);
|
|
733
|
+
}));
|
|
734
|
+
it("openPort honours its signal: aborted already, or aborted later, which closes the port", () => withPair(async (h) => {
|
|
735
|
+
const aborted = new AbortController();
|
|
736
|
+
aborted.abort(/* @__PURE__ */ new Error("not wanted"));
|
|
737
|
+
await expect(h.opener.openPort(void 0, { signal: aborted.signal })).rejects.toThrow(/not wanted/);
|
|
738
|
+
const later = new AbortController();
|
|
739
|
+
const port = await h.opener.openPort(void 0, { signal: later.signal });
|
|
740
|
+
expect(port.closeSignal.aborted).toBe(false);
|
|
741
|
+
later.abort(/* @__PURE__ */ new Error("done with it"));
|
|
742
|
+
await until$1(() => port.closeSignal.aborted, "the port closed");
|
|
743
|
+
}));
|
|
744
|
+
it("reports its maxMessageSize as a positive number", () => withPair(async (h) => {
|
|
745
|
+
expect(h.opener.maxMessageSize).toBeGreaterThan(0);
|
|
746
|
+
}));
|
|
747
|
+
});
|
|
748
|
+
}
|
|
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
|
|
452
994
|
//#region src/loopback.ts
|
|
453
995
|
/**
|
|
454
996
|
* Loopback pair: `call` invokes the registered `handler` directly, no
|
|
@@ -489,4 +1031,4 @@ const makeLoopbackPair = async () => {
|
|
|
489
1031
|
};
|
|
490
1032
|
};
|
|
491
1033
|
//#endregion
|
|
492
|
-
export { describeDuplexAdapter, 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",
|