Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/toclient-raw-response-readable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Fix repeated `text`, `json`, `arrayBuffer`, and `urlParamsBody` reads in `HttpServerResponse.toClientResponse` for raw Web `Response` bodies, preserving the original response for serving.
2 changes: 1 addition & 1 deletion packages/effect/src/unstable/http/HttpServerResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ class ServerHttpClientResponse extends Inspectable.Class implements HttpClientRe
const rawBody = body.body
if (rawBody instanceof Response) {
return Effect.tryPromise({
try: () => rawBody.arrayBuffer().then((buffer) => new Uint8Array(buffer)),
try: () => rawBody.clone().arrayBuffer().then((buffer) => new Uint8Array(buffer)),
catch: (cause) => this.decodeError(cause)
})
}
Expand Down
21 changes: 21 additions & 0 deletions packages/effect/test/unstable/http/HttpServerResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ describe("HttpServerResponse", () => {
assert.strictEqual(roundTrip.headers.get("content-length"), "5")
}))

describe("toClientResponse", () => {
it.effect("supports repeated reads of raw Web Response bodies", () =>
Effect.gen(function*() {
const clientResponse = HttpServerResponse.toClientResponse(
HttpServerResponse.raw(new Response("hello"))
)

assert.strictEqual(yield* clientResponse.text, "hello")
assert.strictEqual(yield* clientResponse.text, "hello")
assert.deepStrictEqual(new Uint8Array(yield* clientResponse.arrayBuffer), new TextEncoder().encode("hello"))
}))

it.effect("leaves the raw Web Response servable after a client-body read", () =>
Effect.gen(function*() {
const response = HttpServerResponse.raw(new Response("hello"))

assert.strictEqual(yield* HttpServerResponse.toClientResponse(response).text, "hello")
assert.strictEqual(yield* Effect.promise(() => HttpServerResponse.toWeb(response).text()), "hello")
}))
})

it.effect("fromClientResponse preserves status, headers, cookies, and json", () =>
Effect.gen(function*() {
const request = HttpClientRequest.get("http://localhost:3000/todos/1?existing=1", {
Expand Down