Skip to content

Commit 51930c0

Browse files
authored
Fix typed multipart errors for missing boundaries (#9262)
1 parent e24cd76 commit 51930c0

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect/platform-node": patch
3+
---
4+
5+
Return a typed multipart parse error instead of a defect when a multipart request is missing its boundary.

‎packages/platform/node/src/NodeMultipartParser.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export class MultipartStream extends Duplex {
8888
super({ readableObjectMode: true })
8989
let currentError: MultipartError | undefined
9090
let currentFile: FileStream | undefined
91+
let initialized = false
9192
this._parser = makeParser({
9293
...(config as any),
9394
onField: (info, value) => {
@@ -112,14 +113,20 @@ export class MultipartStream extends Duplex {
112113
}
113114
},
114115
onError: (error) => {
116+
currentError = error
117+
if (!initialized) return
115118
this.emit("error", error)
116119
currentFile?.emit("error", error)
117-
currentError = error
118120
},
119121
onDone: () => {
120122
this.push(null)
121123
}
122124
})
125+
initialized = true
126+
if (currentError !== undefined) {
127+
// destroy defers the error event so callers can attach their listeners.
128+
this.destroy(currentError as any)
129+
}
123130
}
124131

125132
_resume() {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 Stream from "effect/Stream"
5+
import { Readable } from "node:stream"
6+
7+
describe("NodeMultipart", () => {
8+
it.effect("fails with a typed parse error when the boundary is missing", () =>
9+
Effect.gen(function*() {
10+
const body = Readable.from([
11+
Buffer.from("--x\r\nContent-Disposition: form-data; name=\"a\"\r\n\r\n1\r\n--x--\r\n")
12+
])
13+
const error = yield* NodeMultipart.stream(body, { "content-type": "multipart/form-data" }).pipe(
14+
Stream.runCollect,
15+
Effect.flip
16+
)
17+
18+
assert.strictEqual(error._tag, "MultipartError")
19+
assert.strictEqual(error.reason._tag, "Parse")
20+
}))
21+
})

0 commit comments

Comments
 (0)