2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
// a transform stream is a readable/writable stream where you do
|
|
|
|
// something with the data. Sometimes it's called a "filter",
|
|
|
|
// but that's not a great name for it, since that implies a thing where
|
|
|
|
// some bits pass through, and others are simply ignored. (That would
|
|
|
|
// be a valid example of a transform, of course.)
|
|
|
|
//
|
|
|
|
// While the output is causally related to the input, it's not a
|
|
|
|
// necessarily symmetric or synchronous transformation. For example,
|
|
|
|
// a zlib stream might take multiple plain-text writes(), and then
|
|
|
|
// emit a single compressed chunk some time in the future.
|
2012-10-04 22:26:16 +02:00
|
|
|
//
|
|
|
|
// Here's how this works:
|
|
|
|
//
|
|
|
|
// The Transform stream has all the aspects of the readable and writable
|
|
|
|
// stream classes. When you write(chunk), that calls _write(chunk,cb)
|
|
|
|
// internally, and returns false if there's a lot of pending writes
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-03-01 00:32:32 +01:00
|
|
|
// buffered up. When you call read(), that calls _read(n) until
|
2012-10-04 22:26:16 +02:00
|
|
|
// there's enough pending readable data buffered up.
|
|
|
|
//
|
|
|
|
// In a transform stream, the written data is placed in a buffer. When
|
stream: There is no _read cb, there is only push
This makes it so that `stream.push(chunk)` is the only way to signal the
end of reading, removing the confusing disparity between the
callback-style _read method, and the fact that most real-world streams
do not have a 1:1 corollation between the "please give me data" event,
and the actual arrival of a chunk of data.
It is still possible, of course, to implement a `CallbackReadable` on
top of this. Simply provide a method like this as the callback:
function readCallback(er, chunk) {
if (er)
stream.emit('error', er);
else
stream.push(chunk);
}
However, *only* fs streams actually would behave in this way, so it
makes not a lot of sense to make TCP, TLS, HTTP, and all the rest have
to bend into this uncomfortable paradigm.
2013-03-01 00:32:32 +01:00
|
|
|
// _read(n) is called, it transforms the queued up data, calling the
|
2012-10-04 22:26:16 +02:00
|
|
|
// buffered _write cb's as it consumes chunks. If consuming a single
|
|
|
|
// written chunk would result in multiple output chunks, then the first
|
|
|
|
// outputted bit calls the readcb, and subsequent chunks just go into
|
|
|
|
// the read buffer, and will cause it to emit 'readable' if necessary.
|
|
|
|
//
|
|
|
|
// This way, back-pressure is actually determined by the reading side,
|
|
|
|
// since _read has to be called to start processing a new chunk. However,
|
|
|
|
// a pathological inflate type of transform can cause excessive buffering
|
|
|
|
// here. For example, imagine a stream where every byte of input is
|
|
|
|
// interpreted as an integer from 0-255, and then results in that many
|
|
|
|
// bytes of output. Writing the 4 bytes {ff,ff,ff,ff} would result in
|
|
|
|
// 1kb of data being output. In this case, you could write a very small
|
|
|
|
// amount of input, and end up with a very large amount of output. In
|
|
|
|
// such a pathological inflating mechanism, there'd be no way to tell
|
|
|
|
// the system to stop doing the transform. A single 4MB write could
|
|
|
|
// cause the system to run out of memory.
|
|
|
|
//
|
|
|
|
// However, even in such a pathological case, only a single written chunk
|
|
|
|
// would be consumed, and then the rest would wait (un-transformed) until
|
2013-02-21 19:51:15 +01:00
|
|
|
// the results of the previous transformed chunk were consumed.
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const {
|
|
|
|
ObjectSetPrototypeOf,
|
2020-04-10 17:17:20 +02:00
|
|
|
Symbol
|
2019-11-22 18:04:46 +01:00
|
|
|
} = primordials;
|
2019-04-09 09:55:53 +02:00
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
module.exports = Transform;
|
2018-03-04 22:16:24 +01:00
|
|
|
const {
|
2020-04-10 17:17:20 +02:00
|
|
|
ERR_METHOD_NOT_IMPLEMENTED
|
2018-03-04 22:16:24 +01:00
|
|
|
} = require('internal/errors').codes;
|
2015-01-21 17:36:59 +01:00
|
|
|
const Duplex = require('_stream_duplex');
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectSetPrototypeOf(Transform.prototype, Duplex.prototype);
|
|
|
|
ObjectSetPrototypeOf(Transform, Duplex);
|
2012-10-03 00:44:50 +02:00
|
|
|
|
2020-04-10 17:17:20 +02:00
|
|
|
const kCallback = Symbol('kCallback');
|
2013-01-27 20:56:36 +01:00
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
function Transform(options) {
|
2012-10-08 23:43:17 +02:00
|
|
|
if (!(this instanceof Transform))
|
|
|
|
return new Transform(options);
|
|
|
|
|
2012-10-03 00:44:50 +02:00
|
|
|
Duplex.call(this, options);
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// We have implemented the _read method, and done the other things
|
2013-02-23 01:45:22 +01:00
|
|
|
// that Readable wants before the first _read call, so unset the
|
|
|
|
// sync guard flag.
|
|
|
|
this._readableState.sync = false;
|
|
|
|
|
2020-04-10 17:17:20 +02:00
|
|
|
this[kCallback] = null;
|
|
|
|
|
2015-02-03 02:12:41 +01:00
|
|
|
if (options) {
|
|
|
|
if (typeof options.transform === 'function')
|
|
|
|
this._transform = options.transform;
|
|
|
|
|
|
|
|
if (typeof options.flush === 'function')
|
|
|
|
this._flush = options.flush;
|
|
|
|
}
|
|
|
|
|
2020-04-11 13:16:46 +02:00
|
|
|
// When the writable side finishes, then flush out anything remaining.
|
|
|
|
// Backwards compat. Some Transform streams incorrectly implement _final
|
|
|
|
// instead of or in addition to _flush. By using 'prefinish' instead of
|
|
|
|
// implementing _final we continue supporting this unfortunate use case.
|
2017-05-30 18:55:38 +02:00
|
|
|
this.on('prefinish', prefinish);
|
|
|
|
}
|
|
|
|
|
|
|
|
function prefinish() {
|
2020-04-10 17:17:20 +02:00
|
|
|
if (typeof this._flush === 'function' && !this.destroyed) {
|
2017-05-30 18:55:38 +02:00
|
|
|
this._flush((er, data) => {
|
2020-04-10 17:17:20 +02:00
|
|
|
if (er) {
|
|
|
|
this.destroy(er);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data != null) {
|
|
|
|
this.push(data);
|
|
|
|
}
|
|
|
|
this.push(null);
|
2017-05-30 18:55:38 +02:00
|
|
|
});
|
|
|
|
} else {
|
2020-04-10 17:17:20 +02:00
|
|
|
this.push(null);
|
2017-05-30 18:55:38 +02:00
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 17:17:20 +02:00
|
|
|
Transform.prototype._transform = function(chunk, encoding, callback) {
|
2020-02-22 12:28:26 +01:00
|
|
|
throw new ERR_METHOD_NOT_IMPLEMENTED('_transform()');
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
2020-04-10 17:17:20 +02:00
|
|
|
Transform.prototype._write = function(chunk, encoding, callback) {
|
|
|
|
const rState = this._readableState;
|
|
|
|
const wState = this._writableState;
|
|
|
|
const length = rState.length;
|
|
|
|
|
|
|
|
this._transform(chunk, encoding, (err, val) => {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val != null) {
|
|
|
|
this.push(val);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
wState.ended || // Backwards compat.
|
|
|
|
length === rState.length || // Backwards compat.
|
|
|
|
rState.length < rState.highWaterMark ||
|
|
|
|
rState.length === 0
|
|
|
|
) {
|
|
|
|
callback();
|
|
|
|
} else {
|
|
|
|
this[kCallback] = callback;
|
|
|
|
}
|
|
|
|
});
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|
|
|
|
|
2020-04-10 17:17:20 +02:00
|
|
|
Transform.prototype._read = function() {
|
|
|
|
if (this[kCallback]) {
|
|
|
|
const callback = this[kCallback];
|
|
|
|
this[kCallback] = null;
|
|
|
|
callback();
|
2013-01-27 20:56:36 +01:00
|
|
|
}
|
2012-10-03 00:44:50 +02:00
|
|
|
};
|