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
9 changes: 9 additions & 0 deletions .changeset/fix-node-compression-content-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"effect": patch
"@effect/platform-node": patch
"@effect/platform-node-shared": patch
"@effect/platform-bun": patch
"@effect/platform-deno": patch
---

Preserve the response Content-Type when compressing file, raw, stream, and byte-array responses on Node, Bun, Deno, and the web platform, including headers overridden after body construction.
8 changes: 5 additions & 3 deletions packages/effect/src/unstable/http/internal/compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ export const makeCompressionWeb = (options: {
return Effect.succeed(streamBody(
response,
() => options.transform(algorithm, opts)(singleChunkStream(data)),
body.contentType
response.headers["content-type"] ?? body.contentType
))
}
case "Stream": {
const stream = body.stream
return Effect.succeed(streamBody(
response,
() => options.transform(algorithm, opts)(Stream.toReadableStream(stream)),
body.contentType
response.headers["content-type"] ?? body.contentType
))
}
case "Raw": {
Expand All @@ -85,7 +85,9 @@ export const makeCompressionWeb = (options: {
}
return Effect.succeed(setBodyWithoutLength(
response,
HttpBody.raw(options.transform(algorithm, opts)(readable), { contentType: body.contentType })
HttpBody.raw(options.transform(algorithm, opts)(readable), {
contentType: response.headers["content-type"] ?? body.contentType
})
))
}
default: {
Expand Down
35 changes: 35 additions & 0 deletions packages/effect/test/unstable/http/HttpCompression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,41 @@ const randomBytes = (length: number): Uint8Array => {
}

describe("HttpCompression", () => {
for (
const [name, makeResponse] of [
["Stream", () => HttpServerResponse.stream(Stream.succeed(new TextEncoder().encode(bigJson)))],
["Uint8Array", () => HttpServerResponse.uint8Array(new TextEncoder().encode(bigJson))],
["Raw", () => HttpServerResponse.raw(new Response(bigJson).body, { contentType: "text/plain" })]
] as const
) {
it(`preserves a Content-Type header override when compressing web ${name} responses`, async () => {
const original = HttpServerResponse.setHeader(makeResponse(), "content-type", "application/json")
assert.strictEqual(original.headers["content-type"], "application/json")
const handler = makeHandler(Effect.succeed(original))

const compressed = await get(handler, { "accept-encoding": "gzip" })
assert.strictEqual(compressed.headers.get("content-encoding"), "gzip")
assert.strictEqual(await decompress(await compressed.arrayBuffer(), "gzip"), bigJson)
assert.strictEqual(compressed.headers.get("content-type"), original.headers["content-type"])
})
}

it("preserves a header-only Content-Type when compressing web Raw responses", async () => {
const handler = makeHandler(
Effect.sync(() =>
HttpServerResponse.raw(new Response(bigJson).body, { headers: { "content-type": "application/json" } })
)
)
const uncompressed = await get(handler, { "accept-encoding": "identity" })
assert.strictEqual(uncompressed.headers.get("content-type"), "application/json")
assert.strictEqual(await uncompressed.text(), bigJson)

const compressed = await get(handler, { "accept-encoding": "gzip" })
assert.strictEqual(compressed.headers.get("content-encoding"), "gzip")
assert.strictEqual(await decompress(await compressed.arrayBuffer(), "gzip"), bigJson)
assert.strictEqual(compressed.headers.get("content-type"), uncompressed.headers.get("content-type"))
})

it("compresses a compressible response with gzip", async () => {
const response = await get(makeHandler(bigJsonApp), { "accept-encoding": "gzip" })
assert.strictEqual(response.status, 200)
Expand Down
2 changes: 1 addition & 1 deletion packages/platform/node-shared/src/NodeHttpCompression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const make = (fallback: Platform.Compression): Platform.Compression => ({
}
return Effect.map(compress(body.body, algorithm, options), (result) =>
Response.setHeader(
Response.setBody(response, HttpBody.uint8Array(result, body.contentType)),
Response.setBody(response, HttpBody.uint8Array(result, response.headers["content-type"] ?? body.contentType)),
"content-length",
result.byteLength.toString()
))
Expand Down
9 changes: 7 additions & 2 deletions packages/platform/node/src/NodeHttpPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const compression = NodeHttpCompression.make({
NodeStream.pipeThroughDuplex(body.stream, {
evaluate: () => NodeHttpCompression.compressTransform(algorithm, options)
}),
body.contentType
response.headers["content-type"] ?? body.contentType
)
))
}
Expand All @@ -57,7 +57,12 @@ const compression = NodeHttpCompression.make({
transform.on("error", (cause) => readable.destroy(cause))
transform.on("close", () => readable.destroy())
return Effect.succeed(
compressedBody(response, HttpBody.raw(readable.pipe(transform), { contentType: body.contentType }))
compressedBody(
response,
HttpBody.raw(readable.pipe(transform), {
contentType: response.headers["content-type"] ?? body.contentType
})
)
)
}
default: {
Expand Down
48 changes: 48 additions & 0 deletions packages/platform/node/test/NodeHttpCompression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ const get = (
) => handler(new Request("http://localhost/", headers === undefined ? {} : { headers }))

describe("NodeHttpCompression", () => {
for (
const [name, makeResponse] of [
["Stream", () => HttpServerResponse.stream(Stream.succeed(new TextEncoder().encode(bigJson)))],
["Uint8Array", () => HttpServerResponse.uint8Array(new TextEncoder().encode(bigJson))]
] as const
) {
it(`preserves a Content-Type header override when compressing ${name} responses`, () => {
const original = HttpServerResponse.setHeader(makeResponse(), "content-type", "application/json")
assert.strictEqual(original.headers["content-type"], "application/json")
return withHandler(
Effect.succeed(original),
undefined,
async (handler) => {
const compressed = await get(handler, { "accept-encoding": "gzip" })
assert.strictEqual(compressed.headers.get("content-encoding"), "gzip")
assert.strictEqual(Zlib.gunzipSync(new Uint8Array(await compressed.arrayBuffer())).toString(), bigJson)
assert.strictEqual(compressed.headers.get("content-type"), original.headers["content-type"])
}
)
})
}

it.effect("advertises supported algorithms", () =>
Effect.gen(function*() {
const platform = yield* HttpPlatform.HttpPlatform
Expand Down Expand Up @@ -136,6 +158,32 @@ describe("NodeHttpCompression", () => {
)
})

it.effect("preserves the Content-Type of file responses when compressed with Brotli", () =>
Effect.gen(function*() {
const directory = Fs.mkdtempSync(Path.join(Os.tmpdir(), "effect-http-compression-"))
yield* Effect.addFinalizer(() => Effect.sync(() => Fs.rmSync(directory, { recursive: true })))
const path = Path.join(directory, "index.html")
const contents = `<!doctype html><html><body>${"<p>Hello world</p>".repeat(100)}</body></html>`
Fs.writeFileSync(path, contents)

yield* HttpRouter.add("GET", "/file", HttpServerResponse.file(path)).pipe(
(self) => HttpRouter.serve(self, { middleware: HttpMiddleware.compression() }),
Layer.build
)

const uncompressed = yield* HttpClient.get("/file", { headers: { "accept-encoding": "identity" } })
assert.strictEqual(uncompressed.status, 200)
assert.strictEqual(uncompressed.headers["content-encoding"], undefined)
assert.strictEqual(uncompressed.headers["content-type"], "text/html")
assert.strictEqual(yield* uncompressed.text, contents)

const compressed = yield* HttpClient.get("/file", { headers: { "accept-encoding": "br" } })
assert.strictEqual(compressed.status, 200)
assert.strictEqual(compressed.headers["content-encoding"], "br")
assert.strictEqual(yield* compressed.text, contents)
assert.strictEqual(compressed.headers["content-type"], uncompressed.headers["content-type"])
}).pipe(Effect.provide(NodeHttpServer.layerTest)))

it.effect("closes compressed file bodies for HEAD requests", () =>
Effect.gen(function*() {
const directory = Fs.mkdtempSync(Path.join(Os.tmpdir(), "effect-http-compression-"))
Expand Down
Loading