-
Notifications
You must be signed in to change notification settings - Fork 30k
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
string_decoder : support typed array or data view #22562
string_decoder : support typed array or data view #22562
Conversation
lib/string_decoder.js
Outdated
buf); | ||
throw new ERR_INVALID_ARG_TYPE( | ||
'buf', | ||
['Buffer', 'TypedArray', 'DataView', 'ArrayBufferView'], |
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.
While we're in here, does it make sense to alphabetize the list? Or is the existing ordering logical for other reasons?
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.
I think the list makes sense in that Buffer
will be the most common type of argument (by far), followed by TypedArray
s.
I am not sure why ArrayBufferView
is listed explicitly, since currently there are (afaik) only TypedArray
and DataView
ABVs.
test/parallel/test-string-decoder.js
Outdated
@@ -91,7 +91,7 @@ decoder = new StringDecoder('utf8'); | |||
assert.strictEqual(decoder.write(Buffer.from('E1', 'hex')), ''); | |||
|
|||
// A quick test for lastChar, lastNeed & lastTotal which are undocumented. | |||
assert(decoder.lastChar.equals(new Uint8Array([0xe1, 0, 0, 0]))); | |||
assert(decoder.lastChar.equals(Buffer.from([0xe1, 0, 0, 0]))); |
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.
Why this change?
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.
To elaborate a bit: There's nothing in this change that breaks new Uint8Array()
working in this context, so why is the test case being changed instead of a new test case being added?
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.
Shouldn't test cases be added for DataView
and TypedArray
? Do they even work properly? No code has been added in this PR to alter how they work with string_decoder
. Were they working already or something?
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.
I was working with the suggestion of this comment here:
[ ] string_decoder
This list is for TypedArray/DataView support only.
I was trying not to use Unit8Array
. Hopefully I understood the #1826 correctly.
If this Unit8Array
doesn't need to change to Buffer
to align with others, I can revert this change here.
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.
Buffer extends Uint8Array, I don't think this changed anything.
lib/string_decoder.js
Outdated
@@ -72,9 +72,11 @@ StringDecoder.prototype.write = function write(buf) { | |||
if (typeof buf === 'string') | |||
return buf; | |||
if (!ArrayBuffer.isView(buf)) | |||
throw new ERR_INVALID_ARG_TYPE('buf', | |||
['Buffer', 'Uint8Array', 'ArrayBufferView'], |
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.
Why is Uint8Array
being removed here?
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.
From the suggestion of this comment in #1826, "string_decoder" seems to be on the list TypedArray
/DataView
support only. After viewing the fs PR and the child_process, I just gave my best attempt to follow the "idea" to handle string_decode.
Please let me know if I misunderstood too much off from the context of the checklist & goals from the comment.
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.
I think this is okay and somewhat consistent with how we handle it elsewhere
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.
Hi @BeniCheni , I think broadly we can categorize the changes for these PRs into:
- adding missing tests, for
TypedArray
s andDataView
s wherever onlyBuffer
s orUint8Array
s were tested earlier - Updating code to work with these new types, where necessary(should mostly involve only JS side of things, in
lib
and maybelib/internal
) - Updating documentation to reflect these changes.
Here, your PR is updating the errors being thrown, but is neither adding test cases nor adding functionality to work with the TypedArray
s or DataView
s. Please note, it might well be the case that string_decoder
already works for these, but adding test cases is a way of validation for the same. Hope that helps!
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.
🙏 for the collective reviews. Will analyze & update accordingly.
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.
LGTM when ArrayBufferView
is removed.
Researched & wrote tests to cover
Researched more and updated to revert the previously incorrect removal of
Removed any context of Also confirmed if any context needed to update in |
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.
Reconfirming my LG
Hi @BeniCheni , thanks for updating the tests! I think we could also update |
Hi @SirR4T, updated doc per your suggestion. Notice the last CI instance failed with just a doc change in the commit. Awaiting to get help/advice from other reviewers to validate the failed CI instance. Thoughts, any reviewer(s)? |
Hi @BeniCheni , doc changes look good to me, thanks! But as I'm myself a new enough contributor here, I'll refrain from approving PRs, in deference to someone with more review experience. As for the CI failure, it doesn't seem related to your changes, but instead looks like a certificate failure from npm. This may or may not be related to npm's current issue at the time of this post. |
Resume CI: https://ci.nodejs.org/job/node-test-pull-request/16971/ (:heavy_check_mark:) |
test/parallel/test-string-decoder.js
Outdated
@@ -97,6 +97,21 @@ assert.strictEqual(decoder.lastTotal, 3); | |||
|
|||
assert.strictEqual(decoder.end(), '\ufffd'); | |||
|
|||
// TypedArray with Int8Array test | |||
decoder = new StringDecoder('utf8'); | |||
assert.strictEqual(decoder.write(new Int8Array([1, 2])), '\u0001\u0002'); |
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.
I'd prefer this test use getArrayBufferViews() to cover all possible TypedArray types and DataView. An example usage of this function by @SirR4T is
node/test/parallel/test-fs-write-file-typedarrays.js
Lines 24 to 31 in be5e396
for (const expectView of common.getArrayBufferViews(inputBuffer)) { | |
console.log('Sync test for ', expectView[Symbol.toStringTag]); | |
fs.writeFileSync(filename, expectView); | |
assert.strictEqual( | |
fs.readFileSync(filename, 'utf8'), | |
inputBuffer.toString('utf8') | |
); | |
} |
Hi, @TimothyGu. Updated to use the common |
test/parallel/test-string-decoder.js
Outdated
const arrayBufferViewStr = 'String for ArrayBufferView tests\n'; | ||
const inputBuffer = Buffer.from(arrayBufferViewStr.repeat(8), 'utf8'); | ||
for (const expectView of common.getArrayBufferViews(inputBuffer)) { | ||
console.log( |
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.
If the console.log()
statement is part of the test, please add a comment indicating such. If it is not, I'd prefer to use a comment instead of having the extraneous console output.
Hello @jasnell, |
Ping/Dear reviewers, Would you mind helping to guide this “author ready” PR to landing? (Made all suggested updates according to previous review comments; thanks!) |
Landed in e2325bc |
make -j4 test
(UNIX), orvcbuild test
(Windows) passesFixes: #1826
Per this comment with the checklist, this PR attempts to help out the
string_decoder
scope from the check list. The PR supports explicitlyTypedArray
orDataView
, insead of the originalUint8Array
.