Initial commit
This commit is contained in:
commit
e49726988c
5445 files changed
+1089914
No files matched your search
+49
@@ -0,0 +1,49 @@
|
||||
/// <reference types="node"/>
|
||||
import {Readable as ReadableStream} from 'stream';
|
||||
|
||||
declare namespace intoStream {
|
||||
type Input =
|
||||
| Buffer
|
||||
| NodeJS.TypedArray
|
||||
| ArrayBuffer
|
||||
| string
|
||||
| Iterable<Buffer | string>
|
||||
| AsyncIterable<Buffer | string>;
|
||||
|
||||
/* eslint-disable @typescript-eslint/ban-types */
|
||||
type InputObject =
|
||||
| object
|
||||
| Iterable<object>
|
||||
| AsyncIterable<object>;
|
||||
/* eslint-enable @typescript-eslint/ban-types */
|
||||
}
|
||||
|
||||
declare const intoStream: {
|
||||
/**
|
||||
Convert object `input` into a stream.
|
||||
|
||||
@param input - The object input to convert to a stream.
|
||||
@returns A [readable object stream](https://nodejs.org/api/stream.html#stream_object_mode).
|
||||
*/
|
||||
object: (
|
||||
input: intoStream.InputObject | Promise<intoStream.InputObject>
|
||||
) => ReadableStream;
|
||||
|
||||
/**
|
||||
Convert `input` into a stream. Adheres to the requested chunk size, except for `array` where each element will be a chunk.
|
||||
|
||||
@param input - The input to convert to a stream.
|
||||
@returns A [readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable).
|
||||
|
||||
@example
|
||||
```
|
||||
import intoStream = require('into-stream');
|
||||
|
||||
intoStream('unicorn').pipe(process.stdout);
|
||||
//=> 'unicorn'
|
||||
```
|
||||
*/
|
||||
(input: intoStream.Input | Promise<intoStream.Input>): ReadableStream;
|
||||
};
|
||||
|
||||
export = intoStream;
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
'use strict';
|
||||
const from = require('from2');
|
||||
const pIsPromise = require('p-is-promise');
|
||||
|
||||
const intoStream = input => {
|
||||
if (Array.isArray(input)) {
|
||||
input = input.slice();
|
||||
}
|
||||
|
||||
let promise;
|
||||
let iterator;
|
||||
let asyncIterator;
|
||||
|
||||
prepare(input);
|
||||
|
||||
function prepare(value) {
|
||||
input = value;
|
||||
|
||||
if (
|
||||
input instanceof ArrayBuffer ||
|
||||
(ArrayBuffer.isView(input) && !Buffer.isBuffer(input))
|
||||
) {
|
||||
input = Buffer.from(input);
|
||||
}
|
||||
|
||||
promise = pIsPromise(input) ? input : null;
|
||||
|
||||
// We don't iterate on strings and buffers since slicing them is ~7x faster
|
||||
const shouldIterate = !promise && input[Symbol.iterator] && typeof input !== 'string' && !Buffer.isBuffer(input);
|
||||
iterator = shouldIterate ? input[Symbol.iterator]() : null;
|
||||
|
||||
const shouldAsyncIterate = !promise && input[Symbol.asyncIterator];
|
||||
asyncIterator = shouldAsyncIterate ? input[Symbol.asyncIterator]() : null;
|
||||
}
|
||||
|
||||
return from(function reader(size, callback) {
|
||||
if (promise) {
|
||||
(async () => {
|
||||
try {
|
||||
await prepare(await promise);
|
||||
reader.call(this, size, callback);
|
||||
} catch (error) {
|
||||
callback(error);
|
||||
}
|
||||
})();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (iterator) {
|
||||
const object = iterator.next();
|
||||
setImmediate(callback, null, object.done ? null : object.value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (asyncIterator) {
|
||||
(async () => {
|
||||
try {
|
||||
const object = await asyncIterator.next();
|
||||
setImmediate(callback, null, object.done ? null : object.value);
|
||||
} catch (error) {
|
||||
setImmediate(callback, error);
|
||||
}
|
||||
})();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (input.length === 0) {
|
||||
setImmediate(callback, null, null);
|
||||
return;
|
||||
}
|
||||
|
||||
const chunk = input.slice(0, size);
|
||||
input = input.slice(size);
|
||||
|
||||
setImmediate(callback, null, chunk);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = intoStream;
|
||||
|
||||
module.exports.object = input => {
|
||||
if (Array.isArray(input)) {
|
||||
input = input.slice();
|
||||
}
|
||||
|
||||
let promise;
|
||||
let iterator;
|
||||
let asyncIterator;
|
||||
|
||||
prepare(input);
|
||||
|
||||
function prepare(value) {
|
||||
input = value;
|
||||
promise = pIsPromise(input) ? input : null;
|
||||
iterator = !promise && input[Symbol.iterator] ? input[Symbol.iterator]() : null;
|
||||
asyncIterator = !promise && input[Symbol.asyncIterator] ? input[Symbol.asyncIterator]() : null;
|
||||
}
|
||||
|
||||
return from.obj(function reader(size, callback) {
|
||||
if (promise) {
|
||||
(async () => {
|
||||
try {
|
||||
await prepare(await promise);
|
||||
reader.call(this, size, callback);
|
||||
} catch (error) {
|
||||
callback(error);
|
||||
}
|
||||
})();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (iterator) {
|
||||
const object = iterator.next();
|
||||
setImmediate(callback, null, object.done ? null : object.value);
|
||||
return;
|
||||
}
|
||||
|
||||
if (asyncIterator) {
|
||||
(async () => {
|
||||
try {
|
||||
const object = await asyncIterator.next();
|
||||
setImmediate(callback, null, object.done ? null : object.value);
|
||||
} catch (error) {
|
||||
setImmediate(callback, error);
|
||||
}
|
||||
})();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.push(input);
|
||||
|
||||
setImmediate(callback, null, null);
|
||||
});
|
||||
};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
{
|
||||
"name": "into-stream",
|
||||
"version": "6.0.0",
|
||||
"description": "Convert a string/promise/array/iterable/asynciterable/buffer/typedarray/arraybuffer/object into a stream",
|
||||
"license": "MIT",
|
||||
"repository": "sindresorhus/into-stream",
|
||||
"funding": "https://github.com/sponsors/sindresorhus",
|
||||
"author": {
|
||||
"name": "Sindre Sorhus",
|
||||
"email": "sindresorhus@gmail.com",
|
||||
"url": "https://sindresorhus.com"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && ava && tsd"
|
||||
},
|
||||
"files": [
|
||||
"index.js",
|
||||
"index.d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"stream",
|
||||
"buffer",
|
||||
"string",
|
||||
"object",
|
||||
"array",
|
||||
"iterable",
|
||||
"async",
|
||||
"asynciterable",
|
||||
"promise",
|
||||
"promises",
|
||||
"from",
|
||||
"into",
|
||||
"to",
|
||||
"transform",
|
||||
"convert",
|
||||
"readable",
|
||||
"pull",
|
||||
"gulpfriendly",
|
||||
"value"
|
||||
],
|
||||
"dependencies": {
|
||||
"from2": "^2.3.0",
|
||||
"p-is-promise": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^2.4.0",
|
||||
"get-stream": "^6.0.0",
|
||||
"p-event": "^4.2.0",
|
||||
"p-immediate": "^3.1.0",
|
||||
"tsd": "^0.13.1",
|
||||
"xo": "^0.33.0"
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
# into-stream [](https://travis-ci.com/github/sindresorhus/into-stream)
|
||||
|
||||
> Convert a string/promise/array/iterable/asynciterable/buffer/typedarray/arraybuffer/object into a stream
|
||||
|
||||
Correctly chunks up the input and handles backpressure.
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install into-stream
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const intoStream = require('into-stream');
|
||||
|
||||
intoStream('unicorn').pipe(process.stdout);
|
||||
//=> 'unicorn'
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
### intoStream(input)
|
||||
|
||||
Type: `Buffer | TypedArray | ArrayBuffer | string | Iterable<Buffer | string> | AsyncIterable<Buffer | string> | Promise`\
|
||||
Returns: [Readable stream](https://nodejs.org/api/stream.html#stream_class_stream_readable)
|
||||
|
||||
Adheres to the requested chunk size, except for `array` where each element will be a chunk.
|
||||
|
||||
### intoStream.object(input)
|
||||
|
||||
Type: `object | Iterable<object> | AsyncIterable<object> | Promise`\
|
||||
Returns: [Readable object stream](https://nodejs.org/api/stream.html#stream_object_mode)
|
||||
|
||||
## Related
|
||||
|
||||
- [to-readable-stream](https://github.com/sindresorhus/to-readable-stream) - Simpler version of this module
|
||||
Reference in new issue
Block a user