aboutsummaryrefslogtreecommitdiff
path: root/node_modules/into-stream/index.js
diff options
context:
space:
mode:
authorMinteck <nekostarfan@gmail.com>2021-08-24 14:41:48 +0200
committerMinteck <nekostarfan@gmail.com>2021-08-24 14:41:48 +0200
commitd25e11bee6ca5ca523884da132d18e1400e077b9 (patch)
tree8af39fde19f7ed640a60fb397c7edd647dff1c4c /node_modules/into-stream/index.js
downloadkartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.gz
kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.tar.bz2
kartik-iridium-d25e11bee6ca5ca523884da132d18e1400e077b9.zip
Initial commit
Diffstat (limited to 'node_modules/into-stream/index.js')
-rw-r--r--node_modules/into-stream/index.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/node_modules/into-stream/index.js b/node_modules/into-stream/index.js
new file mode 100644
index 0000000..c35f695
--- /dev/null
+++ b/node_modules/into-stream/index.js
@@ -0,0 +1,79 @@
+'use strict';
+const from = require('from2');
+const pIsPromise = require('p-is-promise');
+
+module.exports = x => {
+ if (Array.isArray(x)) {
+ x = x.slice();
+ }
+
+ let promise;
+ let iterator;
+
+ prepare(x);
+
+ function prepare(value) {
+ x = value;
+ promise = pIsPromise(x) ? x : null;
+ // we don't iterate on strings and buffers since slicing them is ~7x faster
+ const shouldIterate = !promise && x[Symbol.iterator] && typeof x !== 'string' && !Buffer.isBuffer(x);
+ iterator = shouldIterate ? x[Symbol.iterator]() : null;
+ }
+
+ return from(function reader(size, cb) {
+ if (promise) {
+ promise.then(prepare).then(() => reader.call(this, size, cb), cb);
+ return;
+ }
+
+ if (iterator) {
+ const obj = iterator.next();
+ setImmediate(cb, null, obj.done ? null : obj.value);
+ return;
+ }
+
+ if (x.length === 0) {
+ setImmediate(cb, null, null);
+ return;
+ }
+
+ const chunk = x.slice(0, size);
+ x = x.slice(size);
+
+ setImmediate(cb, null, chunk);
+ });
+};
+
+module.exports.obj = x => {
+ if (Array.isArray(x)) {
+ x = x.slice();
+ }
+
+ let promise;
+ let iterator;
+
+ prepare(x);
+
+ function prepare(value) {
+ x = value;
+ promise = pIsPromise(x) ? x : null;
+ iterator = !promise && x[Symbol.iterator] ? x[Symbol.iterator]() : null;
+ }
+
+ return from.obj(function reader(size, cb) {
+ if (promise) {
+ promise.then(prepare).then(() => reader.call(this, size, cb), cb);
+ return;
+ }
+
+ if (iterator) {
+ const obj = iterator.next();
+ setImmediate(cb, null, obj.done ? null : obj.value);
+ return;
+ }
+
+ this.push(x);
+
+ setImmediate(cb, null, null);
+ });
+};