This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
streams2: The new stream base classes
- Loading branch information
Showing
7 changed files
with
829 additions
and
4 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,81 @@ | ||
// 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. | ||
|
||
// a duplex stream is just a stream that is both readable and writable. | ||
// Since JS doesn't have multiple prototypal inheritance, this class | ||
// prototypally inherits from Readable, and then parasitically from | ||
// Writable. | ||
|
||
module.exports = Duplex; | ||
var util = require('util'); | ||
var Readable = require('_stream_readable'); | ||
var Writable = require('_stream_writable'); | ||
|
||
util.inherits(Duplex, Readable); | ||
|
||
Object.keys(Writable.prototype).forEach(function(method) { | ||
if (!Duplex.prototype[method]) | ||
Duplex.prototype[method] = Writable.prototype[method]; | ||
}); | ||
|
||
function Duplex(options) { | ||
Readable.call(this, options); | ||
Writable.call(this, options); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
TooTallNate
|
||
|
||
this.allowHalfOpen = true; | ||
if (options && options.allowHalfOpen === false) | ||
this.allowHalfOpen = false; | ||
|
||
this.once('finish', onfinish); | ||
this.once('end', onend); | ||
} | ||
|
||
// the no-half-open enforcers. | ||
function onfinish() { | ||
// if we allow half-open state, or if the readable side ended, | ||
// then we're ok. | ||
if (this.allowHalfOpen || this._readableState.ended) | ||
return; | ||
|
||
// mark that we're done. | ||
this._readableState.ended = true; | ||
|
||
// tell the user | ||
if (this._readableState.length === 0) | ||
this.emit('end'); | ||
else | ||
this.emit('readable'); | ||
} | ||
|
||
function onend() { | ||
// if we allow half-open state, or if the writable side ended, | ||
// then we're ok. | ||
if (this.allowHalfOpen || this._writableState.ended) | ||
return; | ||
|
||
// just in case the user is about to call write() again. | ||
this.write = function() { | ||
return false; | ||
}; | ||
|
||
// no more data can be written. | ||
this.end(); | ||
} |
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,39 @@ | ||
// 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. | ||
|
||
// a passthrough stream. | ||
// basically just the most minimal sort of Transform stream. | ||
// Every written chunk gets output as-is. | ||
|
||
module.exports = PassThrough; | ||
|
||
var Transform = require('_stream_transform'); | ||
var util = require('util'); | ||
util.inherits(PassThrough, Transform); | ||
|
||
function PassThrough(options) { | ||
Transform.call(this, options); | ||
} | ||
|
||
PassThrough.prototype._transform = function(chunk, output, cb) { | ||
output(chunk); | ||
cb(); | ||
}; |
Oops, something went wrong.
What if we want
d._writableState.objectMode = true
andd._readableState.objectMode = false
Or what if we want different watermarks for either side.