Skip to content

Commit

Permalink
buffer: improve .from() error details
Browse files Browse the repository at this point in the history
This makes sure the original input is passed to the error in case
no matching inputs are found. Instead of passing along all values,
only valid or possibliy valid values are passed through. That way
invalid values end up in the error case with the original input.

PR-URL: #29675
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
BridgeAR authored and targos committed Jan 14, 2020
1 parent 62d3beb commit 83f02c0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,21 @@ Buffer.from = function from(value, encodingOrOffset, length) {
return fromArrayBuffer(value, encodingOrOffset, length);

const valueOf = value.valueOf && value.valueOf();
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
return Buffer.from(valueOf, encodingOrOffset, length);
if (valueOf != null &&
valueOf !== value &&
(typeof valueOf === 'string' || typeof valueOf === 'object')) {
return from(valueOf, encodingOrOffset, length);
}

const b = fromObject(value);
if (b)
return b;

if (typeof value[SymbolToPrimitive] === 'function') {
return Buffer.from(value[SymbolToPrimitive]('string'),
encodingOrOffset,
length);
const primitive = value[SymbolToPrimitive]('string');
if (typeof primitive === 'string') {
return fromString(primitive, encodingOrOffset);
}
}
}

Expand Down

0 comments on commit 83f02c0

Please sign in to comment.