readable-stream
Advanced tools
Comparing version 3.0.6 to 3.1.0
'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } | ||
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } | ||
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } | ||
var codes = {}; | ||
@@ -26,3 +32,5 @@ | ||
var NodeError = function (_Base) { | ||
var NodeError = | ||
/*#__PURE__*/ | ||
function (_Base) { | ||
_inherits(NodeError, _Base); | ||
@@ -33,3 +41,3 @@ | ||
return _possibleConstructorReturn(this, (NodeError.__proto__ || Object.getPrototypeOf(NodeError)).call(this, getMessage(arg1, arg2, arg3))); | ||
return _possibleConstructorReturn(this, _getPrototypeOf(NodeError).call(this, getMessage(arg1, arg2, arg3))); | ||
} | ||
@@ -42,7 +50,6 @@ | ||
NodeError.prototype.code = code; | ||
codes[code] = NodeError; | ||
} | ||
} // https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js | ||
// https://github.com/nodejs/node/blob/v10.8.0/lib/internal/errors.js | ||
function oneOf(expected, thing) { | ||
@@ -54,20 +61,21 @@ if (Array.isArray(expected)) { | ||
}); | ||
if (len > 2) { | ||
return 'one of ' + thing + ' ' + expected.slice(0, len - 1).join(', ') + ', or ' + expected[len - 1]; | ||
return "one of ".concat(thing, " ").concat(expected.slice(0, len - 1).join(', '), ", or ") + expected[len - 1]; | ||
} else if (len === 2) { | ||
return 'one of ' + thing + ' ' + expected[0] + ' or ' + expected[1]; | ||
return "one of ".concat(thing, " ").concat(expected[0], " or ").concat(expected[1]); | ||
} else { | ||
return 'of ' + thing + ' ' + expected[0]; | ||
return "of ".concat(thing, " ").concat(expected[0]); | ||
} | ||
} else { | ||
return 'of ' + thing + ' ' + String(expected); | ||
return "of ".concat(thing, " ").concat(String(expected)); | ||
} | ||
} | ||
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith | ||
function startsWith(str, search, pos) { | ||
return str.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search; | ||
} | ||
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith | ||
function endsWith(str, search, this_len) { | ||
@@ -77,6 +85,7 @@ if (this_len === undefined || this_len > str.length) { | ||
} | ||
return str.substring(this_len - search.length, this_len) === search; | ||
} | ||
} // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes | ||
function includes(str, search, start) { | ||
@@ -99,3 +108,4 @@ if (typeof start !== 'number') { | ||
// determiner: 'must be' or 'must not be' | ||
var determiner = void 0; | ||
var determiner; | ||
if (typeof expected === 'string' && startsWith(expected, 'not ')) { | ||
@@ -108,12 +118,13 @@ determiner = 'must not be'; | ||
var msg = void 0; | ||
var msg; | ||
if (endsWith(name, ' argument')) { | ||
// For cases like 'first argument' | ||
msg = 'The ' + name + ' ' + determiner + ' ' + oneOf(expected, 'type'); | ||
msg = "The ".concat(name, " ").concat(determiner, " ").concat(oneOf(expected, 'type')); | ||
} else { | ||
var type = includes(name, '.') ? 'property' : 'argument'; | ||
msg = 'The "' + name + '" ' + type + ' ' + determiner + ' ' + oneOf(expected, 'type'); | ||
msg = "The \"".concat(name, "\" ").concat(type, " ").concat(determiner, " ").concat(oneOf(expected, 'type')); | ||
} | ||
msg += '. Received type ' + (typeof actual === 'undefined' ? 'undefined' : _typeof(actual)); | ||
msg += ". Received type ".concat(_typeof(actual)); | ||
return msg; | ||
@@ -137,3 +148,2 @@ }, TypeError); | ||
createErrorType('ERR_STREAM_UNSHIFT_AFTER_END_EVENT', 'stream.unshift() after end event'); | ||
module.exports.codes = codes; |
@@ -21,3 +21,2 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// a duplex stream is just a stream that is both readable and writable. | ||
@@ -27,5 +26,3 @@ // Since JS doesn't have multiple prototypal inheritance, this class | ||
// Writable. | ||
'use strict'; | ||
/*<replacement>*/ | ||
@@ -35,11 +32,16 @@ | ||
var keys = []; | ||
for (var key in obj) { | ||
keys.push(key); | ||
}return keys; | ||
} | ||
return keys; | ||
}; | ||
/*</replacement>*/ | ||
module.exports = Duplex; | ||
var Readable = require('./_stream_readable'); | ||
var Writable = require('./_stream_writable'); | ||
@@ -52,2 +54,3 @@ | ||
var keys = objectKeys(Writable.prototype); | ||
for (var v = 0; v < keys.length; v++) { | ||
@@ -61,3 +64,2 @@ var method = keys[v]; | ||
if (!(this instanceof Duplex)) return new Duplex(options); | ||
Readable.call(this, options); | ||
@@ -69,3 +71,2 @@ Writable.call(this, options); | ||
if (options.readable === false) this.readable = false; | ||
if (options.writable === false) this.writable = false; | ||
@@ -89,3 +90,2 @@ | ||
}); | ||
Object.defineProperty(Duplex.prototype, 'writableBuffer', { | ||
@@ -100,3 +100,2 @@ // making it explicit this property is not enumerable | ||
}); | ||
Object.defineProperty(Duplex.prototype, 'writableLength', { | ||
@@ -110,11 +109,9 @@ // making it explicit this property is not enumerable | ||
} | ||
}); | ||
}); // the no-half-open enforcer | ||
// the no-half-open enforcer | ||
function onend() { | ||
// If the writable side ended, then we're ok. | ||
if (this._writableState.ended) return; | ||
if (this._writableState.ended) return; // no more data can be written. | ||
// But allow more writes to happen in this tick. | ||
// no more data can be written. | ||
// But allow more writes to happen in this tick. | ||
process.nextTick(onEndNT, this); | ||
@@ -136,2 +133,3 @@ } | ||
} | ||
return this._readableState.destroyed && this._writableState.destroyed; | ||
@@ -144,6 +142,6 @@ }, | ||
return; | ||
} | ||
} // backward compatibility, the user is explicitly | ||
// managing destroyed | ||
// backward compatibility, the user is explicitly | ||
// managing destroyed | ||
this._readableState.destroyed = value; | ||
@@ -150,0 +148,0 @@ this._writableState.destroyed = value; |
@@ -21,7 +21,5 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// a passthrough stream. | ||
// basically just the most minimal sort of Transform stream. | ||
// Every written chunk gets output as-is. | ||
'use strict'; | ||
@@ -37,3 +35,2 @@ | ||
if (!(this instanceof PassThrough)) return new PassThrough(options); | ||
Transform.call(this, options); | ||
@@ -40,0 +37,0 @@ } |
@@ -21,8 +21,7 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
'use strict'; | ||
module.exports = Readable; | ||
/*<replacement>*/ | ||
/*<replacement>*/ | ||
var Duplex; | ||
@@ -32,4 +31,4 @@ /*</replacement>*/ | ||
Readable.ReadableState = ReadableState; | ||
/*<replacement>*/ | ||
/*<replacement>*/ | ||
var EE = require('events').EventEmitter; | ||
@@ -43,17 +42,26 @@ | ||
/*<replacement>*/ | ||
var Stream = require('./internal/streams/stream'); | ||
/*</replacement>*/ | ||
var Buffer = require('buffer').Buffer; | ||
var OurUint8Array = global.Uint8Array || function () {}; | ||
function _uint8ArrayToBuffer(chunk) { | ||
return Buffer.from(chunk); | ||
} | ||
function _isUint8Array(obj) { | ||
return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; | ||
} | ||
/*<replacement>*/ | ||
/*<replacement>*/ | ||
var debugUtil = require('util'); | ||
var debug = void 0; | ||
var debug; | ||
if (debugUtil && debugUtil.debuglog) { | ||
@@ -66,3 +74,5 @@ debug = debugUtil.debuglog('stream'); | ||
var BufferList = require('./internal/streams/buffer_list'); | ||
var destroyImpl = require('./internal/streams/destroy'); | ||
@@ -80,10 +90,8 @@ | ||
var _require2 = require('../experimentalWarning'), | ||
emitExperimentalWarning = _require2.emitExperimentalWarning; | ||
emitExperimentalWarning = _require2.emitExperimentalWarning; // Lazy loaded to improve the startup performance. | ||
// Lazy loaded to improve the startup performance. | ||
var StringDecoder; | ||
var createReadableStreamAsyncIterator; | ||
var StringDecoder = void 0; | ||
var ReadableAsyncIterator = void 0; | ||
require('inherits')(Readable, Stream); | ||
@@ -96,8 +104,7 @@ | ||
// event emitter implementation with them. | ||
if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn); | ||
// This is a hack to make sure that our error handler is attached before any | ||
if (typeof emitter.prependListener === 'function') return emitter.prependListener(event, fn); // This is a hack to make sure that our error handler is attached before any | ||
// userland ones. NEVER DO THIS. This is here only because this code needs | ||
// to continue to work with older versions of Node.js that do not include | ||
// the prependListener() method. The goal is to eventually remove this hack. | ||
if (!emitter._events || !emitter._events[event]) emitter.on(event, fn);else if (Array.isArray(emitter._events[event])) emitter._events[event].unshift(fn);else emitter._events[event] = [fn, emitter._events[event]]; | ||
@@ -108,6 +115,3 @@ } | ||
Duplex = Duplex || require('./_stream_duplex'); | ||
options = options || {}; | ||
// Duplex streams are both readable and writable, but share | ||
options = options || {}; // Duplex streams are both readable and writable, but share | ||
// the same options object. | ||
@@ -117,17 +121,14 @@ // However, some cases require setting options to different | ||
// These options can be provided separately as readableXXX and writableXXX. | ||
if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; | ||
// object stream flag. Used to make read(n) ignore n and to | ||
if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; // object stream flag. Used to make read(n) ignore n and to | ||
// make all the buffer merging and length checks go away | ||
this.objectMode = !!options.objectMode; | ||
if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode; | ||
// the point at which it stops calling _read() to fill the buffer | ||
if (isDuplex) this.objectMode = this.objectMode || !!options.readableObjectMode; // the point at which it stops calling _read() to fill the buffer | ||
// Note: 0 is a valid value, means "don't call _read preemptively ever" | ||
this.highWaterMark = getHighWaterMark(this, options, 'readableHighWaterMark', isDuplex); | ||
// A linked list is used to store data chunks instead of an array because the | ||
this.highWaterMark = getHighWaterMark(this, options, 'readableHighWaterMark', isDuplex); // A linked list is used to store data chunks instead of an array because the | ||
// linked list can remove elements from the beginning faster than | ||
// array.shift() | ||
this.buffer = new BufferList(); | ||
@@ -140,36 +141,29 @@ this.length = 0; | ||
this.endEmitted = false; | ||
this.reading = false; | ||
// a flag to be able to tell if the event 'readable'/'data' is emitted | ||
this.reading = false; // a flag to be able to tell if the event 'readable'/'data' is emitted | ||
// immediately, or on a later tick. We set this to true at first, because | ||
// any actions that shouldn't happen until "later" should generally also | ||
// not happen before the first read call. | ||
this.sync = true; | ||
// whenever we return null, then we set a flag to say | ||
this.sync = true; // whenever we return null, then we set a flag to say | ||
// that we're awaiting a 'readable' event emission. | ||
this.needReadable = false; | ||
this.emittedReadable = false; | ||
this.readableListening = false; | ||
this.resumeScheduled = false; | ||
this.resumeScheduled = false; // Should close be emitted on destroy. Defaults to true. | ||
// Should close be emitted on destroy. Defaults to true. | ||
this.emitClose = options.emitClose !== false; | ||
this.emitClose = options.emitClose !== false; // has it been destroyed | ||
// has it been destroyed | ||
this.destroyed = false; | ||
// Crypto is kind of old and crusty. Historically, its default string | ||
this.destroyed = false; // Crypto is kind of old and crusty. Historically, its default string | ||
// encoding is 'binary' so we have to make this configurable. | ||
// Everything else in the universe uses 'utf8', though. | ||
this.defaultEncoding = options.defaultEncoding || 'utf8'; | ||
// the number of writers that are awaiting a drain event in .pipe()s | ||
this.awaitDrain = 0; | ||
this.defaultEncoding = options.defaultEncoding || 'utf8'; // the number of writers that are awaiting a drain event in .pipe()s | ||
// if true, a maybeReadMore has been scheduled | ||
this.awaitDrain = 0; // if true, a maybeReadMore has been scheduled | ||
this.readingMore = false; | ||
this.decoder = null; | ||
this.encoding = null; | ||
if (options.encoding) { | ||
@@ -184,12 +178,8 @@ if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; | ||
Duplex = Duplex || require('./_stream_duplex'); | ||
if (!(this instanceof Readable)) return new Readable(options); // Checking for a Stream.Duplex instance is faster here instead of inside | ||
// the ReadableState constructor, at least with V8 6.5 | ||
if (!(this instanceof Readable)) return new Readable(options); | ||
// Checking for a Stream.Duplex instance is faster here instead of inside | ||
// the ReadableState constructor, at least with V8 6.5 | ||
var isDuplex = this instanceof Duplex; | ||
this._readableState = new ReadableState(options, this, isDuplex); // legacy | ||
this._readableState = new ReadableState(options, this, isDuplex); | ||
// legacy | ||
this.readable = true; | ||
@@ -199,3 +189,2 @@ | ||
if (typeof options.read === 'function') this._read = options.read; | ||
if (typeof options.destroy === 'function') this._destroy = options.destroy; | ||
@@ -216,2 +205,3 @@ } | ||
} | ||
return this._readableState.destroyed; | ||
@@ -224,20 +214,20 @@ }, | ||
return; | ||
} | ||
} // backward compatibility, the user is explicitly | ||
// managing destroyed | ||
// backward compatibility, the user is explicitly | ||
// managing destroyed | ||
this._readableState.destroyed = value; | ||
} | ||
}); | ||
Readable.prototype.destroy = destroyImpl.destroy; | ||
Readable.prototype._undestroy = destroyImpl.undestroy; | ||
Readable.prototype._destroy = function (err, cb) { | ||
cb(err); | ||
}; | ||
// Manually shove something into the read() buffer. | ||
}; // Manually shove something into the read() buffer. | ||
// This returns true if the highWaterMark has not been hit yet, | ||
// similar to how Writable.write() returns true if you should | ||
// write() some more. | ||
Readable.prototype.push = function (chunk, encoding) { | ||
@@ -250,2 +240,3 @@ var state = this._readableState; | ||
encoding = encoding || state.defaultEncoding; | ||
if (encoding !== state.encoding) { | ||
@@ -255,2 +246,3 @@ chunk = Buffer.from(chunk, encoding); | ||
} | ||
skipChunkCheck = true; | ||
@@ -263,5 +255,5 @@ } | ||
return readableAddChunk(this, chunk, encoding, false, skipChunkCheck); | ||
}; | ||
}; // Unshift should *always* be something directly out of read() | ||
// Unshift should *always* be something directly out of read() | ||
Readable.prototype.unshift = function (chunk) { | ||
@@ -274,2 +266,3 @@ return readableAddChunk(this, chunk, null, true, false); | ||
var state = stream._readableState; | ||
if (chunk === null) { | ||
@@ -281,2 +274,3 @@ state.reading = false; | ||
if (!skipChunkCheck) er = chunkInvalid(state, chunk); | ||
if (er) { | ||
@@ -297,2 +291,3 @@ stream.emit('error', er); | ||
state.reading = false; | ||
if (state.decoder && !encoding) { | ||
@@ -309,7 +304,7 @@ chunk = state.decoder.write(chunk); | ||
} | ||
} | ||
// We can push more data if we are below the highWaterMark. | ||
} // We can push more data if we are below the highWaterMark. | ||
// Also, if we have no data yet, we can stand some more bytes. | ||
// This is to work around cases where hwm=0, such as the repl. | ||
return !state.ended && (state.length < state.highWaterMark || state.length === 0); | ||
@@ -326,5 +321,5 @@ } | ||
if (addToFront) state.buffer.unshift(chunk);else state.buffer.push(chunk); | ||
if (state.needReadable) emitReadable(stream); | ||
} | ||
maybeReadMore(stream, state); | ||
@@ -335,5 +330,7 @@ } | ||
var er; | ||
if (!_isUint8Array(chunk) && typeof chunk !== 'string' && chunk !== undefined && !state.objectMode) { | ||
er = new ERR_INVALID_ARG_TYPE('chunk', ['string', 'Buffer', 'Uint8Array'], chunk); | ||
} | ||
return er; | ||
@@ -344,15 +341,16 @@ } | ||
return this._readableState.flowing === false; | ||
}; | ||
}; // backwards compatibility. | ||
// backwards compatibility. | ||
Readable.prototype.setEncoding = function (enc) { | ||
if (!StringDecoder) StringDecoder = require('string_decoder/').StringDecoder; | ||
this._readableState.decoder = new StringDecoder(enc); | ||
// if setEncoding(null), decoder.encoding equals utf8 | ||
this._readableState.decoder = new StringDecoder(enc); // if setEncoding(null), decoder.encoding equals utf8 | ||
this._readableState.encoding = this._readableState.decoder.encoding; | ||
return this; | ||
}; | ||
}; // Don't raise the hwm > 8MB | ||
// Don't raise the hwm > 8MB | ||
var MAX_HWM = 0x800000; | ||
function computeNewHighWaterMark(n) { | ||
@@ -372,18 +370,21 @@ if (n >= MAX_HWM) { | ||
} | ||
return n; | ||
} | ||
} // This function is designed to be inlinable, so please take care when making | ||
// changes to the function body. | ||
// This function is designed to be inlinable, so please take care when making | ||
// changes to the function body. | ||
function howMuchToRead(n, state) { | ||
if (n <= 0 || state.length === 0 && state.ended) return 0; | ||
if (state.objectMode) return 1; | ||
if (n !== n) { | ||
// Only flow one buffer at a time | ||
if (state.flowing && state.length) return state.buffer.head.data.length;else return state.length; | ||
} | ||
// If we're asking for more than the current hwm, then raise the hwm. | ||
} // If we're asking for more than the current hwm, then raise the hwm. | ||
if (n > state.highWaterMark) state.highWaterMark = computeNewHighWaterMark(n); | ||
if (n <= state.length) return n; | ||
// Don't have enough | ||
if (n <= state.length) return n; // Don't have enough | ||
if (!state.ended) { | ||
@@ -393,6 +394,7 @@ state.needReadable = true; | ||
} | ||
return state.length; | ||
} | ||
} // you can override either this method, or the async _read(n) below. | ||
// you can override either this method, or the async _read(n) below. | ||
Readable.prototype.read = function (n) { | ||
@@ -403,8 +405,6 @@ debug('read', n); | ||
var nOrig = n; | ||
if (n !== 0) state.emittedReadable = false; | ||
// if we're doing read(0) to trigger a readable event, but we | ||
if (n !== 0) state.emittedReadable = false; // if we're doing read(0) to trigger a readable event, but we | ||
// already have a bunch of data in the buffer, then just trigger | ||
// the 'readable' event and move on. | ||
if (n === 0 && state.needReadable && ((state.highWaterMark !== 0 ? state.length >= state.highWaterMark : state.length > 0) || state.ended)) { | ||
@@ -416,11 +416,8 @@ debug('read: emitReadable', state.length, state.ended); | ||
n = howMuchToRead(n, state); | ||
n = howMuchToRead(n, state); // if we've ended, and we're now clear, then finish it up. | ||
// if we've ended, and we're now clear, then finish it up. | ||
if (n === 0 && state.ended) { | ||
if (state.length === 0) endReadable(this); | ||
return null; | ||
} | ||
// All the actual chunk generation logic needs to be | ||
} // All the actual chunk generation logic needs to be | ||
// *below* the call to _read. The reason is that in certain | ||
@@ -446,15 +443,15 @@ // synthetic stream cases, such as passthrough streams, _read | ||
// 3. Actually pull the requested chunks out of the buffer and return. | ||
// if we need a readable event, then we need to do some reading. | ||
// if we need a readable event, then we need to do some reading. | ||
var doRead = state.needReadable; | ||
debug('need readable', doRead); | ||
debug('need readable', doRead); // if we currently have less than the highWaterMark, then also read some | ||
// if we currently have less than the highWaterMark, then also read some | ||
if (state.length === 0 || state.length - n < state.highWaterMark) { | ||
doRead = true; | ||
debug('length less than watermark', doRead); | ||
} | ||
} // however, if we've ended, then there's no point, and if we're already | ||
// reading, then it's unnecessary. | ||
// however, if we've ended, then there's no point, and if we're already | ||
// reading, then it's unnecessary. | ||
if (state.ended || state.reading) { | ||
@@ -466,10 +463,11 @@ doRead = false; | ||
state.reading = true; | ||
state.sync = true; | ||
// if the length is currently zero, then we *need* a readable event. | ||
if (state.length === 0) state.needReadable = true; | ||
// call internal read method | ||
state.sync = true; // if the length is currently zero, then we *need* a readable event. | ||
if (state.length === 0) state.needReadable = true; // call internal read method | ||
this._read(state.highWaterMark); | ||
state.sync = false; | ||
// If _read pushed data synchronously, then `reading` will be false, | ||
state.sync = false; // If _read pushed data synchronously, then `reading` will be false, | ||
// and we need to re-evaluate how much data we can return to the user. | ||
if (!state.reading) n = howMuchToRead(nOrig, state); | ||
@@ -492,5 +490,4 @@ } | ||
// as soon as we *do* get something into the buffer. | ||
if (!state.ended) state.needReadable = true; | ||
if (!state.ended) state.needReadable = true; // If we tried to read() past the EOF, then emit end on the next tick. | ||
// If we tried to read() past the EOF, then emit end on the next tick. | ||
if (nOrig !== n && state.ended) endReadable(this); | ||
@@ -500,3 +497,2 @@ } | ||
if (ret !== null) this.emit('data', ret); | ||
return ret; | ||
@@ -507,4 +503,6 @@ }; | ||
if (state.ended) return; | ||
if (state.decoder) { | ||
var chunk = state.decoder.end(); | ||
if (chunk && chunk.length) { | ||
@@ -515,2 +513,3 @@ state.buffer.push(chunk); | ||
} | ||
state.ended = true; | ||
@@ -526,2 +525,3 @@ | ||
state.needReadable = false; | ||
if (!state.emittedReadable) { | ||
@@ -532,10 +532,11 @@ state.emittedReadable = true; | ||
} | ||
} | ||
// Don't emit readable right away in sync mode, because this can trigger | ||
} // Don't emit readable right away in sync mode, because this can trigger | ||
// another read() call => stack overflow. This way, it might trigger | ||
// a nextTick recursion warning, but that's not so bad. | ||
function emitReadable(stream) { | ||
var state = stream._readableState; | ||
state.needReadable = false; | ||
if (!state.emittedReadable) { | ||
@@ -551,7 +552,6 @@ debug('emitReadable', state.flowing); | ||
debug('emitReadable_', state.destroyed, state.length, state.ended); | ||
if (!state.destroyed && (state.length || state.ended)) { | ||
stream.emit('readable'); | ||
} | ||
// The stream needs another readable event if | ||
} // The stream needs another readable event if | ||
// 1. It is not flowing, as the flow mechanism will take | ||
@@ -562,7 +562,7 @@ // care of it. | ||
// another readable later. | ||
state.needReadable = !state.flowing && !state.ended && state.length <= state.highWaterMark; | ||
flow(stream); | ||
} | ||
// at this point, the user has presumably seen the 'readable' event, | ||
} // at this point, the user has presumably seen the 'readable' event, | ||
// and called read() to consume some data. that may have triggered | ||
@@ -573,2 +573,4 @@ // in turn another _read(n) call, in which case reading = true if | ||
// then go ahead and try to read some more preemptively. | ||
function maybeReadMore(stream, state) { | ||
@@ -583,16 +585,17 @@ if (!state.readingMore) { | ||
var len = state.length; | ||
while (!state.reading && !state.ended && state.length < state.highWaterMark) { | ||
debug('maybeReadMore read 0'); | ||
stream.read(0); | ||
if (len === state.length) | ||
// didn't get any data, stop spinning. | ||
if (len === state.length) // didn't get any data, stop spinning. | ||
break;else len = state.length; | ||
} | ||
state.readingMore = false; | ||
} | ||
// abstract method. to be overridden in specific implementation classes. | ||
} // abstract method. to be overridden in specific implementation classes. | ||
// call cb(er, data) where data is <= n in length. | ||
// for virtual (non-string, non-buffer) streams, "length" is somewhat | ||
// arbitrary, and perhaps not very meaningful. | ||
Readable.prototype._read = function (n) { | ||
@@ -610,5 +613,7 @@ this.emit('error', new ERR_METHOD_NOT_IMPLEMENTED('_read()')); | ||
break; | ||
case 1: | ||
state.pipes = [state.pipes, dest]; | ||
break; | ||
default: | ||
@@ -618,13 +623,13 @@ state.pipes.push(dest); | ||
} | ||
state.pipesCount += 1; | ||
debug('pipe count=%d opts=%j', state.pipesCount, pipeOpts); | ||
var doEnd = (!pipeOpts || pipeOpts.end !== false) && dest !== process.stdout && dest !== process.stderr; | ||
var endFn = doEnd ? onend : unpipe; | ||
if (state.endEmitted) process.nextTick(endFn);else src.once('end', endFn); | ||
dest.on('unpipe', onunpipe); | ||
dest.on('unpipe', onunpipe); | ||
function onunpipe(readable, unpipeInfo) { | ||
debug('onunpipe'); | ||
if (readable === src) { | ||
@@ -641,15 +646,15 @@ if (unpipeInfo && unpipeInfo.hasUnpiped === false) { | ||
dest.end(); | ||
} | ||
// when the dest drains, it reduces the awaitDrain counter | ||
} // when the dest drains, it reduces the awaitDrain counter | ||
// on the source. This would be more elegant with a .once() | ||
// handler in flow(), but adding and removing repeatedly is | ||
// too slow. | ||
var ondrain = pipeOnDrain(src); | ||
dest.on('drain', ondrain); | ||
var cleanedUp = false; | ||
var cleanedUp = false; | ||
function cleanup() { | ||
debug('cleanup'); | ||
// cleanup event handlers once the pipe is broken | ||
debug('cleanup'); // cleanup event handlers once the pipe is broken | ||
dest.removeListener('close', onclose); | ||
@@ -663,6 +668,3 @@ dest.removeListener('finish', onfinish); | ||
src.removeListener('data', ondata); | ||
cleanedUp = true; | ||
// if the reader is waiting for a drain event from this | ||
cleanedUp = true; // if the reader is waiting for a drain event from this | ||
// specific writer, then it would cause it to never start | ||
@@ -672,2 +674,3 @@ // flowing again. | ||
// If we don't know, then assume that we are waiting for one. | ||
if (state.awaitDrain && (!dest._writableState || dest._writableState.needDrain)) ondrain(); | ||
@@ -677,2 +680,3 @@ } | ||
src.on('data', ondata); | ||
function ondata(chunk) { | ||
@@ -682,2 +686,3 @@ debug('ondata'); | ||
debug('dest.write', ret); | ||
if (ret === false) { | ||
@@ -692,8 +697,9 @@ // If the user unpiped during `dest.write()`, it is possible | ||
} | ||
src.pause(); | ||
} | ||
} | ||
} // if the dest has an error, then stop piping into it. | ||
// however, don't suppress the throwing behavior for this. | ||
// if the dest has an error, then stop piping into it. | ||
// however, don't suppress the throwing behavior for this. | ||
function onerror(er) { | ||
@@ -704,8 +710,7 @@ debug('onerror', er); | ||
if (EElistenerCount(dest, 'error') === 0) dest.emit('error', er); | ||
} | ||
} // Make sure our error handler is attached before userland ones. | ||
// Make sure our error handler is attached before userland ones. | ||
prependListener(dest, 'error', onerror); | ||
// Both close and finish should trigger unpipe, but only once. | ||
prependListener(dest, 'error', onerror); // Both close and finish should trigger unpipe, but only once. | ||
function onclose() { | ||
@@ -715,3 +720,5 @@ dest.removeListener('finish', onfinish); | ||
} | ||
dest.once('close', onclose); | ||
function onfinish() { | ||
@@ -722,2 +729,3 @@ debug('onfinish'); | ||
} | ||
dest.once('finish', onfinish); | ||
@@ -728,8 +736,7 @@ | ||
src.unpipe(dest); | ||
} | ||
} // tell the dest that it's being piped to | ||
// tell the dest that it's being piped to | ||
dest.emit('pipe', src); | ||
// start the flow if it hasn't been started already. | ||
dest.emit('pipe', src); // start the flow if it hasn't been started already. | ||
if (!state.flowing) { | ||
@@ -748,2 +755,3 @@ debug('pipe resume'); | ||
if (state.awaitDrain) state.awaitDrain--; | ||
if (state.awaitDrain === 0 && EElistenerCount(src, 'data')) { | ||
@@ -758,15 +766,13 @@ state.flowing = true; | ||
var state = this._readableState; | ||
var unpipeInfo = { hasUnpiped: false }; | ||
var unpipeInfo = { | ||
hasUnpiped: false | ||
}; // if we're not piping anywhere, then do nothing. | ||
// if we're not piping anywhere, then do nothing. | ||
if (state.pipesCount === 0) return this; | ||
if (state.pipesCount === 0) return this; // just one destination. most common case. | ||
// just one destination. most common case. | ||
if (state.pipesCount === 1) { | ||
// passed in one, but it's not the right one. | ||
if (dest && dest !== state.pipes) return this; | ||
if (!dest) dest = state.pipes; // got a match. | ||
if (!dest) dest = state.pipes; | ||
// got a match. | ||
state.pipes = null; | ||
@@ -777,5 +783,4 @@ state.pipesCount = 0; | ||
return this; | ||
} | ||
} // slow case. multiple pipe destinations. | ||
// slow case. multiple pipe destinations. | ||
@@ -791,21 +796,22 @@ if (!dest) { | ||
for (var i = 0; i < len; i++) { | ||
dests[i].emit('unpipe', this, { hasUnpiped: false }); | ||
}return this; | ||
} | ||
dests[i].emit('unpipe', this, { | ||
hasUnpiped: false | ||
}); | ||
} | ||
// try to find the right one. | ||
return this; | ||
} // try to find the right one. | ||
var index = indexOf(state.pipes, dest); | ||
if (index === -1) return this; | ||
state.pipes.splice(index, 1); | ||
state.pipesCount -= 1; | ||
if (state.pipesCount === 1) state.pipes = state.pipes[0]; | ||
dest.emit('unpipe', this, unpipeInfo); | ||
return this; | ||
}; | ||
}; // set up data events if they are asked for | ||
// Ensure readable listeners eventually get something | ||
// set up data events if they are asked for | ||
// Ensure readable listeners eventually get something | ||
Readable.prototype.on = function (ev, fn) { | ||
@@ -818,5 +824,4 @@ var res = Stream.prototype.on.call(this, ev, fn); | ||
// a few lines down. This is needed to support once('readable'). | ||
state.readableListening = this.listenerCount('readable') > 0; | ||
state.readableListening = this.listenerCount('readable') > 0; // Try start flowing on next tick if stream isn't explicitly paused | ||
// Try start flowing on next tick if stream isn't explicitly paused | ||
if (state.flowing !== false) this.resume(); | ||
@@ -829,2 +834,3 @@ } else if (ev === 'readable') { | ||
debug('on readable', state.length, state.reading); | ||
if (state.length) { | ||
@@ -840,2 +846,3 @@ emitReadable(this); | ||
}; | ||
Readable.prototype.addListener = Readable.prototype.on; | ||
@@ -876,5 +883,4 @@ | ||
function updateReadableListening(self) { | ||
self._readableState.readableListening = self.listenerCount('readable') > 0; | ||
self._readableState.readableListening = self.listenerCount('readable') > 0; // crude way to check if we should resume | ||
// crude way to check if we should resume | ||
if (self.listenerCount('data') > 0) { | ||
@@ -888,16 +894,18 @@ self.resume(); | ||
self.read(0); | ||
} | ||
} // pause() and resume() are remnants of the legacy readable stream API | ||
// If the user uses them, then switch into old mode. | ||
// pause() and resume() are remnants of the legacy readable stream API | ||
// If the user uses them, then switch into old mode. | ||
Readable.prototype.resume = function () { | ||
var state = this._readableState; | ||
if (!state.flowing) { | ||
debug('resume'); | ||
// we flow only if there is no one listening | ||
debug('resume'); // we flow only if there is no one listening | ||
// for readable, but we still have to call | ||
// resume() | ||
state.flowing = !state.readableListening; | ||
resume(this, state); | ||
} | ||
return this; | ||
@@ -915,2 +923,3 @@ }; | ||
debug('resume', state.reading); | ||
if (!state.reading) { | ||
@@ -928,2 +937,3 @@ stream.read(0); | ||
debug('call pause flowing=%j', this._readableState.flowing); | ||
if (this._readableState.flowing !== false) { | ||
@@ -934,2 +944,3 @@ debug('pause'); | ||
} | ||
return this; | ||
@@ -941,8 +952,11 @@ }; | ||
debug('flow', state.flowing); | ||
while (state.flowing && stream.read() !== null) {} | ||
} | ||
// wrap an old-style stream as the async data source. | ||
while (state.flowing && stream.read() !== null) { | ||
; | ||
} | ||
} // wrap an old-style stream as the async data source. | ||
// This is *not* part of the readable stream interface. | ||
// It is an ugly unfortunate mess of history. | ||
Readable.prototype.wrap = function (stream) { | ||
@@ -953,5 +967,5 @@ var _this = this; | ||
var paused = false; | ||
stream.on('end', function () { | ||
debug('wrapped end'); | ||
if (state.decoder && !state.ended) { | ||
@@ -964,11 +978,10 @@ var chunk = state.decoder.end(); | ||
}); | ||
stream.on('data', function (chunk) { | ||
debug('wrapped data'); | ||
if (state.decoder) chunk = state.decoder.write(chunk); | ||
if (state.decoder) chunk = state.decoder.write(chunk); // don't skip over falsy values in objectMode | ||
// don't skip over falsy values in objectMode | ||
if (state.objectMode && (chunk === null || chunk === undefined)) return;else if (!state.objectMode && (!chunk || !chunk.length)) return; | ||
var ret = _this.push(chunk); | ||
if (!ret) { | ||
@@ -978,6 +991,5 @@ paused = true; | ||
} | ||
}); | ||
}); // proxy all the other methods. | ||
// important when wrapping filters and duplexes. | ||
// proxy all the other methods. | ||
// important when wrapping filters and duplexes. | ||
for (var i in stream) { | ||
@@ -991,13 +1003,14 @@ if (this[i] === undefined && typeof stream[i] === 'function') { | ||
} | ||
} | ||
} // proxy certain important events. | ||
// proxy certain important events. | ||
for (var n = 0; n < kProxyEvents.length; n++) { | ||
stream.on(kProxyEvents[n], this.emit.bind(this, kProxyEvents[n])); | ||
} | ||
} // when we try to consume some more bytes, simply unpause the | ||
// underlying stream. | ||
// when we try to consume some more bytes, simply unpause the | ||
// underlying stream. | ||
this._read = function (n) { | ||
debug('wrapped _read', n); | ||
if (paused) { | ||
@@ -1015,4 +1028,8 @@ paused = false; | ||
emitExperimentalWarning('Readable[Symbol.asyncIterator]'); | ||
if (ReadableAsyncIterator === undefined) ReadableAsyncIterator = require('./internal/streams/async_iterator'); | ||
return new ReadableAsyncIterator(this); | ||
if (createReadableStreamAsyncIterator === undefined) { | ||
createReadableStreamAsyncIterator = require('./internal/streams/async_iterator'); | ||
} | ||
return createReadableStreamAsyncIterator(this); | ||
}; | ||
@@ -1030,3 +1047,2 @@ } | ||
}); | ||
Object.defineProperty(Readable.prototype, 'readableBuffer', { | ||
@@ -1041,3 +1057,2 @@ // making it explicit this property is not enumerable | ||
}); | ||
Object.defineProperty(Readable.prototype, 'readableFlowing', { | ||
@@ -1056,7 +1071,5 @@ // making it explicit this property is not enumerable | ||
} | ||
}); | ||
}); // exposed for testing purposes only. | ||
// exposed for testing purposes only. | ||
Readable._fromList = fromList; | ||
Object.defineProperty(Readable.prototype, 'readableLength', { | ||
@@ -1070,12 +1083,10 @@ // making it explicit this property is not enumerable | ||
} | ||
}); | ||
// Pluck off n bytes from an array of buffers. | ||
}); // Pluck off n bytes from an array of buffers. | ||
// Length is the combined lengths of all the buffers in the list. | ||
// This function is designed to be inlinable, so please take care when making | ||
// changes to the function body. | ||
function fromList(n, state) { | ||
// nothing buffered | ||
if (state.length === 0) return null; | ||
var ret; | ||
@@ -1090,3 +1101,2 @@ if (state.objectMode) ret = state.buffer.shift();else if (!n || n >= state.length) { | ||
} | ||
return ret; | ||
@@ -1097,4 +1107,4 @@ } | ||
var state = stream._readableState; | ||
debug('endReadable', state.endEmitted); | ||
debug('endReadable', state.endEmitted); | ||
if (!state.endEmitted) { | ||
@@ -1107,5 +1117,4 @@ state.ended = true; | ||
function endReadableNT(state, stream) { | ||
debug('endReadableNT', state.endEmitted, state.length); | ||
debug('endReadableNT', state.endEmitted, state.length); // Check that we didn't get one last unshift. | ||
// Check that we didn't get one last unshift. | ||
if (!state.endEmitted && state.length === 0) { | ||
@@ -1122,3 +1131,4 @@ state.endEmitted = true; | ||
} | ||
return -1; | ||
} |
@@ -21,3 +21,2 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// a transform stream is a readable/writable stream where you do | ||
@@ -64,3 +63,2 @@ // something with the data. Sometimes it's called a "filter", | ||
// the results of the previous transformed chunk were consumed. | ||
'use strict'; | ||
@@ -83,3 +81,2 @@ | ||
ts.transforming = false; | ||
var cb = ts.writecb; | ||
@@ -93,10 +90,8 @@ | ||
ts.writecb = null; | ||
if (data != null) // single equals check for both `null` and `undefined` | ||
this.push(data); | ||
cb(er); | ||
var rs = this._readableState; | ||
rs.reading = false; | ||
if (rs.needReadable || rs.length < rs.highWaterMark) { | ||
@@ -109,5 +104,3 @@ this._read(rs.highWaterMark); | ||
if (!(this instanceof Transform)) return new Transform(options); | ||
Duplex.call(this, options); | ||
this._transformState = { | ||
@@ -120,10 +113,8 @@ afterTransform: afterTransform.bind(this), | ||
writeencoding: null | ||
}; | ||
}; // start out asking for a readable event once data is transformed. | ||
// start out asking for a readable event once data is transformed. | ||
this._readableState.needReadable = true; | ||
// we have implemented the _read method, and done the other things | ||
this._readableState.needReadable = true; // we have implemented the _read method, and done the other things | ||
// that Readable wants before the first _read call, so unset the | ||
// sync guard flag. | ||
this._readableState.sync = false; | ||
@@ -133,7 +124,6 @@ | ||
if (typeof options.transform === 'function') this._transform = options.transform; | ||
if (typeof options.flush === 'function') this._flush = options.flush; | ||
} | ||
} // When the writable side finishes, then flush out anything remaining. | ||
// When the writable side finishes, then flush out anything remaining. | ||
this.on('prefinish', prefinish); | ||
@@ -157,5 +147,3 @@ } | ||
return Duplex.prototype.push.call(this, chunk, encoding); | ||
}; | ||
// This is the part where you do stuff! | ||
}; // This is the part where you do stuff! | ||
// override this function in implementation classes. | ||
@@ -170,2 +158,4 @@ // 'chunk' is an input chunk. | ||
// never call cb(), then you'll never get another chunk. | ||
Transform.prototype._transform = function (chunk, encoding, cb) { | ||
@@ -180,2 +170,3 @@ cb(new ERR_METHOD_NOT_IMPLEMENTED('_transform()')); | ||
ts.writeencoding = encoding; | ||
if (!ts.transforming) { | ||
@@ -185,7 +176,7 @@ var rs = this._readableState; | ||
} | ||
}; | ||
// Doesn't matter what the args are here. | ||
}; // Doesn't matter what the args are here. | ||
// _transform does all the work. | ||
// That we got here means that the readable side wants more data. | ||
Transform.prototype._read = function (n) { | ||
@@ -196,2 +187,3 @@ var ts = this._transformState; | ||
ts.transforming = true; | ||
this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); | ||
@@ -213,13 +205,10 @@ } else { | ||
if (er) return stream.emit('error', er); | ||
if (data != null) // single equals check for both `null` and `undefined` | ||
stream.push(data); | ||
// TODO(BridgeAR): Write a test for these two error cases | ||
stream.push(data); // TODO(BridgeAR): Write a test for these two error cases | ||
// if there's nothing in the write buffer, then that means | ||
// that nothing more will ever be provided | ||
if (stream._writableState.length) throw new ERR_TRANSFORM_WITH_LENGTH_0(); | ||
if (stream._transformState.transforming) throw new ERR_TRANSFORM_ALREADY_TRANSFORMING(); | ||
return stream.push(null); | ||
} |
@@ -21,12 +21,10 @@ // Copyright Joyent, Inc. and other Node contributors. | ||
// USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// A bit simpler than readable streams. | ||
// Implement an async ._write(chunk, encoding, cb), and it'll handle all | ||
// the drain event emission and buffering. | ||
'use strict'; | ||
module.exports = Writable; | ||
/* <replacement> */ | ||
/* <replacement> */ | ||
function WriteReq(chunk, encoding, cb) { | ||
@@ -37,6 +35,6 @@ this.chunk = chunk; | ||
this.next = null; | ||
} | ||
} // It seems a linked list but it is not | ||
// there will be only 2 of these for each stream | ||
// It seems a linked list but it is not | ||
// there will be only 2 of these for each stream | ||
function CorkedRequest(state) { | ||
@@ -47,2 +45,3 @@ var _this = this; | ||
this.entry = null; | ||
this.finish = function () { | ||
@@ -55,2 +54,4 @@ onCorkedFinish(_this, state); | ||
/*<replacement>*/ | ||
var Duplex; | ||
@@ -60,4 +61,4 @@ /*</replacement>*/ | ||
Writable.WritableState = WritableState; | ||
/*<replacement>*/ | ||
/*<replacement>*/ | ||
var internalUtil = { | ||
@@ -69,10 +70,15 @@ deprecate: require('util-deprecate') | ||
/*<replacement>*/ | ||
var Stream = require('./internal/streams/stream'); | ||
/*</replacement>*/ | ||
var Buffer = require('buffer').Buffer; | ||
var OurUint8Array = global.Uint8Array || function () {}; | ||
function _uint8ArrayToBuffer(chunk) { | ||
return Buffer.from(chunk); | ||
} | ||
function _isUint8Array(obj) { | ||
@@ -103,6 +109,3 @@ return Buffer.isBuffer(obj) || obj instanceof OurUint8Array; | ||
Duplex = Duplex || require('./_stream_duplex'); | ||
options = options || {}; | ||
// Duplex streams are both readable and writable, but share | ||
options = options || {}; // Duplex streams are both readable and writable, but share | ||
// the same options object. | ||
@@ -112,96 +115,75 @@ // However, some cases require setting options to different | ||
// These options can be provided separately as readableXXX and writableXXX. | ||
if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; | ||
// object stream flag to indicate whether or not this stream | ||
if (typeof isDuplex !== 'boolean') isDuplex = stream instanceof Duplex; // object stream flag to indicate whether or not this stream | ||
// contains buffers or objects. | ||
this.objectMode = !!options.objectMode; | ||
if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; | ||
// the point at which write() starts returning false | ||
if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; // the point at which write() starts returning false | ||
// Note: 0 is a valid value, means that we always return false if | ||
// the entire buffer is not flushed immediately on write() | ||
this.highWaterMark = getHighWaterMark(this, options, 'writableHighWaterMark', isDuplex); | ||
// if _final has been called | ||
this.finalCalled = false; | ||
this.highWaterMark = getHighWaterMark(this, options, 'writableHighWaterMark', isDuplex); // if _final has been called | ||
// drain event flag. | ||
this.needDrain = false; | ||
// at the start of calling end() | ||
this.ending = false; | ||
// when end() has been called, and returned | ||
this.ended = false; | ||
// when 'finish' is emitted | ||
this.finished = false; | ||
this.finalCalled = false; // drain event flag. | ||
// has it been destroyed | ||
this.destroyed = false; | ||
this.needDrain = false; // at the start of calling end() | ||
// should we decode strings into buffers before passing to _write? | ||
this.ending = false; // when end() has been called, and returned | ||
this.ended = false; // when 'finish' is emitted | ||
this.finished = false; // has it been destroyed | ||
this.destroyed = false; // should we decode strings into buffers before passing to _write? | ||
// this is here so that some node-core streams can optimize string | ||
// handling at a lower level. | ||
var noDecode = options.decodeStrings === false; | ||
this.decodeStrings = !noDecode; | ||
// Crypto is kind of old and crusty. Historically, its default string | ||
this.decodeStrings = !noDecode; // Crypto is kind of old and crusty. Historically, its default string | ||
// encoding is 'binary' so we have to make this configurable. | ||
// Everything else in the universe uses 'utf8', though. | ||
this.defaultEncoding = options.defaultEncoding || 'utf8'; | ||
// not an actual buffer we keep track of, but a measurement | ||
this.defaultEncoding = options.defaultEncoding || 'utf8'; // not an actual buffer we keep track of, but a measurement | ||
// of how much we're waiting to get pushed to some underlying | ||
// socket or file. | ||
this.length = 0; | ||
// a flag to see when we're in the middle of a write. | ||
this.writing = false; | ||
this.length = 0; // a flag to see when we're in the middle of a write. | ||
// when true all writes will be buffered until .uncork() call | ||
this.corked = 0; | ||
this.writing = false; // when true all writes will be buffered until .uncork() call | ||
// a flag to be able to tell if the onwrite cb is called immediately, | ||
this.corked = 0; // a flag to be able to tell if the onwrite cb is called immediately, | ||
// or on a later tick. We set this to true at first, because any | ||
// actions that shouldn't happen until "later" should generally also | ||
// not happen before the first write call. | ||
this.sync = true; | ||
// a flag to know if we're processing previously buffered items, which | ||
this.sync = true; // a flag to know if we're processing previously buffered items, which | ||
// may call the _write() callback in the same tick, so that we don't | ||
// end up in an overlapped onwrite situation. | ||
this.bufferProcessing = false; | ||
// the callback that's passed to _write(chunk,cb) | ||
this.bufferProcessing = false; // the callback that's passed to _write(chunk,cb) | ||
this.onwrite = function (er) { | ||
onwrite(stream, er); | ||
}; | ||
}; // the callback that the user supplies to write(chunk,encoding,cb) | ||
// the callback that the user supplies to write(chunk,encoding,cb) | ||
this.writecb = null; | ||
// the amount that is being written when _write is called. | ||
this.writecb = null; // the amount that is being written when _write is called. | ||
this.writelen = 0; | ||
this.bufferedRequest = null; | ||
this.lastBufferedRequest = null; | ||
// number of pending user-supplied write callbacks | ||
this.lastBufferedRequest = null; // number of pending user-supplied write callbacks | ||
// this must be 0 before 'finish' can be emitted | ||
this.pendingcb = 0; | ||
// emit prefinish if the only thing we're waiting for is _write cbs | ||
this.pendingcb = 0; // emit prefinish if the only thing we're waiting for is _write cbs | ||
// This is relevant for synchronous Transform streams | ||
this.prefinished = false; | ||
// True if the error was already emitted and should not be thrown again | ||
this.errorEmitted = false; | ||
this.prefinished = false; // True if the error was already emitted and should not be thrown again | ||
// Should close be emitted on destroy. Defaults to true. | ||
this.emitClose = options.emitClose !== false; | ||
this.errorEmitted = false; // Should close be emitted on destroy. Defaults to true. | ||
// count buffered requests | ||
this.bufferedRequestCount = 0; | ||
this.emitClose = options.emitClose !== false; // count buffered requests | ||
// allocate the first CorkedRequest, there is always | ||
this.bufferedRequestCount = 0; // allocate the first CorkedRequest, there is always | ||
// one allocated and free to use, and we maintain at most two | ||
this.corkedRequestsFree = new CorkedRequest(this); | ||
@@ -213,2 +195,3 @@ } | ||
var out = []; | ||
while (current) { | ||
@@ -218,2 +201,3 @@ out.push(current); | ||
} | ||
return out; | ||
@@ -230,7 +214,8 @@ }; | ||
} catch (_) {} | ||
})(); | ||
})(); // Test _writableState for inheritance to account for Duplex streams, | ||
// whose prototype chain only points to Readable. | ||
// Test _writableState for inheritance to account for Duplex streams, | ||
// whose prototype chain only points to Readable. | ||
var realHasInstance; | ||
if (typeof Symbol === 'function' && Symbol.hasInstance && typeof Function.prototype[Symbol.hasInstance] === 'function') { | ||
@@ -242,3 +227,2 @@ realHasInstance = Function.prototype[Symbol.hasInstance]; | ||
if (this !== Writable) return false; | ||
return object && object._writableState instanceof WritableState; | ||
@@ -254,21 +238,15 @@ } | ||
function Writable(options) { | ||
Duplex = Duplex || require('./_stream_duplex'); | ||
// Writable ctor is applied to Duplexes, too. | ||
Duplex = Duplex || require('./_stream_duplex'); // Writable ctor is applied to Duplexes, too. | ||
// `realHasInstance` is necessary because using plain `instanceof` | ||
// would return false, as no `_writableState` property is attached. | ||
// Trying to use the custom `instanceof` for Writable here will also break the | ||
// Node.js LazyTransform implementation, which has a non-trivial getter for | ||
// `_writableState` that would lead to infinite recursion. | ||
// Checking for a Stream.Duplex instance is faster here instead of inside | ||
// the WritableState constructor, at least with V8 6.5 | ||
var isDuplex = this instanceof Duplex; | ||
if (!isDuplex && !realHasInstance.call(Writable, this)) return new Writable(options); | ||
this._writableState = new WritableState(options, this, isDuplex); // legacy. | ||
this._writableState = new WritableState(options, this, isDuplex); | ||
// legacy. | ||
this.writable = true; | ||
@@ -278,7 +256,4 @@ | ||
if (typeof options.write === 'function') this._write = options.write; | ||
if (typeof options.writev === 'function') this._writev = options.writev; | ||
if (typeof options.destroy === 'function') this._destroy = options.destroy; | ||
if (typeof options.final === 'function') this._final = options.final; | ||
@@ -288,5 +263,5 @@ } | ||
Stream.call(this); | ||
} | ||
} // Otherwise people can pipe Writable streams, which is just wrong. | ||
// Otherwise people can pipe Writable streams, which is just wrong. | ||
Writable.prototype.pipe = function () { | ||
@@ -297,11 +272,11 @@ this.emit('error', new ERR_STREAM_CANNOT_PIPE()); | ||
function writeAfterEnd(stream, cb) { | ||
var er = new ERR_STREAM_WRITE_AFTER_END(); | ||
// TODO: defer error events consistently everywhere, not just the cb | ||
var er = new ERR_STREAM_WRITE_AFTER_END(); // TODO: defer error events consistently everywhere, not just the cb | ||
stream.emit('error', er); | ||
process.nextTick(cb, er); | ||
} | ||
// Checks that a user-supplied chunk is valid, especially for the particular | ||
} // Checks that a user-supplied chunk is valid, especially for the particular | ||
// mode the stream is in. Currently this means that `null` is never accepted | ||
// and undefined/non-string values are only allowed in object mode. | ||
function validChunk(stream, state, chunk, cb) { | ||
@@ -315,2 +290,3 @@ var er; | ||
} | ||
if (er) { | ||
@@ -321,2 +297,3 @@ stream.emit('error', er); | ||
} | ||
return true; | ||
@@ -328,2 +305,3 @@ } | ||
var ret = false; | ||
var isBuf = !state.objectMode && _isUint8Array(chunk); | ||
@@ -341,5 +319,3 @@ | ||
if (isBuf) encoding = 'buffer';else if (!encoding) encoding = state.defaultEncoding; | ||
if (typeof cb !== 'function') cb = nop; | ||
if (state.ending) writeAfterEnd(this, cb);else if (isBuf || validChunk(this, state, chunk, cb)) { | ||
@@ -349,3 +325,2 @@ state.pendingcb++; | ||
} | ||
return ret; | ||
@@ -363,3 +338,2 @@ }; | ||
state.corked--; | ||
if (!state.writing && !state.corked && !state.bufferProcessing && state.bufferedRequest) clearBuffer(this, state); | ||
@@ -391,2 +365,3 @@ } | ||
} | ||
return chunk; | ||
@@ -403,10 +378,10 @@ } | ||
} | ||
}); | ||
// if we're already writing something, then just put this | ||
}); // if we're already writing something, then just put this | ||
// in the queue, and wait our turn. Otherwise, call _write | ||
// If we return false, then we need a drain event, so set that flag. | ||
function writeOrBuffer(stream, state, isBuf, chunk, encoding, cb) { | ||
if (!isBuf) { | ||
var newChunk = decodeChunk(state, chunk, encoding); | ||
if (chunk !== newChunk) { | ||
@@ -418,8 +393,7 @@ isBuf = true; | ||
} | ||
var len = state.objectMode ? 1 : chunk.length; | ||
state.length += len; | ||
var ret = state.length < state.highWaterMark; // we must ensure that previous needDrain will not be reset to false. | ||
var ret = state.length < state.highWaterMark; | ||
// we must ensure that previous needDrain will not be reset to false. | ||
if (!ret) state.needDrain = true; | ||
@@ -436,2 +410,3 @@ | ||
}; | ||
if (last) { | ||
@@ -442,2 +417,3 @@ last.next = state.lastBufferedRequest; | ||
} | ||
state.bufferedRequestCount += 1; | ||
@@ -466,5 +442,5 @@ } else { | ||
// to avoid piling up things on the stack | ||
process.nextTick(cb, er); | ||
// this can emit finish, and it will always happen | ||
process.nextTick(cb, er); // this can emit finish, and it will always happen | ||
// after error | ||
process.nextTick(finishMaybe, stream, state); | ||
@@ -478,5 +454,5 @@ stream._writableState.errorEmitted = true; | ||
stream._writableState.errorEmitted = true; | ||
stream.emit('error', er); | ||
// this can emit finish, but finish must | ||
stream.emit('error', er); // this can emit finish, but finish must | ||
// always follow error | ||
finishMaybe(stream, state); | ||
@@ -497,7 +473,4 @@ } | ||
var cb = state.writecb; | ||
if (typeof cb !== 'function') throw new ERR_MULTIPLE_CALLBACK(); | ||
onwriteStateUpdate(state); | ||
if (er) onwriteError(stream, state, sync, er, cb);else { | ||
@@ -524,7 +497,7 @@ // Check if we're actually ready to finish, but don't emit yet | ||
finishMaybe(stream, state); | ||
} | ||
// Must force callback to be called on nextTick, so that we don't | ||
} // Must force callback to be called on nextTick, so that we don't | ||
// emit 'drain' before the write() consumer gets the 'false' return | ||
// value, and has a chance to attach a 'drain' listener. | ||
function onwriteDrain(stream, state) { | ||
@@ -535,5 +508,5 @@ if (state.length === 0 && state.needDrain) { | ||
} | ||
} | ||
} // if there's something in the buffer waiting, then process it | ||
// if there's something in the buffer waiting, then process it | ||
function clearBuffer(stream, state) { | ||
@@ -549,5 +522,5 @@ state.bufferProcessing = true; | ||
holder.entry = entry; | ||
var count = 0; | ||
var allBuffers = true; | ||
while (entry) { | ||
@@ -559,10 +532,10 @@ buffer[count] = entry; | ||
} | ||
buffer.allBuffers = allBuffers; | ||
doWrite(stream, state, true, state.length, buffer, '', holder.finish); // doWrite is almost always async, defer these to save a bit of time | ||
// as the hot path ends with doWrite | ||
doWrite(stream, state, true, state.length, buffer, '', holder.finish); | ||
// doWrite is almost always async, defer these to save a bit of time | ||
// as the hot path ends with doWrite | ||
state.pendingcb++; | ||
state.lastBufferedRequest = null; | ||
if (holder.next) { | ||
@@ -574,2 +547,3 @@ state.corkedRequestsFree = holder.next; | ||
} | ||
state.bufferedRequestCount = 0; | ||
@@ -583,10 +557,9 @@ } else { | ||
var len = state.objectMode ? 1 : chunk.length; | ||
doWrite(stream, state, false, len, chunk, encoding, cb); | ||
entry = entry.next; | ||
state.bufferedRequestCount--; | ||
// if we didn't call the onwrite immediately, then | ||
state.bufferedRequestCount--; // if we didn't call the onwrite immediately, then | ||
// it means that we need to wait until it does. | ||
// also, that means that the chunk and cb are currently | ||
// being processed, so move the buffer counter past them. | ||
if (state.writing) { | ||
@@ -622,13 +595,11 @@ break; | ||
if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); | ||
if (chunk !== null && chunk !== undefined) this.write(chunk, encoding); // .end() fully uncorks | ||
// .end() fully uncorks | ||
if (state.corked) { | ||
state.corked = 1; | ||
this.uncork(); | ||
} | ||
} // ignore unnecessary end() calls. | ||
// ignore unnecessary end() calls. | ||
if (!state.ending) endWritable(this, state, cb); | ||
return this; | ||
@@ -650,8 +621,11 @@ }; | ||
} | ||
function callFinal(stream, state) { | ||
stream._final(function (err) { | ||
state.pendingcb--; | ||
if (err) { | ||
stream.emit('error', err); | ||
} | ||
state.prefinished = true; | ||
@@ -662,2 +636,3 @@ stream.emit('prefinish'); | ||
} | ||
function prefinish(stream, state) { | ||
@@ -678,4 +653,6 @@ if (!state.prefinished && !state.finalCalled) { | ||
var need = needFinish(state); | ||
if (need) { | ||
prefinish(stream, state); | ||
if (state.pendingcb === 0) { | ||
@@ -686,2 +663,3 @@ state.finished = true; | ||
} | ||
return need; | ||
@@ -693,5 +671,7 @@ } | ||
finishMaybe(stream, state); | ||
if (cb) { | ||
if (state.finished) process.nextTick(cb);else stream.once('finish', cb); | ||
} | ||
state.ended = true; | ||
@@ -704,2 +684,3 @@ stream.writable = false; | ||
corkReq.entry = null; | ||
while (entry) { | ||
@@ -710,5 +691,5 @@ var cb = entry.callback; | ||
entry = entry.next; | ||
} | ||
} // reuse the free corkReq. | ||
// reuse the free corkReq. | ||
state.corkedRequestsFree.next = corkReq; | ||
@@ -726,2 +707,3 @@ } | ||
} | ||
return this._writableState.destroyed; | ||
@@ -734,14 +716,14 @@ }, | ||
return; | ||
} | ||
} // backward compatibility, the user is explicitly | ||
// managing destroyed | ||
// backward compatibility, the user is explicitly | ||
// managing destroyed | ||
this._writableState.destroyed = value; | ||
} | ||
}); | ||
Writable.prototype.destroy = destroyImpl.destroy; | ||
Writable.prototype._undestroy = destroyImpl.undestroy; | ||
Writable.prototype._destroy = function (err, cb) { | ||
cb(err); | ||
}; |
'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _Object$setPrototypeO; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var finished = require('./end-of-stream'); | ||
var kLastResolve = Symbol('lastResolve'); | ||
@@ -15,16 +17,17 @@ var kLastReject = Symbol('lastReject'); | ||
var AsyncIteratorRecord = function AsyncIteratorRecord(value, done) { | ||
_classCallCheck(this, AsyncIteratorRecord); | ||
function createIterResult(value, done) { | ||
return { | ||
value: value, | ||
done: done | ||
}; | ||
} | ||
this.done = done; | ||
this.value = value; | ||
}; | ||
function readAndResolve(iter) { | ||
var resolve = iter[kLastResolve]; | ||
if (resolve !== null) { | ||
var data = iter[kStream].read(); | ||
// we defer if data is null | ||
var data = iter[kStream].read(); // we defer if data is null | ||
// we can be expecting either 'end' or | ||
// 'error' | ||
if (data !== null) { | ||
@@ -34,3 +37,3 @@ iter[kLastPromise] = null; | ||
iter[kLastReject] = null; | ||
resolve(new AsyncIteratorRecord(data, false)); | ||
resolve(createIterResult(data, false)); | ||
} | ||
@@ -46,26 +49,2 @@ } | ||
function onEnd(iter) { | ||
var resolve = iter[kLastResolve]; | ||
if (resolve !== null) { | ||
iter[kLastPromise] = null; | ||
iter[kLastResolve] = null; | ||
iter[kLastReject] = null; | ||
resolve(new AsyncIteratorRecord(null, true)); | ||
} | ||
iter[kEnded] = true; | ||
} | ||
function onError(iter, err) { | ||
var reject = iter[kLastReject]; | ||
// reject if we are waiting for data in the Promise | ||
// returned by next() and store the error | ||
if (reject !== null) { | ||
iter[kLastPromise] = null; | ||
iter[kLastResolve] = null; | ||
iter[kLastReject] = null; | ||
reject(err); | ||
} | ||
iter[kError] = err; | ||
} | ||
function wrapForNext(lastPromise, iter) { | ||
@@ -79,40 +58,15 @@ return function (resolve, reject) { | ||
var ReadableAsyncIterator = function () { | ||
function ReadableAsyncIterator(stream) { | ||
var AsyncIteratorPrototype = Object.getPrototypeOf(function () {}); | ||
var ReadableStreamAsyncIteratorPrototype = Object.setPrototypeOf((_Object$setPrototypeO = { | ||
get stream() { | ||
return this[kStream]; | ||
}, | ||
next: function () { | ||
var _this = this; | ||
_classCallCheck(this, ReadableAsyncIterator); | ||
this[kStream] = stream; | ||
this[kLastResolve] = null; | ||
this[kLastReject] = null; | ||
this[kError] = null; | ||
this[kEnded] = false; | ||
this[kLastPromise] = null; | ||
stream.on('readable', onReadable.bind(null, this)); | ||
stream.on('end', onEnd.bind(null, this)); | ||
stream.on('error', onError.bind(null, this)); | ||
// the function passed to new Promise | ||
// is cached so we avoid allocating a new | ||
// closure at every run | ||
this[kHandlePromise] = function (resolve, reject) { | ||
var data = _this[kStream].read(); | ||
if (data) { | ||
_this[kLastPromise] = null; | ||
_this[kLastResolve] = null; | ||
_this[kLastReject] = null; | ||
resolve(new AsyncIteratorRecord(data, false)); | ||
} else { | ||
_this[kLastResolve] = resolve; | ||
_this[kLastReject] = reject; | ||
} | ||
}; | ||
} | ||
ReadableAsyncIterator.prototype.next = function next() { | ||
// if we have detected an error in the meanwhile | ||
// reject straight away | ||
var error = this[kError]; | ||
if (error !== null) { | ||
@@ -123,11 +77,27 @@ return Promise.reject(error); | ||
if (this[kEnded]) { | ||
return Promise.resolve(new AsyncIteratorRecord(null, true)); | ||
return Promise.resolve(createIterResult(null, true)); | ||
} | ||
// if we have multiple next() calls | ||
if (this[kStream].destroyed) { | ||
// We need to defer via nextTick because if .destroy(err) is | ||
// called, the error will be emitted via nextTick, and | ||
// we cannot guarantee that there is no error lingering around | ||
// waiting to be emitted. | ||
return new Promise(function (resolve, reject) { | ||
process.nextTick(function () { | ||
if (_this[kError]) { | ||
reject(_this[kError]); | ||
} else { | ||
resolve(createIterResult(null, true)); | ||
} | ||
}); | ||
}); | ||
} // if we have multiple next() calls | ||
// we will wait for the previous Promise to finish | ||
// this logic is optimized to support for await loops, | ||
// where next() is only called once at a time | ||
var lastPromise = this[kLastPromise]; | ||
var promise = void 0; | ||
var promise; | ||
@@ -140,4 +110,5 @@ if (lastPromise) { | ||
var data = this[kStream].read(); | ||
if (data !== null) { | ||
return Promise.resolve(new AsyncIteratorRecord(data, false)); | ||
return Promise.resolve(createIterResult(data, false)); | ||
} | ||
@@ -149,33 +120,92 @@ | ||
this[kLastPromise] = promise; | ||
return promise; | ||
}; | ||
} | ||
}, _defineProperty(_Object$setPrototypeO, Symbol.asyncIterator, function () { | ||
return this; | ||
}), _defineProperty(_Object$setPrototypeO, "return", function () { | ||
var _this2 = this; | ||
ReadableAsyncIterator.prototype.return = function _return() { | ||
var _this2 = this; | ||
// destroy(err, cb) is a private API | ||
// we can guarantee we have that here, because we control the | ||
// Readable class this is attached to | ||
return new Promise(function (resolve, reject) { | ||
_this2[kStream].destroy(null, function (err) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
// destroy(err, cb) is a private API | ||
// we can guarantee we have that here, because we control the | ||
// Readable class this is attached to | ||
return new Promise(function (resolve, reject) { | ||
_this2[kStream].destroy(null, function (err) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(new AsyncIteratorRecord(null, true)); | ||
}); | ||
resolve(createIterResult(null, true)); | ||
}); | ||
}; | ||
}); | ||
}), _Object$setPrototypeO), AsyncIteratorPrototype); | ||
_createClass(ReadableAsyncIterator, [{ | ||
key: 'stream', | ||
get: function () { | ||
return this[kStream]; | ||
var createReadableStreamAsyncIterator = function (stream) { | ||
var _Object$create; | ||
var iterator = Object.create(ReadableStreamAsyncIteratorPrototype, (_Object$create = {}, _defineProperty(_Object$create, kStream, { | ||
value: stream, | ||
writable: true | ||
}), _defineProperty(_Object$create, kLastResolve, { | ||
value: null, | ||
writable: true | ||
}), _defineProperty(_Object$create, kLastReject, { | ||
value: null, | ||
writable: true | ||
}), _defineProperty(_Object$create, kError, { | ||
value: null, | ||
writable: true | ||
}), _defineProperty(_Object$create, kEnded, { | ||
value: stream._readableState.endEmitted, | ||
writable: true | ||
}), _defineProperty(_Object$create, kLastPromise, { | ||
value: null, | ||
writable: true | ||
}), _defineProperty(_Object$create, kHandlePromise, { | ||
value: function (resolve, reject) { | ||
var data = iterator[kStream].read(); | ||
if (data) { | ||
iterator[kLastPromise] = null; | ||
iterator[kLastResolve] = null; | ||
iterator[kLastReject] = null; | ||
resolve(createIterResult(data, false)); | ||
} else { | ||
iterator[kLastResolve] = resolve; | ||
iterator[kLastReject] = reject; | ||
} | ||
}, | ||
writable: true | ||
}), _Object$create)); | ||
finished(stream, function (err) { | ||
if (err && err.code !== 'ERR_STREAM_PREMATURE_CLOSE') { | ||
var reject = iterator[kLastReject]; // reject if we are waiting for data in the Promise | ||
// returned by next() and store the error | ||
if (reject !== null) { | ||
iterator[kLastPromise] = null; | ||
iterator[kLastResolve] = null; | ||
iterator[kLastReject] = null; | ||
reject(err); | ||
} | ||
iterator[kError] = err; | ||
return; | ||
} | ||
}]); | ||
return ReadableAsyncIterator; | ||
}(); | ||
var resolve = iterator[kLastResolve]; | ||
module.exports = ReadableAsyncIterator; | ||
if (resolve !== null) { | ||
iterator[kLastPromise] = null; | ||
iterator[kLastResolve] = null; | ||
iterator[kLastReject] = null; | ||
resolve(createIterResult(null, true)); | ||
} | ||
iterator[kEnded] = true; | ||
}); | ||
stream.on('readable', onReadable.bind(null, iterator)); | ||
return iterator; | ||
}; | ||
module.exports = createReadableStreamAsyncIterator; |
'use strict'; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
var _require = require('buffer'), | ||
@@ -11,3 +13,3 @@ Buffer = _require.Buffer; | ||
var custom = inspect && custom || 'inspect'; | ||
var custom = inspect && inspect.custom || 'inspect'; | ||
@@ -18,6 +20,6 @@ function copyBuffer(src, target, offset) { | ||
module.exports = function () { | ||
module.exports = | ||
/*#__PURE__*/ | ||
function () { | ||
function BufferList() { | ||
_classCallCheck(this, BufferList); | ||
this.head = null; | ||
@@ -28,4 +30,9 @@ this.tail = null; | ||
BufferList.prototype.push = function push(v) { | ||
var entry = { data: v, next: null }; | ||
var _proto = BufferList.prototype; | ||
_proto.push = function push(v) { | ||
var entry = { | ||
data: v, | ||
next: null | ||
}; | ||
if (this.length > 0) this.tail.next = entry;else this.head = entry; | ||
@@ -36,4 +43,7 @@ this.tail = entry; | ||
BufferList.prototype.unshift = function unshift(v) { | ||
var entry = { data: v, next: this.head }; | ||
_proto.unshift = function unshift(v) { | ||
var entry = { | ||
data: v, | ||
next: this.head | ||
}; | ||
if (this.length === 0) this.tail = entry; | ||
@@ -44,3 +54,3 @@ this.head = entry; | ||
BufferList.prototype.shift = function shift() { | ||
_proto.shift = function shift() { | ||
if (this.length === 0) return; | ||
@@ -53,3 +63,3 @@ var ret = this.head.data; | ||
BufferList.prototype.clear = function clear() { | ||
_proto.clear = function clear() { | ||
this.head = this.tail = null; | ||
@@ -59,12 +69,15 @@ this.length = 0; | ||
BufferList.prototype.join = function join(s) { | ||
_proto.join = function join(s) { | ||
if (this.length === 0) return ''; | ||
var p = this.head; | ||
var ret = '' + p.data; | ||
while (p = p.next) { | ||
ret += s + p.data; | ||
}return ret; | ||
} | ||
return ret; | ||
}; | ||
BufferList.prototype.concat = function concat(n) { | ||
_proto.concat = function concat(n) { | ||
if (this.length === 0) return Buffer.alloc(0); | ||
@@ -74,2 +87,3 @@ var ret = Buffer.allocUnsafe(n >>> 0); | ||
var i = 0; | ||
while (p) { | ||
@@ -80,10 +94,10 @@ copyBuffer(p.data, ret, i); | ||
} | ||
return ret; | ||
}; | ||
} // Consumes a specified amount of bytes or characters from the buffered data. | ||
; | ||
// Consumes a specified amount of bytes or characters from the buffered data. | ||
_proto.consume = function consume(n, hasStrings) { | ||
var ret; | ||
BufferList.prototype.consume = function consume(n, hasStrings) { | ||
var ret; | ||
if (n < this.head.data.length) { | ||
@@ -100,13 +114,12 @@ // `slice` is the same for buffers and strings. | ||
} | ||
return ret; | ||
}; | ||
BufferList.prototype.first = function first() { | ||
_proto.first = function first() { | ||
return this.head.data; | ||
}; | ||
} // Consumes a specified amount of characters from the buffered data. | ||
; | ||
// Consumes a specified amount of characters from the buffered data. | ||
BufferList.prototype._getString = function _getString(n) { | ||
_proto._getString = function _getString(n) { | ||
var p = this.head; | ||
@@ -116,2 +129,3 @@ var c = 1; | ||
n -= ret.length; | ||
while (p = p.next) { | ||
@@ -122,2 +136,3 @@ var str = p.data; | ||
n -= nb; | ||
if (n === 0) { | ||
@@ -131,14 +146,15 @@ if (nb === str.length) { | ||
} | ||
break; | ||
} | ||
++c; | ||
} | ||
this.length -= c; | ||
return ret; | ||
}; | ||
} // Consumes a specified amount of bytes from the buffered data. | ||
; | ||
// Consumes a specified amount of bytes from the buffered data. | ||
BufferList.prototype._getBuffer = function _getBuffer(n) { | ||
_proto._getBuffer = function _getBuffer(n) { | ||
var ret = Buffer.allocUnsafe(n); | ||
@@ -149,2 +165,3 @@ var p = this.head; | ||
n -= p.data.length; | ||
while (p = p.next) { | ||
@@ -155,2 +172,3 @@ var buf = p.data; | ||
n -= nb; | ||
if (n === 0) { | ||
@@ -164,13 +182,21 @@ if (nb === buf.length) { | ||
} | ||
break; | ||
} | ||
++c; | ||
} | ||
this.length -= c; | ||
return ret; | ||
}; | ||
} // Make sure the linked list only shows the minimal necessary information. | ||
; | ||
BufferList.prototype[custom] = function () { | ||
var obj = inspect({ length: this.length }); | ||
return this.constructor.name + ' ' + obj; | ||
_proto[custom] = function (_, options) { | ||
return inspect(this, _objectSpread({}, options, { | ||
// Only inspect one level. | ||
depth: 0, | ||
// It should not recurse. | ||
customInspect: false | ||
})); | ||
}; | ||
@@ -177,0 +203,0 @@ |
@@ -1,5 +0,3 @@ | ||
'use strict'; | ||
'use strict'; // undocumented cb() API, needed for core, not for public API | ||
// undocumented cb() API, needed for core, not for public API | ||
function destroy(err, cb) { | ||
@@ -17,13 +15,13 @@ var _this = this; | ||
} | ||
return this; | ||
} | ||
// we set destroyed to true before firing error callbacks in order | ||
} // we set destroyed to true before firing error callbacks in order | ||
// to make it re-entrance safe in case destroy() is called within callbacks | ||
if (this._readableState) { | ||
this._readableState.destroyed = true; | ||
} | ||
} // if this is a duplex stream mark the writable part as destroyed as well | ||
// if this is a duplex stream mark the writable part as destroyed as well | ||
if (this._writableState) { | ||
@@ -36,2 +34,3 @@ this._writableState.destroyed = true; | ||
process.nextTick(emitErrorAndCloseNT, _this, err); | ||
if (_this._writableState) { | ||
@@ -38,0 +37,0 @@ _this._writableState.errorEmitted = true; |
// Ported from https://github.com/mafintosh/end-of-stream with | ||
// permission from the author, Mathias Buus (@mafintosh). | ||
'use strict'; | ||
@@ -26,5 +25,3 @@ | ||
if (!opts) opts = {}; | ||
callback = once(callback || noop); | ||
var ws = stream._writableState; | ||
@@ -57,2 +54,3 @@ var rs = stream._readableState; | ||
} | ||
if (writable && !(ws && ws.ended)) { | ||
@@ -81,3 +79,2 @@ return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE()); | ||
stream.on('close', onclose); | ||
return function () { | ||
@@ -84,0 +81,0 @@ stream.removeListener('complete', onfinish); |
// Ported from https://github.com/mafintosh/pump with | ||
// permission from the author, Mathias Buus (@mafintosh). | ||
'use strict'; | ||
var eos = void 0; | ||
var eos; | ||
function once(callback) { | ||
var called = false;return function () { | ||
if (called) return;called = true;callback.apply(undefined, arguments); | ||
var called = false; | ||
return function () { | ||
if (called) return; | ||
called = true; | ||
callback.apply(void 0, arguments); | ||
}; | ||
@@ -29,3 +31,2 @@ } | ||
callback = once(callback); | ||
var closed = false; | ||
@@ -35,5 +36,7 @@ stream.on('close', function () { | ||
}); | ||
if (eos === undefined) eos = require('./end-of-stream'); | ||
eos(stream, { readable: reading, writable: writing }, function (err) { | ||
eos(stream, { | ||
readable: reading, | ||
writable: writing | ||
}, function (err) { | ||
if (err) return callback(err); | ||
@@ -43,3 +46,2 @@ closed = true; | ||
}); | ||
var destroyed = false; | ||
@@ -49,8 +51,6 @@ return function (err) { | ||
if (destroyed) return; | ||
destroyed = true; | ||
destroyed = true; // request.destroy just do .end - .abort is what we want | ||
// request.destroy just do .end - .abort is what we want | ||
if (isRequest(stream)) return stream.abort(); | ||
if (typeof stream.destroy === 'function') return stream.destroy(); | ||
callback(err || new ERR_STREAM_DESTROYED('pipe')); | ||
@@ -75,3 +75,3 @@ }; | ||
function pipeline() { | ||
for (var _len = arguments.length, streams = Array(_len), _key = 0; _key < _len; _key++) { | ||
for (var _len = arguments.length, streams = new Array(_len), _key = 0; _key < _len; _key++) { | ||
streams[_key] = arguments[_key]; | ||
@@ -81,3 +81,2 @@ } | ||
var callback = popCallback(streams); | ||
if (Array.isArray(streams[0])) streams = streams[0]; | ||
@@ -89,3 +88,3 @@ | ||
var error = void 0; | ||
var error; | ||
var destroys = streams.map(function (stream, i) { | ||
@@ -102,3 +101,2 @@ var reading = i < streams.length - 1; | ||
}); | ||
return streams.reduce(pipe); | ||
@@ -105,0 +103,0 @@ } |
@@ -11,11 +11,13 @@ 'use strict'; | ||
var hwm = highWaterMarkFrom(options, isDuplex, duplexKey); | ||
if (hwm != null) { | ||
if (!Number.isInteger(hwm) || hwm < 0) { | ||
if (!(isFinite(hwm) && Math.floor(hwm) === hwm) || hwm < 0) { | ||
var name = isDuplex ? duplexKey : 'highWaterMark'; | ||
throw new ERR_INVALID_OPT_VALUE(name, hwm); | ||
} | ||
return Math.floor(hwm); | ||
} | ||
} // Default value | ||
// Default value | ||
return state.objectMode ? 16 : 16 * 1024; | ||
@@ -22,0 +24,0 @@ } |
{ | ||
"name": "readable-stream", | ||
"version": "3.0.6", | ||
"version": "3.1.0", | ||
"description": "Streams3, a user-land copy of the stream library from Node.js", | ||
@@ -15,23 +15,23 @@ "main": "readable.js", | ||
"devDependencies": { | ||
"@babel/cli": "^7.2.0", | ||
"@babel/core": "^7.2.0", | ||
"@babel/plugin-proposal-async-generator-functions": "^7.2.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.2.0", | ||
"@babel/plugin-proposal-optional-catch-binding": "^7.2.0", | ||
"@babel/plugin-transform-arrow-functions": "^7.2.0", | ||
"@babel/plugin-transform-async-to-generator": "^7.2.0", | ||
"@babel/plugin-transform-block-scoping": "^7.2.0", | ||
"@babel/plugin-transform-classes": "^7.2.2", | ||
"@babel/plugin-transform-computed-properties": "^7.2.0", | ||
"@babel/plugin-transform-destructuring": "^7.2.0", | ||
"@babel/plugin-transform-for-of": "^7.2.0", | ||
"@babel/plugin-transform-modules-commonjs": "^7.2.0", | ||
"@babel/plugin-transform-shorthand-properties": "^7.2.0", | ||
"@babel/plugin-transform-spread": "^7.2.0", | ||
"@babel/plugin-transform-template-literals": "^7.2.0", | ||
"@babel/polyfill": "^7.0.0", | ||
"@babel/preset-env": "^7.2.0", | ||
"airtap": "0.0.9", | ||
"assert": "^1.4.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-plugin-transform-async-generator-functions": "^6.24.1", | ||
"babel-plugin-transform-async-to-generator": "^6.24.1", | ||
"babel-plugin-transform-es2015-arrow-functions": "^6.5.2", | ||
"babel-plugin-transform-es2015-block-scoping": "^6.26.0", | ||
"babel-plugin-transform-es2015-classes": "^6.24.1", | ||
"babel-plugin-transform-es2015-computed-properties": "^6.24.1", | ||
"babel-plugin-transform-es2015-destructuring": "^6.18.0", | ||
"babel-plugin-transform-es2015-for-of": "^6.8.0", | ||
"babel-plugin-transform-es2015-parameters": "^6.24.1", | ||
"babel-plugin-transform-es2015-shorthand-properties": "^6.24.1", | ||
"babel-plugin-transform-es2015-spread": "^6.22.0", | ||
"babel-plugin-transform-es2015-template-literals": "^6.8.0", | ||
"babel-plugin-transform-inline-imports-commonjs": "^1.2.0", | ||
"babel-polyfill": "^6.9.1", | ||
"babel-preset-env": "^1.7.0", | ||
"bl": "^2.0.0", | ||
"buffer": "^5.1.0", | ||
"deep-strict-equal": "^0.2.0", | ||
@@ -57,3 +57,3 @@ "glob": "^7.1.2", | ||
"report": "nyc report --reporter=lcov", | ||
"update-browser-errors": "babel --presets env -o errors-browser.js errors.js" | ||
"update-browser-errors": "babel --presets @babel/env -o errors-browser.js errors.js" | ||
}, | ||
@@ -60,0 +60,0 @@ "repository": { |
@@ -18,3 +18,3 @@ # readable-stream | ||
Full documentation may be found on the [Node.js website](https://nodejs.org/dist/v10.11.0/docs/api/stream.html). | ||
Full documentation may be found on the [Node.js website](https://nodejs.org/dist/v10.14.2/docs/api/stream.html). | ||
@@ -21,0 +21,0 @@ If you want to guarantee a stable streams base, regardless of what version of |
374716
24
2510