Skip to content

Commit 122d811

Browse files
fix: remove lodash dependency to save 5 MB, Fixes #200 (#201)
* Remove lodash usage #200 * No longer a dep * no longer required * chore: fix lint issue in build to get pr passing Co-authored-by: Scott Willeke <[email protected]>
1 parent a562cfb commit 122d811

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
"serverless-functions"
2727
],
2828
"dependencies": {
29-
"lodash": "^4.17.11",
3029
"mime-types": "^2.1.21",
3130
"mustache": "^4.0.0"
3231
},

src/StaticFileHandler.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,46 @@ const mimetypes = require("mime-types")
55
const Mustache = require("mustache")
66
const path = require("path")
77
const util = require("util")
8-
const _ = require("lodash")
98
const readFileAsync = util.promisify(fs.readFile)
109
const accessAsync = util.promisify(fs.access)
1110

11+
// originally from lodash, but never called with a defaultValue
12+
// https://gist.github.com/jeneg/9767afdcca45601ea44930ea03e0febf
13+
function __get(value, path, defaultValue) {
14+
return String(path)
15+
.split(".")
16+
.reduce((acc, v) => {
17+
if (v.startsWith("[")) {
18+
const [, arrPart] = v.split("[")
19+
v = arrPart.split("]")[0]
20+
}
21+
22+
if (v.endsWith("]") && !v.startsWith("[")) {
23+
const [objPart, arrPart, ...rest] = v.split("[")
24+
const [firstIndex] = arrPart.split("]")
25+
const otherParts = rest
26+
.join("")
27+
.replaceAll("[", "")
28+
.replaceAll("]", ".")
29+
.split(".")
30+
.filter((str) => str !== "")
31+
32+
return [...acc, objPart, firstIndex, ...otherParts]
33+
}
34+
35+
return [...acc, v]
36+
}, [])
37+
.reduce((acc, v) => {
38+
try {
39+
acc = acc[v] !== undefined ? acc[v] : defaultValue
40+
} catch (e) {
41+
return defaultValue
42+
}
43+
44+
return acc
45+
}, value)
46+
}
47+
1248
class StaticFileHandler {
1349
/**
1450
* Initializes a new instance of @see StaticFileHandler
@@ -253,14 +289,14 @@ class StaticFileHandler {
253289
function isV2ProxyAPI(evt) {
254290
return (
255291
evt.version === "2.0" &&
256-
typeof _.get(evt, "requestContext.http.method") === "string"
292+
typeof __get(evt, "requestContext.http.method") === "string"
257293
)
258294
}
259295
function isV1ProxyAPI(evt) {
260296
return (
261297
// docs say there is a .version but there isn't!
262298
// evt.version === "1.0" &&
263-
typeof _.get(evt, "requestContext.httpMethod") === "string"
299+
typeof __get(evt, "requestContext.httpMethod") === "string"
264300
)
265301
}
266302
// serverless-offline doesn't provide the `isBase64Encoded` prop, but does add the isOffline. Fixes issue #10: https://github.com/activescott/serverless-aws-static-file-handler/issues/10
@@ -272,7 +308,7 @@ class StaticFileHandler {
272308
"requestContext.http.method",
273309
]
274310
const addendum = logProps
275-
.map((propName) => `event.${propName} was '${_.get(event, propName)}'`)
311+
.map((propName) => `event.${propName} was '${__get(event, propName)}'`)
276312
.join(" ")
277313
throw new Error(
278314
"API Gateway method does not appear to be setup for Lambda Proxy Integration. Please confirm that `integration` property of the http event is not specified or set to `integration: proxy`." +

0 commit comments

Comments
 (0)