From 55011d2c71e9113aaead1eff2220da3c853fd7bf Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 5 Feb 2024 14:25:21 +0100 Subject: [PATCH] lib: only build the ESM facade for builtins when they are needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We previously build the ESM facade (synthetic modules re-exporting builtin's exports) for builtins even when they are not directly import'ed (which rarely happens for internal builtins as that requires --expose-internals). This patch removes the eager generation to avoid the overhead and the extra promises created in facade building when it's not reqested by the user. When the facade is needed the ESM loader that can be requested it in the translator on-demand. Drive-by: set the ModuleWrap prototype to null in the built-in snapshot. PR-URL: https://github.com/nodejs/node/pull/51669 Reviewed-By: Michaƫl Zasso Reviewed-By: Geoffrey Booth Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel --- lib/internal/bootstrap/realm.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js index 0ebec1c02d0..9cb6a8add71 100644 --- a/lib/internal/bootstrap/realm.js +++ b/lib/internal/bootstrap/realm.js @@ -198,6 +198,9 @@ const { setInternalLoaders, } = internalBinding('builtins'); +const { ModuleWrap } = internalBinding('module_wrap'); +ObjectSetPrototypeOf(ModuleWrap.prototype, null); + const getOwn = (target, property, receiver) => { return ObjectPrototypeHasOwnProperty(target, property) ? ReflectGet(target, property, receiver) : @@ -338,16 +341,11 @@ class BuiltinModule { const internal = StringPrototypeStartsWith(this.id, 'internal/'); this.exportKeys = internal ? [] : ObjectKeys(this.exports); } - this.getESMFacade(); - this.syncExports(); return this.exports; } getESMFacade() { if (this.module) return this.module; - const { ModuleWrap } = internalBinding('module_wrap'); - // TODO(aduh95): move this to C++, alongside the initialization of the class. - ObjectSetPrototypeOf(ModuleWrap.prototype, null); const url = `node:${this.id}`; const builtin = this; const exportsKeys = ArrayPrototypeSlice(this.exportKeys);