stream-read-all
Advanced tools
Comparing version 4.0.0 to 5.0.0
65
index.js
@@ -1,52 +0,33 @@ | ||
import { Transform } from 'node:stream' | ||
/** | ||
* @module stream-read-all | ||
* [options.objectMode]{boolean} | ||
*/ | ||
class StreamReader extends Transform { | ||
constructor (options) { | ||
super(options) | ||
this.options = options || {} | ||
if (this.options.objectMode) { | ||
this.buf = [] | ||
} else { | ||
this.buf = Buffer.alloc ? Buffer.alloc(0) : new Buffer(0) | ||
} | ||
async function streamReadAll (stream, options = {}) { | ||
if (!(stream && stream.pipe)) { | ||
throw new Error('Please supply a Readable stream as input') | ||
} | ||
return new Promise((resolve, reject) => { | ||
const buf = [] | ||
stream.on('data', chunk => { | ||
buf.push(chunk) | ||
}) | ||
_transform (chunk, enc, done) { | ||
if (chunk) { | ||
if (this.options.objectMode) { | ||
this.buf.push(chunk) | ||
/* End not guaranteed to emit, resolve on close */ | ||
stream.on('close', () => { | ||
if (options.objectMode) { | ||
resolve(buf) | ||
} else { | ||
this.buf = Buffer.concat([ this.buf, chunk ]) | ||
resolve(Buffer.concat(buf)) | ||
} | ||
} | ||
done() | ||
} | ||
_flush (done) { | ||
this.push(this.buf) | ||
this.push(null) | ||
done() | ||
} | ||
} | ||
/** | ||
* @return {Promise} | ||
* @alias module:stream-read-all | ||
*/ | ||
function streamReadAll (stream, options) { | ||
const streamReader = new StreamReader(options) | ||
stream.pipe(streamReader) | ||
return new Promise((resolve, reject) => { | ||
streamReader.resume() | ||
streamReader.on('end', () => { | ||
resolve(streamReader.buf) | ||
}) | ||
streamReader.on('error', reject) | ||
stream.on('error', err => { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
export default streamReadAll | ||
async function streamReadText (stream, encoding = 'utf8') { | ||
const result = await streamReadAll(stream) | ||
return result.toString(encoding) | ||
} | ||
export { streamReadAll, streamReadText } |
{ | ||
"name": "stream-read-all", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Returns a promise which fulfils with the supplied stream's content", | ||
"repository": "https://github.com/75lb/stream-read-all", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/75lb/stream-read-all.git" | ||
}, | ||
"type": "module", | ||
@@ -27,4 +30,4 @@ "exports": { | ||
"test": "npm run dist && npm run test:ci", | ||
"test:ci": "test-runner test.js", | ||
"dist": "rollup -c" | ||
"test:ci": "75lb-nature test-runner test.js", | ||
"dist": "75lb-nature cjs-build index.js" | ||
}, | ||
@@ -41,7 +44,3 @@ "files": [ | ||
"envs": [] | ||
}, | ||
"devDependencies": { | ||
"rollup": "^3.19.1", | ||
"test-runner": "^0.10.1" | ||
} | ||
} |
@@ -10,10 +10,10 @@ [![view on npm](https://badgen.net/npm/v/stream-read-all)](https://www.npmjs.org/package/stream-read-all) | ||
Returns a promise which fulfils with the supplied stream's content. Supports both regular and [object mode](https://nodejs.org/dist/latest-v19.x/docs/api/stream.html#object-mode) streams. | ||
Returns a promise which fulfils with the supplied stream's content. Supports any [Readable](https://nodejs.org/docs/latest/api/stream.html#readable-streams) stream as input in either regular or [object mode](https://nodejs.org/docs/latest/api/stream.html#object-mode). | ||
This example script... | ||
For example, this script... | ||
```js | ||
import streamReadAll from 'stream-read-all' | ||
const stdin = await streamReadAll(process.stdin) | ||
console.log(stdin.toString()) | ||
import { streamReadAll } from 'stream-read-all' | ||
const readable = await streamReadAll(process.stdin) | ||
console.log(readable.toString()) | ||
``` | ||
@@ -28,4 +28,14 @@ | ||
The above `streamReadAll` function returns either a `Buffer` in regular mode or an array of objects in object mode. Alternatively, you can use `streamReadText` which is identical to the above except it returns text. The second argument is optional, specifying the character encoding to use (as in the [buffer.toString()](https://nodejs.org/docs/latest/api/buffer.html#buftostringencoding-start-end) first argument). | ||
```js | ||
import { streamReadText } from 'stream-read-all' | ||
const readable = fs.createReadStream('./package.json') | ||
const text = await streamReadText(readable, 'hex') | ||
console.log(text) | ||
// prints the package.json file content in hex format | ||
``` | ||
* * * | ||
© 2017-23 Lloyd Brookes <75pound@gmail.com>. | ||
© 2017-25 Lloyd Brookes <opensource@75lb.com>. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
5706
0
40
0
62