diff --git a/node_modules/minipass/LICENSE b/node_modules/minipass/LICENSE index 20a4762540923..bf1dece2e1f12 100644 --- a/node_modules/minipass/LICENSE +++ b/node_modules/minipass/LICENSE @@ -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 diff --git a/node_modules/minipass/index.d.ts b/node_modules/minipass/index.d.ts new file mode 100644 index 0000000000000..edbef54824801 --- /dev/null +++ b/node_modules/minipass/index.d.ts @@ -0,0 +1,149 @@ +/// +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 { + src: Minipass + dest: Writable + opts: PipeOptions +} + +type DualIterable = Iterable & AsyncIterable + +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 +{ + 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[] + + /** + * 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] + : [Options] + ) + + 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 + collect(): Promise + + concat(): RType extends BufferOrString ? Promise : never + destroy(er?: any): void + pipe(dest: W, opts?: PipeOptions): W + unpipe(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 + [Symbol.asyncIterator](): AsyncIterator +} + +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 extends string + ? StringOptions + : T extends Buffer + ? BufferOptions + : ObjectModeOptions diff --git a/node_modules/minipass/index.js b/node_modules/minipass/index.js index 1835dd9bcf512..e8797aab6cc27 100644 --- a/node_modules/minipass/index.js +++ b/node_modules/minipass/index.js @@ -5,7 +5,6 @@ const proc = typeof process === 'object' && process ? process : { } const EE = require('events') const Stream = require('stream') -const Yallist = require('yallist') const SD = require('string_decoder').StringDecoder const EOF = Symbol('EOF') @@ -27,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' @@ -51,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 @@ -66,6 +103,7 @@ 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 @@ -105,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') @@ -123,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 @@ -137,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) @@ -158,27 +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 we are still flowing after flushing the buffer we can emit the - // chunk otherwise we have to buffer it. - this.flowing - ? this.emit('data', chunk) - : this[BUFFERPUSH](chunk) - } else + if (this.flowing) + this.emit('data', chunk) + else this[BUFFERPUSH](chunk) if (this[BUFFERLENGTH] !== 0) this.emit('readable') if (cb) - cb() + fn(cb) return this.flowing } @@ -187,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[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 = new Yallist([ - Array.from(this.buffer).join('') - ]) - else - this.buffer = new Yallist([ - Buffer.concat(Array.from(this.buffer), this[BUFFERLENGTH]) - ]) - } + if (this[OBJECTMODE]) + n = null - return this[READ](n || null, this.buffer.head.value) - } finally { - this[MAYBE_EMIT_END]() + 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 } @@ -291,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] () { @@ -299,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() } @@ -325,35 +378,52 @@ module.exports = class Minipass extends Stream { 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) - } else if (ev === 'error' && this[EMITTED_ERROR]) { + 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 () { @@ -376,65 +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() @@ -536,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]) diff --git a/node_modules/minipass/package.json b/node_modules/minipass/package.json index 1728e5108c4c2..47c90a4fcf4a7 100644 --- a/node_modules/minipass/package.json +++ b/node_modules/minipass/package.json @@ -1,15 +1,19 @@ { "name": "minipass", - "version": "3.1.6", + "version": "3.3.4", "description": "minimal implementation of a PassThrough stream", "main": "index.js", "dependencies": { "yallist": "^4.0.0" }, "devDependencies": { + "@types/node": "^17.0.41", "end-of-stream": "^1.4.0", - "tap": "^15.0.9", - "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" }, "scripts": { "test": "tap", @@ -28,6 +32,7 @@ "author": "Isaac Z. Schlueter (http://blog.izs.me/)", "license": "ISC", "files": [ + "index.d.ts", "index.js" ], "tap": { @@ -35,5 +40,16 @@ }, "engines": { "node": ">=8" + }, + "prettier": { + "semi": false, + "printWidth": 80, + "tabWidth": 2, + "useTabs": false, + "singleQuote": true, + "jsxSingleQuote": false, + "bracketSameLine": true, + "arrowParens": "avoid", + "endOfLine": "lf" } } diff --git a/package-lock.json b/package-lock.json index b04fe367ca08f..73e0d0dd918b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4775,9 +4775,10 @@ "license": "MIT" }, "node_modules/minipass": { - "version": "3.1.6", + "version": "3.3.4", + "resolved": "https://registry.npmjs.org/minipass/-/minipass-3.3.4.tgz", + "integrity": "sha512-I9WPbWHCGu8W+6k1ZiGpPu0GkoKBeorkfKNuAFBNS1HNFJvke82sxvI5bzcCNpWPorkOO5QQ+zomzzwRxejXiw==", "inBundle": true, - "license": "ISC", "dependencies": { "yallist": "^4.0.0" },