Skip to content

Commit 92affe4

Browse files
author
Stephen Belanger
committed
lib: add diagnostics_channel events to module loading
1 parent f00969e commit 92affe4

7 files changed

+260
-10
lines changed

lib/internal/modules/cjs/loader.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ const {
149149
packageImportsResolve
150150
} = require('internal/modules/esm/resolve');
151151

152+
const dc = require('diagnostics_channel');
153+
const onLoad = dc.tracingChannel('module.require');
154+
152155
const isWindows = process.platform === 'win32';
153156

154157
const relativeResolveCache = ObjectCreate(null);
@@ -1102,7 +1105,10 @@ Module.prototype.require = function(id) {
11021105
}
11031106
requireDepth++;
11041107
try {
1105-
return Module._load(id, this, /* isMain */ false);
1108+
return onLoad.traceSync(Module._load, {
1109+
parentFilename: this.filename,
1110+
id,
1111+
}, Module, id, this, /* isMain */ false);
11061112
} finally {
11071113
requireDepth--;
11081114
}

lib/internal/modules/esm/loader.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ function getTranslators() {
5656
}
5757
const { getOptionValue } = require('internal/options');
5858

59+
const dc = require('diagnostics_channel');
60+
const onLoad = dc.tracingChannel('module.import');
61+
5962
/**
6063
* @typedef {object} ExportedHooks
6164
* @property {Function} globalPreload Global preload hook.
@@ -374,14 +377,18 @@ class ESMLoader {
374377
return module;
375378
};
376379
const ModuleJob = require('internal/modules/esm/module_job');
377-
const job = new ModuleJob(
378-
this, url, undefined, evalInstance, false, false);
379-
this.moduleMap.set(url, undefined, job);
380-
const { module } = await job.run();
380+
const namespace = await onLoad.tracePromise(async () => {
381+
const job = new ModuleJob(
382+
this, url, undefined, evalInstance, false, false);
383+
this.moduleMap.set(url, undefined, job);
384+
385+
const { module } = await job.run();
386+
return module.getNamespace();
387+
}, { parentURL: '<eval>', url });
381388

382389
return {
383390
__proto__: null,
384-
namespace: module.getNamespace(),
391+
namespace,
385392
};
386393
}
387394

@@ -513,9 +520,14 @@ class ESMLoader {
513520
const jobs = new Array(count);
514521

515522
for (let i = 0; i < count; i++) {
516-
jobs[i] = this.getModuleJob(specifiers[i], parentURL, importAssertions)
517-
.then((job) => job.run())
518-
.then(({ module }) => module.getNamespace());
523+
jobs[i] = onLoad.tracePromise(async () => {
524+
const job = await this.getModuleJob(specifiers[i], parentURL, importAssertions);
525+
const { module } = await job.run();
526+
return module.getNamespace();
527+
}, {
528+
parentURL,
529+
url: specifiers[i]
530+
});
519531
}
520532

521533
const namespaces = await SafePromiseAllReturnArrayLike(jobs);

lib/internal/modules/esm/translators.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ const { ModuleWrap } = moduleWrap;
5151
const asyncESM = require('internal/process/esm_loader');
5252
const { emitWarningSync } = require('internal/process/warning');
5353

54+
const dc = require('diagnostics_channel');
55+
const onLoad = dc.tracingChannel('module.import');
56+
5457
let cjsParse;
5558
async function initCJSParse() {
5659
if (typeof WebAssembly === 'undefined') {
@@ -164,7 +167,10 @@ translators.set('commonjs', async function commonjsStrategy(url, source,
164167
asyncESM.esmLoader.cjsCache.delete(module);
165168
} else {
166169
try {
167-
exports = CJSModule._load(filename, undefined, isMain);
170+
exports = onLoad.traceSync(CJSModule._load, {
171+
parentURL: undefined,
172+
url
173+
}, CJSModule, filename, undefined, isMain);
168174
} catch (err) {
169175
enrichCJSError(err, undefined, filename);
170176
throw err;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dc = require('diagnostics_channel');
5+
6+
const trace = dc.tracingChannel('module.import');
7+
const events = [];
8+
let lastEvent;
9+
10+
function track(name) {
11+
return (event) => {
12+
// Verify every event after the first is the same object
13+
if (events.length) {
14+
assert.strictEqual(event, lastEvent);
15+
}
16+
lastEvent = event;
17+
18+
events.push({ name, ...event });
19+
}
20+
}
21+
22+
trace.subscribe({
23+
start: common.mustCall(track('start')),
24+
end: common.mustCall(track('end')),
25+
asyncStart: common.mustCall(track('asyncStart')),
26+
asyncEnd: common.mustCall(track('asyncEnd')),
27+
error: common.mustCall(track('error')),
28+
});
29+
30+
import('does-not-exist').then(
31+
common.mustNotCall(),
32+
common.mustCall((error) => {
33+
// Verify order and contents of each event
34+
assert.deepStrictEqual(events, [
35+
{
36+
name: 'start',
37+
parentURL: `file://${module.filename}`,
38+
url: 'does-not-exist',
39+
},
40+
{
41+
name: 'end',
42+
parentURL: `file://${module.filename}`,
43+
url: 'does-not-exist',
44+
},
45+
{
46+
name: 'error',
47+
parentURL: `file://${module.filename}`,
48+
url: 'does-not-exist',
49+
error,
50+
},
51+
{
52+
name: 'asyncStart',
53+
parentURL: `file://${module.filename}`,
54+
url: 'does-not-exist',
55+
error,
56+
},
57+
{
58+
name: 'asyncEnd',
59+
parentURL: `file://${module.filename}`,
60+
url: 'does-not-exist',
61+
error,
62+
},
63+
]);
64+
})
65+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dc = require('diagnostics_channel');
5+
6+
const trace = dc.tracingChannel('module.import');
7+
const events = [];
8+
let lastEvent;
9+
10+
function track (name) {
11+
return (event) => {
12+
// Verify every event after the first is the same object
13+
if (events.length) {
14+
assert.strictEqual(event, lastEvent);
15+
}
16+
lastEvent = event;
17+
18+
events.push({ name, ...event });
19+
}
20+
}
21+
22+
trace.subscribe({
23+
start: common.mustCall(track('start')),
24+
end: common.mustCall(track('end')),
25+
asyncStart: common.mustCall(track('asyncStart')),
26+
asyncEnd: common.mustCall(track('asyncEnd')),
27+
error: common.mustNotCall(track('error')),
28+
});
29+
30+
import('http').then(
31+
common.mustCall((result) => {
32+
// Verify order and contents of each event
33+
assert.deepStrictEqual(events, [
34+
{
35+
name: 'start',
36+
parentURL: `file://${module.filename}`,
37+
url: 'http',
38+
},
39+
{
40+
name: 'end',
41+
parentURL: `file://${module.filename}`,
42+
url: 'http',
43+
},
44+
{
45+
name: 'asyncStart',
46+
parentURL: `file://${module.filename}`,
47+
url: 'http',
48+
result,
49+
},
50+
{
51+
name: 'asyncEnd',
52+
parentURL: `file://${module.filename}`,
53+
url: 'http',
54+
result,
55+
},
56+
]);
57+
}),
58+
common.mustNotCall(),
59+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dc = require('diagnostics_channel');
5+
6+
const trace = dc.tracingChannel('module.require');
7+
const events = [];
8+
let lastEvent;
9+
10+
function track (name) {
11+
return (event) => {
12+
// Verify every event after the first is the same object
13+
if (events.length) {
14+
assert.strictEqual(event, lastEvent);
15+
}
16+
lastEvent = event;
17+
18+
events.push({ name, ...event });
19+
}
20+
}
21+
22+
trace.subscribe({
23+
start: common.mustCall(track('start')),
24+
end: common.mustCall(track('end')),
25+
asyncStart: common.mustNotCall(track('asyncStart')),
26+
asyncEnd: common.mustNotCall(track('asyncEnd')),
27+
error: common.mustCall(track('error')),
28+
});
29+
30+
let error;
31+
try {
32+
require('does-not-exist');
33+
} catch (err) {
34+
error = err;
35+
}
36+
37+
// Verify order and contents of each event
38+
assert.deepStrictEqual(events, [
39+
{
40+
name: 'start',
41+
parentFilename: module.filename,
42+
id: 'does-not-exist',
43+
},
44+
{
45+
name: 'error',
46+
parentFilename: module.filename,
47+
id: 'does-not-exist',
48+
error,
49+
},
50+
{
51+
name: 'end',
52+
parentFilename: module.filename,
53+
id: 'does-not-exist',
54+
error,
55+
},
56+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const dc = require('diagnostics_channel');
5+
6+
const trace = dc.tracingChannel('module.require');
7+
const events = [];
8+
let lastEvent;
9+
10+
function track (name) {
11+
return (event) => {
12+
// Verify every event after the first is the same object
13+
if (events.length) {
14+
assert.strictEqual(event, lastEvent);
15+
}
16+
lastEvent = event;
17+
18+
events.push({ name, ...event });
19+
}
20+
}
21+
22+
trace.subscribe({
23+
start: common.mustCall(track('start')),
24+
end: common.mustCall(track('end')),
25+
asyncStart: common.mustNotCall(track('asyncStart')),
26+
asyncEnd: common.mustNotCall(track('asyncEnd')),
27+
error: common.mustNotCall(track('error')),
28+
});
29+
30+
const result = require('http');
31+
32+
// Verify order and contents of each event
33+
assert.deepStrictEqual(events, [
34+
{
35+
name: 'start',
36+
parentFilename: module.filename,
37+
id: 'http',
38+
},
39+
{
40+
name: 'end',
41+
parentFilename: module.filename,
42+
id: 'http',
43+
result,
44+
},
45+
]);
46+

0 commit comments

Comments
 (0)