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

Commit 455e9b3

Browse files
author
Mateusz Krzeszowiak
committed
Update packages, fix tests and filter passed request headers
1 parent ae258aa commit 455e9b3

File tree

14 files changed

+2510
-2972
lines changed

14 files changed

+2510
-2972
lines changed

__tests__/lib/brotli/compress.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('brotli compress', () => {
2323
});
2424

2525
test('works for Buffer input', async () => {
26-
const input = Buffer.from('test input to compress');
26+
const input = global.Buffer.from('test input to compress');
2727

2828
const output = await compress(input);
2929
expect(output).toBeTruthy();

__tests__/lib/brotli/middleware.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('brotli middleware', () => {
2222
return jest.fn(() => true);
2323
});
2424
jest.mock('../../../lib/brotli/compress', () => {
25-
return jest.fn(input => Buffer.from(input));
25+
return jest.fn(input => global.Buffer.from(input));
2626
});
2727
jest.mock('../../../lib/getOriginUrl', () => {
2828
return jest.fn(() => 'http://example.com/brotli-supported');
@@ -66,7 +66,7 @@ describe('brotli middleware', () => {
6666
return jest.fn(() => true);
6767
});
6868
jest.mock('../../../lib/brotli/compress', () => {
69-
return jest.fn(input => Buffer.from(input));
69+
return jest.fn(input => global.Buffer.from(input));
7070
});
7171
jest.mock('../../../lib/getOriginUrl', () => {
7272
return jest.fn(() => 'http://example.com/content-encoding');
@@ -95,7 +95,7 @@ describe('brotli middleware', () => {
9595
return jest.fn(() => true);
9696
});
9797
jest.mock('../../../lib/brotli/compress', () => {
98-
return jest.fn(input => Buffer.from(input));
98+
return jest.fn(input => global.Buffer.from(input));
9999
});
100100
jest.mock('../../../lib/getOriginUrl', () => {
101101
return jest.fn(() => 'http://example.com/null-parameter');
@@ -115,7 +115,7 @@ describe('brotli middleware', () => {
115115
return jest.fn(() => true);
116116
});
117117
jest.mock('../../../lib/brotli/compress', () => {
118-
return jest.fn(input => Buffer.from(input));
118+
return jest.fn(input => global.Buffer.from(input));
119119
});
120120
jest.mock('../../../lib/getOriginUrl', () => {
121121
return jest.fn(() => 'http://example.com/response-body-snapshot');
@@ -135,7 +135,7 @@ describe('brotli middleware', () => {
135135
return jest.fn(() => true);
136136
});
137137
jest.mock('../../../lib/brotli/compress', () => {
138-
return jest.fn(input => Buffer.from(input));
138+
return jest.fn(input => global.Buffer.from(input));
139139
});
140140
jest.mock('../../../lib/getOriginUrl', () => {
141141
return jest.fn(
@@ -197,7 +197,7 @@ describe('brotli middleware', () => {
197197
return jest.fn(() => true);
198198
});
199199
jest.mock('../../../lib/brotli/compress', () => {
200-
return jest.fn(input => Buffer.from(input));
200+
return jest.fn(input => global.Buffer.from(input));
201201
});
202202
jest.mock('../../../lib/getOriginUrl', () => {
203203
return jest.fn(() => 'http://example.com/response-headers-forward');

__tests__/lib/gzip/middleware.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('gzip middleware', () => {
2525
return jest.fn(() => true);
2626
});
2727
jest.mock('../../../lib/gzip/compress', () => {
28-
return jest.fn(input => Buffer.from(input));
28+
return jest.fn(input => global.Buffer.from(input));
2929
});
3030
jest.mock('../../../lib/getOriginUrl', () => {
3131
return jest.fn(() => 'http://example.com/response-gzip-supported');
@@ -105,7 +105,7 @@ describe('gzip middleware', () => {
105105
return jest.fn(() => true);
106106
});
107107
jest.mock('../../../lib/gzip/compress', () => {
108-
return jest.fn(input => Buffer.from(input));
108+
return jest.fn(input => global.Buffer.from(input));
109109
});
110110
jest.mock('../../../lib/getOriginUrl', () => {
111111
return jest.fn(() => 'http://example.com/content-encoding');
@@ -134,7 +134,7 @@ describe('gzip middleware', () => {
134134
return jest.fn(() => true);
135135
});
136136
jest.mock('../../../lib/gzip/compress', () => {
137-
return jest.fn(input => Buffer.from(input));
137+
return jest.fn(input => global.Buffer.from(input));
138138
});
139139
jest.mock('../../../lib/getOriginUrl', () => {
140140
return jest.fn(() => 'http://example.com/null-parameter');
@@ -154,7 +154,7 @@ describe('gzip middleware', () => {
154154
return jest.fn(() => true);
155155
});
156156
jest.mock('../../../lib/gzip/compress', () => {
157-
return jest.fn(input => Buffer.from(input));
157+
return jest.fn(input => global.Buffer.from(input));
158158
});
159159
jest.mock('../../../lib/getOriginUrl', () => {
160160
return jest.fn(() => 'http://example.com/response-body-snapshot');
@@ -174,7 +174,7 @@ describe('gzip middleware', () => {
174174
return jest.fn(() => true);
175175
});
176176
jest.mock('../../../lib/gzip/compress', () => {
177-
return jest.fn(input => Buffer.from(input));
177+
return jest.fn(input => global.Buffer.from(input));
178178
});
179179
jest.mock('../../../lib/getOriginUrl', () => {
180180
return jest.fn(
@@ -235,7 +235,7 @@ describe('gzip middleware', () => {
235235
return jest.fn(() => true);
236236
});
237237
jest.mock('../../../lib/gzip/compress', () => {
238-
return jest.fn(input => Buffer.from(input));
238+
return jest.fn(input => global.Buffer.from(input));
239239
});
240240
jest.mock('../../../lib/getOriginUrl', () => {
241241
return jest.fn(() => 'http://example.com/response-headers-forward');

__tests__/lib/imagemin/middleware.test.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('imagemin middleware', () => {
131131
return jest.fn(() => true);
132132
});
133133
jest.mock('../../../lib/imagemin/compress', () => {
134-
return jest.fn(input => Buffer.from(input));
134+
return jest.fn(input => global.Buffer.from(input));
135135
});
136136
jest.mock('../../../lib/getOriginUrl', () => {
137137
return jest.fn(() => 'http://example.com/null-parameter');
@@ -185,7 +185,7 @@ describe('imagemin middleware', () => {
185185
nock('http://example.com')
186186
.get('/request-headers-pass')
187187
.reply(function(uri, requestBody, cb) {
188-
expect(this.req.headers['x-test-header']).toBe('test-value');
188+
expect(this.req.headers['accept-encoding']).toBe('image/png');
189189

190190
cb(null, [200, input, { 'content-type': 'image/png' }]);
191191
});
@@ -194,10 +194,47 @@ describe('imagemin middleware', () => {
194194
await middleware(
195195
{
196196
headers: {
197-
'x-test-header': [
197+
'accept-encoding': [
198198
{
199-
value: 'test-value',
200-
key: 'X-Test-Header',
199+
value: 'image/png',
200+
key: 'Accept-Encoding',
201+
},
202+
],
203+
},
204+
},
205+
{ headers: {} }
206+
);
207+
});
208+
209+
test('preserves host request headers to origin server', async () => {
210+
jest.mock('../../../lib/imagemin/isSupported', () => {
211+
return jest.fn(() => true);
212+
});
213+
jest.mock('../../../lib/imagemin/compress', () => {
214+
return jest.fn(input => input);
215+
});
216+
jest.mock('../../../lib/getOriginUrl', () => {
217+
return jest.fn(() => 'http://example.com/request-headers-host');
218+
});
219+
const input = await readFileAsync(
220+
path.resolve(__dirname, './image-png.png')
221+
);
222+
nock('http://example.com')
223+
.get('/request-headers-host')
224+
.reply(function(uri, requestBody, cb) {
225+
expect(this.req.headers['host']).toBe('example.com');
226+
227+
cb(null, [200, input, { 'content-type': 'image/png' }]);
228+
});
229+
230+
const middleware = require('../../../lib/imagemin/middleware');
231+
await middleware(
232+
{
233+
headers: {
234+
host: [
235+
{
236+
value: 'localhost',
237+
key: 'Host',
201238
},
202239
],
203240
},

__tests__/lib/webp-accept/middleware.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('webp middleware', () => {
106106
return jest.fn(() => true);
107107
});
108108
jest.mock('../../../lib/webp-accept/compress', () => {
109-
return jest.fn(input => Buffer.from(input));
109+
return jest.fn(input => global.Buffer.from(input));
110110
});
111111
jest.mock('../../../lib/getOriginUrl', () => {
112112
return jest.fn(() => 'http://example.com/null-parameter');

__tests__/lib/webp-url/middleware.test.js

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('webp middleware', () => {
149149
nock('http://example.com')
150150
.get('/request-headers-pass')
151151
.reply(function(uri, requestBody, cb) {
152-
expect(this.req.headers['x-test-header']).toBe('test-value');
152+
expect(this.req.headers['accept-encoding']).toBe('image/png');
153153

154154
cb(null, [200, input, { 'content-type': 'image/jpg' }]);
155155
});
@@ -158,10 +158,47 @@ describe('webp middleware', () => {
158158
await middleware(
159159
{
160160
headers: {
161-
'x-test-header': [
161+
'accept-encoding': [
162162
{
163-
value: 'test-value',
164-
key: 'X-Test-Header',
163+
value: 'image/png',
164+
key: 'Accept-Encoding',
165+
},
166+
],
167+
},
168+
},
169+
{ headers: {} }
170+
);
171+
});
172+
173+
test('preserves host request header to origin server', async () => {
174+
jest.mock('../../../lib/webp-url/isSupported', () => {
175+
return jest.fn(() => true);
176+
});
177+
jest.mock('../../../lib/webp-url/compress', () => {
178+
return jest.fn(input => input);
179+
});
180+
jest.mock('../../../lib/getOriginUrl', () => {
181+
return jest.fn(() => 'http://example.com/request-headers-host');
182+
});
183+
const input = await readFileAsync(
184+
path.resolve(__dirname, './image-jpg.jpg')
185+
);
186+
nock('http://example.com')
187+
.get('/request-headers-host')
188+
.reply(function(uri, requestBody, cb) {
189+
expect(this.req.headers['host']).toBe('example.com');
190+
191+
cb(null, [200, input, { 'content-type': 'image/jpg' }]);
192+
});
193+
194+
const middleware = require('../../../lib/webp-url/middleware');
195+
await middleware(
196+
{
197+
headers: {
198+
host: [
199+
{
200+
value: 'localhost',
201+
key: 'Host',
165202
},
166203
],
167204
},
@@ -175,7 +212,7 @@ describe('webp middleware', () => {
175212
return jest.fn(() => true);
176213
});
177214
jest.mock('../../../lib/webp-url/compress', () => {
178-
return jest.fn(input => Buffer.from(input));
215+
return jest.fn(input => global.Buffer.from(input));
179216
});
180217
jest.mock('../../../lib/getOriginUrl', () => {
181218
return jest.fn(() => 'http://example.com/null-parameter');

__tests__/origin-request.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('origin-request handler', () => {
6060
'x-orig-size': [{ key: 'X-Orig-Size', value: '0' }],
6161
},
6262
status: 200,
63-
statusDescription: null,
63+
statusDescription: 'OK',
6464
});
6565
});
6666

@@ -139,7 +139,7 @@ describe('origin-request handler', () => {
139139
'x-orig-size': [{ key: 'X-Orig-Size', value: '0' }],
140140
},
141141
status: 200,
142-
statusDescription: null,
142+
statusDescription: 'OK',
143143
});
144144
});
145145

@@ -195,7 +195,7 @@ describe('origin-request handler', () => {
195195
'x-orig-size': [{ key: 'X-Orig-Size', value: '0' }],
196196
},
197197
status: 200,
198-
statusDescription: null,
198+
statusDescription: 'OK',
199199
});
200200
});
201201

lib/brotli/compress.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const compress = require('iltorb').compress;
1010
*/
1111
module.exports = input => {
1212
if (typeof input === 'string') {
13-
input = Buffer.from(input);
13+
input = global.Buffer.from(input);
1414
}
1515

1616
return compress(input, { quality: 6 });

lib/imagemin/middleware.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = async (request, response = null) => {
3232
logger.log(`Fetching image for imagemin from ${originUrl}`);
3333

3434
const originResponse = await got(originUrl, {
35-
encoding: null,
35+
responseType: 'buffer',
3636
headers: requestToHeaders(request),
3737
});
3838
const base64Body = (await imageminCompress(originResponse.body)).toString(
@@ -41,9 +41,7 @@ module.exports = async (request, response = null) => {
4141

4242
if (base64Body.length > 1003520) {
4343
logger.log(
44-
`Compressed response body size(${
45-
base64Body.length
46-
}B) exceeds limit, skipping.`
44+
`Compressed response body size(${base64Body.length}B) exceeds limit, skipping.`
4745
);
4846
return response;
4947
}

lib/responseHelper.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const isError = response => {
88
return false;
99
};
1010

11-
const allowedHeaders = [
11+
const allowedResponseHeaders = [
1212
'date',
1313
'etag',
1414
'cache-control',
@@ -19,10 +19,23 @@ const allowedHeaders = [
1919
'access-control-expose-headers',
2020
];
2121

22+
const allowedRequestHeaders = [
23+
'accept',
24+
'accept-language',
25+
'accept-encoding',
26+
'pragma',
27+
'user-agent',
28+
'cache-control',
29+
'access-control-allow-headers',
30+
'access-control-allow-methods',
31+
'access-control-allow-origin',
32+
'access-control-expose-headers',
33+
];
34+
2235
const headersToLambda = headers => {
2336
const convertedHeaders = {};
2437
for (let name in headers) {
25-
if (!allowedHeaders.includes(name)) {
38+
if (!allowedResponseHeaders.includes(name)) {
2639
continue;
2740
}
2841

@@ -40,12 +53,19 @@ const headersToLambda = headers => {
4053
return convertedHeaders;
4154
};
4255

43-
const requestToHeaders = request =>
44-
Object.keys(request.headers || {}).reduce((headers, header) => {
45-
headers[header] = request.headers[header][0].value;
56+
const requestToHeaders = request => {
57+
const convertedHeaders = {};
58+
const headers = request.headers;
59+
for (let name in headers) {
60+
if (!allowedRequestHeaders.includes(name)) {
61+
continue;
62+
}
4663

47-
return headers;
48-
}, {});
64+
convertedHeaders[name] = headers[name][0].value;
65+
}
66+
67+
return convertedHeaders;
68+
};
4969

5070
module.exports = {
5171
isError,

0 commit comments

Comments
 (0)