| 1 | // Copyright Joyent, Inc. and other Node contributors.
|
|---|
| 2 | //
|
|---|
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a
|
|---|
| 4 | // copy of this software and associated documentation files (the
|
|---|
| 5 | // "Software"), to deal in the Software without restriction, including
|
|---|
| 6 | // without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 7 | // distribute, sublicense, and/or sell copies of the Software, and to permit
|
|---|
| 8 | // persons to whom the Software is furnished to do so, subject to the
|
|---|
| 9 | // following conditions:
|
|---|
| 10 | //
|
|---|
| 11 | // The above copyright notice and this permission notice shall be included
|
|---|
| 12 | // in all copies or substantial portions of the Software.
|
|---|
| 13 | //
|
|---|
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|---|
| 15 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|---|
| 16 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|---|
| 17 | // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|---|
| 18 | // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|---|
| 19 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|---|
| 20 | // USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 21 |
|
|---|
| 22 | // a transform stream is a readable/writable stream where you do
|
|---|
| 23 | // 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 where
|
|---|
| 25 | // some bits pass through, and others are simply ignored. (That would
|
|---|
| 26 | // be a valid example of a transform, of course.)
|
|---|
| 27 | //
|
|---|
| 28 | // While the output is causally related to the input, it's not a
|
|---|
| 29 | // necessarily symmetric or synchronous transformation. For example,
|
|---|
| 30 | // a zlib stream might take multiple plain-text writes(), and then
|
|---|
| 31 | // 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 writable
|
|---|
| 36 | // stream classes. When you write(chunk), that calls _write(chunk,cb)
|
|---|
| 37 | // internally, and returns false if there's a lot of pending writes
|
|---|
| 38 | // buffered up. When you call read(), that calls _read(n) until
|
|---|
| 39 | // there's enough pending readable data buffered up.
|
|---|
| 40 | //
|
|---|
| 41 | // In a transform stream, the written data is placed in a buffer. When
|
|---|
| 42 | // _read(n) is called, it transforms the queued up data, calling the
|
|---|
| 43 | // buffered _write cb's as it consumes chunks. If consuming a single
|
|---|
| 44 | // written chunk would result in multiple output chunks, then the first
|
|---|
| 45 | // outputted bit calls the readcb, and subsequent chunks just go into
|
|---|
| 46 | // 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 buffering
|
|---|
| 51 | // here. For example, imagine a stream where every byte of input is
|
|---|
| 52 | // interpreted as an integer from 0-255, and then results in that many
|
|---|
| 53 | // bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
|
|---|
| 54 | // 1kb of data being output. In this case, you could write a very small
|
|---|
| 55 | // amount of input, and end up with a very large amount of output. In
|
|---|
| 56 | // such a pathological inflating mechanism, there'd be no way to tell
|
|---|
| 57 | // the system to stop doing the transform. A single 4MB write could
|
|---|
| 58 | // cause the system to run out of memory.
|
|---|
| 59 | //
|
|---|
| 60 | // However, even in such a pathological case, only a single written chunk
|
|---|
| 61 | // would be consumed, and then the rest would wait (un-transformed) until
|
|---|
| 62 | // the results of the previous transformed chunk were consumed.
|
|---|
| 63 |
|
|---|
| 64 | 'use strict';
|
|---|
| 65 |
|
|---|
| 66 | module.exports = Transform;
|
|---|
| 67 | var _require$codes = require('../errors').codes,
|
|---|
| 68 | ERR_METHOD_NOT_IMPLEMENTED = _require$codes.ERR_METHOD_NOT_IMPLEMENTED,
|
|---|
| 69 | ERR_MULTIPLE_CALLBACK = _require$codes.ERR_MULTIPLE_CALLBACK,
|
|---|
| 70 | ERR_TRANSFORM_ALREADY_TRANSFORMING = _require$codes.ERR_TRANSFORM_ALREADY_TRANSFORMING,
|
|---|
| 71 | ERR_TRANSFORM_WITH_LENGTH_0 = _require$codes.ERR_TRANSFORM_WITH_LENGTH_0;
|
|---|
| 72 | var Duplex = require('./_stream_duplex');
|
|---|
| 73 | require('inherits')(Transform, Duplex);
|
|---|
| 74 | function afterTransform(er, data) {
|
|---|
| 75 | var ts = this._transformState;
|
|---|
| 76 | ts.transforming = false;
|
|---|
| 77 | var cb = ts.writecb;
|
|---|
| 78 | if (cb === null) {
|
|---|
| 79 | return this.emit('error', new ERR_MULTIPLE_CALLBACK());
|
|---|
| 80 | }
|
|---|
| 81 | ts.writechunk = null;
|
|---|
| 82 | ts.writecb = null;
|
|---|
| 83 | if (data != null)
|
|---|
| 84 | // single equals check for both `null` and `undefined`
|
|---|
| 85 | this.push(data);
|
|---|
| 86 | cb(er);
|
|---|
| 87 | var rs = this._readableState;
|
|---|
| 88 | rs.reading = false;
|
|---|
| 89 | if (rs.needReadable || rs.length < rs.highWaterMark) {
|
|---|
| 90 | this._read(rs.highWaterMark);
|
|---|
| 91 | }
|
|---|
| 92 | }
|
|---|
| 93 | function Transform(options) {
|
|---|
| 94 | if (!(this instanceof Transform)) return new Transform(options);
|
|---|
| 95 | Duplex.call(this, options);
|
|---|
| 96 | this._transformState = {
|
|---|
| 97 | afterTransform: afterTransform.bind(this),
|
|---|
| 98 | needTransform: false,
|
|---|
| 99 | transforming: false,
|
|---|
| 100 | writecb: null,
|
|---|
| 101 | writechunk: null,
|
|---|
| 102 | writeencoding: null
|
|---|
| 103 | };
|
|---|
| 104 |
|
|---|
| 105 | // start out asking for a readable event once data is transformed.
|
|---|
| 106 | this._readableState.needReadable = true;
|
|---|
| 107 |
|
|---|
| 108 | // we have implemented the _read method, and done the other things
|
|---|
| 109 | // that Readable wants before the first _read call, so unset the
|
|---|
| 110 | // sync guard flag.
|
|---|
| 111 | this._readableState.sync = false;
|
|---|
| 112 | if (options) {
|
|---|
| 113 | if (typeof options.transform === 'function') this._transform = options.transform;
|
|---|
| 114 | if (typeof options.flush === 'function') this._flush = options.flush;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | // When the writable side finishes, then flush out anything remaining.
|
|---|
| 118 | this.on('prefinish', prefinish);
|
|---|
| 119 | }
|
|---|
| 120 | function prefinish() {
|
|---|
| 121 | var _this = this;
|
|---|
| 122 | if (typeof this._flush === 'function' && !this._readableState.destroyed) {
|
|---|
| 123 | this._flush(function (er, data) {
|
|---|
| 124 | done(_this, er, data);
|
|---|
| 125 | });
|
|---|
| 126 | } else {
|
|---|
| 127 | done(this, null, null);
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | Transform.prototype.push = function (chunk, encoding) {
|
|---|
| 131 | this._transformState.needTransform = false;
|
|---|
| 132 | return Duplex.prototype.push.call(this, chunk, encoding);
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | // This is the part where you do stuff!
|
|---|
| 136 | // override this function in implementation classes.
|
|---|
| 137 | // 'chunk' is an input chunk.
|
|---|
| 138 | //
|
|---|
| 139 | // Call `push(newChunk)` to pass along transformed output
|
|---|
| 140 | // to the readable side. You may call 'push' zero or more times.
|
|---|
| 141 | //
|
|---|
| 142 | // Call `cb(err)` when you are done with this chunk. If you pass
|
|---|
| 143 | // an error, then that'll put the hurt on the whole operation. If you
|
|---|
| 144 | // never call cb(), then you'll never get another chunk.
|
|---|
| 145 | Transform.prototype._transform = function (chunk, encoding, cb) {
|
|---|
| 146 | cb(new ERR_METHOD_NOT_IMPLEMENTED('_transform()'));
|
|---|
| 147 | };
|
|---|
| 148 | Transform.prototype._write = function (chunk, encoding, cb) {
|
|---|
| 149 | var ts = this._transformState;
|
|---|
| 150 | ts.writecb = cb;
|
|---|
| 151 | ts.writechunk = chunk;
|
|---|
| 152 | ts.writeencoding = encoding;
|
|---|
| 153 | if (!ts.transforming) {
|
|---|
| 154 | var rs = this._readableState;
|
|---|
| 155 | if (ts.needTransform || rs.needReadable || rs.length < rs.highWaterMark) this._read(rs.highWaterMark);
|
|---|
| 156 | }
|
|---|
| 157 | };
|
|---|
| 158 |
|
|---|
| 159 | // Doesn't matter what the args are here.
|
|---|
| 160 | // _transform does all the work.
|
|---|
| 161 | // That we got here means that the readable side wants more data.
|
|---|
| 162 | Transform.prototype._read = function (n) {
|
|---|
| 163 | var ts = this._transformState;
|
|---|
| 164 | if (ts.writechunk !== null && !ts.transforming) {
|
|---|
| 165 | ts.transforming = true;
|
|---|
| 166 | this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform);
|
|---|
| 167 | } else {
|
|---|
| 168 | // mark that we need a transform, so that any data that comes in
|
|---|
| 169 | // will get processed, now that we've asked for it.
|
|---|
| 170 | ts.needTransform = true;
|
|---|
| 171 | }
|
|---|
| 172 | };
|
|---|
| 173 | Transform.prototype._destroy = function (err, cb) {
|
|---|
| 174 | Duplex.prototype._destroy.call(this, err, function (err2) {
|
|---|
| 175 | cb(err2);
|
|---|
| 176 | });
|
|---|
| 177 | };
|
|---|
| 178 | function done(stream, er, data) {
|
|---|
| 179 | if (er) return stream.emit('error', er);
|
|---|
| 180 | if (data != null)
|
|---|
| 181 | // single equals check for both `null` and `undefined`
|
|---|
| 182 | stream.push(data);
|
|---|
| 183 |
|
|---|
| 184 | // TODO(BridgeAR): Write a test for these two error cases
|
|---|
| 185 | // if there's nothing in the write buffer, then that means
|
|---|
| 186 | // that nothing more will ever be provided
|
|---|
| 187 | if (stream._writableState.length) throw new ERR_TRANSFORM_WITH_LENGTH_0();
|
|---|
| 188 | if (stream._transformState.transforming) throw new ERR_TRANSFORM_ALREADY_TRANSFORMING();
|
|---|
| 189 | return stream.push(null);
|
|---|
| 190 | } |
|---|