react / wstein / node_modules / browserify / node_modules / readable-stream / lib / _stream_transform.js
80537 views// Copyright Joyent, Inc. and other Node contributors.1//2// Permission is hereby granted, free of charge, to any person obtaining a3// copy of this software and associated documentation files (the4// "Software"), to deal in the Software without restriction, including5// without limitation the rights to use, copy, modify, merge, publish,6// distribute, sublicense, and/or sell copies of the Software, and to permit7// persons to whom the Software is furnished to do so, subject to the8// following conditions:9//10// The above copyright notice and this permission notice shall be included11// in all copies or substantial portions of the Software.12//13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF15// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN16// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,17// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR18// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE19// USE OR OTHER DEALINGS IN THE SOFTWARE.202122// a transform stream is a readable/writable stream where you do23// something with the data. Sometimes it's called a "filter",24// but that's not a great name for it, since that implies a thing where25// some bits pass through, and others are simply ignored. (That would26// be a valid example of a transform, of course.)27//28// While the output is causally related to the input, it's not a29// necessarily symmetric or synchronous transformation. For example,30// a zlib stream might take multiple plain-text writes(), and then31// emit a single compressed chunk some time in the future.32//33// Here's how this works:34//35// The Transform stream has all the aspects of the readable and writable36// stream classes. When you write(chunk), that calls _write(chunk,cb)37// internally, and returns false if there's a lot of pending writes38// buffered up. When you call read(), that calls _read(n) until39// there's enough pending readable data buffered up.40//41// In a transform stream, the written data is placed in a buffer. When42// _read(n) is called, it transforms the queued up data, calling the43// buffered _write cb's as it consumes chunks. If consuming a single44// written chunk would result in multiple output chunks, then the first45// outputted bit calls the readcb, and subsequent chunks just go into46// the read buffer, and will cause it to emit 'readable' if necessary.47//48// This way, back-pressure is actually determined by the reading side,49// since _read has to be called to start processing a new chunk. However,50// a pathological inflate type of transform can cause excessive buffering51// here. For example, imagine a stream where every byte of input is52// interpreted as an integer from 0-255, and then results in that many53// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in54// 1kb of data being output. In this case, you could write a very small55// amount of input, and end up with a very large amount of output. In56// such a pathological inflating mechanism, there'd be no way to tell57// the system to stop doing the transform. A single 4MB write could58// cause the system to run out of memory.59//60// However, even in such a pathological case, only a single written chunk61// would be consumed, and then the rest would wait (un-transformed) until62// the results of the previous transformed chunk were consumed.6364module.exports = Transform;6566var Duplex = require('./_stream_duplex');6768/*<replacement>*/69var util = require('core-util-is');70util.inherits = require('inherits');71/*</replacement>*/7273util.inherits(Transform, Duplex);747576function TransformState(options, stream) {77this.afterTransform = function(er, data) {78return afterTransform(stream, er, data);79};8081this.needTransform = false;82this.transforming = false;83this.writecb = null;84this.writechunk = null;85}8687function afterTransform(stream, er, data) {88var ts = stream._transformState;89ts.transforming = false;9091var cb = ts.writecb;9293if (!cb)94return stream.emit('error', new Error('no writecb in Transform class'));9596ts.writechunk = null;97ts.writecb = null;9899if (!util.isNullOrUndefined(data))100stream.push(data);101102if (cb)103cb(er);104105var rs = stream._readableState;106rs.reading = false;107if (rs.needReadable || rs.length < rs.highWaterMark) {108stream._read(rs.highWaterMark);109}110}111112113function Transform(options) {114if (!(this instanceof Transform))115return new Transform(options);116117Duplex.call(this, options);118119this._transformState = new TransformState(options, this);120121// when the writable side finishes, then flush out anything remaining.122var stream = this;123124// start out asking for a readable event once data is transformed.125this._readableState.needReadable = true;126127// we have implemented the _read method, and done the other things128// that Readable wants before the first _read call, so unset the129// sync guard flag.130this._readableState.sync = false;131132this.once('prefinish', function() {133if (util.isFunction(this._flush))134this._flush(function(er) {135done(stream, er);136});137else138done(stream);139});140}141142Transform.prototype.push = function(chunk, encoding) {143this._transformState.needTransform = false;144return Duplex.prototype.push.call(this, chunk, encoding);145};146147// This is the part where you do stuff!148// override this function in implementation classes.149// 'chunk' is an input chunk.150//151// Call `push(newChunk)` to pass along transformed output152// to the readable side. You may call 'push' zero or more times.153//154// Call `cb(err)` when you are done with this chunk. If you pass155// an error, then that'll put the hurt on the whole operation. If you156// never call cb(), then you'll never get another chunk.157Transform.prototype._transform = function(chunk, encoding, cb) {158throw new Error('not implemented');159};160161Transform.prototype._write = function(chunk, encoding, cb) {162var ts = this._transformState;163ts.writecb = cb;164ts.writechunk = chunk;165ts.writeencoding = encoding;166if (!ts.transforming) {167var rs = this._readableState;168if (ts.needTransform ||169rs.needReadable ||170rs.length < rs.highWaterMark)171this._read(rs.highWaterMark);172}173};174175// Doesn't matter what the args are here.176// _transform does all the work.177// That we got here means that the readable side wants more data.178Transform.prototype._read = function(n) {179var ts = this._transformState;180181if (!util.isNull(ts.writechunk) && ts.writecb && !ts.transforming) {182ts.transforming = true;183this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);184} else {185// mark that we need a transform, so that any data that comes in186// will get processed, now that we've asked for it.187ts.needTransform = true;188}189};190191192function done(stream, er) {193if (er)194return stream.emit('error', er);195196// if there's nothing in the write buffer, then that means197// that nothing more will ever be provided198var ws = stream._writableState;199var ts = stream._transformState;200201if (ws.length)202throw new Error('calling transform done when ws.length != 0');203204if (ts.transforming)205throw new Error('calling transform done when still transforming');206207return stream.push(null);208}209210211