Skip to content

Commit 10d499b

Browse files
committed
Avoid splitting WHITELIST on each request and refactor code
1 parent abcf9ee commit 10d499b

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

index.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,30 @@ const Sharp = require('sharp');
77
const PathPattern = /(.*\/)?(.*)\/(.*)/;
88

99
// parameters
10-
const {BUCKET, URL, WHITELIST} = process.env;
10+
const {BUCKET, URL} = process.env;
11+
const WHITELIST = process.env.WHITELIST
12+
? Object.freeze(process.env.WHITELIST.split(' '))
13+
: null;
1114

1215

1316
exports.handler = async (event) => {
1417
const path = event.queryStringParameters.path;
1518
const parts = PathPattern.exec(path);
1619
const dir = parts[1] || '';
17-
const sizeOption = parts[2];
18-
const options = parts[2].split('_');
20+
const resizeOption = parts[2]; // e.g. "150x150_max"
21+
const sizeAndAction = resizeOption.split('_');
1922
const filename = parts[3];
2023

21-
22-
const sizes = options[0].split("x");
23-
const action = options.length > 1 ? options[1] : null;
24+
const sizes = sizeAndAction[0].split("x");
25+
const action = sizeAndAction.length > 1 ? sizeAndAction[1] : null;
2426

2527
// Whitelist validation.
26-
if (WHITELIST) {
27-
const whitelistArr = WHITELIST.split(' ');
28-
if (!whitelistArr.includes(sizeOption)) {
29-
return {
30-
statusCode: 400,
31-
body: `Unknown size parameter "${sizeOption}"`,
32-
headers: {"Content-Type": "text/plain"}
33-
};
34-
}
28+
if (WHITELIST && !WHITELIST.includes(resizeOption)) {
29+
return {
30+
statusCode: 400,
31+
body: `WHITELIST is set but does not contain the size parameter "${resizeOption}"`,
32+
headers: {"Content-Type": "text/plain"}
33+
};
3534
}
3635

3736
// Action validation.

0 commit comments

Comments
 (0)