Comparing version 2.2.4 to 2.3.0
68
index.js
@@ -10,2 +10,5 @@ 'use strict' | ||
const FLUSH = Symbol('flush') | ||
const doIter = process.env._MP_NO_ITERATOR_SYMBOLS_ !== '1' | ||
const ASYNCITERATOR = doIter && Symbol.asyncIterator || Symbol('asyncIterator not implemented') | ||
const ITERATOR = doIter && Symbol.iterator || Symbol('iterator not implemented') | ||
const FLUSHCHUNK = Symbol('flushChunk') | ||
@@ -31,3 +34,3 @@ const SD = require('string_decoder').StringDecoder | ||
class MiniPass extends EE { | ||
module.exports = class MiniPass extends EE { | ||
constructor (options) { | ||
@@ -304,4 +307,63 @@ super() | ||
} | ||
// const all = await stream.collect() | ||
collect () { | ||
return new Promise((resolve, reject) => { | ||
const buf = [] | ||
this.on('data', c => buf.push(c)) | ||
this.on('end', () => resolve(buf)) | ||
this.on('error', reject) | ||
}) | ||
} | ||
// for await (let chunk of stream) | ||
[ASYNCITERATOR] () { | ||
const next = () => { | ||
const res = this.read() | ||
if (res !== null) | ||
return Promise.resolve({ done: false, value: res }) | ||
if (this[EOF]) | ||
return Promise.resolve({ done: true }) | ||
let resolve = null | ||
let reject = null | ||
const onerr = er => { | ||
this.removeListener('data', ondata) | ||
this.removeListener('end', onend) | ||
reject(er) | ||
} | ||
const ondata = value => { | ||
this.removeListener('error', onerr) | ||
this.removeListener('end', onend) | ||
this.pause() | ||
resolve({ value: value, done: !!this[EOF] }) | ||
} | ||
const onend = () => { | ||
this.removeListener('error', onerr) | ||
this.removeListener('data', ondata) | ||
resolve({ done: true }) | ||
} | ||
return new Promise((res, rej) => { | ||
reject = rej | ||
resolve = res | ||
this.once('error', onerr) | ||
this.once('end', onend) | ||
this.once('data', ondata) | ||
this.resume() | ||
}) | ||
} | ||
return { next } | ||
} | ||
// for (let chunk of stream) | ||
[ITERATOR] () { | ||
const next = () => { | ||
const value = this.read() | ||
const done = value === null | ||
return { value, done } | ||
} | ||
return { next } | ||
} | ||
} | ||
module.exports = MiniPass |
{ | ||
"name": "minipass", | ||
"version": "2.2.4", | ||
"version": "2.3.0", | ||
"description": "minimal implementation of a PassThrough stream", | ||
@@ -12,3 +12,3 @@ "main": "index.js", | ||
"end-of-stream": "^1.4.0", | ||
"tap": "^10.7.0", | ||
"tap": "^11.1.4", | ||
"through2": "^2.0.3" | ||
@@ -15,0 +15,0 @@ }, |
@@ -18,5 +18,3 @@ # minipass | ||
some other stream. Calling `read()` requires the buffer to be | ||
flattened in some cases, which requires copying memory. Also, | ||
`read()` always returns Buffers, even if an `encoding` option is | ||
specified. | ||
flattened in some cases, which requires copying memory. | ||
@@ -48,1 +46,81 @@ There is also no `unpipe()` method. Once you start piping, there is | ||
``` | ||
### collecting | ||
```js | ||
mp.collect().then(all => { | ||
// all is an array of all the data emitted | ||
// encoding is supported in this case, so | ||
// so the result will be a collection of strings if | ||
// an encoding is specified, or buffers/objects if not. | ||
// | ||
// In an async function, you may do | ||
// const data = await stream.collect() | ||
}) | ||
``` | ||
### iteration | ||
You can iterate over streams synchronously or asynchronously in | ||
platforms that support it. | ||
Synchronous iteration will end when the currently available data is | ||
consumed, even if the `end` event has not been reached. In string and | ||
buffer mode, the data is concatenated, so unless multiple writes are | ||
occurring in the same tick as the `read()`, sync iteration loops will | ||
generally only have a single iteration. | ||
To consume chunks in this way exactly as they have been written, with | ||
no flattening, create the stream with the `{ objectMode: true }` | ||
option. | ||
```js | ||
const mp = new Minipass({ objectMode: true }) | ||
mp.write('a') | ||
mp.write('b') | ||
for (let letter of mp) { | ||
console.log(letter) // a, b | ||
} | ||
mp.write('c') | ||
mp.write('d') | ||
for (let letter of mp) { | ||
console.log(letter) // c, d | ||
} | ||
mp.write('e') | ||
mp.end() | ||
for (let letter of mp) { | ||
console.log(letter) // e | ||
} | ||
for (let letter of mp) { | ||
console.log(letter) // nothing | ||
} | ||
``` | ||
Asynchronous iteration will continue until the end event is reached, | ||
consuming all of the data. | ||
```js | ||
const mp = new Minipass({ encoding: 'utf8' }) | ||
// some source of some data | ||
let i = 5 | ||
const inter = setInterval(() => { | ||
if (i --> 0) | ||
mp.write(Buffer.from('foo\n', 'utf8')) | ||
else { | ||
mp.end() | ||
clearInterval(inter) | ||
} | ||
}, 100) | ||
// consume the data with asynchronous iteration | ||
async function consume () { | ||
for await (let chunk of mp) { | ||
console.log(chunk) | ||
} | ||
return 'ok' | ||
} | ||
consume().then(res => console.log(res)) | ||
// logs `foo\n` 5 times, and then `ok` | ||
``` |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
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
13431
318
125
1