update
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
The ISC License
|
||||
|
||||
Copyright (c) npm, Inc. and Contributors
|
||||
Copyright (c) 2017-2022 npm, Inc., Isaac Z. Schlueter, and Contributors
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
|
||||
+129
-14
@@ -7,7 +7,7 @@ stream](https://nodejs.org/api/stream.html#stream_class_stream_passthrough)
|
||||
fast](https://docs.google.com/spreadsheets/d/1oObKSrVwLX_7Ut4Z6g3fZW-AX1j1-k6w-cDsrkaSbHM/edit#gid=0)
|
||||
for objects, strings, and buffers.
|
||||
|
||||
Supports pipe()ing (including multi-pipe() and backpressure transmission),
|
||||
Supports `pipe()`ing (including multi-`pipe()` and backpressure transmission),
|
||||
buffering data until either a `data` event handler or `pipe()` is added (so
|
||||
you don't lose the first chunk), and most other cases where PassThrough is
|
||||
a good idea.
|
||||
@@ -17,9 +17,6 @@ from this stream via `'data'` events or by calling `pipe()` into some other
|
||||
stream. Calling `read()` requires the buffer to be flattened in some
|
||||
cases, which requires copying memory.
|
||||
|
||||
There is also no `unpipe()` method. Once you start piping, there is no
|
||||
stopping it!
|
||||
|
||||
If you set `objectMode: true` in the options, then whatever is written will
|
||||
be emitted. Otherwise, it'll do a minimal amount of Buffer copying to
|
||||
ensure proper Streams semantics when `read(n)` is called.
|
||||
@@ -44,8 +41,8 @@ out:
|
||||
- [minipass-flush](http://npm.im/minipass-flush)
|
||||
- [minipass-pipeline](http://npm.im/minipass-pipeline)
|
||||
- [tap](http://npm.im/tap)
|
||||
- [tap-parser](http://npm.im/tap)
|
||||
- [treport](http://npm.im/tap)
|
||||
- [tap-parser](http://npm.im/tap-parser)
|
||||
- [treport](http://npm.im/treport)
|
||||
- [minipass-fetch](http://npm.im/minipass-fetch)
|
||||
- [pacote](http://npm.im/pacote)
|
||||
- [make-fetch-happen](http://npm.im/make-fetch-happen)
|
||||
@@ -60,9 +57,13 @@ out:
|
||||
There are several things that make Minipass streams different from (and in
|
||||
some ways superior to) Node.js core streams.
|
||||
|
||||
Please read these caveats if you are familiar with noode-core streams and
|
||||
Please read these caveats if you are familiar with node-core streams and
|
||||
intend to use Minipass streams in your programs.
|
||||
|
||||
You can avoid most of these differences entirely (for a very
|
||||
small performance penalty) by setting `{async: true}` in the
|
||||
constructor options.
|
||||
|
||||
### Timing
|
||||
|
||||
Minipass streams are designed to support synchronous use-cases. Thus, data
|
||||
@@ -82,6 +83,82 @@ This non-deferring approach makes Minipass streams much easier to reason
|
||||
about, especially in the context of Promises and other flow-control
|
||||
mechanisms.
|
||||
|
||||
Example:
|
||||
|
||||
```js
|
||||
const Minipass = require('minipass')
|
||||
const stream = new Minipass({ async: true })
|
||||
stream.on('data', () => console.log('data event'))
|
||||
console.log('before write')
|
||||
stream.write('hello')
|
||||
console.log('after write')
|
||||
// output:
|
||||
// before write
|
||||
// data event
|
||||
// after write
|
||||
```
|
||||
|
||||
### Exception: Async Opt-In
|
||||
|
||||
If you wish to have a Minipass stream with behavior that more
|
||||
closely mimics Node.js core streams, you can set the stream in
|
||||
async mode either by setting `async: true` in the constructor
|
||||
options, or by setting `stream.async = true` later on.
|
||||
|
||||
```js
|
||||
const Minipass = require('minipass')
|
||||
const asyncStream = new Minipass({ async: true })
|
||||
asyncStream.on('data', () => console.log('data event'))
|
||||
console.log('before write')
|
||||
asyncStream.write('hello')
|
||||
console.log('after write')
|
||||
// output:
|
||||
// before write
|
||||
// after write
|
||||
// data event <-- this is deferred until the next tick
|
||||
```
|
||||
|
||||
Switching _out_ of async mode is unsafe, as it could cause data
|
||||
corruption, and so is not enabled. Example:
|
||||
|
||||
```js
|
||||
const Minipass = require('minipass')
|
||||
const stream = new Minipass({ encoding: 'utf8' })
|
||||
stream.on('data', chunk => console.log(chunk))
|
||||
stream.async = true
|
||||
console.log('before writes')
|
||||
stream.write('hello')
|
||||
setStreamSyncAgainSomehow(stream) // <-- this doesn't actually exist!
|
||||
stream.write('world')
|
||||
console.log('after writes')
|
||||
// hypothetical output would be:
|
||||
// before writes
|
||||
// world
|
||||
// after writes
|
||||
// hello
|
||||
// NOT GOOD!
|
||||
```
|
||||
|
||||
To avoid this problem, once set into async mode, any attempt to
|
||||
make the stream sync again will be ignored.
|
||||
|
||||
```js
|
||||
const Minipass = require('minipass')
|
||||
const stream = new Minipass({ encoding: 'utf8' })
|
||||
stream.on('data', chunk => console.log(chunk))
|
||||
stream.async = true
|
||||
console.log('before writes')
|
||||
stream.write('hello')
|
||||
stream.async = false // <-- no-op, stream already async
|
||||
stream.write('world')
|
||||
console.log('after writes')
|
||||
// actual output:
|
||||
// before writes
|
||||
// after writes
|
||||
// hello
|
||||
// world
|
||||
```
|
||||
|
||||
### No High/Low Water Marks
|
||||
|
||||
Node.js core streams will optimistically fill up a buffer, returning `true`
|
||||
@@ -97,6 +174,9 @@ If the data has nowhere to go, then `write()` returns false, and the data
|
||||
sits in a buffer, to be drained out immediately as soon as anyone consumes
|
||||
it.
|
||||
|
||||
Since nothing is ever buffered unnecessarily, there is much less
|
||||
copying data, and less bookkeeping about buffer capacity levels.
|
||||
|
||||
### Hazards of Buffering (or: Why Minipass Is So Fast)
|
||||
|
||||
Since data written to a Minipass stream is immediately written all the way
|
||||
@@ -181,6 +261,8 @@ moving on to the next entry in an archive parse stream, etc.) then be sure
|
||||
to call `stream.pause()` on creation, and then `stream.resume()` once you
|
||||
are ready to respond to the `end` event.
|
||||
|
||||
However, this is _usually_ not a problem because:
|
||||
|
||||
### Emit `end` When Asked
|
||||
|
||||
One hazard of immediately emitting `'end'` is that you may not yet have had
|
||||
@@ -197,6 +279,18 @@ To prevent calling handlers multiple times who would not expect multiple
|
||||
ends to occur, all listeners are removed from the `'end'` event whenever it
|
||||
is emitted.
|
||||
|
||||
### Emit `error` When Asked
|
||||
|
||||
The most recent error object passed to the `'error'` event is
|
||||
stored on the stream. If a new `'error'` event handler is added,
|
||||
and an error was previously emitted, then the event handler will
|
||||
be called immediately (or on `process.nextTick` in the case of
|
||||
async streams).
|
||||
|
||||
This makes it much more difficult to end up trying to interact
|
||||
with a broken stream, if the error handler is added after an
|
||||
error was previously emitted.
|
||||
|
||||
### Impact of "immediate flow" on Tee-streams
|
||||
|
||||
A "tee stream" is a stream piping to multiple destinations:
|
||||
@@ -221,7 +315,7 @@ src.pipe(dest1) // 'foo' chunk flows to dest1 immediately, and is gone
|
||||
src.pipe(dest2) // gets nothing!
|
||||
```
|
||||
|
||||
The solution is to create a dedicated tee-stream junction that pipes to
|
||||
One solution is to create a dedicated tee-stream junction that pipes to
|
||||
both locations, and then pipe to _that_ instead.
|
||||
|
||||
```js
|
||||
@@ -258,6 +352,13 @@ tee.on('data', handler2)
|
||||
src.pipe(tee)
|
||||
```
|
||||
|
||||
All of the hazards in this section are avoided by setting `{
|
||||
async: true }` in the Minipass constructor, or by setting
|
||||
`stream.async = true` afterwards. Note that this does add some
|
||||
overhead, so should only be done in cases where you are willing
|
||||
to lose a bit of performance in order to avoid having to refactor
|
||||
program logic.
|
||||
|
||||
## USAGE
|
||||
|
||||
It's a stream! Use it like a stream and it'll most likely do what you
|
||||
@@ -279,6 +380,10 @@ mp.end('bar')
|
||||
by default if you write() something other than a string or Buffer at any
|
||||
point. Setting `objectMode: true` will prevent setting any encoding
|
||||
value.
|
||||
* `async` Defaults to `false`. Set to `true` to defer data
|
||||
emission until next tick. This reduces performance slightly,
|
||||
but makes Minipass streams use timing behavior closer to Node
|
||||
core streams. See [Timing](#timing) for more details.
|
||||
|
||||
### API
|
||||
|
||||
@@ -300,9 +405,19 @@ streams.
|
||||
from being emitted for empty streams until the stream is resumed.
|
||||
* `resume()` - Resume the stream. If there's data in the buffer, it is all
|
||||
discarded. Any buffered events are immediately emitted.
|
||||
* `pipe(dest)` - Send all output to the stream provided. There is no way
|
||||
to unpipe. When data is emitted, it is immediately written to any and
|
||||
all pipe destinations.
|
||||
* `pipe(dest)` - Send all output to the stream provided. When
|
||||
data is emitted, it is immediately written to any and all pipe
|
||||
destinations. (Or written on next tick in `async` mode.)
|
||||
* `unpipe(dest)` - Stop piping to the destination stream. This
|
||||
is immediate, meaning that any asynchronously queued data will
|
||||
_not_ make it to the destination when running in `async` mode.
|
||||
* `options.end` - Boolean, end the destination stream when
|
||||
the source stream ends. Default `true`.
|
||||
* `options.proxyErrors` - Boolean, proxy `error` events from
|
||||
the source stream to the destination stream. Note that
|
||||
errors are _not_ proxied after the pipeline terminates,
|
||||
either due to the source emitting `'end'` or manually
|
||||
unpiping with `src.unpipe(dest)`. Default `false`.
|
||||
* `on(ev, fn)`, `emit(ev, fn)` - Minipass streams are EventEmitters. Some
|
||||
events are given special treatment, however. (See below under "events".)
|
||||
* `promise()` - Returns a Promise that resolves when the stream emits
|
||||
@@ -472,7 +587,7 @@ const mp = new Minipass({ encoding: 'utf8' })
|
||||
// some source of some data
|
||||
let i = 5
|
||||
const inter = setInterval(() => {
|
||||
if (i --> 0)
|
||||
if (i-- > 0)
|
||||
mp.write(Buffer.from('foo\n', 'utf8'))
|
||||
else {
|
||||
mp.end()
|
||||
@@ -598,9 +713,9 @@ class NDJSONDecode extends Minipass {
|
||||
const jsonData = (this._jsonBuffer + chunk).split('\n')
|
||||
this._jsonBuffer = jsonData.pop()
|
||||
for (let i = 0; i < jsonData.length; i++) {
|
||||
let parsed
|
||||
try {
|
||||
super.write(parsed)
|
||||
// JSON.parse can throw, emit an error on that
|
||||
super.write(JSON.parse(jsonData[i]))
|
||||
} catch (er) {
|
||||
this.emit('error', er)
|
||||
continue
|
||||
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
/// <reference types="node" />
|
||||
import { EventEmitter } from 'events'
|
||||
import { Stream } from 'stream'
|
||||
|
||||
export type Encoding = BufferEncoding | 'buffer' | null
|
||||
|
||||
interface Writable extends EventEmitter {
|
||||
end(): any
|
||||
write(chunk: any, ...args: any[]): any
|
||||
}
|
||||
|
||||
interface Readable extends EventEmitter {
|
||||
pause(): any
|
||||
resume(): any
|
||||
pipe(): any
|
||||
}
|
||||
|
||||
interface Pipe<R, W> {
|
||||
src: Minipass<R, W>
|
||||
dest: Writable
|
||||
opts: PipeOptions
|
||||
}
|
||||
|
||||
type DualIterable<T> = Iterable<T> & AsyncIterable<T>
|
||||
|
||||
type ContiguousData = Buffer | ArrayBufferLike | ArrayBufferView | string
|
||||
|
||||
type BufferOrString = Buffer | string
|
||||
|
||||
export default class Minipass<
|
||||
RType extends any = Buffer,
|
||||
WType extends any = RType extends BufferOrString ? ContiguousData : RType
|
||||
>
|
||||
extends Stream
|
||||
implements DualIterable<RType>
|
||||
{
|
||||
static isStream(stream: any): stream is Readable | Writable
|
||||
|
||||
readonly bufferLength: number
|
||||
readonly flowing: boolean
|
||||
readonly writable: boolean
|
||||
readonly readable: boolean
|
||||
readonly paused: boolean
|
||||
readonly emittedEnd: boolean
|
||||
readonly destroyed: boolean
|
||||
|
||||
/**
|
||||
* Not technically private or readonly, but not safe to mutate.
|
||||
*/
|
||||
private readonly buffer: RType[]
|
||||
private readonly pipes: Pipe<RType, WType>[]
|
||||
|
||||
/**
|
||||
* Technically writable, but mutating it can change the type,
|
||||
* so is not safe to do in TypeScript.
|
||||
*/
|
||||
readonly objectMode: boolean
|
||||
async: boolean
|
||||
|
||||
/**
|
||||
* Note: encoding is not actually read-only, and setEncoding(enc)
|
||||
* exists. However, this type definition will insist that TypeScript
|
||||
* programs declare the type of a Minipass stream up front, and if
|
||||
* that type is string, then an encoding MUST be set in the ctor. If
|
||||
* the type is Buffer, then the encoding must be missing, or set to
|
||||
* 'buffer' or null. If the type is anything else, then objectMode
|
||||
* must be set in the constructor options. So there is effectively
|
||||
* no allowed way that a TS program can set the encoding after
|
||||
* construction, as doing so will destroy any hope of type safety.
|
||||
* TypeScript does not provide many options for changing the type of
|
||||
* an object at run-time, which is what changing the encoding does.
|
||||
*/
|
||||
readonly encoding: Encoding
|
||||
// setEncoding(encoding: Encoding): void
|
||||
|
||||
// Options required if not reading buffers
|
||||
constructor(
|
||||
...args: RType extends Buffer
|
||||
? [] | [Options<RType>]
|
||||
: [Options<RType>]
|
||||
)
|
||||
|
||||
write(chunk: WType, cb?: () => void): boolean
|
||||
write(chunk: WType, encoding?: Encoding, cb?: () => void): boolean
|
||||
read(size?: number): RType
|
||||
end(cb?: () => void): this
|
||||
end(chunk: any, cb?: () => void): this
|
||||
end(chunk: any, encoding?: Encoding, cb?: () => void): this
|
||||
pause(): void
|
||||
resume(): void
|
||||
promise(): Promise<void>
|
||||
collect(): Promise<RType[]>
|
||||
|
||||
concat(): RType extends BufferOrString ? Promise<RType> : never
|
||||
destroy(er?: any): void
|
||||
pipe<W extends Writable>(dest: W, opts?: PipeOptions): W
|
||||
unpipe<W extends Writable>(dest: W): void
|
||||
|
||||
/**
|
||||
* alias for on()
|
||||
*/
|
||||
addEventHandler(event: string, listener: (...args: any[]) => any): this
|
||||
|
||||
on(event: string, listener: (...args: any[]) => any): this
|
||||
on(event: 'data', listener: (chunk: RType) => any): this
|
||||
on(event: 'error', listener: (error: any) => any): this
|
||||
on(
|
||||
event:
|
||||
| 'readable'
|
||||
| 'drain'
|
||||
| 'resume'
|
||||
| 'end'
|
||||
| 'prefinish'
|
||||
| 'finish'
|
||||
| 'close',
|
||||
listener: () => any
|
||||
): this
|
||||
|
||||
[Symbol.iterator](): Iterator<RType>
|
||||
[Symbol.asyncIterator](): AsyncIterator<RType>
|
||||
}
|
||||
|
||||
interface StringOptions {
|
||||
encoding: BufferEncoding
|
||||
objectMode?: boolean
|
||||
async?: boolean
|
||||
}
|
||||
|
||||
interface BufferOptions {
|
||||
encoding?: null | 'buffer'
|
||||
objectMode?: boolean
|
||||
async?: boolean
|
||||
}
|
||||
|
||||
interface ObjectModeOptions {
|
||||
objectMode: true
|
||||
async?: boolean
|
||||
}
|
||||
|
||||
export declare interface PipeOptions {
|
||||
end?: boolean
|
||||
proxyErrors?: boolean
|
||||
}
|
||||
|
||||
export declare type Options<T> = T extends string
|
||||
? StringOptions
|
||||
: T extends Buffer
|
||||
? BufferOptions
|
||||
: ObjectModeOptions
|
||||
+203
-99
@@ -1,13 +1,17 @@
|
||||
'use strict'
|
||||
const proc = typeof process === 'object' && process ? process : {
|
||||
stdout: null,
|
||||
stderr: null,
|
||||
}
|
||||
const EE = require('events')
|
||||
const Stream = require('stream')
|
||||
const Yallist = require('yallist')
|
||||
const SD = require('string_decoder').StringDecoder
|
||||
|
||||
const EOF = Symbol('EOF')
|
||||
const MAYBE_EMIT_END = Symbol('maybeEmitEnd')
|
||||
const EMITTED_END = Symbol('emittedEnd')
|
||||
const EMITTING_END = Symbol('emittingEnd')
|
||||
const EMITTED_ERROR = Symbol('emittedError')
|
||||
const CLOSED = Symbol('closed')
|
||||
const READ = Symbol('read')
|
||||
const FLUSH = Symbol('flush')
|
||||
@@ -22,6 +26,12 @@ const BUFFERPUSH = Symbol('bufferPush')
|
||||
const BUFFERSHIFT = Symbol('bufferShift')
|
||||
const OBJECTMODE = Symbol('objectMode')
|
||||
const DESTROYED = Symbol('destroyed')
|
||||
const EMITDATA = Symbol('emitData')
|
||||
const EMITEND = Symbol('emitEnd')
|
||||
const EMITEND2 = Symbol('emitEnd2')
|
||||
const ASYNC = Symbol('async')
|
||||
|
||||
const defer = fn => Promise.resolve().then(fn)
|
||||
|
||||
// TODO remove when Node v8 support drops
|
||||
const doIter = global._MP_NO_ITERATOR_SYMBOLS_ !== '1'
|
||||
@@ -46,14 +56,46 @@ const isArrayBuffer = b => b instanceof ArrayBuffer ||
|
||||
|
||||
const isArrayBufferView = b => !Buffer.isBuffer(b) && ArrayBuffer.isView(b)
|
||||
|
||||
class Pipe {
|
||||
constructor (src, dest, opts) {
|
||||
this.src = src
|
||||
this.dest = dest
|
||||
this.opts = opts
|
||||
this.ondrain = () => src[RESUME]()
|
||||
dest.on('drain', this.ondrain)
|
||||
}
|
||||
unpipe () {
|
||||
this.dest.removeListener('drain', this.ondrain)
|
||||
}
|
||||
// istanbul ignore next - only here for the prototype
|
||||
proxyErrors () {}
|
||||
end () {
|
||||
this.unpipe()
|
||||
if (this.opts.end)
|
||||
this.dest.end()
|
||||
}
|
||||
}
|
||||
|
||||
class PipeProxyErrors extends Pipe {
|
||||
unpipe () {
|
||||
this.src.removeListener('error', this.proxyErrors)
|
||||
super.unpipe()
|
||||
}
|
||||
constructor (src, dest, opts) {
|
||||
super(src, dest, opts)
|
||||
this.proxyErrors = er => dest.emit('error', er)
|
||||
src.on('error', this.proxyErrors)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = class Minipass extends Stream {
|
||||
constructor (options) {
|
||||
super()
|
||||
this[FLOWING] = false
|
||||
// whether we're explicitly paused
|
||||
this[PAUSED] = false
|
||||
this.pipes = new Yallist()
|
||||
this.buffer = new Yallist()
|
||||
this.pipes = []
|
||||
this.buffer = []
|
||||
this[OBJECTMODE] = options && options.objectMode || false
|
||||
if (this[OBJECTMODE])
|
||||
this[ENCODING] = null
|
||||
@@ -61,11 +103,13 @@ module.exports = class Minipass extends Stream {
|
||||
this[ENCODING] = options && options.encoding || null
|
||||
if (this[ENCODING] === 'buffer')
|
||||
this[ENCODING] = null
|
||||
this[ASYNC] = options && !!options.async || false
|
||||
this[DECODER] = this[ENCODING] ? new SD(this[ENCODING]) : null
|
||||
this[EOF] = false
|
||||
this[EMITTED_END] = false
|
||||
this[EMITTING_END] = false
|
||||
this[CLOSED] = false
|
||||
this[EMITTED_ERROR] = null
|
||||
this.writable = true
|
||||
this.readable = true
|
||||
this[BUFFERLENGTH] = 0
|
||||
@@ -99,6 +143,9 @@ module.exports = class Minipass extends Stream {
|
||||
get objectMode () { return this[OBJECTMODE] }
|
||||
set objectMode (om) { this[OBJECTMODE] = this[OBJECTMODE] || !!om }
|
||||
|
||||
get ['async'] () { return this[ASYNC] }
|
||||
set ['async'] (a) { this[ASYNC] = this[ASYNC] || !!a }
|
||||
|
||||
write (chunk, encoding, cb) {
|
||||
if (this[EOF])
|
||||
throw new Error('write after end')
|
||||
@@ -117,6 +164,8 @@ module.exports = class Minipass extends Stream {
|
||||
if (!encoding)
|
||||
encoding = 'utf8'
|
||||
|
||||
const fn = this[ASYNC] ? defer : f => f()
|
||||
|
||||
// convert array buffers and typed array views into buffers
|
||||
// at some point in the future, we may want to do the opposite!
|
||||
// leave strings and buffers as-is
|
||||
@@ -131,19 +180,40 @@ module.exports = class Minipass extends Stream {
|
||||
this.objectMode = true
|
||||
}
|
||||
|
||||
// this ensures at this point that the chunk is a buffer or string
|
||||
// handle object mode up front, since it's simpler
|
||||
// this yields better performance, fewer checks later.
|
||||
if (this[OBJECTMODE]) {
|
||||
/* istanbul ignore if - maybe impossible? */
|
||||
if (this.flowing && this[BUFFERLENGTH] !== 0)
|
||||
this[FLUSH](true)
|
||||
|
||||
if (this.flowing)
|
||||
this.emit('data', chunk)
|
||||
else
|
||||
this[BUFFERPUSH](chunk)
|
||||
|
||||
if (this[BUFFERLENGTH] !== 0)
|
||||
this.emit('readable')
|
||||
|
||||
if (cb)
|
||||
fn(cb)
|
||||
|
||||
return this.flowing
|
||||
}
|
||||
|
||||
// at this point the chunk is a buffer or string
|
||||
// don't buffer it up or send it to the decoder
|
||||
if (!this.objectMode && !chunk.length) {
|
||||
if (!chunk.length) {
|
||||
if (this[BUFFERLENGTH] !== 0)
|
||||
this.emit('readable')
|
||||
if (cb)
|
||||
cb()
|
||||
fn(cb)
|
||||
return this.flowing
|
||||
}
|
||||
|
||||
// fast-path writing strings of same encoding to a stream with
|
||||
// an empty buffer, skipping the buffer/decoder dance
|
||||
if (typeof chunk === 'string' && !this[OBJECTMODE] &&
|
||||
if (typeof chunk === 'string' &&
|
||||
// unless it is a string already ready for us to use
|
||||
!(encoding === this[ENCODING] && !this[DECODER].lastNeed)) {
|
||||
chunk = Buffer.from(chunk, encoding)
|
||||
@@ -152,22 +222,20 @@ module.exports = class Minipass extends Stream {
|
||||
if (Buffer.isBuffer(chunk) && this[ENCODING])
|
||||
chunk = this[DECODER].write(chunk)
|
||||
|
||||
if (this.flowing) {
|
||||
// if we somehow have something in the buffer, but we think we're
|
||||
// flowing, then we need to flush all that out first, or we get
|
||||
// chunks coming in out of order. Can't emit 'drain' here though,
|
||||
// because we're mid-write, so that'd be bad.
|
||||
if (this[BUFFERLENGTH] !== 0)
|
||||
this[FLUSH](true)
|
||||
// Note: flushing CAN potentially switch us into not-flowing mode
|
||||
if (this.flowing && this[BUFFERLENGTH] !== 0)
|
||||
this[FLUSH](true)
|
||||
|
||||
if (this.flowing)
|
||||
this.emit('data', chunk)
|
||||
} else
|
||||
else
|
||||
this[BUFFERPUSH](chunk)
|
||||
|
||||
if (this[BUFFERLENGTH] !== 0)
|
||||
this.emit('readable')
|
||||
|
||||
if (cb)
|
||||
cb()
|
||||
fn(cb)
|
||||
|
||||
return this.flowing
|
||||
}
|
||||
@@ -176,35 +244,31 @@ module.exports = class Minipass extends Stream {
|
||||
if (this[DESTROYED])
|
||||
return null
|
||||
|
||||
try {
|
||||
if (this[BUFFERLENGTH] === 0 || n === 0 || n > this[BUFFERLENGTH])
|
||||
return null
|
||||
|
||||
if (this[OBJECTMODE])
|
||||
n = null
|
||||
|
||||
if (this.buffer.length > 1 && !this[OBJECTMODE]) {
|
||||
if (this.encoding)
|
||||
this.buffer = new Yallist([
|
||||
Array.from(this.buffer).join('')
|
||||
])
|
||||
else
|
||||
this.buffer = new Yallist([
|
||||
Buffer.concat(Array.from(this.buffer), this[BUFFERLENGTH])
|
||||
])
|
||||
}
|
||||
|
||||
return this[READ](n || null, this.buffer.head.value)
|
||||
} finally {
|
||||
if (this[BUFFERLENGTH] === 0 || n === 0 || n > this[BUFFERLENGTH]) {
|
||||
this[MAYBE_EMIT_END]()
|
||||
return null
|
||||
}
|
||||
|
||||
if (this[OBJECTMODE])
|
||||
n = null
|
||||
|
||||
if (this.buffer.length > 1 && !this[OBJECTMODE]) {
|
||||
if (this.encoding)
|
||||
this.buffer = [this.buffer.join('')]
|
||||
else
|
||||
this.buffer = [Buffer.concat(this.buffer, this[BUFFERLENGTH])]
|
||||
}
|
||||
|
||||
const ret = this[READ](n || null, this.buffer[0])
|
||||
this[MAYBE_EMIT_END]()
|
||||
return ret
|
||||
}
|
||||
|
||||
[READ] (n, chunk) {
|
||||
if (n === chunk.length || n === null)
|
||||
this[BUFFERSHIFT]()
|
||||
else {
|
||||
this.buffer.head.value = chunk.slice(n)
|
||||
this.buffer[0] = chunk.slice(n)
|
||||
chunk = chunk.slice(0, n)
|
||||
this[BUFFERLENGTH] -= n
|
||||
}
|
||||
@@ -280,7 +344,7 @@ module.exports = class Minipass extends Stream {
|
||||
this[BUFFERLENGTH] += 1
|
||||
else
|
||||
this[BUFFERLENGTH] += chunk.length
|
||||
return this.buffer.push(chunk)
|
||||
this.buffer.push(chunk)
|
||||
}
|
||||
|
||||
[BUFFERSHIFT] () {
|
||||
@@ -288,7 +352,7 @@ module.exports = class Minipass extends Stream {
|
||||
if (this[OBJECTMODE])
|
||||
this[BUFFERLENGTH] -= 1
|
||||
else
|
||||
this[BUFFERLENGTH] -= this.buffer.head.value.length
|
||||
this[BUFFERLENGTH] -= this.buffer[0].length
|
||||
}
|
||||
return this.buffer.shift()
|
||||
}
|
||||
@@ -310,37 +374,56 @@ module.exports = class Minipass extends Stream {
|
||||
|
||||
const ended = this[EMITTED_END]
|
||||
opts = opts || {}
|
||||
if (dest === process.stdout || dest === process.stderr)
|
||||
if (dest === proc.stdout || dest === proc.stderr)
|
||||
opts.end = false
|
||||
else
|
||||
opts.end = opts.end !== false
|
||||
opts.proxyErrors = !!opts.proxyErrors
|
||||
|
||||
const p = { dest: dest, opts: opts, ondrain: _ => this[RESUME]() }
|
||||
this.pipes.push(p)
|
||||
|
||||
dest.on('drain', p.ondrain)
|
||||
this[RESUME]()
|
||||
// piping an ended stream ends immediately
|
||||
if (ended && p.opts.end)
|
||||
p.dest.end()
|
||||
if (ended) {
|
||||
if (opts.end)
|
||||
dest.end()
|
||||
} else {
|
||||
this.pipes.push(!opts.proxyErrors ? new Pipe(this, dest, opts)
|
||||
: new PipeProxyErrors(this, dest, opts))
|
||||
if (this[ASYNC])
|
||||
defer(() => this[RESUME]())
|
||||
else
|
||||
this[RESUME]()
|
||||
}
|
||||
|
||||
return dest
|
||||
}
|
||||
|
||||
unpipe (dest) {
|
||||
const p = this.pipes.find(p => p.dest === dest)
|
||||
if (p) {
|
||||
this.pipes.splice(this.pipes.indexOf(p), 1)
|
||||
p.unpipe()
|
||||
}
|
||||
}
|
||||
|
||||
addListener (ev, fn) {
|
||||
return this.on(ev, fn)
|
||||
}
|
||||
|
||||
on (ev, fn) {
|
||||
try {
|
||||
return super.on(ev, fn)
|
||||
} finally {
|
||||
if (ev === 'data' && !this.pipes.length && !this.flowing)
|
||||
this[RESUME]()
|
||||
else if (isEndish(ev) && this[EMITTED_END]) {
|
||||
super.emit(ev)
|
||||
this.removeAllListeners(ev)
|
||||
}
|
||||
const ret = super.on(ev, fn)
|
||||
if (ev === 'data' && !this.pipes.length && !this.flowing)
|
||||
this[RESUME]()
|
||||
else if (ev === 'readable' && this[BUFFERLENGTH] !== 0)
|
||||
super.emit('readable')
|
||||
else if (isEndish(ev) && this[EMITTED_END]) {
|
||||
super.emit(ev)
|
||||
this.removeAllListeners(ev)
|
||||
} else if (ev === 'error' && this[EMITTED_ERROR]) {
|
||||
if (this[ASYNC])
|
||||
defer(() => fn.call(this, this[EMITTED_ERROR]))
|
||||
else
|
||||
fn.call(this, this[EMITTED_ERROR])
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
get emittedEnd () {
|
||||
@@ -363,63 +446,84 @@ module.exports = class Minipass extends Stream {
|
||||
}
|
||||
}
|
||||
|
||||
emit (ev, data) {
|
||||
emit (ev, data, ...extra) {
|
||||
// error and close are only events allowed after calling destroy()
|
||||
if (ev !== 'error' && ev !== 'close' && ev !== DESTROYED && this[DESTROYED])
|
||||
return
|
||||
else if (ev === 'data') {
|
||||
if (!data)
|
||||
return
|
||||
|
||||
if (this.pipes.length)
|
||||
this.pipes.forEach(p =>
|
||||
p.dest.write(data) === false && this.pause())
|
||||
return !data ? false
|
||||
: this[ASYNC] ? defer(() => this[EMITDATA](data))
|
||||
: this[EMITDATA](data)
|
||||
} else if (ev === 'end') {
|
||||
// only actual end gets this treatment
|
||||
if (this[EMITTED_END] === true)
|
||||
return
|
||||
|
||||
this[EMITTED_END] = true
|
||||
this.readable = false
|
||||
|
||||
if (this[DECODER]) {
|
||||
data = this[DECODER].end()
|
||||
if (data) {
|
||||
this.pipes.forEach(p => p.dest.write(data))
|
||||
super.emit('data', data)
|
||||
}
|
||||
}
|
||||
|
||||
this.pipes.forEach(p => {
|
||||
p.dest.removeListener('drain', p.ondrain)
|
||||
if (p.opts.end)
|
||||
p.dest.end()
|
||||
})
|
||||
return this[EMITEND]()
|
||||
} else if (ev === 'close') {
|
||||
this[CLOSED] = true
|
||||
// don't emit close before 'end' and 'finish'
|
||||
if (!this[EMITTED_END] && !this[DESTROYED])
|
||||
return
|
||||
const ret = super.emit('close')
|
||||
this.removeAllListeners('close')
|
||||
return ret
|
||||
} else if (ev === 'error') {
|
||||
this[EMITTED_ERROR] = data
|
||||
const ret = super.emit('error', data)
|
||||
this[MAYBE_EMIT_END]()
|
||||
return ret
|
||||
} else if (ev === 'resume') {
|
||||
const ret = super.emit('resume')
|
||||
this[MAYBE_EMIT_END]()
|
||||
return ret
|
||||
} else if (ev === 'finish' || ev === 'prefinish') {
|
||||
const ret = super.emit(ev)
|
||||
this.removeAllListeners(ev)
|
||||
return ret
|
||||
}
|
||||
|
||||
// TODO: replace with a spread operator when Node v4 support drops
|
||||
const args = new Array(arguments.length)
|
||||
args[0] = ev
|
||||
args[1] = data
|
||||
if (arguments.length > 2) {
|
||||
for (let i = 2; i < arguments.length; i++) {
|
||||
args[i] = arguments[i]
|
||||
// Some other unknown event
|
||||
const ret = super.emit(ev, data, ...extra)
|
||||
this[MAYBE_EMIT_END]()
|
||||
return ret
|
||||
}
|
||||
|
||||
[EMITDATA] (data) {
|
||||
for (const p of this.pipes) {
|
||||
if (p.dest.write(data) === false)
|
||||
this.pause()
|
||||
}
|
||||
const ret = super.emit('data', data)
|
||||
this[MAYBE_EMIT_END]()
|
||||
return ret
|
||||
}
|
||||
|
||||
[EMITEND] () {
|
||||
if (this[EMITTED_END])
|
||||
return
|
||||
|
||||
this[EMITTED_END] = true
|
||||
this.readable = false
|
||||
if (this[ASYNC])
|
||||
defer(() => this[EMITEND2]())
|
||||
else
|
||||
this[EMITEND2]()
|
||||
}
|
||||
|
||||
[EMITEND2] () {
|
||||
if (this[DECODER]) {
|
||||
const data = this[DECODER].end()
|
||||
if (data) {
|
||||
for (const p of this.pipes) {
|
||||
p.dest.write(data)
|
||||
}
|
||||
super.emit('data', data)
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return super.emit.apply(this, args)
|
||||
} finally {
|
||||
if (!isEndish(ev))
|
||||
this[MAYBE_EMIT_END]()
|
||||
else
|
||||
this.removeAllListeners(ev)
|
||||
for (const p of this.pipes) {
|
||||
p.end()
|
||||
}
|
||||
const ret = super.emit('end')
|
||||
this.removeAllListeners('end')
|
||||
return ret
|
||||
}
|
||||
|
||||
// const all = await stream.collect()
|
||||
@@ -452,8 +556,8 @@ module.exports = class Minipass extends Stream {
|
||||
promise () {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.on(DESTROYED, () => reject(new Error('stream destroyed')))
|
||||
this.on('end', () => resolve())
|
||||
this.on('error', er => reject(er))
|
||||
this.on('end', () => resolve())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -521,7 +625,7 @@ module.exports = class Minipass extends Stream {
|
||||
this[DESTROYED] = true
|
||||
|
||||
// throw away all buffered data, it's never coming out
|
||||
this.buffer = new Yallist()
|
||||
this.buffer.length = 0
|
||||
this[BUFFERLENGTH] = 0
|
||||
|
||||
if (typeof this.close === 'function' && !this[CLOSED])
|
||||
|
||||
+33
-15
@@ -1,25 +1,25 @@
|
||||
{
|
||||
"_args": [
|
||||
[
|
||||
"minipass@3.1.3",
|
||||
"/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series"
|
||||
"minipass@3.3.4",
|
||||
"/home/node/nuxt"
|
||||
]
|
||||
],
|
||||
"_from": "minipass@3.1.3",
|
||||
"_id": "minipass@3.1.3",
|
||||
"_from": "minipass@3.3.4",
|
||||
"_id": "minipass@3.3.4",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-Mgd2GdMVzY+x3IJ+oHnVM+KG3lA5c8tnabyJKmHSaG2kAGpudxuOf8ToDkhumF7UzME7DecbQE9uOZhNm7PuJg==",
|
||||
"_integrity": "sha512-I9WPbWHCGu8W+6k1ZiGpPu0GkoKBeorkfKNuAFBNS1HNFJvke82sxvI5bzcCNpWPorkOO5QQ+zomzzwRxejXiw==",
|
||||
"_location": "/minipass",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "version",
|
||||
"registry": true,
|
||||
"raw": "minipass@3.1.3",
|
||||
"raw": "minipass@3.3.4",
|
||||
"name": "minipass",
|
||||
"escapedName": "minipass",
|
||||
"rawSpec": "3.1.3",
|
||||
"rawSpec": "3.3.4",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "3.1.3"
|
||||
"fetchSpec": "3.3.4"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/cacache",
|
||||
@@ -27,11 +27,13 @@
|
||||
"/minipass-collect",
|
||||
"/minipass-flush",
|
||||
"/minipass-pipeline",
|
||||
"/ssri"
|
||||
"/minizlib",
|
||||
"/ssri",
|
||||
"/tar"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/minipass/-/minipass-3.1.3.tgz",
|
||||
"_spec": "3.1.3",
|
||||
"_where": "/mnt/Foxconn/Digitalent/Deverloper/liff-push_2series",
|
||||
"_resolved": "https://registry.npmjs.org/minipass/-/minipass-3.3.4.tgz",
|
||||
"_spec": "3.3.4",
|
||||
"_where": "/home/node/nuxt",
|
||||
"author": {
|
||||
"name": "Isaac Z. Schlueter",
|
||||
"email": "i@izs.me",
|
||||
@@ -45,14 +47,19 @@
|
||||
},
|
||||
"description": "minimal implementation of a PassThrough stream",
|
||||
"devDependencies": {
|
||||
"@types/node": "^17.0.41",
|
||||
"end-of-stream": "^1.4.0",
|
||||
"tap": "^14.6.5",
|
||||
"through2": "^2.0.3"
|
||||
"prettier": "^2.6.2",
|
||||
"tap": "^16.2.0",
|
||||
"through2": "^2.0.3",
|
||||
"ts-node": "^10.8.1",
|
||||
"typescript": "^4.7.3"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=8"
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"index.js"
|
||||
],
|
||||
"homepage": "https://github.com/isaacs/minipass#readme",
|
||||
@@ -63,6 +70,17 @@
|
||||
"license": "ISC",
|
||||
"main": "index.js",
|
||||
"name": "minipass",
|
||||
"prettier": {
|
||||
"semi": false,
|
||||
"printWidth": 80,
|
||||
"tabWidth": 2,
|
||||
"useTabs": false,
|
||||
"singleQuote": true,
|
||||
"jsxSingleQuote": false,
|
||||
"bracketSameLine": true,
|
||||
"arrowParens": "avoid",
|
||||
"endOfLine": "lf"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/isaacs/minipass.git"
|
||||
@@ -76,5 +94,5 @@
|
||||
"tap": {
|
||||
"check-coverage": true
|
||||
},
|
||||
"version": "3.1.3"
|
||||
"version": "3.3.4"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user