This commit is contained in:
2022-07-18 02:50:52 +00:00
parent befd344ab0
commit 06181b34d6
8569 changed files with 818704 additions and 352705 deletions
+2 -5
View File
@@ -74,7 +74,7 @@ declare const getStream: {
*/
buffer(
stream: Stream,
options?: getStream.OptionsWithEncoding
options?: getStream.Options
): Promise<Buffer>;
/**
@@ -86,7 +86,7 @@ declare const getStream: {
- When `encoding` is set to `buffer`, it collects an array of buffers. `maxBuffer` refers to the summed byte lengths of every buffer in the array.
- When `encoding` is set to anything else, it collects an array of strings. `maxBuffer` refers to the summed character lengths of every string in the array.
*/
array<StreamObjectModeType = unknown>(
array<StreamObjectModeType>(
stream: Stream,
options?: getStream.Options
): Promise<StreamObjectModeType[]>;
@@ -100,9 +100,6 @@ declare const getStream: {
): Promise<string[]>;
MaxBufferError: typeof MaxBufferErrorClass;
// TODO: Remove this for the next major release
default: typeof getStream;
};
export = getStream;
+15 -12
View File
@@ -1,7 +1,11 @@
'use strict';
const pump = require('pump');
const {constants: BufferConstants} = require('buffer');
const stream = require('stream');
const {promisify} = require('util');
const bufferStream = require('./buffer-stream');
const streamPipelinePromisified = promisify(stream.pipeline);
class MaxBufferError extends Error {
constructor() {
super('maxBuffer exceeded');
@@ -11,7 +15,7 @@ class MaxBufferError extends Error {
async function getStream(inputStream, options) {
if (!inputStream) {
return Promise.reject(new Error('Expected a stream'));
throw new Error('Expected a stream');
}
options = {
@@ -20,25 +24,26 @@ async function getStream(inputStream, options) {
};
const {maxBuffer} = options;
const stream = bufferStream(options);
let stream;
await new Promise((resolve, reject) => {
const rejectPromise = error => {
if (error) { // A null check
// Don't retrieve an oversized buffer.
if (error && stream.getBufferedLength() <= BufferConstants.MAX_LENGTH) {
error.bufferedData = stream.getBufferedValue();
}
reject(error);
};
stream = pump(inputStream, bufferStream(options), error => {
if (error) {
(async () => {
try {
await streamPipelinePromisified(inputStream, stream);
resolve();
} catch (error) {
rejectPromise(error);
return;
}
resolve();
});
})();
stream.on('data', () => {
if (stream.getBufferedLength() > maxBuffer) {
@@ -51,8 +56,6 @@ async function getStream(inputStream, options) {
}
module.exports = getStream;
// TODO: Remove this for the next major release
module.exports.default = getStream;
module.exports.buffer = (stream, options) => getStream(stream, {...options, encoding: 'buffer'});
module.exports.array = (stream, options) => getStream(stream, {...options, array: true});
module.exports.MaxBufferError = MaxBufferError;
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
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:
+18 -20
View File
@@ -1,59 +1,57 @@
{
"_args": [
[
"get-stream@5.1.0",
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
"get-stream@6.0.1",
"/home/node/nuxt"
]
],
"_from": "get-stream@5.1.0",
"_id": "get-stream@5.1.0",
"_from": "get-stream@6.0.1",
"_id": "get-stream@6.0.1",
"_inBundle": false,
"_integrity": "sha512-EXr1FOzrzTfGeL0gQdeFEvOMm2mzMOglyiOXSTpPC+iAjAKftbr3jpCMWynogwYnM+eSj9sHGc6wjIcDvYiygw==",
"_integrity": "sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==",
"_location": "/get-stream",
"_phantomChildren": {},
"_requested": {
"type": "version",
"registry": true,
"raw": "get-stream@5.1.0",
"raw": "get-stream@6.0.1",
"name": "get-stream",
"escapedName": "get-stream",
"rawSpec": "5.1.0",
"rawSpec": "6.0.1",
"saveSpec": null,
"fetchSpec": "5.1.0"
"fetchSpec": "6.0.1"
},
"_requiredBy": [
"/execa"
],
"_resolved": "https://registry.npmjs.org/get-stream/-/get-stream-5.1.0.tgz",
"_spec": "5.1.0",
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
"_resolved": "https://registry.npmjs.org/get-stream/-/get-stream-6.0.1.tgz",
"_spec": "6.0.1",
"_where": "/home/node/nuxt",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"bugs": {
"url": "https://github.com/sindresorhus/get-stream/issues"
},
"dependencies": {
"pump": "^3.0.0"
},
"description": "Get a stream as a string, buffer, or array",
"devDependencies": {
"@types/node": "^11.13.0",
"ava": "^1.4.1",
"@types/node": "^14.0.27",
"ava": "^2.4.0",
"into-stream": "^5.0.0",
"tsd": "^0.7.2",
"tsd": "^0.13.1",
"xo": "^0.24.0"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"files": [
"index.js",
"index.d.ts",
"buffer-stream.js"
],
"funding": "https://github.com/sponsors/sindresorhus",
"homepage": "https://github.com/sindresorhus/get-stream#readme",
"keywords": [
"get",
@@ -80,5 +78,5 @@
"scripts": {
"test": "xo && ava && tsd"
},
"version": "5.1.0"
"version": "6.0.1"
}
+18 -17
View File
@@ -1,15 +1,13 @@
# get-stream [![Build Status](https://travis-ci.org/sindresorhus/get-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stream)
# get-stream
> Get a stream as a string, buffer, or array
## Install
```
$ npm install get-stream
```
## Usage
```js
@@ -44,40 +42,39 @@ const getStream = require('get-stream');
})();
```
## API
The methods returns a promise that resolves when the `end` event fires on the stream, indicating that there is no more data to be read. The stream is switched to flowing mode.
### getStream(stream, [options])
### getStream(stream, options?)
Get the `stream` as a string.
#### options
Type: `Object`
Type: `object`
##### encoding
Type: `string`<br>
Default: `utf8`
Type: `string`\
Default: `'utf8'`
[Encoding](https://nodejs.org/api/buffer.html#buffer_buffer) of the incoming stream.
##### maxBuffer
Type: `number`<br>
Type: `number`\
Default: `Infinity`
Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected with a `getStream.MaxBufferError` error.
### getStream.buffer(stream, [options])
### getStream.buffer(stream, options?)
Get the `stream` as a buffer.
It honors the `maxBuffer` option as above, but it refers to byte length rather than string length.
### getStream.array(stream, [options])
### getStream.array(stream, options?)
Get the `stream` as an array of values.
@@ -89,7 +86,6 @@ It honors both the `maxBuffer` and `encoding` options. The behavior changes slig
- When `encoding` is set to anything else, it collects an array of strings. `maxBuffer` refers to the summed character lengths of every string in the array.
## Errors
If the input stream emits an `error` event, the promise will be rejected with the error. The buffered data will be attached to the `bufferedData` property of the error.
@@ -105,19 +101,24 @@ If the input stream emits an `error` event, the promise will be rejected with th
})()
```
## FAQ
### How is this different from [`concat-stream`](https://github.com/maxogden/concat-stream)?
This module accepts a stream instead of being one and returns a promise instead of using a callback. The API is simpler and it only supports returning a string, buffer, or array. It doesn't have a fragile type inference. You explicitly choose what you want. And it doesn't depend on the huge `readable-stream` package.
## Related
- [get-stdin](https://github.com/sindresorhus/get-stdin) - Get stdin as a string or buffer
---
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-get-stream?utm_source=npm-get-stream&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>