Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport v14.x] module: add isPreloading indicator #36988

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,14 @@ added: v0.1.16
The identifier for the module. Typically this is the fully resolved
filename.

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

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

### `module.loaded`
<!-- YAML
added: v0.1.16
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
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