Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,44 @@ console.log(buf2.toString());

A `TypeError` will be thrown if `str` is not a string.

### Class Method: Buffer.from(object[, offsetOrEncoding[, length]])
<!-- YAML
added: REPLACEME
-->

* `object` {Object} An object supporting `Symbol.toPrimitive` or `valueOf()`
* `offsetOrEncoding` {number|string} A byte-offset or encoding, depending on
the value returned either by `object.valueOf()` or
`object[Symbol.toPrimitive]()`.
* `length` {number} A length, depending on the value returned either by
`object.valueOf()` or `object[Symbol.toPrimitive]()`.

For objects whose `valueOf()` function returns a value not strictly equal to
`object`, returns `Buffer.from(object.valueOf(), offsetOrEncoding, length)`.

For example:

```js
const buf = Buffer.from(new String('this is a test'));
// <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
```

For objects that support `Symbol.toPrimitive`, returns
`Buffer.from(object[Symbol.toPrimitive](), offsetOrEncoding, length)`.

For example:

```js
class Foo {
[Symbol.toPrimitive]() {
return 'this is a test';
}
}

const buf = Buffer.from(new Foo(), 'utf8');
// <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
```

### Class Method: Buffer.isBuffer(obj)
<!-- YAML
added: v0.1.101
Expand Down
18 changes: 16 additions & 2 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,26 @@ Buffer.from = function(value, encodingOrOffset, length) {
if (isAnyArrayBuffer(value))
return fromArrayBuffer(value, encodingOrOffset, length);

if (value == null)
throw new TypeError(kFromErrorMsg);

if (typeof value === 'number')
throw new TypeError('"value" argument must not be a number');
Copy link
Member

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.

Copy link
Member Author

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.


const valueOf = value.valueOf && value.valueOf();
if (valueOf != null && valueOf !== value)
return Buffer.from(valueOf, encodingOrOffset, length);

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[Symbol.toPrimitive] === 'function') {
return Buffer.from(value[Symbol.toPrimitive]('string'),
encodingOrOffset,
length);
}

throw new TypeError(kFromErrorMsg);
};

Expand Down
57 changes: 57 additions & 0 deletions test/parallel/test-buffer-from.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

require('../common');
const { deepStrictEqual, throws } = require('assert');
const { Buffer } = require('buffer');
const { runInNewContext } = require('vm');

const checkString = 'test';

const check = Buffer.from(checkString);

class MyString extends String {
constructor() {
super(checkString);
}
}

class MyPrimitive {
[Symbol.toPrimitive]() {
return checkString;
}
}

class MyBadPrimitive {
[Symbol.toPrimitive]() {
return 1;
}
}

deepStrictEqual(Buffer.from(new String(checkString)), check);
deepStrictEqual(Buffer.from(new MyString()), check);
deepStrictEqual(Buffer.from(new MyPrimitive()), check);
deepStrictEqual(Buffer.from(
runInNewContext('new String(checkString)', {checkString})),
check);

const err = new RegExp('^TypeError: First argument must be a string, Buffer, ' +
'ArrayBuffer, Array, or array-like object\\.$');

[
{},
new Boolean(true),
{ valueOf() { return null; } },
{ valueOf() { return undefined; } },
{ valueOf: null },
Object.create(null)
].forEach((input) => {
throws(() => Buffer.from(input), err);
});

[
new Number(true),
new MyBadPrimitive()
].forEach((input) => {
throws(() => Buffer.from(input),
/^TypeError: "value" argument must not be a number$/);
});