Skip to content

Commit 83f02c0

Browse files
BridgeARtargos
authored andcommitted
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 62d3beb commit 83f02c0

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
@@ -292,17 +292,21 @@ Buffer.from = function from(value, encodingOrOffset, length) {
292292
return fromArrayBuffer(value, encodingOrOffset, length);
293293

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

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

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

0 commit comments

Comments
 (0)