镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
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 <rtrott@gmail.com>
1 parent 62d3beb commit 83f02c0

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎lib/buffer.js‎

Lines changed: 9 additions & 5 deletions
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)