Skip to content

Commit b74793f

Browse files
committed
lib: expose primordials object
Expose the internal `primordials` object to help with Node.js core development. ```console $ node --expose-internals -r internal/test/binding lib/fs.js (node:5299) internal/test/binding: These APIs are for internal testing only. Do not use them. (Use `node --trace-warnings ...` to show where the warning was created) ```
1 parent 2ba8728 commit b74793f

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

lib/internal/bootstrap/loaders.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const {
5252
ObjectKeys,
5353
ObjectPrototypeHasOwnProperty,
5454
ReflectGet,
55+
ReflectGetOwnPropertyDescriptor,
5556
SafeMap,
5657
SafeSet,
5758
String,
@@ -216,12 +217,19 @@ class NativeModule {
216217
}
217218

218219
// Used by user-land module loaders to compile and load builtins.
219-
compileForPublicLoader() {
220+
compileForPublicLoader(isPreloading) {
220221
if (!this.canBeRequiredByUsers) {
221222
// No code because this is an assertion against bugs
222223
// eslint-disable-next-line no-restricted-syntax
223224
throw new Error(`Should not compile ${this.id} for public use`);
224225
}
226+
if (isPreloading) {
227+
ObjectDefineProperty(
228+
this, 'isPreloading',
229+
ReflectGetOwnPropertyDescriptor(
230+
nativeModuleRequire('internal/modules/cjs/loader').Module.prototype,
231+
'isPreloading'));
232+
}
225233
this.compileForInternalLoader();
226234
if (!this.exportKeys) {
227235
// When using --expose-internals, we do not want to reflect the named

lib/internal/modules/cjs/helpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
3232
// TODO: Use this set when resolving pkg#exports conditions in loader.js.
3333
const cjsConditions = new SafeSet(['require', 'node', ...userConditions]);
3434

35-
function loadNativeModule(filename, request) {
35+
function loadNativeModule(filename, request, isPreloading) {
3636
const mod = NativeModule.map.get(filename);
3737
if (mod) {
3838
debug('load native module %s', request);
39-
mod.compileForPublicLoader();
39+
mod.compileForPublicLoader(isPreloading);
4040
return mod;
4141
}
4242
}

lib/internal/modules/cjs/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ Module._load = function(request, parent, isMain) {
781781
}
782782
}
783783

784-
const mod = loadNativeModule(filename, request);
784+
const mod = loadNativeModule(filename, request, isPreloading);
785785
if (mod && mod.canBeRequiredByUsers) return mod.exports;
786786

787787
// Don't call updateChildren(), Module constructor already does.

lib/internal/test/binding.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@ process.emitWarning(
44
'These APIs are for internal testing only. Do not use them.',
55
'internal/test/binding');
66

7-
module.exports = { internalBinding };
7+
if (module.isPreloading) {
8+
globalThis.internalBinding = internalBinding;
9+
globalThis.primordials = primordials;
10+
}
11+
12+
module.exports = { internalBinding, primordials };

0 commit comments

Comments
 (0)