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'));