diff options
Diffstat (limited to 'node_modules/minipass/index.js')
-rw-r--r-- | node_modules/minipass/index.js | 80 |
1 files changed, 36 insertions, 44 deletions
diff --git a/node_modules/minipass/index.js b/node_modules/minipass/index.js index 56cbd665d..c072352d4 100644 --- a/node_modules/minipass/index.js +++ b/node_modules/minipass/index.js @@ -1,6 +1,5 @@ 'use strict' const EE = require('events') -const Stream = require('stream') const Yallist = require('yallist') const SD = require('string_decoder').StringDecoder @@ -30,6 +29,12 @@ const ASYNCITERATOR = doIter && Symbol.asyncIterator const ITERATOR = doIter && Symbol.iterator || Symbol('iterator not implemented') +// Buffer in node 4.x < 4.5.0 doesn't have working Buffer.from +// or Buffer.alloc, and Buffer in node 10 deprecated the ctor. +// .M, this is fine .\^/M.. +const B = Buffer.alloc ? Buffer + : /* istanbul ignore next */ require('safe-buffer').Buffer + // events that mean 'the stream is over' // these are treated specially, and re-emitted // if they are listened for after emitting. @@ -44,9 +49,9 @@ const isArrayBuffer = b => b instanceof ArrayBuffer || b.constructor.name === 'ArrayBuffer' && b.byteLength >= 0 -const isArrayBufferView = b => !Buffer.isBuffer(b) && ArrayBuffer.isView(b) +const isArrayBufferView = b => !B.isBuffer(b) && ArrayBuffer.isView(b) -module.exports = class Minipass extends Stream { +module.exports = class Minipass extends EE { constructor (options) { super() this[FLOWING] = false @@ -97,7 +102,7 @@ module.exports = class Minipass extends Stream { } get objectMode () { return this[OBJECTMODE] } - set objectMode (om) { this[OBJECTMODE] = this[OBJECTMODE] || !!om } + set objectMode (ॐ ) { this[OBJECTMODE] = this[OBJECTMODE] || !!ॐ } write (chunk, encoding, cb) { if (this[EOF]) @@ -121,11 +126,11 @@ module.exports = class Minipass extends Stream { // at some point in the future, we may want to do the opposite! // leave strings and buffers as-is // anything else switches us into object mode - if (!this[OBJECTMODE] && !Buffer.isBuffer(chunk)) { + if (!this[OBJECTMODE] && !B.isBuffer(chunk)) { if (isArrayBufferView(chunk)) - chunk = Buffer.from(chunk.buffer, chunk.byteOffset, chunk.byteLength) + chunk = B.from(chunk.buffer, chunk.byteOffset, chunk.byteLength) else if (isArrayBuffer(chunk)) - chunk = Buffer.from(chunk) + chunk = B.from(chunk) else if (typeof chunk !== 'string') // use the setter so we throw if we have encoding set this.objectMode = true @@ -134,11 +139,12 @@ module.exports = class Minipass extends Stream { // this ensures at this point that the chunk is a buffer or string // don't buffer it up or send it to the decoder if (!this.objectMode && !chunk.length) { + const ret = this.flowing if (this[BUFFERLENGTH] !== 0) this.emit('readable') if (cb) cb() - return this.flowing + return ret } // fast-path writing strings of same encoding to a stream with @@ -146,30 +152,22 @@ module.exports = class Minipass extends Stream { if (typeof chunk === 'string' && !this[OBJECTMODE] && // unless it is a string already ready for us to use !(encoding === this[ENCODING] && !this[DECODER].lastNeed)) { - chunk = Buffer.from(chunk, encoding) + chunk = B.from(chunk, encoding) } - if (Buffer.isBuffer(chunk) && this[ENCODING]) + if (B.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. + try { + return this.flowing + ? (this.emit('data', chunk), this.flowing) + : (this[BUFFERPUSH](chunk), false) + } finally { if (this[BUFFERLENGTH] !== 0) - this[FLUSH](true) - this.emit('data', chunk) - } else - this[BUFFERPUSH](chunk) - - if (this[BUFFERLENGTH] !== 0) - this.emit('readable') - - if (cb) - cb() - - return this.flowing + this.emit('readable') + if (cb) + cb() + } } read (n) { @@ -190,7 +188,7 @@ module.exports = class Minipass extends Stream { ]) else this.buffer = new Yallist([ - Buffer.concat(Array.from(this.buffer), this[BUFFERLENGTH]) + B.concat(Array.from(this.buffer), this[BUFFERLENGTH]) ]) } @@ -293,10 +291,10 @@ module.exports = class Minipass extends Stream { return this.buffer.shift() } - [FLUSH] (noDrain) { + [FLUSH] () { do {} while (this[FLUSHCHUNK](this[BUFFERSHIFT]())) - if (!noDrain && !this.buffer.length && !this[EOF]) + if (!this.buffer.length && !this[EOF]) this.emit('drain') } @@ -425,17 +423,12 @@ module.exports = class Minipass extends Stream { // const all = await stream.collect() collect () { const buf = [] - if (!this[OBJECTMODE]) - buf.dataLength = 0 - // set the promise first, in case an error is raised - // by triggering the flow here. - const p = this.promise() + buf.dataLength = 0 this.on('data', c => { buf.push(c) - if (!this[OBJECTMODE]) - buf.dataLength += c.length + buf.dataLength += c.length }) - return p.then(() => buf) + return this.promise().then(() => buf) } // const data = await stream.concat() @@ -445,7 +438,7 @@ module.exports = class Minipass extends Stream { : this.collect().then(buf => this[OBJECTMODE] ? Promise.reject(new Error('cannot concat in objectMode')) - : this[ENCODING] ? buf.join('') : Buffer.concat(buf, buf.dataLength)) + : this[ENCODING] ? buf.join('') : B.concat(buf, buf.dataLength)) } // stream.promise().then(() => done, er => emitted error) @@ -536,10 +529,9 @@ module.exports = class Minipass extends Stream { } static isStream (s) { - return !!s && (s instanceof Minipass || s instanceof Stream || - s instanceof EE && ( - typeof s.pipe === 'function' || // readable - (typeof s.write === 'function' && typeof s.end === 'function') // writable - )) + return !!s && (s instanceof Minipass || s instanceof EE && ( + typeof s.pipe === 'function' || // readable + (typeof s.write === 'function' && typeof s.end === 'function') // writable + )) } } |