forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A faster version of Transform which avoids the Writable overhead. Refs: nodejs#33397
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
'use strict'; | ||
|
||
class FastTransform extends Readable { | ||
constructor(options) { | ||
super(options); | ||
this._writableState = { | ||
length: 0, | ||
needDrain: false, | ||
ended: false, | ||
finished: false, | ||
errored: false | ||
}; | ||
} | ||
get writable () { | ||
const wState = this._writableState | ||
return ( | ||
!wState.ended && | ||
!wState.errored && | ||
!wState.destroyed | ||
) | ||
} | ||
get writableEnded () { | ||
return this._writableState.ended; | ||
} | ||
get writableFinished () { | ||
return this._writableState.finished; | ||
} | ||
get writableLength () { | ||
return wState.length + rState.length | ||
} | ||
get writableCorked () { | ||
// TODO | ||
} | ||
get writableHighWaterMark () { | ||
return this._readableState.highWaterMark; | ||
} | ||
get writableBuffer () { | ||
// TODO | ||
} | ||
get writableObjectMode () { | ||
return this._readableState.objectMode; | ||
} | ||
_read () { | ||
const rState = this._readableState; | ||
const wState = this._writableState; | ||
|
||
if (!wState.needDrain) { | ||
return | ||
} | ||
|
||
if (wState.length + rState.length > rState.highWaterMark) { | ||
return | ||
} | ||
|
||
wState.needDrain = false; | ||
this.emit('drain'); | ||
} | ||
write (chunk) { | ||
const rState = this._readableState; | ||
const wState = this._writableState; | ||
|
||
const len = chunk.length; | ||
|
||
wState.length += len; | ||
this._transform(chunk, null, (err, data) => { | ||
wState.length -= len; | ||
if (err) { | ||
wState.errored = true; | ||
this.destroy(err); | ||
} else if (data != null) { | ||
this.push(data); | ||
} | ||
this._read(); | ||
}); | ||
|
||
wState.needDrain = wState.length + rState.length > rState.highWaterMark; | ||
|
||
return wState.needDrain; | ||
} | ||
end () { | ||
const wState = this._writableState; | ||
|
||
wState.ended = true; | ||
if (this._flush) { | ||
this._flush(chunk, (err, data) => { | ||
if (err) { | ||
this.destroy(err); | ||
} else { | ||
if (data != null) { | ||
this.push(data); | ||
} | ||
this.push(null); | ||
wState.finished = true; | ||
this.emit('finish'); | ||
} | ||
}) | ||
} else { | ||
this.push(null); | ||
wState.finished = true; | ||
this.emit('finish'); | ||
} | ||
} | ||
} | ||
|
||
module.exports = FastTransform; |
This file contains 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