Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
cjihrig committed Sep 10, 2018
commit d262489ce53194eca325e2cf3eeb2f4eb3d01128
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