diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index acc566e37bb4bf..362383be48f05c 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 caa4042339bd37..bce466db749f6c 100644 --- a/lib/internal/streams/pipeline.js +++ b/lib/internal/streams/pipeline.js @@ -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'; } diff --git a/lib/internal/util.js b/lib/internal/util.js index 43b99740683b39..5cb1b281ba406d 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -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, @@ -381,6 +390,7 @@ module.exports = { join, normalizeEncoding, objectToString, + once, promisify, spliceOne, removeColors,