|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import * as Socket from "effect/socket/Socket"; |
| 4 | + |
| 5 | +import { |
| 6 | + joinSocketFrameParts, |
| 7 | + SOCKET_FRAME_PART_LENGTH, |
| 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 | + // Emoji put surrogate pairs across part boundaries. |
| 42 | + const message = JSON.stringify({ |
| 43 | + _tag: "Chunk", |
| 44 | + values: ["😀".repeat(SOCKET_FRAME_PART_LENGTH), "x".repeat(SOCKET_FRAME_PART_LENGTH)], |
| 45 | + }); |
| 46 | + const sent: Array<string | Uint8Array> = []; |
| 47 | + const serverWriter = yield* splitLargeSocketFrames(recordingSocket(sent)).writer; |
| 48 | + yield* serverWriter.write(message); |
| 49 | + yield* serverWriter.write('{"_tag":"Pong"}'); |
| 50 | + |
| 51 | + const parts = sent.filter((frame) => typeof frame === "string"); |
| 52 | + assert.isAbove(parts.length, 3); |
| 53 | + assert.isTrue(parts.every((part) => part.length <= SOCKET_FRAME_PART_LENGTH + 1)); |
| 54 | + assert.isTrue( |
| 55 | + parts.every((part) => new TextDecoder().decode(new TextEncoder().encode(part)) === part), |
| 56 | + ); |
| 57 | + |
| 58 | + const [first, ...rest] = parts; |
| 59 | + const reader = yield* joinSocketFrameParts(replayingSocket([[first!, ...rest]])).reader; |
| 60 | + const received = yield* reader.pull; |
| 61 | + |
| 62 | + assert.strictEqual(received.length, parts.length); |
| 63 | + const held = received.slice(0, -2); |
| 64 | + assert.isTrue(held.every((frame) => JSON.parse(frame as string)._tag === "Pong")); |
| 65 | + assert.strictEqual(received.at(-2), message); |
| 66 | + assert.strictEqual(received.at(-1), '{"_tag":"Pong"}'); |
| 67 | + }).pipe(Effect.scoped), |
| 68 | +); |
| 69 | + |
| 70 | +it.effect("a server that predates parts still reaches the client whole", () => |
| 71 | + Effect.gen(function* () { |
| 72 | + const message = JSON.stringify({ |
| 73 | + _tag: "Chunk", |
| 74 | + values: ["x".repeat(3 * SOCKET_FRAME_PART_LENGTH)], |
| 75 | + }); |
| 76 | + const reader = yield* joinSocketFrameParts(replayingSocket([[message]])).reader; |
| 77 | + assert.deepStrictEqual(yield* reader.pull, [message]); |
| 78 | + }).pipe(Effect.scoped), |
| 79 | +); |
0 commit comments