The best way to control Claude Code, Codex and more from any computer or phone

main
ts 78 lines 3.11 KB
Raw
1 import * as NodeMultipart from "@effect/platform-node/NodeMultipart"
2 import { assert, describe, it } from "@effect/vitest"
3 import * as Effect from "effect/Effect"
4 import * as Fiber from "effect/Fiber"
5 import * as Stream from "effect/Stream"
6 import { Readable } from "node:stream"
7 import { setImmediate as nextTurn } from "node:timers/promises"
8
9 const headers = { "content-type": "multipart/form-data; boundary=x" }
10 const form = "--x\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\nb\r\n--x--\r\n"
11 const destroy = (source: Readable, error?: Error) =>
12 Effect.promise(() => {
13 const closed = new Promise((resolve) => source.once("close", resolve))
14 source.destroy(error)
15 return closed
16 })
17
18 describe("NodeMultipart", () => {
19 it.effect("removes its source listeners after parsing", () =>
20 Effect.gen(function*() {
21 const source = Readable.from([Buffer.from(form)])
22 yield* NodeMultipart.stream(source, headers).pipe(Stream.runDrain)
23 yield* Effect.promise(() => nextTurn())
24
25 assert.deepStrictEqual(source.eventNames(), [])
26 }))
27
28 it.effect("fails with a typed parse error when the source terminates early", () =>
29 Effect.gen(function*() {
30 const cause = new Error("boom")
31 const destroyed = new Readable({ read() {} }).on("error", () => {})
32 yield* destroy(destroyed, cause)
33 const error = yield* NodeMultipart.stream(destroyed, headers).pipe(Stream.runDrain, Effect.flip)
34 assert.strictEqual(error.reason._tag, "Parse")
35 assert.strictEqual(error.reason.cause, cause)
36
37 const closed = new Readable({ read() {} })
38 closed.push("--x\r\nContent-Disposition: form-data; name=\"a\"; filename=\"a.txt\"\r\n\r\nb")
39 const closedError = yield* NodeMultipart.stream(closed, headers).pipe(
40 Stream.runForEach((part) => {
41 closed.destroy()
42 return Effect.andThen(
43 Effect.promise(() => nextTurn()),
44 part._tag === "File" ? Stream.runDrain(part.content) : Effect.void
45 )
46 }),
47 Effect.flip
48 )
49 assert.strictEqual(closedError._tag, "MultipartError")
50 }))
51
52 it.effect("leaves the source open on interruption and handles its later error", () =>
53 Effect.gen(function*() {
54 const source = new Readable({ read() {} })
55 source.push(form.slice(0, 20))
56 const fiber = yield* NodeMultipart.stream(source, headers).pipe(Stream.runDrain, Effect.forkChild)
57 yield* Effect.promise(() => nextTurn())
58 yield* Fiber.interrupt(fiber)
59 assert.isFalse(source.destroyed)
60
61 yield* destroy(source, new Error("late"))
62 assert.deepStrictEqual(source.eventNames(), [])
63 }))
64
65 it.effect("fails with a typed parse error when the boundary is missing", () =>
66 Effect.gen(function*() {
67 const body = Readable.from([
68 Buffer.from("--x\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\n1\r\n--x--\r\n")
69 ])
70 const error = yield* NodeMultipart.stream(body, { "content-type": "multipart/form-data" }).pipe(
71 Stream.runCollect,
72 Effect.flip
73 )
74
75 assert.strictEqual(error._tag, "MultipartError")
76 assert.strictEqual(error.reason._tag, "Parse")
77 }))
78 })