diff --git a/.changeset/hip-cobras-compare.md b/.changeset/hip-cobras-compare.md new file mode 100644 index 00000000000..3a8c99612a4 --- /dev/null +++ b/.changeset/hip-cobras-compare.md @@ -0,0 +1,5 @@ +--- +'@whatwg-node/server': patch +--- + +Support \`Content-Encoding: none\` diff --git a/packages/server/src/plugins/useContentEncoding.ts b/packages/server/src/plugins/useContentEncoding.ts index 679cb33d1bd..e052755ba27 100644 --- a/packages/server/src/plugins/useContentEncoding.ts +++ b/packages/server/src/plugins/useContentEncoding.ts @@ -7,7 +7,7 @@ export function useContentEncoding(): ServerAdapterPlugin diff --git a/packages/server/src/utils.ts b/packages/server/src/utils.ts index 06c76ba1126..26614a8776c 100644 --- a/packages/server/src/utils.ts +++ b/packages/server/src/utils.ts @@ -613,7 +613,7 @@ export function getSupportedEncodings() { export function handleResponseDecompression(response: Response, ResponseCtor: typeof Response) { const contentEncodingHeader = response?.headers.get('content-encoding'); - if (!contentEncodingHeader) { + if (!contentEncodingHeader || contentEncodingHeader === 'none') { return response; } if (!response?.body) { diff --git a/packages/server/test/compression.spec.ts b/packages/server/test/compression.spec.ts index 4ef64b910e4..8baf5491dae 100644 --- a/packages/server/test/compression.spec.ts +++ b/packages/server/test/compression.spec.ts @@ -7,10 +7,11 @@ describe('Compression', () => { const exampleData = JSON.stringify({ hello: 'world', }); + const encodings = [...getSupportedEncodings(), 'none']; describe('Adapter', () => { runTestsForEachFetchImpl( (_, { fetchAPI, createServerAdapter }) => { - for (const encoding of getSupportedEncodings()) { + for (const encoding of encodings) { describe(encoding, () => { it('from the server to the client with "accept-encoding"', async () => { const adapter = createServerAdapter(() => new fetchAPI.Response(exampleData), { @@ -34,7 +35,15 @@ describe('Compression', () => { plugins: [useContentEncoding()], }, ); - const stream = new CompressionStream(encoding); + if (encoding === 'none') { + const res = await adapter.fetch('/', { + method: 'POST', + body: exampleData, + }); + await expect(res.text()).resolves.toEqual(exampleData); + return; + } + const stream = new CompressionStream(encoding as CompressionFormat); const writer = stream.writable.getWriter(); writer.write(exampleData); writer.close(); @@ -77,7 +86,7 @@ describe('Compression', () => { expect(acceptedEncodings).toContain('deflate'); await expect(res.text()).resolves.toEqual(exampleData); }); - for (const encoding of getSupportedEncodings()) { + for (const encoding of encodings) { describe(encoding, () => { it(`from the server to the client`, async () => { const adapter = createServerAdapter(() => new fetchAPI.Response(exampleData), { @@ -89,7 +98,9 @@ describe('Compression', () => { 'accept-encoding': encoding, }, }); - expect(res.headers.get('content-encoding')).toEqual(encoding); + expect(res.headers.get('content-encoding')).toEqual( + encoding === 'none' ? null : encoding, + ); expect(res.status).toEqual(200); await expect(res.text()).resolves.toEqual(exampleData); }); @@ -104,7 +115,15 @@ describe('Compression', () => { }, ); server.addOnceHandler(adapter); - const stream = new CompressionStream(encoding); + if (encoding === 'none') { + const res = await fetchAPI.fetch(server.url, { + method: 'POST', + body: exampleData, + }); + await expect(res.text()).resolves.toEqual(exampleData); + return; + } + const stream = new CompressionStream(encoding as CompressionFormat); const writer = stream.writable.getWriter(); writer.write(exampleData); writer.close();