From c03d5eebe0006eb92830f66296967946850eff02 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 16 Jan 2024 01:17:29 +0100 Subject: [PATCH 1/2] module: fix crash when built-in module export a `default` key --- lib/internal/bootstrap/realm.js | 3 ++- test/parallel/test-process-default.js | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-process-default.js diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js index 57ab47178d033d..456bde8379f89b 100644 --- a/lib/internal/bootstrap/realm.js +++ b/lib/internal/bootstrap/realm.js @@ -350,7 +350,8 @@ class BuiltinModule { const url = `node:${this.id}`; const builtin = this; const exportsKeys = ArrayPrototypeSlice(this.exportKeys); - ArrayPrototypePush(exportsKeys, 'default'); + if (!ArrayPrototypeIncludes(exportsKeys, 'default')) + ArrayPrototypePush(exportsKeys, 'default'); this.module = new ModuleWrap( url, undefined, exportsKeys, function() { diff --git a/test/parallel/test-process-default.js b/test/parallel/test-process-default.js new file mode 100644 index 00000000000000..a6ceda2af3ee25 --- /dev/null +++ b/test/parallel/test-process-default.js @@ -0,0 +1,8 @@ +'use strict'; +const common = require('../common'); +const assert = require('node:assert'); + +process.default = 1; +import('node:process').then(common.mustCall((processModule) => { + assert.strictEqual(processModule.default.default, 1); +})); From da6e2ca80b02234f766ba3263d04b1c66800b2b1 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 16 Jan 2024 10:35:42 +0100 Subject: [PATCH 2/2] Update lib/internal/bootstrap/realm.js Co-authored-by: Geoffrey Booth --- lib/internal/bootstrap/realm.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/internal/bootstrap/realm.js b/lib/internal/bootstrap/realm.js index 456bde8379f89b..f030f537a084d7 100644 --- a/lib/internal/bootstrap/realm.js +++ b/lib/internal/bootstrap/realm.js @@ -350,8 +350,9 @@ class BuiltinModule { const url = `node:${this.id}`; const builtin = this; const exportsKeys = ArrayPrototypeSlice(this.exportKeys); - if (!ArrayPrototypeIncludes(exportsKeys, 'default')) + if (!ArrayPrototypeIncludes(exportsKeys, 'default')) { ArrayPrototypePush(exportsKeys, 'default'); + } this.module = new ModuleWrap( url, undefined, exportsKeys, function() {