Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
module: add isPreloading indicator
Adds a `module.isPreloading` property that is `true` only during the
preload (`-r`) phase of Node.js bootstrap. This provides modules an
easy, non-hacky way of knowing if they are being loaded during preload.

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: #36263
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
jasnell authored and aduh95 committed Jan 18, 2021
commit c38b2ad4ab108f464dfc6cda63f08f8d14c60602
8 changes: 8 additions & 0 deletions doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ const requireUtil = createRequireFromPath('../src/utils/');
requireUtil('./some-tool');
```

### `module.isPreloading`
<!-- YAML
added: REPLACEME
-->

* Type: {boolean} `true` if the module is running during the Node.js preload
phase.

### `module.syncBuiltinESMExports()`
<!-- YAML
added: v12.12.0
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const relativeResolveCache = ObjectCreate(null);

let requireDepth = 0;
let statCache = null;
let isPreloading = false;

function stat(filename) {
filename = path.toNamespacedPath(filename);
Expand Down Expand Up @@ -219,6 +220,10 @@ ObjectDefineProperty(Module, 'wrapper', {
}
});

ObjectDefineProperty(Module.prototype, 'isPreloading', {
get() { return isPreloading; }
});
Comment on lines +223 to +225
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be a good idea to also backport NativeModule.prototype.isPreloading:

const isPreloadingDesc = { get() { return isPreloading; } };
ObjectDefineProperty(Module.prototype, 'isPreloading', isPreloadingDesc);
ObjectDefineProperty(NativeModule.prototype, 'isPreloading', isPreloadingDesc);


let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
debug = fn;
});
Expand Down Expand Up @@ -1202,6 +1207,8 @@ Module._preloadModules = function(requests) {
if (!ArrayIsArray(requests))
return;

isPreloading = true;

// Preloaded modules have a dummy parent module which is deemed to exist
// in the current working directory. This seeds the search path for
// preloaded modules.
Expand All @@ -1210,11 +1217,13 @@ Module._preloadModules = function(requests) {
parent.paths = Module._nodeModulePaths(process.cwd());
} catch (e) {
if (e.code !== 'ENOENT') {
isPreloading = false;
throw e;
}
}
for (let n = 0; n < requests.length; n++)
parent.require(requests[n]);
isPreloading = false;
};

Module.syncBuiltinESMExports = function syncBuiltinESMExports() {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/ispreloading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const assert = require('assert');
assert(module.isPreloading);
13 changes: 13 additions & 0 deletions test/parallel/test-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ const fixtureE = fixtures.path('intrinsic-mutation.js');
const fixtureF = fixtures.path('print-intrinsic-mutation-name.js');
const fixtureG = fixtures.path('worker-from-argv.js');
const fixtureThrows = fixtures.path('throws_error4.js');
const fixtureIsPreloading = fixtures.path('ispreloading.js');

// Assert that module.isPreloading is false here
assert(!module.isPreloading);

// Test that module.isPreloading is set in preloaded module
// Test preloading a single module works
childProcess.exec(
`"${nodeBinary}" ${preloadOption([fixtureIsPreloading])} "${fixtureB}"`,
function(err, stdout, stderr) {
assert.ifError(err);
assert.strictEqual(stdout, 'B\n');
});

// Test preloading a single module works
childProcess.exec(`"${nodeBinary}" ${preloadOption([fixtureA])} "${fixtureB}"`,
Expand Down