|
1 | 1 | import { assert, it } from "@effect/vitest"; |
2 | | -import * as Effect from "effect/Effect"; |
3 | | -import * as Socket from "effect/socket/Socket"; |
4 | 2 |
|
5 | | -import { |
6 | | - joinSocketFrameParts, |
7 | | - SOCKET_FRAME_PART_BYTES, |
8 | | - splitLargeSocketFrames, |
9 | | -} from "./socketFrameParts.ts"; |
10 | | - |
11 | | -const unusedUpgrade = () => Effect.void; |
12 | | - |
13 | | -function recordingSocket(sent: Array<string | Uint8Array>) { |
14 | | - return Socket.make({ |
15 | | - reader: Effect.succeed({ pull: Effect.never, upgrade: unusedUpgrade }), |
16 | | - writer: Effect.succeed({ |
17 | | - write: (chunk) => |
18 | | - Effect.sync(() => { |
19 | | - if (!Socket.isCloseEvent(chunk)) sent.push(chunk); |
20 | | - }), |
21 | | - writeAll: (chunks) => Effect.sync(() => void sent.push(...chunks)), |
22 | | - }), |
23 | | - }); |
24 | | -} |
25 | | - |
26 | | -function replayingSocket(batches: ReadonlyArray<readonly [string, ...Array<string>]>) { |
27 | | - let next = 0; |
28 | | - return Socket.make({ |
29 | | - reader: Effect.succeed({ |
30 | | - pull: Effect.suspend(() => |
31 | | - next < batches.length ? Effect.succeed(batches[next++]!) : Effect.never, |
32 | | - ), |
33 | | - upgrade: unusedUpgrade, |
34 | | - }), |
35 | | - writer: Effect.succeed({ write: () => Effect.void, writeAll: () => Effect.void }), |
36 | | - }); |
37 | | -} |
38 | | - |
39 | | -it.effect("a large message reaches the client whole, with a frame for every part on the way", () => |
40 | | - Effect.gen(function* () { |
41 | | - // Two-, three- and four-byte characters, the last as surrogate pairs that |
42 | | - // fall across part boundaries. |
43 | | - const message = JSON.stringify({ |
44 | | - _tag: "Chunk", |
45 | | - values: ["é€😀x".repeat(SOCKET_FRAME_PART_BYTES / 4)], |
46 | | - }); |
47 | | - const sent: Array<string | Uint8Array> = []; |
48 | | - const serverWriter = yield* splitLargeSocketFrames(recordingSocket(sent)).writer; |
49 | | - yield* serverWriter.write(message); |
50 | | - yield* serverWriter.write('{"_tag":"Pong"}'); |
51 | | - |
52 | | - const parts = sent.filter((frame) => typeof frame === "string"); |
53 | | - assert.isAbove(parts.length, 3); |
54 | | - const encoded = parts.map((part) => new TextEncoder().encode(part)); |
55 | | - assert.isTrue(encoded.every((bytes) => bytes.length <= SOCKET_FRAME_PART_BYTES)); |
56 | | - assert.isTrue( |
57 | | - encoded.every((bytes, index) => new TextDecoder().decode(bytes) === parts[index]), |
58 | | - ); |
59 | | - |
60 | | - const [first, ...rest] = parts; |
61 | | - const reader = yield* joinSocketFrameParts(replayingSocket([[first!, ...rest]])).reader; |
62 | | - const received = yield* reader.pull; |
63 | | - |
64 | | - assert.strictEqual(received.length, parts.length); |
65 | | - const held = received.slice(0, -2); |
66 | | - assert.isTrue(held.every((frame) => JSON.parse(frame as string)._tag === "Pong")); |
67 | | - assert.strictEqual(received.at(-2), message); |
68 | | - assert.strictEqual(received.at(-1), '{"_tag":"Pong"}'); |
69 | | - }).pipe(Effect.scoped), |
70 | | -); |
71 | | - |
72 | | -it.effect("a server that predates parts still reaches the client whole", () => |
73 | | - Effect.gen(function* () { |
74 | | - const message = JSON.stringify({ |
75 | | - _tag: "Chunk", |
76 | | - values: ["x".repeat(3 * SOCKET_FRAME_PART_BYTES)], |
77 | | - }); |
78 | | - const reader = yield* joinSocketFrameParts(replayingSocket([[message]])).reader; |
79 | | - assert.deepStrictEqual(yield* reader.pull, [message]); |
80 | | - }).pipe(Effect.scoped), |
81 | | -); |
| 3 | +import { SOCKET_FRAME_PART_BYTES, splitSocketFrame } from "./socketFrameParts.ts"; |
| 4 | + |
| 5 | +it("splits a large frame into parts that each fit the budget and stay valid UTF-8", () => { |
| 6 | + // Surrogate pairs fall across part boundaries; splitting one would turn it into U+FFFD on the wire. |
| 7 | + const parts = splitSocketFrame("é€😀x".repeat(SOCKET_FRAME_PART_BYTES / 4)); |
| 8 | + assert.isAbove(parts.length, 1); |
| 9 | + for (const part of parts) { |
| 10 | + const bytes = new TextEncoder().encode(part); |
| 11 | + assert.isAtMost(bytes.length, SOCKET_FRAME_PART_BYTES); |
| 12 | + assert.strictEqual(new TextDecoder().decode(bytes), part); |
| 13 | + } |
| 14 | +}); |
0 commit comments