-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
stream: cleanup and fix Readable.wrap #34204
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -26,6 +26,7 @@ const { | |
NumberIsInteger, | ||
NumberIsNaN, | ||
ObjectDefineProperties, | ||
ObjectKeys, | ||
ObjectSetPrototypeOf, | ||
Set, | ||
SymbolAsyncIterator, | ||
|
@@ -1007,83 +1008,49 @@ function flow(stream) { | |
// This is *not* part of the readable stream interface. | ||
// It is an ugly unfortunate mess of history. | ||
Readable.prototype.wrap = function(stream) { | ||
const state = this._readableState; | ||
let paused = false; | ||
|
||
stream.on('end', () => { | ||
debug('wrapped end'); | ||
if (state.decoder && !state.ended) { | ||
const chunk = state.decoder.end(); | ||
if (chunk && chunk.length) | ||
this.push(chunk); | ||
} | ||
|
||
this.push(null); | ||
}); | ||
// TODO (ronag): Should this.destroy(err) emit | ||
// 'error' on the wrapped stream? Would require | ||
// a static factory method, e.g. Readable.wrap(stream). | ||
|
||
stream.on('data', (chunk) => { | ||
debug('wrapped data'); | ||
if (state.decoder) | ||
chunk = state.decoder.write(chunk); | ||
|
||
// Don't skip over falsy values in objectMode. | ||
if (state.objectMode && (chunk === null || chunk === undefined)) | ||
return; | ||
else if (!state.objectMode && (!chunk || !chunk.length)) | ||
return; | ||
|
||
const ret = this.push(chunk); | ||
if (!ret) { | ||
if (!this.push(chunk) && stream.pause) { | ||
paused = true; | ||
stream.pause(); | ||
} | ||
}); | ||
|
||
// Proxy all the other methods. Important when wrapping filters and duplexes. | ||
for (const i in stream) { | ||
if (this[i] === undefined && typeof stream[i] === 'function') { | ||
this[i] = function methodWrap(method) { | ||
return function methodWrapReturnFunction() { | ||
return stream[method].apply(stream, arguments); | ||
}; | ||
}(i); | ||
} | ||
} | ||
stream.on('end', () => { | ||
this.push(null); | ||
}); | ||
|
||
stream.on('error', (err) => { | ||
errorOrDestroy(this, err); | ||
}); | ||
|
||
stream.on('close', () => { | ||
// TODO(ronag): Update readable state? | ||
this.emit('close'); | ||
this.destroy(); | ||
}); | ||
|
||
stream.on('destroy', () => { | ||
// TODO(ronag): this.destroy()? | ||
this.emit('destroy'); | ||
this.destroy(); | ||
}); | ||
|
||
stream.on('pause', () => { | ||
// TODO(ronag): this.pause()? | ||
this.emit('pause'); | ||
}); | ||
|
||
stream.on('resume', () => { | ||
// TODO(ronag): this.resume()? | ||
this.emit('resume'); | ||
}); | ||
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. I'm a little unsure what to do exactly when the wrapped stream emits pause and resume. I don't think it's relevant? |
||
|
||
// When we try to consume some more bytes, simply unpause the | ||
// underlying stream. | ||
this._read = (n) => { | ||
debug('wrapped _read', n); | ||
if (paused) { | ||
this._read = () => { | ||
if (paused && stream.resume) { | ||
paused = false; | ||
stream.resume(); | ||
} | ||
}; | ||
|
||
// Proxy all the other methods. Important when wrapping filters and duplexes. | ||
for (const i of ObjectKeys(stream)) { | ||
if (this[i] === undefined && typeof stream[i] === 'function') { | ||
this[i] = stream[i].bind(stream); | ||
} | ||
} | ||
|
||
return this; | ||
}; | ||
|
||
|
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,27 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
const Readable = require('_stream_readable'); | ||
const EE = require('events').EventEmitter; | ||
|
||
const oldStream = new EE(); | ||
oldStream.pause = () => {}; | ||
oldStream.resume = () => {}; | ||
|
||
{ | ||
new Readable({ | ||
autoDestroy: false, | ||
destroy: common.mustCall() | ||
}) | ||
.wrap(oldStream); | ||
oldStream.emit('destroy'); | ||
} | ||
|
||
{ | ||
new Readable({ | ||
autoDestroy: false, | ||
destroy: common.mustCall() | ||
}) | ||
.wrap(oldStream); | ||
oldStream.emit('close'); | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Readable
should already handle all of the above. Not sure why this was put here. Legacy?