-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
stream: simplify Transform stream implementation #32763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6099382
stream: simplify Transform stream implementation
ronag 8a54b1f
fixup: zlib
ronag 8cf4a58
fixup: zlib
ronag 2f5da16
fixup: test
ronag e12e6b4
fixup: cleanup
ronag 5cd52b7
fixup: lint
ronag dce04fb
fixup
ronag 6d59617
fixup: backwards compat notes
ronag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,66 +65,32 @@ | |
|
|
||
| const { | ||
| ObjectSetPrototypeOf, | ||
| Symbol | ||
| } = primordials; | ||
|
|
||
| module.exports = Transform; | ||
| const { | ||
| ERR_METHOD_NOT_IMPLEMENTED, | ||
| ERR_MULTIPLE_CALLBACK, | ||
| ERR_TRANSFORM_ALREADY_TRANSFORMING, | ||
| ERR_TRANSFORM_WITH_LENGTH_0 | ||
| ERR_METHOD_NOT_IMPLEMENTED | ||
| } = require('internal/errors').codes; | ||
| const Duplex = require('_stream_duplex'); | ||
| ObjectSetPrototypeOf(Transform.prototype, Duplex.prototype); | ||
| ObjectSetPrototypeOf(Transform, Duplex); | ||
|
|
||
|
|
||
| function afterTransform(er, data) { | ||
| const ts = this._transformState; | ||
| ts.transforming = false; | ||
|
|
||
| const cb = ts.writecb; | ||
|
|
||
| if (cb === null) { | ||
| return this.emit('error', new ERR_MULTIPLE_CALLBACK()); | ||
| } | ||
|
|
||
| ts.writechunk = null; | ||
| ts.writecb = null; | ||
|
|
||
| if (data != null) // Single equals check for both `null` and `undefined` | ||
| this.push(data); | ||
|
|
||
| cb(er); | ||
|
|
||
| const rs = this._readableState; | ||
| rs.reading = false; | ||
| if (rs.needReadable || rs.length < rs.highWaterMark) { | ||
| this._read(rs.highWaterMark); | ||
| } | ||
| } | ||
|
|
||
| const kCallback = Symbol('kCallback'); | ||
|
|
||
| function Transform(options) { | ||
| if (!(this instanceof Transform)) | ||
| return new Transform(options); | ||
|
|
||
| Duplex.call(this, options); | ||
|
|
||
| this._transformState = { | ||
| afterTransform: afterTransform.bind(this), | ||
| needTransform: false, | ||
| transforming: false, | ||
| writecb: null, | ||
| writechunk: null, | ||
| writeencoding: null | ||
| }; | ||
|
|
||
| // 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; | ||
|
|
||
| this[kCallback] = null; | ||
|
|
||
| if (options) { | ||
| if (typeof options.transform === 'function') | ||
| this._transform = options.transform; | ||
|
|
@@ -133,89 +99,67 @@ function Transform(options) { | |
| this._flush = options.flush; | ||
| } | ||
|
|
||
| // When the writable side finishes, then flush out anything remaining. | ||
| // TODO(ronag): Unfortunately _final is invoked asynchronously. | ||
| // Use `prefinish` hack. `prefinish` is emitted synchronously when | ||
| // and only when `_final` is not defined. Implementing `_final` | ||
| // to a Transform should be an error. | ||
| this.on('prefinish', prefinish); | ||
| } | ||
|
|
||
| function prefinish() { | ||
| if (typeof this._flush === 'function' && !this._readableState.destroyed) { | ||
| if (typeof this._flush === 'function' && !this.destroyed) { | ||
| this._flush((er, data) => { | ||
| done(this, er, data); | ||
| if (er) { | ||
| this.destroy(er); | ||
| return; | ||
| } | ||
|
|
||
| if (data != null) { | ||
| this.push(data); | ||
| } | ||
| this.push(null); | ||
| }); | ||
| } else { | ||
| done(this, null, null); | ||
| this.push(null); | ||
| } | ||
| } | ||
|
|
||
| Transform.prototype.push = function(chunk, encoding) { | ||
| this._transformState.needTransform = false; | ||
| return Duplex.prototype.push.call(this, chunk, encoding); | ||
| }; | ||
|
|
||
| // This is the part where you do stuff! | ||
| // override this function in implementation classes. | ||
| // 'chunk' is an input chunk. | ||
| // | ||
| // Call `push(newChunk)` to pass along transformed output | ||
| // to the readable side. You may call 'push' zero or more times. | ||
| // | ||
| // Call `cb(err)` when you are done with this chunk. If you pass | ||
| // an error, then that'll put the hurt on the whole operation. If you | ||
| // never call cb(), then you'll never get another chunk. | ||
| Transform.prototype._transform = function(chunk, encoding, cb) { | ||
| Transform.prototype._transform = function(chunk, encoding, callback) { | ||
| throw new ERR_METHOD_NOT_IMPLEMENTED('_transform()'); | ||
| }; | ||
|
|
||
| Transform.prototype._write = function(chunk, encoding, cb) { | ||
| const ts = this._transformState; | ||
| ts.writecb = cb; | ||
| ts.writechunk = chunk; | ||
| ts.writeencoding = encoding; | ||
| if (!ts.transforming) { | ||
| const rs = this._readableState; | ||
| if (ts.needTransform || | ||
| rs.needReadable || | ||
| rs.length < rs.highWaterMark) | ||
| this._read(rs.highWaterMark); | ||
| } | ||
| 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; | ||
| } | ||
| }); | ||
| }; | ||
|
|
||
| // 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) { | ||
| const ts = this._transformState; | ||
|
|
||
| if (ts.writechunk !== null && !ts.transforming) { | ||
| ts.transforming = true; | ||
| this._transform(ts.writechunk, ts.writeencoding, ts.afterTransform); | ||
| } else { | ||
| // Mark that we need a transform, so that any data that comes in | ||
| // will get processed, now that we've asked for it. | ||
| ts.needTransform = true; | ||
| Transform.prototype._read = function() { | ||
| if (this[kCallback]) { | ||
| const callback = this[kCallback]; | ||
| this[kCallback] = null; | ||
| callback(); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| Transform.prototype._destroy = function(err, cb) { | ||
| Duplex.prototype._destroy.call(this, err, (err2) => { | ||
| cb(err2); | ||
| }); | ||
| }; | ||
|
|
||
|
|
||
| function done(stream, er, data) { | ||
| if (er) | ||
| return stream.emit('error', er); | ||
|
||
|
|
||
| if (data != null) // Single equals check for both `null` and `undefined` | ||
| stream.push(data); | ||
|
|
||
| // These two error cases are coherence checks that can likely not be tested. | ||
| 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,10 +45,9 @@ const Transform = require('_stream_transform'); | |
|
|
||
| assert.strictEqual(tx.readableLength, 10); | ||
| assert.strictEqual(transformed, 10); | ||
| assert.strictEqual(tx._transformState.writechunk.length, 5); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tests internal stuff that don't exist anymore |
||
| assert.deepStrictEqual(tx.writableBuffer.map(function(c) { | ||
| return c.chunk.length; | ||
| }), [6, 7, 8, 9, 10]); | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }), [5, 6, 7, 8, 9, 10]); | ||
| } | ||
|
|
||
| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.