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: #22793 Fixes: #22778 Reviewed-By: Bryan English <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent aa05c8b commit f0a4017

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

lib/internal/fs/utils.js

+2
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
const pathModule = require('path');
1415
const util = require('util');
1516
const kType = Symbol('type');
@@ -123,6 +124,7 @@ function getDirents(path, [names, types], callback) {
123124
if (typeof callback == 'function') {
124125
const len = names.length;
125126
let toFinish = 0;
127+
callback = once(callback);
126128
for (i = 0; i < len; i++) {
127129
const type = types[i];
128130
if (type === UV_DIRENT_UNKNOWN) {

lib/internal/streams/pipeline.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@
55

66
let eos;
77

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

13-
function once(callback) {
14-
let called = false;
15-
return function(err) {
16-
if (called) return;
17-
called = true;
18-
callback(err);
19-
};
20-
}
21-
2214
function noop(err) {
2315
// Rethrow the error if it exists to avoid swallowing it
2416
if (err) throw err;

lib/internal/util.js

+10
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ function isInsideNodeModules() {
364364
return false;
365365
}
366366

367+
function once(callback) {
368+
let called = false;
369+
return function(...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)