Skip to content

Commit 2627663

Browse files
committed
Addressed the feedbacks
1 parent 3bfccbc commit 2627663

File tree

3 files changed

+50
-30
lines changed

3 files changed

+50
-30
lines changed

packages/event-handler/src/rest/middleware/compress.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ const compress = (options?: CompressionOptions): Middleware => {
7575
// Check if response should be compressed
7676
if (
7777
isEncodedOrChunked ||
78-
reqCtx.request.method === 'HEAD' || // HEAD request
79-
(contentLength && Number(contentLength) < threshold) || // content-length below threshold
80-
!shouldCompress(reqCtx.res) || // not compressible type
81-
!shouldTransform(reqCtx.res) || // cache-control: no-transform
78+
reqCtx.request.method === 'HEAD' ||
79+
(contentLength && Number(contentLength) < threshold) ||
80+
!shouldCompress(reqCtx.res) ||
81+
!shouldTransform(reqCtx.res) ||
8282
!reqCtx.res.body
8383
) {
8484
return;

packages/event-handler/tests/unit/rest/helpers.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ export const createNoNextMiddleware = (
7070
export const createSettingHeadersMiddleware = (headers: {
7171
[key: string]: string;
7272
}): Middleware => {
73-
return async (_params, _options, next) => {
73+
return async (_params, options, next) => {
7474
await next();
7575
Object.entries(headers).forEach(([key, value]) => {
76-
_options.res.headers.set(key, value);
76+
options.res.headers.set(key, value);
7777
});
7878
};
7979
};

packages/event-handler/tests/unit/rest/middleware/compress.test.ts

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,50 @@ describe('Compress Middleware', () => {
107107
expect(result.headers?.['content-encoding']).toEqual('gzip');
108108
});
109109

110-
it('skips compression for non-compressible content types', async () => {
111-
// Prepare
112-
const event = createTestEvent('/test', 'GET');
113-
const app = new Router();
114-
app.get(
115-
'/test',
116-
[
117-
compress(),
118-
createSettingHeadersMiddleware({
119-
'content-length': '2000',
120-
'content-type': 'image/jpeg',
121-
}),
122-
],
123-
async () => {
124-
return { test: 'x'.repeat(2000) };
125-
}
126-
);
127-
128-
// Act
129-
const result = await app.resolve(event, context);
130-
131-
// Assess
132-
expect(result.headers?.['content-encoding']).toBeUndefined();
133-
});
110+
it.each([
111+
'image/jpeg',
112+
'image/png',
113+
'image/gif',
114+
'audio/mpeg',
115+
'audio/mp4',
116+
'audio/ogg',
117+
'video/mp4',
118+
'video/mpeg',
119+
'video/webm',
120+
'application/zip',
121+
'application/gzip',
122+
'application/x-gzip',
123+
'application/octet-stream',
124+
'application/pdf',
125+
'application/msword',
126+
'text/event-stream',
127+
])(
128+
'skips compression for non-compressible content types',
129+
async (contentType) => {
130+
// Prepare
131+
const event = createTestEvent('/test', 'GET');
132+
const app = new Router();
133+
app.get(
134+
'/test',
135+
[
136+
compress(),
137+
createSettingHeadersMiddleware({
138+
'content-length': '2000',
139+
'content-type': contentType,
140+
}),
141+
],
142+
async () => {
143+
return { test: 'x'.repeat(2000) };
144+
}
145+
);
146+
147+
// Act
148+
const result = await app.resolve(event, context);
149+
150+
// Assess
151+
expect(result.headers?.['content-encoding']).toBeUndefined();
152+
}
153+
);
134154

135155
it('skips compression when cache-control no-transform is set', async () => {
136156
// Prepare

0 commit comments

Comments
 (0)