Skip to content

Commit 96dd1b9

Browse files
committed
refine promise timings
1 parent ae46f45 commit 96dd1b9

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

lib/internal/async_hooks.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,18 @@ function bufferBootstrapHooks() {
289289
}
290290
}
291291

292+
function clearBootstrapHooksBuffer() {
293+
if (!bootstrapBuffer)
294+
return;
295+
if (bootstrapHooks) {
296+
stopBootstrapHooksBuffer();
297+
process._tickCallback();
298+
}
299+
const _bootstrapBuffer = bootstrapBuffer;
300+
bootstrapBuffer = null;
301+
return _bootstrapBuffer;
302+
}
303+
292304
function stopBootstrapHooksBuffer() {
293305
if (!bootstrapHooks)
294306
return;
@@ -302,18 +314,11 @@ function stopBootstrapHooksBuffer() {
302314
bootstrapHooks = null;
303315
if (async_hook_fields[kTotals] === 0) {
304316
disableHooks();
305-
// Flush microtasks to ensure disable has run.
306-
process._tickCallback();
307317
}
308318
}
309319

310-
function clearBootstrapHooksBuffer() {
311-
if (bootstrapHooks)
312-
stopBootstrapHooksBuffer();
313-
bootstrapBuffer = null;
314-
}
315-
316320
function emitBootstrapHooksBuffer() {
321+
const bootstrapBuffer = clearBootstrapHooksBuffer();
317322
if (!bootstrapBuffer || async_hook_fields[kTotals] === 0) {
318323
return;
319324
}
@@ -333,7 +338,6 @@ function emitBootstrapHooksBuffer() {
333338
break;
334339
}
335340
}
336-
bootstrapBuffer = null;
337341
}
338342

339343
let wantPromiseHook = false;

lib/internal/modules/cjs/loader.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,12 @@ Module.prototype.load = function(filename) {
821821
if (module !== undefined && module.module !== undefined) {
822822
if (module.module.getStatus() >= kInstantiated)
823823
module.module.setExport('default', exports);
824-
} else { // preemptively cache
824+
} else {
825+
// Preemptively cache
826+
// We use a function to defer promise creation for async hooks.
825827
ESMLoader.moduleMap.set(
826828
url,
827-
new ModuleJob(ESMLoader, url, () =>
829+
() => new ModuleJob(ESMLoader, url, () =>
828830
new ModuleWrap(function() {
829831
this.setExport('default', exports);
830832
}, ['default'], url)

lib/internal/modules/esm/loader.js

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ class Loader {
146146
async getModuleJob(specifier, parentURL) {
147147
const { url, format } = await this.resolve(specifier, parentURL);
148148
let job = this.moduleMap.get(url);
149+
// CJS injects jobs as functions to defer promise creation for async hooks.
150+
if (typeof job === 'function')
151+
this.moduleMap.set(url, job = job());
149152
if (job !== undefined)
150153
return job;
151154

lib/internal/modules/esm/module_map.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ModuleMap extends SafeMap {
1616
}
1717
set(url, job) {
1818
validateString(url, 'url');
19-
if (job instanceof ModuleJob !== true) {
19+
if (job instanceof ModuleJob !== true && typeof job !== 'function') {
2020
throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob', job);
2121
}
2222
debug(`Storing ${url} in ModuleMap`);

test/parallel/test-internal-module-map-asserts.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const ModuleMap = require('internal/modules/esm/module_map');
1212
code: 'ERR_INVALID_ARG_TYPE',
1313
type: TypeError,
1414
message: /^The "url" argument must be of type string/
15-
}, 15);
15+
}, 12);
1616

1717
const moduleMap = new ModuleMap();
1818

@@ -21,7 +21,7 @@ const ModuleMap = require('internal/modules/esm/module_map');
2121
// but I think it's useless, and was not simple to mock...
2222
const job = undefined;
2323

24-
[{}, [], true, 1, () => {}].forEach((value) => {
24+
[{}, [], true, 1].forEach((value) => {
2525
assert.throws(() => moduleMap.get(value), errorReg);
2626
assert.throws(() => moduleMap.has(value), errorReg);
2727
assert.throws(() => moduleMap.set(value, job), errorReg);
@@ -34,11 +34,11 @@ const ModuleMap = require('internal/modules/esm/module_map');
3434
code: 'ERR_INVALID_ARG_TYPE',
3535
type: TypeError,
3636
message: /^The "job" argument must be of type ModuleJob/
37-
}, 5);
37+
}, 4);
3838

3939
const moduleMap = new ModuleMap();
4040

41-
[{}, [], true, 1, () => {}].forEach((value) => {
41+
[{}, [], true, 1].forEach((value) => {
4242
assert.throws(() => moduleMap.set('', value), errorReg);
4343
});
4444
}

0 commit comments

Comments
 (0)