From 32d7adf16e6b8142ba2a30d5216b9bf34234f412 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Wed, 17 Mar 2021 13:58:56 +0100 Subject: [PATCH] fix(gatsby): be less aggressive when marking builtin methods as unsafe (#30216) (#30287) Co-authored-by: Michal Piechowiak --- .../cache-dir/ssr-builtin-trackers/http.js | 2 +- .../cache-dir/ssr-builtin-trackers/https.js | 2 +- .../tracking-unsafe-module-wrapper.js | 34 ++++++++++++------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/gatsby/cache-dir/ssr-builtin-trackers/http.js b/packages/gatsby/cache-dir/ssr-builtin-trackers/http.js index 7393f9e40d32e..0560030cb200f 100644 --- a/packages/gatsby/cache-dir/ssr-builtin-trackers/http.js +++ b/packages/gatsby/cache-dir/ssr-builtin-trackers/http.js @@ -1,3 +1,3 @@ const { wrapModuleWithTracking } = require(`./tracking-unsafe-module-wrapper`) -module.exports = wrapModuleWithTracking(`http`) +module.exports = wrapModuleWithTracking(`http`, { ignore: [`http.Agent`] }) diff --git a/packages/gatsby/cache-dir/ssr-builtin-trackers/https.js b/packages/gatsby/cache-dir/ssr-builtin-trackers/https.js index fa4b85606e09a..76fd598098912 100644 --- a/packages/gatsby/cache-dir/ssr-builtin-trackers/https.js +++ b/packages/gatsby/cache-dir/ssr-builtin-trackers/https.js @@ -1,3 +1,3 @@ const { wrapModuleWithTracking } = require(`./tracking-unsafe-module-wrapper`) -module.exports = wrapModuleWithTracking(`https`) +module.exports = wrapModuleWithTracking(`https`, { ignore: [`https.Agent`] }) diff --git a/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js b/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js index 419cd613f8bdb..c6393c5f27fc9 100644 --- a/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js +++ b/packages/gatsby/cache-dir/ssr-builtin-trackers/tracking-unsafe-module-wrapper.js @@ -1,32 +1,38 @@ // 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 @@ -34,9 +40,13 @@ function createProxyHandler(prefix) { } } -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