diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index c4a426996cf1a0..39f4c32906433a 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -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) { diff --git a/lib/internal/streams/pipeline.js b/lib/internal/streams/pipeline.js index 849b3d39dbe25b..67b624262f3ff6 100644 --- a/lib/internal/streams/pipeline.js +++ b/lib/internal/streams/pipeline.js @@ -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; diff --git a/lib/internal/util.js b/lib/internal/util.js index 03d83d49056f0d..a922276085f404 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -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, @@ -380,6 +389,7 @@ module.exports = { join, normalizeEncoding, objectToString, + once, promisify, spliceOne, removeColors,