-
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
util: extract out encoding validation functions #18421
Conversation
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 we go this route, I think validateEnDecoder()
should be split into two separate functions.
+1 to splitting out |
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.
Please do not change the validation or the error messages. Otherwise LG
lib/internal/encoding.js
Outdated
} | ||
|
||
function validateArgument(prop, expected, propName) { | ||
if (typeof prop !== expected && !isArrayBufferView(prop)) |
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.
This is not only a extraction but it changes the validation itself as it also validates that the input is not an ArrayBufferView
. I would like to keep it as is.
lib/internal/encoding.js
Outdated
@@ -344,8 +355,7 @@ function makeTextDecoderICU() { | |||
class TextDecoder { | |||
constructor(encoding = 'utf-8', options = {}) { | |||
encoding = `${encoding}`; | |||
if (typeof options !== 'object') | |||
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'Object'); | |||
validateArgument(options, 'object', 'options'); |
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.
By combining the check for the type with the second ERR_INVALID_ARG_TYPE
argument the error message gets changed. That should not be the case. That applies to all validateArgument
calls.
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.
@BridgeAR the string
type checks error messages is not affected.
lib/internal/encoding.js
Outdated
} | ||
|
||
function validateArgument(prop, expected, propName) { | ||
if (typeof prop !== expected.toLowerCase()) |
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.
Using toLowerCase
is a performance overhead.
I suggest to either split the validation in two functions or to just keep these validations as they were. You can of course also add a fourth argument.
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.
@BridgeAR is this last commit inline with your suggestion?
lib/internal/encoding.js
Outdated
if (isArrayBuffer(input)) { | ||
validateArgumentBuffer(input, 'input'); | ||
|
||
if (isArrayBuffer(input)) |
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.
This results in a second isArrayBuffer
check and is definitely not desirable. Please keep that check as it was before (as in: remove validateArgumentBuffer
again).
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.
ok.
PR-URL: nodejs#18421 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
Landed in b0b2045 I fixed the commit message while landing (adding the subsystem & shortening it) |
PR-URL: #18421 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #18421 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
PR-URL: #18421 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
Should this be backported to |
PR-URL: nodejs#18421 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
I saw some duplicate validations within
internal/encoding.js
, so I extracted them out to separate functions.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
util