Skip to content

Commit 0875867

Browse files
GeoffreyBoothtargos
authored andcommitted
esm: refactor test-esm-loader-resolve-type
PR-URL: #49493 Backport-PR-URL: #50669 Reviewed-By: Jacob Smith <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 9b7c9d9 commit 0875867

File tree

3 files changed

+47
-41
lines changed

3 files changed

+47
-41
lines changed

‎test/es-module/test-esm-loader-resolve-type.mjs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ try{
1616
{recursive: true}
1717
);
1818

19-
deepStrictEqual(awaitspawnPromisified(
19+
constoutput=awaitspawnPromisified(
2020
execPath,
2121
[
2222
'--no-warnings',
@@ -29,15 +29,17 @@ try{
2929
console.log(JSON.stringify({before, after }));`,
3030
],
3131
{cwd: base},
32-
),{
32+
);
33+
34+
deepStrictEqual(output,{
35+
code: 0,
36+
signal: null,
3337
stderr: '',
3438
stdout: JSON.stringify({
3539
before: {importedESM: 0,importedCJS: 0},
3640
// Dynamic import in the eval script should increment ESM counter but not CJS counter
3741
after: {importedESM: 1,importedCJS: 0},
3842
})+'\n',
39-
code: 0,
40-
signal: null,
4143
});
4244
}finally{
4345
awaitrm(base,{recursive: true,force: true});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @type{MessagePort} */
2+
letport;
3+
exportfunctioninitialize(data){
4+
port=data.port;
5+
}
6+
7+
exportasyncfunctionresolve(specifier,context,next){
8+
constnextResult=awaitnext(specifier,context);
9+
const{ format }=nextResult;
10+
11+
if(format==='module'||specifier.endsWith('.mjs')){
12+
port.postMessage({type: 'module'});
13+
}elseif(format==null||format==='commonjs'){
14+
port.postMessage({type: 'commonjs'});
15+
}
16+
17+
returnnextResult;
18+
}
Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,30 @@
1+
import*asfixturesfrom'../../common/fixtures.mjs';
2+
import{register}from'node:module';
3+
import{MessageChannel}from'node:worker_threads';
4+
15
letimportedESM=0;
26
letimportedCJS=0;
7+
exportfunctiongetModuleTypeStats(){
8+
return{ importedESM, importedCJS };
9+
};
310

4-
exportfunctionglobalPreload({ port }){
5-
port.on('message',(int32)=>{
6-
port.postMessage({ importedESM, importedCJS });
7-
Atomics.store(int32,0,1);
8-
Atomics.notify(int32,0);
9-
});
10-
port.unref();
11-
return`
12-
const{receiveMessageOnPort } = getBuiltin('worker_threads');
13-
global.getModuleTypeStats = async function getModuleTypeStats(){
14-
const sab = new SharedArrayBuffer(4);
15-
const int32 = new Int32Array(sab);
16-
port.postMessage(int32);
17-
// Artificial timeout to keep the event loop alive.
18-
// https://bugs.chromium.org/p/v8/issues/detail?id=13238
19-
// TODO(targos) Remove when V8 issue is resolved.
20-
const timeout = setTimeout(() =>{throw new Error('timeout')}, 1_000);
21-
await Atomics.waitAsync(int32, 0, 0).value;
22-
clearTimeout(timeout);
23-
return receiveMessageOnPort(port).message;
24-
};
25-
`;
26-
}
27-
28-
exportasyncfunctionload(url,context,next){
29-
returnnext(url);
30-
}
11+
const{ port1, port2 }=newMessageChannel();
3112

32-
exportasyncfunctionresolve(specifier,context,next){
33-
constnextResult=awaitnext(specifier,context);
34-
const{ format }=nextResult;
13+
register(fixtures.fileURL('es-module-loaders/hook-resolve-type-loader.mjs'),{
14+
data: {port: port2},
15+
transferList: [port2],
16+
});
3517

36-
if(format==='module'||specifier.endsWith('.mjs')){
37-
importedESM++;
38-
}elseif(format==null||format==='commonjs'){
39-
importedCJS++;
18+
port1.on('message',({ type })=>{
19+
switch(type){
20+
case'module':
21+
importedESM++;
22+
break;
23+
case'commonjs':
24+
importedCJS++;
25+
break;
4026
}
27+
});
4128

42-
returnnextResult;
43-
}
44-
29+
port1.unref();
30+
port2.unref();

0 commit comments

Comments
(0)