Skip to content

Commit

Permalink
fix(gatsby): be less aggressive when marking builtin methods as unsafe (
Browse files Browse the repository at this point in the history
#30216) (#30287)

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
wardpeet and pieh authored Mar 17, 2021
1 parent a2a16e4 commit 32d7adf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/ssr-builtin-trackers/http.js
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`] })
2 changes: 1 addition & 1 deletion packages/gatsby/cache-dir/ssr-builtin-trackers/https.js
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`] })
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

0 comments on commit 32d7adf

Please sign in to comment.