-
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
buffer: support boxed strings and toPrimitive #13725
Conversation
lib/buffer.js
Outdated
@@ -170,18 +170,31 @@ Object.defineProperty(Buffer, Symbol.species, { | |||
* Buffer.from(arrayBuffer[, byteOffset[, length]]) | |||
**/ | |||
Buffer.from = function(value, encodingOrOffset, length) { | |||
if (value === undefined || value === null) |
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.
== null
?
lib/buffer.js
Outdated
|
||
if (typeof value === 'number') | ||
throw new TypeError('"value" argument must not be a number'); | ||
|
||
if (typeof value === 'string') |
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.
IMHO we should be promoting this and the other more common cases to the top of this function. This is why typeof value === 'number'
was at the very end for example.
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.
yeah, good point, I updated and shifted the number check back down a bit. It cannot be at the bottom like before because doing so causes the valueOf option to go into a recursion loop.
throw new TypeError(kFromErrorMsg); | ||
|
||
if (typeof value === 'number') | ||
throw new TypeError('"value" argument must not be a number'); |
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 guess it would be good to use internal/errors
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.
I'm going to be updating the buffer errors all at once very soon.
lib/buffer.js
Outdated
if (typeof value === 'number') | ||
throw new TypeError('"value" argument must not be a number'); | ||
|
||
const valueOf = value.valueOf(); |
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 wonder if we should be more cautious here or not, in case someone passes say Object.create(null)
. Perhaps value.valueOf && value.valueOf()
at the very least ?
lib/buffer.js
Outdated
throw new TypeError('"value" argument must not be a number'); | ||
if (typeof value === 'object' && | ||
typeof value[Symbol.toPrimitive] === 'function') { | ||
return Buffer.from(value[Symbol.toPrimitive](), encodingOrOffset, length); |
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.
We should call value[Symbol.toPrimitive]
with a hint
argument of 'string'
, I would say
lib/buffer.js
Outdated
const valueOf = value.valueOf(); | ||
if (valueOf !== value) | ||
return Buffer.from(valueOf, encodingOrOffset, length); | ||
|
||
var b = fromObject(value); |
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.
Now that we're supposedly no longer bound by Crankshaft's inlining limitations, perhaps we might also move the isUint8Array()
case from fromObject()
into this function, right below the isAnyArrayBuffer()
case?
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'm updating to handle the other suggestions, I'll look at this one separately.
Is there any real use to supporting |
doc/api/buffer.md
Outdated
|
||
```js | ||
const buf = Buffer.from(new String('this is a test')); | ||
``` |
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 it's just me, but I'm missing the outcome. Like:
buf.toString() === 'this is a test'
Or even:
console.log(buf)
outputs:
<Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
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.
Not sure I understand your point 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.
You show the correct usage, but not the expected outcome. It feels incomplete, but I don't have a strong opinion.
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.
Except something @addaleax wrote somewhere, maybe we'll be able to run the snippets in the docs as a sort of acceptance test. For that we need some assertion on outcomes.
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.
ah, ok
lib/buffer.js
Outdated
var b = fromObject(value); | ||
if (b) | ||
return b; | ||
|
||
if (typeof value === 'number') | ||
throw new TypeError('"value" argument must not be a number'); | ||
if (typeof value === 'object' && |
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.
tiny nit: you can try to access properties on any !(value === undefined || value === null)
value so maybe just (typeof value[Symbol.toPrimitive] === 'function')
?
@Fishrock123 ... it's more about principle of least-surprise. The lack of support for |
9baef7f
to
b38d925
Compare
Looks like this needs rebasing and lint errors fixed. |
Add support for `Buffer.from(new String('...'))` and `Buffer.from({[Symbol.toPrimitive]() { return '...'; }})`
Ugh. The most recent commit on master broke linting and was force pushed 2 hours after landing. It appears that it was force pushed as I was submitting this which broke everything. Rebasing now and will restart it |
b38d925
to
9b4c8b2
Compare
Add support for `Buffer.from(new String('...'))` and `Buffer.from({[Symbol.toPrimitive]() { return '...'; }})` PR-URL: #13725 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Landed in 22cf25c |
Add support for `Buffer.from(new String('...'))` and `Buffer.from({[Symbol.toPrimitive]() { return '...'; }})` PR-URL: #13725 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add support for `Buffer.from(new String('...'))` and `Buffer.from({[Symbol.toPrimitive]() { return '...'; }})` PR-URL: #13725 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add support for `Buffer.from(new String('...'))` and `Buffer.from({[Symbol.toPrimitive]() { return '...'; }})` PR-URL: #13725 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add support for `Buffer.from(new String('...'))` and `Buffer.from({[Symbol.toPrimitive]() { return '...'; }})` PR-URL: #13725 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add support for `Buffer.from(new String('...'))` and `Buffer.from({[Symbol.toPrimitive]() { return '...'; }})` PR-URL: #13725 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Add support for `Buffer.from(new String('...'))` and `Buffer.from({[Symbol.toPrimitive]() { return '...'; }})` PR-URL: #13725 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Release team decided not to land on v6.x, if you disagree let us know. |
Add support for
Buffer.from(new String('...'))
andBuffer.from({[Symbol.toPrimitive]() { return '...'; }})
/cc @bnoordhuis @addaleax
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
buffer