From da65d8a5d1d546ed0cd838dad77b5804d3598c94 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Wed, 7 Apr 2021 12:00:00 +0200 Subject: [PATCH 1/2] =?UTF-8?q?lib:=20cache=C2=A0`process.binding(?= =?UTF-8?q?=E2=80=A6)`=C2=A0wrappers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: https://github.com/nodejs/node/pull/37819 --- lib/internal/legacy/processbinding.js | 19 +++++++++++++++++-- test/parallel/test-process-binding-util.js | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/internal/legacy/processbinding.js b/lib/internal/legacy/processbinding.js index 3d48e3c9882e58..fd357643801ff0 100644 --- a/lib/internal/legacy/processbinding.js +++ b/lib/internal/legacy/processbinding.js @@ -2,13 +2,14 @@ const { ArrayPrototypeFilter, ArrayPrototypeIncludes, + ArrayPrototypeMap, ObjectFromEntries, ObjectEntries, SafeArrayIterator, } = primordials; -const { types } = require('util'); +const types = require('internal/util/types'); -module.exports = { +const factories = { util() { return ObjectFromEntries(new SafeArrayIterator(ArrayPrototypeFilter( ObjectEntries(types), @@ -34,3 +35,17 @@ module.exports = { }))); } }; + +module.exports = ObjectFromEntries( + new SafeArrayIterator( + ArrayPrototypeMap(ObjectEntries(factories), ({ 0: key, 1: factory }) => { + let result; + return [key, () => { + if (!result) { + result = factory(); + } + return result; + }]; + }), + ), +); diff --git a/test/parallel/test-process-binding-util.js b/test/parallel/test-process-binding-util.js index d1e602de681cfa..b59f20ddffe8c5 100644 --- a/test/parallel/test-process-binding-util.js +++ b/test/parallel/test-process-binding-util.js @@ -28,3 +28,5 @@ assert.deepStrictEqual( for (const k of Object.keys(utilBinding)) { assert.strictEqual(utilBinding[k], util.types[k]); } + +assert.strictEqual(utilBinding, process.binding('util')); From b57a51762268c0089a0c9246a56ccc70eb207e34 Mon Sep 17 00:00:00 2001 From: ExE Boss <3889017+ExE-Boss@users.noreply.github.com> Date: Wed, 7 Apr 2021 18:00:00 +0200 Subject: [PATCH 2/2] =?UTF-8?q?lib:=20cache=C2=A0`process.binding(?= =?UTF-8?q?=E2=80=A6)`=C2=A0wrappers=20in=C2=A0`loaders.js`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/internal/bootstrap/loaders.js | 7 ++++++- lib/internal/legacy/processbinding.js | 17 +---------------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/lib/internal/bootstrap/loaders.js b/lib/internal/bootstrap/loaders.js index bdc328ae5eacad..b585b4af2e3db6 100644 --- a/lib/internal/bootstrap/loaders.js +++ b/lib/internal/bootstrap/loaders.js @@ -136,13 +136,18 @@ const legacyWrapperList = new SafeSet([ 'DEP0111'); } if (legacyWrapperList.has(module)) { - return nativeModuleRequire('internal/legacy/processbinding')[module](); + return bindingObj[module] ??= + nativeModuleRequire('internal/legacy/processbinding')[module](); } return internalBinding(module); } // eslint-disable-next-line no-restricted-syntax throw new Error(`No such module: ${module}`); }; +} + +{ + const bindingObj = ObjectCreate(null); process._linkedBinding = function _linkedBinding(module) { module = String(module); diff --git a/lib/internal/legacy/processbinding.js b/lib/internal/legacy/processbinding.js index fd357643801ff0..9a0efb74c548d5 100644 --- a/lib/internal/legacy/processbinding.js +++ b/lib/internal/legacy/processbinding.js @@ -2,14 +2,13 @@ const { ArrayPrototypeFilter, ArrayPrototypeIncludes, - ArrayPrototypeMap, ObjectFromEntries, ObjectEntries, SafeArrayIterator, } = primordials; const types = require('internal/util/types'); -const factories = { +module.exports = { util() { return ObjectFromEntries(new SafeArrayIterator(ArrayPrototypeFilter( ObjectEntries(types), @@ -35,17 +34,3 @@ const factories = { }))); } }; - -module.exports = ObjectFromEntries( - new SafeArrayIterator( - ArrayPrototypeMap(ObjectEntries(factories), ({ 0: key, 1: factory }) => { - let result; - return [key, () => { - if (!result) { - result = factory(); - } - return result; - }]; - }), - ), -);