Skip to content

Commit

Permalink
lib: expose primordials object
Browse files Browse the repository at this point in the history
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)
```
  • Loading branch information
aduh95 committed Jan 15, 2021
1 parent 2ba8728 commit b74793f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/internal/bootstrap/loaders.js
Expand Up @@ -52,6 +52,7 @@ const {
ObjectKeys,
ObjectPrototypeHasOwnProperty,
ReflectGet,
ReflectGetOwnPropertyDescriptor,
SafeMap,
SafeSet,
String,
Expand Down Expand Up @@ -216,12 +217,19 @@ class NativeModule {
}

// Used by user-land module loaders to compile and load builtins.
compileForPublicLoader() {
compileForPublicLoader(isPreloading) {
if (!this.canBeRequiredByUsers) {
// No code because this is an assertion against bugs
// eslint-disable-next-line no-restricted-syntax
throw new Error(`Should not compile ${this.id} for public use`);
}
if (isPreloading) {
ObjectDefineProperty(
this, 'isPreloading',
ReflectGetOwnPropertyDescriptor(
nativeModuleRequire('internal/modules/cjs/loader').Module.prototype,
'isPreloading'));
}
this.compileForInternalLoader();
if (!this.exportKeys) {
// When using --expose-internals, we do not want to reflect the named
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/cjs/helpers.js
Expand Up @@ -32,11 +32,11 @@ let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
// TODO: Use this set when resolving pkg#exports conditions in loader.js.
const cjsConditions = new SafeSet(['require', 'node', ...userConditions]);

function loadNativeModule(filename, request) {
function loadNativeModule(filename, request, isPreloading) {
const mod = NativeModule.map.get(filename);
if (mod) {
debug('load native module %s', request);
mod.compileForPublicLoader();
mod.compileForPublicLoader(isPreloading);
return mod;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/loader.js
Expand Up @@ -781,7 +781,7 @@ Module._load = function(request, parent, isMain) {
}
}

const mod = loadNativeModule(filename, request);
const mod = loadNativeModule(filename, request, isPreloading);
if (mod && mod.canBeRequiredByUsers) return mod.exports;

// Don't call updateChildren(), Module constructor already does.
Expand Down
7 changes: 6 additions & 1 deletion lib/internal/test/binding.js
Expand Up @@ -4,4 +4,9 @@ process.emitWarning(
'These APIs are for internal testing only. Do not use them.',
'internal/test/binding');

module.exports = { internalBinding };
if (module.isPreloading) {
globalThis.internalBinding = internalBinding;
globalThis.primordials = primordials;
}

module.exports = { internalBinding, primordials };

0 comments on commit b74793f

Please sign in to comment.