Skip to content

Commit

Permalink
chore: apply suggestion
Browse files Browse the repository at this point in the history
Co-authored-by: Aras Abbasi <[email protected]>
Signed-off-by: KaKa <[email protected]>
  • Loading branch information
climba03003 and Uzlopak authored Mar 29, 2024
1 parent 81bb72c commit ce46b67
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit ce46b67

Please sign in to comment.