-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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: support Uint8Array input to methods #11608
Closed
Closed
Changes from all commits
Commits
Show all changes
2 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
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
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
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
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
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,102 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const Buffer = require('buffer').Buffer; | ||
|
||
const { Readable, Writable } = require('stream'); | ||
|
||
const ABC = new Uint8Array([0x41, 0x42, 0x43]); | ||
const DEF = new Uint8Array([0x44, 0x45, 0x46]); | ||
const GHI = new Uint8Array([0x47, 0x48, 0x49]); | ||
|
||
{ | ||
// Simple Writable test. | ||
|
||
let n = 0; | ||
const writable = new Writable({ | ||
write: common.mustCall((chunk, encoding, cb) => { | ||
assert(chunk instanceof Buffer); | ||
if (n++ === 0) { | ||
assert.strictEqual(String(chunk), 'ABC'); | ||
} else { | ||
assert.strictEqual(String(chunk), 'DEF'); | ||
} | ||
|
||
cb(); | ||
}, 2) | ||
}); | ||
|
||
writable.write(ABC); | ||
writable.end(DEF); | ||
} | ||
|
||
{ | ||
// Writable test, pass in Uint8Array in object mode. | ||
|
||
const writable = new Writable({ | ||
objectMode: true, | ||
write: common.mustCall((chunk, encoding, cb) => { | ||
assert(!(chunk instanceof Buffer)); | ||
assert(chunk instanceof Uint8Array); | ||
assert.strictEqual(chunk, ABC); | ||
assert.strictEqual(encoding, 'utf8'); | ||
cb(); | ||
}) | ||
}); | ||
|
||
writable.end(ABC); | ||
} | ||
|
||
{ | ||
// Writable test, multiple writes carried out via writev. | ||
let callback; | ||
|
||
const writable = new Writable({ | ||
write: common.mustCall((chunk, encoding, cb) => { | ||
assert(chunk instanceof Buffer); | ||
assert.strictEqual(encoding, 'buffer'); | ||
assert.strictEqual(String(chunk), 'ABC'); | ||
callback = cb; | ||
}), | ||
writev: common.mustCall((chunks, cb) => { | ||
assert.strictEqual(chunks.length, 2); | ||
assert.strictEqual(chunks[0].encoding, 'buffer'); | ||
assert.strictEqual(chunks[1].encoding, 'buffer'); | ||
assert.strictEqual(chunks[0].chunk + chunks[1].chunk, 'DEFGHI'); | ||
}) | ||
}); | ||
|
||
writable.write(ABC); | ||
writable.write(DEF); | ||
writable.end(GHI); | ||
callback(); | ||
} | ||
|
||
{ | ||
// Simple Readable test. | ||
const readable = new Readable({ | ||
read() {} | ||
}); | ||
|
||
readable.push(DEF); | ||
readable.unshift(ABC); | ||
|
||
const buf = readable.read(); | ||
assert(buf instanceof Buffer); | ||
assert.deepStrictEqual([...buf], [...ABC, ...DEF]); | ||
} | ||
|
||
{ | ||
// Readable test, setEncoding. | ||
const readable = new Readable({ | ||
read() {} | ||
}); | ||
|
||
readable.setEncoding('utf8'); | ||
|
||
readable.push(DEF); | ||
readable.unshift(ABC); | ||
|
||
const out = readable.read(); | ||
assert.strictEqual(out, 'ABCDEF'); | ||
} |
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.
Maybe move this into
internal/util
?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.
@joyeecheung The thing is that the streams modules can’t really unconditionally rely on internal modules because they are also exported as https://github.com/nodejs/readable-stream. @mscdex expressed a preference for having this code polyfilled on the
readable-stream
side and I agree, but ultimately that’s @nodejs/streams’ call …(Please let me know if I’m misunderstanding you!)
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.
Oh yes, forgot about the vendoring out part :) Nevermind then!