-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby): be less aggressive when marking builtin methods as unsafe (
- Loading branch information
Showing
3 changed files
with
24 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
const { wrapModuleWithTracking } = require(`./tracking-unsafe-module-wrapper`) | ||
|
||
module.exports = wrapModuleWithTracking(`http`) | ||
module.exports = wrapModuleWithTracking(`http`, { ignore: [`http.Agent`] }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
const { wrapModuleWithTracking } = require(`./tracking-unsafe-module-wrapper`) | ||
|
||
module.exports = wrapModuleWithTracking(`https`) | ||
module.exports = wrapModuleWithTracking(`https`, { ignore: [`https.Agent`] }) |
34 changes: 22 additions & 12 deletions
34
packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,52 @@ | ||
// initializing global here for unsafe builtin usage at import time | ||
global.unsafeBuiltinUsage = [] | ||
|
||
function createProxyHandler(prefix) { | ||
function createProxyHandler(prefix, options) { | ||
return { | ||
get: function (target, key) { | ||
const value = target[key] | ||
const path = key && key.toString ? `${prefix}.${key.toString()}` : prefix | ||
|
||
if (options.ignore.includes(path)) { | ||
return value | ||
} | ||
|
||
if (typeof value === `function`) { | ||
return function wrapper(...args) { | ||
const myErrorHolder = { | ||
name: `Unsafe builtin usage ${prefix}.${key}`, | ||
name: `Unsafe builtin usage ${path}`, | ||
} | ||
Error.captureStackTrace(myErrorHolder, wrapper) | ||
|
||
// loadPageDataSync already is tracked with dedicated warning messages, | ||
// - loadPageDataSync already is tracked with dedicated warning messages, | ||
// so skipping marking it to avoid multiple messages for same usage | ||
if (!myErrorHolder.stack.includes(`loadPageDataSync`)) { | ||
// - node-gyp-build will use fs.readDirSync in attempt to load binaries | ||
// this should be ok to ignore. | ||
if ( | ||
!myErrorHolder.stack.includes(`loadPageDataSync`) && | ||
!myErrorHolder.stack.includes(`node-gyp-build`) | ||
) { | ||
global.unsafeBuiltinUsage.push(myErrorHolder.stack) | ||
} | ||
|
||
return value.apply(target, args) | ||
} | ||
} else if (typeof value === `object` && value !== null) { | ||
return new Proxy( | ||
value, | ||
createProxyHandler( | ||
key && key.toString ? `${prefix}.${key.toString()}` : prefix | ||
) | ||
) | ||
return new Proxy(value, createProxyHandler(path, options)) | ||
} | ||
|
||
return value | ||
}, | ||
} | ||
} | ||
|
||
function wrapModuleWithTracking(moduleName) { | ||
function wrapModuleWithTracking(moduleName, options = {}) { | ||
if (!options.ignore) { | ||
options.ignore = [] | ||
} | ||
|
||
const mod = require(moduleName) | ||
return new Proxy(mod, createProxyHandler(moduleName)) | ||
return new Proxy(mod, createProxyHandler(moduleName, options)) | ||
} | ||
|
||
exports.wrapModuleWithTracking = wrapModuleWithTracking |