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: https://github.com/nodejs/node/pull/22793
Fixes: https://github.com/nodejs/node/issues/22778
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
This commit is contained in:
cjihrig 2018-09-10 09:57:15 -04:00 committed by Daniel Bevenius
parent a93f035212
commit 466cda0548
3 changed files with 13 additions and 9 deletions

View File

@ -10,6 +10,7 @@ const {
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { isUint8Array, isArrayBufferView } = require('internal/util/types');
const { once } = require('internal/util');
const pathModule = require('path');
const util = require('util');
const kType = Symbol('type');
@ -123,6 +124,7 @@ function getDirents(path, [names, types], callback) {
if (typeof callback == 'function') {
const len = names.length;
let toFinish = 0;
callback = once(callback);
for (i = 0; i < len; i++) {
const type = types[i];
if (type === UV_DIRENT_UNKNOWN) {

View File

@ -5,21 +5,13 @@
let eos;
const { once } = require('internal/util');
const {
ERR_INVALID_CALLBACK,
ERR_MISSING_ARGS,
ERR_STREAM_DESTROYED
} = require('internal/errors').codes;
function once(callback) {
let called = false;
return function(err) {
if (called) return;
called = true;
callback(err);
};
}
function isRequest(stream) {
return stream.setHeader && typeof stream.abort === 'function';
}

View File

@ -365,6 +365,15 @@ function isInsideNodeModules() {
return false;
}
function once(callback) {
let called = false;
return function(...args) {
if (called) return;
called = true;
callback(...args);
};
}
module.exports = {
assertCrypto,
cachedResult,
@ -381,6 +390,7 @@ module.exports = {
join,
normalizeEncoding,
objectToString,
once,
promisify,
spliceOne,
removeColors,