mirror of
https://github.com/nodejs/node.git
synced 2025-05-05 17:10:40 +00:00

This will be a start to generalize all argument validation errors. As currently we throw ARG/OPT, OUT_OF_RANGE, and other more specific errors. The OPT errors didn't bring much to the errors as it's just another variant of ARG error which is sometimes more confusing (some of our code used OPT errors to denote just argument validation errors presumably because of similarity of OPT to 'option' and not 'options-object') and they don't specify the name of the options object where the invalid value is located. Much better approach would be to just specify path to the invalid value in the name of the value as it is done in this PR (i.e. 'options.format', 'options.publicKey.type' etc) Also since this decreases a variety of errors we have it'd be easier to reuse validation code across the codebase. Refs: https://github.com/nodejs/node/pull/31251 Refs: https://github.com/nodejs/node/pull/34070#discussion_r467251009 Signed-off-by: Denys Otrishko <shishugi@gmail.com> PR-URL: https://github.com/nodejs/node/pull/34682 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
const stream = require('stream');
|
|
const { inspect } = require('util');
|
|
|
|
{
|
|
// This test ensures that the stream implementation correctly handles values
|
|
// for highWaterMark which exceed the range of signed 32 bit integers and
|
|
// rejects invalid values.
|
|
|
|
// This number exceeds the range of 32 bit integer arithmetic but should still
|
|
// be handled correctly.
|
|
const ovfl = Number.MAX_SAFE_INTEGER;
|
|
|
|
const readable = stream.Readable({ highWaterMark: ovfl });
|
|
assert.strictEqual(readable._readableState.highWaterMark, ovfl);
|
|
|
|
const writable = stream.Writable({ highWaterMark: ovfl });
|
|
assert.strictEqual(writable._writableState.highWaterMark, ovfl);
|
|
|
|
for (const invalidHwm of [true, false, '5', {}, -5, NaN]) {
|
|
for (const type of [stream.Readable, stream.Writable]) {
|
|
assert.throws(() => {
|
|
type({ highWaterMark: invalidHwm });
|
|
}, {
|
|
name: 'TypeError',
|
|
code: 'ERR_INVALID_ARG_VALUE',
|
|
message: "The property 'options.highWaterMark' is invalid. " +
|
|
`Received ${inspect(invalidHwm)}`
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
{
|
|
// This test ensures that the push method's implementation
|
|
// correctly handles the edge case where the highWaterMark and
|
|
// the state.length are both zero
|
|
|
|
const readable = stream.Readable({ highWaterMark: 0 });
|
|
|
|
for (let i = 0; i < 3; i++) {
|
|
const needMoreData = readable.push();
|
|
assert.strictEqual(needMoreData, true);
|
|
}
|
|
}
|
|
|
|
{
|
|
// This test ensures that the read(n) method's implementation
|
|
// correctly handles the edge case where the highWaterMark, state.length
|
|
// and n are all zero
|
|
|
|
const readable = stream.Readable({ highWaterMark: 0 });
|
|
|
|
readable._read = common.mustCall();
|
|
readable.read(0);
|
|
}
|