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

View File

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

View File

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