-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
57 lines (43 loc) · 1.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import {Readable as ReadableStream} from 'node:stream';
import {Buffer} from 'node:buffer';
function baseIntoStream(isObjectMode, input) {
if (input === undefined || input === null) {
throw new TypeError('Input should not be undefined or null.');
}
async function * reader() {
let value = await input;
if (!value) {
return;
}
if (Array.isArray(value)) {
value = [...value];
}
if (
!isObjectMode
&& (
value instanceof ArrayBuffer
|| (ArrayBuffer.isView(value) && !Buffer.isBuffer(value))
)
) {
value = Buffer.from(value);
}
// We don't iterate on strings and buffers since yielding them is ~7x faster.
if (typeof value !== 'string' && !Buffer.isBuffer(value) && value?.[Symbol.iterator]) {
for (const element of value) {
yield element;
}
return;
}
if (value?.[Symbol.asyncIterator]) {
for await (const element of value) {
yield await element;
}
return;
}
yield value;
}
return ReadableStream.from(reader(), {objectMode: isObjectMode});
}
const intoStream = baseIntoStream.bind(undefined, false);
export default intoStream;
intoStream.object = baseIntoStream.bind(undefined, true);