Skip to content

Commit 32d7adf

Browse files
wardpeetpieh
andauthored
fix(gatsby): be less aggressive when marking builtin methods as unsafe (#30216) (#30287)
Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent a2a16e4 commit 32d7adf

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
const { wrapModuleWithTracking } = require(`./tracking-unsafe-module-wrapper`)
22

3-
module.exports = wrapModuleWithTracking(`http`)
3+
module.exports = wrapModuleWithTracking(`http`, { ignore: [`http.Agent`] })
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
const { wrapModuleWithTracking } = require(`./tracking-unsafe-module-wrapper`)
22

3-
module.exports = wrapModuleWithTracking(`https`)
3+
module.exports = wrapModuleWithTracking(`https`, { ignore: [`https.Agent`] })
Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
11
// initializing global here for unsafe builtin usage at import time
22
global.unsafeBuiltinUsage = []
33

4-
function createProxyHandler(prefix) {
4+
function createProxyHandler(prefix, options) {
55
return {
66
get: function (target, key) {
77
const value = target[key]
8+
const path = key && key.toString ? `${prefix}.${key.toString()}` : prefix
9+
10+
if (options.ignore.includes(path)) {
11+
return value
12+
}
13+
814
if (typeof value === `function`) {
915
return function wrapper(...args) {
1016
const myErrorHolder = {
11-
name: `Unsafe builtin usage ${prefix}.${key}`,
17+
name: `Unsafe builtin usage ${path}`,
1218
}
1319
Error.captureStackTrace(myErrorHolder, wrapper)
1420

15-
// loadPageDataSync already is tracked with dedicated warning messages,
21+
// - loadPageDataSync already is tracked with dedicated warning messages,
1622
// so skipping marking it to avoid multiple messages for same usage
17-
if (!myErrorHolder.stack.includes(`loadPageDataSync`)) {
23+
// - node-gyp-build will use fs.readDirSync in attempt to load binaries
24+
// this should be ok to ignore.
25+
if (
26+
!myErrorHolder.stack.includes(`loadPageDataSync`) &&
27+
!myErrorHolder.stack.includes(`node-gyp-build`)
28+
) {
1829
global.unsafeBuiltinUsage.push(myErrorHolder.stack)
1930
}
2031

2132
return value.apply(target, args)
2233
}
2334
} else if (typeof value === `object` && value !== null) {
24-
return new Proxy(
25-
value,
26-
createProxyHandler(
27-
key && key.toString ? `${prefix}.${key.toString()}` : prefix
28-
)
29-
)
35+
return new Proxy(value, createProxyHandler(path, options))
3036
}
3137

3238
return value
3339
},
3440
}
3541
}
3642

37-
function wrapModuleWithTracking(moduleName) {
43+
function wrapModuleWithTracking(moduleName, options = {}) {
44+
if (!options.ignore) {
45+
options.ignore = []
46+
}
47+
3848
const mod = require(moduleName)
39-
return new Proxy(mod, createProxyHandler(moduleName))
49+
return new Proxy(mod, createProxyHandler(moduleName, options))
4050
}
4151

4252
exports.wrapModuleWithTracking = wrapModuleWithTracking

0 commit comments

Comments
 (0)