Skip to content

Commit f0a4017

Browse files
cjihrigtargos
authored andcommitted
fs: ensure readdir() callback is only called once
This commit ensures that the readdir() callback can only be called once when the withFileTypes parameter is supplied. PR-URL: #22793Fixes: #22778 Reviewed-By: Bryan English <bryan@bryanenglish.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
1 parent aa05c8b commit f0a4017

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

‎lib/internal/fs/utils.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ERR_OUT_OF_RANGE
1111
}=require('internal/errors').codes;
1212
const{ isUint8Array, isArrayBufferView }=require('internal/util/types');
13+
const{ once }=require('internal/util');
1314
constpathModule=require('path');
1415
constutil=require('util');
1516
constkType=Symbol('type');
@@ -123,6 +124,7 @@ function getDirents(path, [names, types], callback) {
123124
if(typeofcallback=='function'){
124125
constlen=names.length;
125126
lettoFinish=0;
127+
callback=once(callback);
126128
for(i=0;i<len;i++){
127129
consttype=types[i];
128130
if(type===UV_DIRENT_UNKNOWN){

‎lib/internal/streams/pipeline.js‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@
55

66
leteos;
77

8+
const{ once }=require('internal/util');
89
const{
910
ERR_MISSING_ARGS,
1011
ERR_STREAM_DESTROYED
1112
}=require('internal/errors').codes;
1213

13-
functiononce(callback){
14-
letcalled=false;
15-
returnfunction(err){
16-
if(called)return;
17-
called=true;
18-
callback(err);
19-
};
20-
}
21-
2214
functionnoop(err){
2315
// Rethrow the error if it exists to avoid swallowing it
2416
if(err)throwerr;

‎lib/internal/util.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ function isInsideNodeModules() {
364364
returnfalse;
365365
}
366366

367+
functiononce(callback){
368+
letcalled=false;
369+
returnfunction(...args){
370+
if(called)return;
371+
called=true;
372+
callback(...args);
373+
};
374+
}
375+
367376
module.exports={
368377
assertCrypto,
369378
cachedResult,
@@ -380,6 +389,7 @@ module.exports = {
380389
join,
381390
normalizeEncoding,
382391
objectToString,
392+
once,
383393
promisify,
384394
spliceOne,
385395
removeColors,

0 commit comments

Comments
 (0)