Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Commit 2509426

Browse files
committed
Add content-lenght to responses and skip when optimization create bigger resource
1 parent 835d196 commit 2509426

File tree

11 files changed

+67
-6
lines changed

11 files changed

+67
-6
lines changed

__tests__/lib/gzip/__snapshots__/middleware.test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@ Object {
1010
"value": "gzip",
1111
},
1212
],
13+
"content-length": Array [
14+
Object {
15+
"key": "Content-Length",
16+
"value": "0",
17+
},
18+
],
1319
}
1420
`;

__tests__/lib/gzip/middleware.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ describe('gzip middleware', () => {
5252
});
5353

5454
expect(response.headers).toEqual({
55+
'content-length': [{ key: 'Content-Length', value: '0' }],
5556
'content-encoding': [{ key: 'Content-Encoding', value: 'gzip' }],
5657
});
5758
});

__tests__/lib/imagemin/__snapshots__/middleware.test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ exports[`imagemin middleware response body matches snapshot when supported 1`] =
44

55
exports[`imagemin middleware response headers match snapshot when supported 1`] = `
66
Object {
7+
"content-length": Array [
8+
Object {
9+
"key": "Content-Length",
10+
"value": "295787",
11+
},
12+
],
713
"content-type": Array [
814
Object {
915
"key": "Content-Type",

__tests__/lib/webp/__snapshots__/middleware.test.js.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ exports[`webp middleware response body matches snapshot when supported 1`] = `"/
44

55
exports[`webp middleware response headers match snapshot when supported 1`] = `
66
Object {
7+
"content-length": Array [
8+
Object {
9+
"key": "Content-Length",
10+
"value": "154016",
11+
},
12+
],
713
"content-type": Array [
814
Object {
915
"key": "Content-Type",

__tests__/lib/webp/middleware.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ describe('webp middleware', () => {
8484
});
8585

8686
expect(response.headers).toEqual({
87+
'content-length': [{ key: 'Content-Length', value: '154016' }],
8788
'content-type': [{ key: 'Content-Type', value: 'image/webp' }],
8889
});
8990
});

__tests__/origin-request.test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,26 @@ describe('origin-request handler', () => {
4141
};
4242
nock('http://example.com')
4343
.get('/gzip-supported.js')
44-
.reply(200, '', { 'content-type': 'text/javascript', 'content-length': '0' });
44+
.reply(200, 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', { 'content-type': 'text/javascript', 'content-length': '30' });
4545

4646
const callback = jest.fn();
4747

4848
await originRequest.handler(event, null, callback);
4949

5050
expect(callback).toHaveBeenCalledWith(null, {
51-
body: 'H4sIAAAAAAACAwMAAAAAAAAAAAA=',
51+
body: 'H4sIAAAAAAACA3N0xAcAG2p/MR4AAAA=',
5252
bodyEncoding: 'base64',
5353
headers: {
54+
'content-length': [
55+
{ key: 'Content-Length', value: '23' },
56+
],
5457
'content-encoding': [
5558
{ key: 'Content-Encoding', value: 'gzip' },
5659
],
5760
'content-type': [
5861
{ key: 'Content-Type', value: 'text/javascript' },
5962
],
60-
'x-orig-size': [{ key: 'X-Orig-Size', value: '0' }],
63+
'x-orig-size': [{ key: 'X-Orig-Size', value: '30' }],
6164
},
6265
status: 200,
6366
statusDescription: 'OK',

lib/gzip/middleware.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ module.exports = async ({ request, response }) => {
1313
if (isSupported(request)) {
1414
const gzipCompress = require('./compress');
1515

16-
const base64Body = (await gzipCompress(response.body)).toString(
16+
const buffer = await gzipCompress(response.body);
17+
const contentLength = buffer.byteLength;
18+
const base64Body = buffer.toString(
1719
'base64'
1820
);
1921

@@ -26,6 +28,9 @@ module.exports = async ({ request, response }) => {
2628
'content-encoding': [
2729
{ key: 'Content-Encoding', value: 'gzip' },
2830
],
31+
'content-length': [
32+
{ key: 'Content-Length', value: `${contentLength}` },
33+
],
2934
},
3035
},
3136
bodyEncoding: 'base64',

lib/imagemin/middleware.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,24 @@ module.exports = async ({ request, response }) => {
99
if (isSupported(request)) {
1010
const imageminCompress = require('./compress');
1111

12-
const base64Body = (await imageminCompress(response.body)).toString(
12+
const buffer = await imageminCompress(response.body);
13+
const contentLength = buffer.byteLength;
14+
const base64Body = buffer.toString(
1315
'base64'
1416
);
1517

1618
response = {
1719
...response,
1820
body: base64Body,
1921
bodyEncoding: 'base64',
22+
headers: {
23+
...response.headers,
24+
...{
25+
'content-length': [
26+
{ key: 'Content-Length', value: `${contentLength}` },
27+
],
28+
}
29+
}
2030
};
2131
}
2232

lib/limitSize/middleware.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-check
22

33
const logger = require('../logger');
4+
const { readHeader } = require('../responseHelper');
45

56
/**
67
* Prevents lambda from throwing 5xx errors because the response it generated exceeds the limit.
@@ -15,6 +16,15 @@ module.exports = async ({ request, response }) => {
1516
);
1617
response = request;
1718
}
19+
20+
const newContentLength = readHeader(response, 'content-length');
21+
const originalContentLength = readHeader(response, 'x-orig-size');
22+
if (originalContentLength && newContentLength && newContentLength >= originalContentLength) {
23+
logger.log(
24+
`Compressed response body size (${newContentLength}B) is not smaller than original size (${originalContentLength}B), skipping.`
25+
);
26+
response = request;
27+
}
1828
}
1929

2030
return { request, response };

lib/responseHelper.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,16 @@ const requestToHeaders = request => {
6969
return convertedHeaders;
7070
};
7171

72+
const readHeader = (request, headerName) => {
73+
if (request.headers && request.headers[headerName] && request.headers[headerName][0]) {
74+
return request.headers[headerName][0].value;
75+
}
76+
return undefined;
77+
};
78+
7279
module.exports = {
7380
isError,
7481
headersToLambda,
7582
requestToHeaders,
83+
readHeader,
7684
};

0 commit comments

Comments
 (0)