mirror of
https://github.com/nodejs/node.git
synced 2025-05-07 15:35:41 +00:00

So far we did not throw an error for all types of invalid input. Functions do not return a buffer anymore and `number` and `symbol` validation is also improved. PR-URL: https://github.com/nodejs/node/pull/26825 Fixes: https://github.com/nodejs/node/issues/26741 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
require('../common');
|
|
const { deepStrictEqual, throws } = require('assert');
|
|
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
|
|
);
|
|
|
|
[
|
|
[{}, 'object'],
|
|
[new Boolean(true), 'boolean'],
|
|
[{ valueOf() { return null; } }, 'object'],
|
|
[{ valueOf() { return undefined; } }, 'object'],
|
|
[{ valueOf: null }, 'object'],
|
|
[Object.create(null), 'object'],
|
|
[new Number(true), 'number'],
|
|
[new MyBadPrimitive(), 'number'],
|
|
[Symbol(), 'symbol'],
|
|
[5n, 'bigint'],
|
|
[(one, two, three) => {}, 'function'],
|
|
[undefined, 'undefined'],
|
|
[null, 'object']
|
|
].forEach(([input, actualType]) => {
|
|
const errObj = {
|
|
code: 'ERR_INVALID_ARG_TYPE',
|
|
name: 'TypeError',
|
|
message: 'The first argument must be one of type string, Buffer, ' +
|
|
'ArrayBuffer, Array, or Array-like Object. Received ' +
|
|
`type ${actualType}`
|
|
};
|
|
throws(() => Buffer.from(input), errObj);
|
|
throws(() => Buffer.from(input, 'hex'), errObj);
|
|
});
|
|
|
|
Buffer.allocUnsafe(10); // Should not throw.
|
|
Buffer.from('deadbeaf', 'hex'); // Should not throw.
|