Skip to content

Commit

Permalink
fs: ensure readdir() callback is only called once
Browse files Browse the repository at this point in the history
This commit ensures that the readdir() callback can only be
called once when the withFileTypes parameter is supplied.

PR-URL: nodejs#22793
Fixes: nodejs#22778
Reviewed-By: Bryan English <[email protected]>
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
  • Loading branch information
cjihrig authored and danbev committed Sep 13, 2018
1 parent a93f035 commit 466cda0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 1 addition & 9 deletions lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -381,6 +390,7 @@ module.exports = {
join,
normalizeEncoding,
objectToString,
once,
promisify,
spliceOne,
removeColors,
Expand Down

0 comments on commit 466cda0

Please sign in to comment.