Skip to content

Commit

Permalink
feat(server/compression): support Content-Encoding: null
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jul 24, 2024
1 parent 811d36c commit f7bcbea
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-cobras-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@whatwg-node/server': patch
---

Support \`Content-Encoding: none\`
2 changes: 1 addition & 1 deletion packages/server/src/plugins/useContentEncoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function useContentEncoding<TServerContext>(): ServerAdapterPlugin<TServe
onRequest({ request, setRequest, fetchAPI, endResponse }) {
if (request.body) {
const contentEncodingHeader = request.headers.get('content-encoding');
if (contentEncodingHeader) {
if (contentEncodingHeader && contentEncodingHeader !== 'none') {
const contentEncodings = contentEncodingHeader?.split(',');
if (
!contentEncodings.every(encoding =>
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 24 additions & 5 deletions packages/server/test/compression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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), {
Expand All @@ -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();
Expand Down Expand Up @@ -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), {
Expand All @@ -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);
});
Expand All @@ -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();
Expand Down

0 comments on commit f7bcbea

Please sign in to comment.