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: #22793
Fixes: #22778
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Minwoo Jung <minwoo@nodesource.com>
  • Loading branch information
cjihrig authored and targos committed Sep 20, 2018
1 parent aa05c8b commit f0a4017
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,20 +5,12 @@

let eos;

const { once } = require('internal/util');
const {
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 noop(err) {
// Rethrow the error if it exists to avoid swallowing it
if (err) throw err;
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,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 @@ -380,6 +389,7 @@ module.exports = {
join,
normalizeEncoding,
objectToString,
once,
promisify,
spliceOne,
removeColors,
Expand Down

0 comments on commit f0a4017

Please sign in to comment.