Skip to content

Commit 4df3652

Browse files
committed
buffer: improve .from() error details
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 <[email protected]>
1 parent f570de8 commit 4df3652

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/buffer.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -291,17 +291,21 @@ Buffer.from = function from(value, encodingOrOffset, length) {
291291
return fromArrayBuffer(value, encodingOrOffset, length);
292292

293293
const valueOf = value.valueOf && value.valueOf();
294-
if (valueOf !== null && valueOf !== undefined && valueOf !== value)
295-
return Buffer.from(valueOf, encodingOrOffset, length);
294+
if (valueOf != null &&
295+
valueOf !== value &&
296+
(typeof valueOf === 'string' || typeof valueOf === 'object')) {
297+
return from(valueOf, encodingOrOffset, length);
298+
}
296299

297300
const b = fromObject(value);
298301
if (b)
299302
return b;
300303

301304
if (typeof value[SymbolToPrimitive] === 'function') {
302-
return Buffer.from(value[SymbolToPrimitive]('string'),
303-
encodingOrOffset,
304-
length);
305+
const primitive = value[SymbolToPrimitive]('string');
306+
if (typeof primitive === 'string') {
307+
return fromString(primitive, encodingOrOffset);
308+
}
305309
}
306310
}
307311

0 commit comments

Comments
 (0)