Skip to content

Commit 3bad36e

Browse files
committed
esm: spawn only one hooks thread
1 parent 0f461aa commit 3bad36e

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { spawnPromisified } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import { strictEqual } from 'node:assert';
4+
import { execPath } from 'node:process';
5+
import { describe, it } from 'node:test';
6+
7+
describe('off-thread hooks', { concurrency: true }, () => {
8+
it('uses only one hooks thread to support multiple application threads', async () => {
9+
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
10+
'--no-warnings',
11+
'--import',
12+
`data:text/javascript,${encodeURIComponent(`
13+
import { register } from 'node:module';
14+
register('${fixtures.fileURL('es-module-loaders/hooks-log.mjs')}');
15+
`)}`,
16+
fixtures.path('es-module-loaders/workers-spawned.mjs'),
17+
]);
18+
19+
strictEqual(stderr, '');
20+
strictEqual(stdout.split('\n').filter(line => line.startsWith('initialize')).length, 1);
21+
strictEqual(code, 0);
22+
strictEqual(signal, null);
23+
});
24+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { writeFileSync } from 'node:fs';
2+
3+
let initializeCount = 0;
4+
let resolveCount = 0;
5+
let loadCount = 0;
6+
7+
export function initialize() {
8+
writeFileSync(1, `initialize ${++initializeCount}\n`);
9+
}
10+
11+
export function resolve(specifier, context, next) {
12+
writeFileSync(1, `resolve ${++resolveCount} ${specifier}\n`);
13+
return next(specifier, context);
14+
}
15+
16+
export function load(url, context, next) {
17+
writeFileSync(1, `load ${++loadCount} ${url}\n`);
18+
return next(url, context);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { foo } from './module-named-exports.mjs';
2+
3+
console.log(foo);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Worker } from 'worker_threads';
2+
import { fileURLToPath } from 'node:url';
3+
4+
const workerPath = fileURLToPath(new URL('./worker-log.mjs', import.meta.url));
5+
6+
// Spawn two workers
7+
new Worker(workerPath);
8+
new Worker(workerPath);

0 commit comments

Comments
 (0)