From ce46b6732cb9eef03068c04496cbb733aa8a1b50 Mon Sep 17 00:00:00 2001 From: KaKa <23028015+climba03003@users.noreply.github.com> Date: Fri, 29 Mar 2024 22:27:35 +0800 Subject: [PATCH] chore: apply suggestion Co-authored-by: Aras Abbasi Signed-off-by: KaKa <23028015+climba03003@users.noreply.github.com> --- lib/utils.js | 52 +++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index a277957..37e5fb1 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -35,42 +35,44 @@ function isStream (stream) { return stream !== null && typeof stream === 'object' && typeof stream.pipe === 'function' } -// It is a helper used to provide a async iteratable for -// Readable.from +/** + * Provide a async iteratable for Readable.from + */ async function * intoAsyncIterator (payload) { - const isBuffer = Buffer.isBuffer(payload) - - if ( - !isBuffer && - ( + if (typeof payload === 'object') { + if (Buffer.isBuffer(payload)) { + yield payload + return + } + + if ( // ArrayBuffer payload instanceof ArrayBuffer || // NodeJS.TypedArray ArrayBuffer.isView(payload) - ) - ) { - payload = Buffer.from(payload) - } - - const isObject = typeof payload === 'object' + ) { + yield Buffer.from(payload) + return + } - // Iterator - if (!isBuffer && isObject && Symbol.iterator in payload) { - for (const chunk of payload) { - yield chunk + // Iterator + if (Symbol.iterator in payload) { + for (const chunk of payload) { + yield chunk + } + return } - return - } - // Async Iterator - if (!isBuffer && isObject && Symbol.asyncIterator in payload) { - for await (const chunk of payload) { - yield chunk + // Async Iterator + if (Symbol.asyncIterator in payload) { + for await (const chunk of payload) { + yield chunk + } + return } - return } - // string | Buffer + // string yield payload }