Skip to content

Commit aecd5eb

Browse files
author
zhangyongsheng
committed
module: only set cache when finding module succeeds
PR-URL: #36642Fixes: #36638 Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f2c2615 commit aecd5eb

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
lines changed

‎benchmark/module/module-require.js‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict';
2+
3+
constfs=require('fs');
4+
constpath=require('path');
5+
constcommon=require('../common.js');
6+
consttmpdir=require('../../test/common/tmpdir');
7+
constbenchmarkDirectory=path.join(tmpdir.path,'nodejs-benchmark-module');
8+
9+
constbench=common.createBenchmark(main,{
10+
type: ['.js','.json','dir'],
11+
n: [1e4],
12+
});
13+
14+
functionmain({ type, n }){
15+
tmpdir.refresh();
16+
createEntryPoint(n);
17+
18+
switch(type){
19+
case'.js':
20+
measureJSFile(n);
21+
break;
22+
case'.json':
23+
measureJSONFile(n);
24+
break;
25+
case'dir':
26+
measureDir(n);
27+
}
28+
29+
tmpdir.refresh();
30+
}
31+
32+
functionmeasureJSFile(n){
33+
bench.start();
34+
for(leti=0;i<n;i++){
35+
require(`${benchmarkDirectory}/${i}`);
36+
}
37+
bench.end(n);
38+
}
39+
40+
functionmeasureJSONFile(n){
41+
bench.start();
42+
for(leti=0;i<n;i++){
43+
require(`${benchmarkDirectory}/json_${i}.json`);
44+
}
45+
bench.end(n);
46+
}
47+
48+
functionmeasureDir(n){
49+
bench.start();
50+
for(leti=0;i<n;i++){
51+
require(`${benchmarkDirectory}${i}`);
52+
}
53+
bench.end(n);
54+
}
55+
56+
functioncreateEntryPoint(n){
57+
fs.mkdirSync(benchmarkDirectory);
58+
59+
constJSFileContent='module.exports = [];';
60+
constJSONFileContent='[]';
61+
62+
for(leti=0;i<n;i++){
63+
// JS file.
64+
fs.writeFileSync(`${benchmarkDirectory}/${i}.js`,JSFileContent);
65+
66+
// JSON file.
67+
fs.writeFileSync(`${benchmarkDirectory}/json_${i}.json`,JSONFileContent);
68+
69+
// Dir.
70+
fs.mkdirSync(`${benchmarkDirectory}${i}`);
71+
fs.writeFileSync(
72+
`${benchmarkDirectory}${i}/package.json`,
73+
'{"main": "index.js"}'
74+
);
75+
fs.writeFileSync(
76+
`${benchmarkDirectory}${i}/index.js`,
77+
JSFileContent
78+
);
79+
}
80+
}

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ function stat(filename){
148148
if(result!==undefined)returnresult;
149149
}
150150
constresult=internalModuleStat(filename);
151-
if(statCache!==null)statCache.set(filename,result);
151+
if(statCache!==null&&result>=0){
152+
// Only set cache when `internalModuleStat(filename)` succeeds.
153+
statCache.set(filename,result);
154+
}
152155
returnresult;
153156
}
154157

‎test/parallel/test-module-cache.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
require('../common');
3+
constassert=require('assert');
4+
constfs=require('fs');
5+
constpath=require('path');
6+
consttmpdir=require('../common/tmpdir');
7+
tmpdir.refresh();
8+
9+
constfilePath=path.join(tmpdir.path,'test-module-cache.json');
10+
assert.throws(
11+
()=>require(filePath),
12+
{code: 'MODULE_NOT_FOUND'}
13+
);
14+
15+
fs.writeFileSync(filePath,'[]');
16+
17+
constcontent=require(filePath);
18+
assert.strictEqual(Array.isArray(content),true);
19+
assert.strictEqual(content.length,0);

0 commit comments

Comments
(0)