Skip to content

Commit 127167a

Browse files
committed
fix HttpServerRequest.toWeb missing duplex option for streaming bodies
1 parent e976756 commit 127167a

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

.changeset/tall-buses-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect/platform": patch
3+
---
4+
5+
fix HttpServerRequest.toWeb missing duplex option for streaming bodies in Node

packages/platform-node/test/HttpServer.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,4 +755,30 @@ describe("HttpServer", () => {
755755
)
756756
expect(root).toEqual("root")
757757
}).pipe(Effect.provide(NodeHttpServer.layerTest)))
758+
759+
describe("HttpServerRequest.toWeb", () => {
760+
it.scoped("converts POST request with body", () =>
761+
Effect.gen(function*() {
762+
yield* HttpRouter.empty.pipe(
763+
HttpRouter.post(
764+
"/echo",
765+
Effect.gen(function*() {
766+
const request = yield* HttpServerRequest.HttpServerRequest
767+
const webRequest = HttpServerRequest.toWeb(request)
768+
assert(webRequest !== undefined, "toWeb returned undefined")
769+
const body = yield* Effect.promise(() => webRequest.json())
770+
return HttpServerResponse.unsafeJson({ received: body })
771+
})
772+
),
773+
HttpServer.serveEffect()
774+
)
775+
const client = yield* HttpClient.HttpClient
776+
const res = yield* client.post("/echo", {
777+
body: HttpBody.unsafeJson({ message: "hello" })
778+
})
779+
assert.strictEqual(res.status, 200)
780+
const json = yield* res.json
781+
assert.deepStrictEqual(json, { received: { message: "hello" } })
782+
}).pipe(Effect.provide(NodeHttpServer.layerTest)))
783+
})
758784
})

packages/platform/src/HttpServerRequest.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,13 @@ export const toWeb = (self: HttpServerRequest): Request | undefined => {
241241
}
242242
const ourl = toURL(self)
243243
if (Option.isNone(ourl)) return undefined
244+
const body = hasBody(self.method) ? Stream.toReadableStream(self.stream) : undefined
244245
return new Request(ourl.value, {
245246
method: self.method,
246-
body: hasBody(self.method) ? Stream.toReadableStream(self.stream) : undefined,
247-
headers: self.headers
247+
body,
248+
headers: self.headers,
249+
// @ts-expect-error - duplex is required for streaming bodies in Node 18+
250+
duplex: body !== undefined ? "half" : undefined
248251
})
249252
}
250253

0 commit comments

Comments
 (0)