0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

lib: extract validation functions

PR-URL: https://github.com/nodejs/node/pull/18421
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Timothy O. Peters 2018-01-28 19:25:56 +01:00 committed by Ruben Bridgewater
parent 3206e20af6
commit b0b204571b
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -32,6 +32,21 @@ function lazyBuffer() {
return Buffer;
}
function validateEncoder(obj) {
if (obj == null || obj[kEncoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
}
function validateDecoder(obj) {
if (obj == null || obj[kDecoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
}
function validateArgument(prop, expected, propName, expectedName) {
if (typeof prop !== expected)
throw new errors.Error('ERR_INVALID_ARG_TYPE', propName, expectedName);
}
const CONVERTER_FLAGS_FLUSH = 0x1;
const CONVERTER_FLAGS_FATAL = 0x2;
const CONVERTER_FLAGS_IGNORE_BOM = 0x4;
@ -288,20 +303,17 @@ class TextEncoder {
}
get encoding() {
if (this == null || this[kEncoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
validateEncoder(this);
return 'utf-8';
}
encode(input = '') {
if (this == null || this[kEncoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
validateEncoder(this);
return encodeUtf8String(`${input}`);
}
[inspect](depth, opts) {
if (this == null || this[kEncoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
validateEncoder(this);
if (typeof depth === 'number' && depth < 0)
return opts.stylize('[Object]', 'special');
var ctor = getConstructorOf(this);
@ -329,8 +341,7 @@ const { hasConverter, TextDecoder } =
makeTextDecoderJS();
function hasTextDecoder(encoding = 'utf-8') {
if (typeof encoding !== 'string')
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'encoding', 'string');
validateArgument(encoding, 'string', 'encoding', 'string');
return hasConverter(getEncodingFromLabel(encoding));
}
@ -344,8 +355,7 @@ function makeTextDecoderICU() {
class TextDecoder {
constructor(encoding = 'utf-8', options = {}) {
encoding = `${encoding}`;
if (typeof options !== 'object')
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'Object');
validateArgument(options, 'object', 'options', 'Object');
const enc = getEncodingFromLabel(encoding);
if (enc === undefined)
@ -369,17 +379,14 @@ function makeTextDecoderICU() {
decode(input = empty, options = {}) {
if (this == null || this[kDecoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
validateDecoder(this);
if (isArrayBuffer(input)) {
input = lazyBuffer().from(input);
} else if (!isArrayBufferView(input)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'input',
['ArrayBuffer', 'ArrayBufferView']);
}
if (typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
}
validateArgument(options, 'object', 'options', 'Object');
var flags = 0;
if (options !== null)
@ -416,8 +423,7 @@ function makeTextDecoderJS() {
class TextDecoder {
constructor(encoding = 'utf-8', options = {}) {
encoding = `${encoding}`;
if (typeof options !== 'object')
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'Object');
validateArgument(options, 'object', 'options', 'Object');
const enc = getEncodingFromLabel(encoding);
if (enc === undefined || !hasConverter(enc))
@ -440,8 +446,7 @@ function makeTextDecoderJS() {
}
decode(input = empty, options = {}) {
if (this == null || this[kDecoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
validateDecoder(this);
if (isArrayBuffer(input)) {
input = lazyBuffer().from(input);
} else if (isArrayBufferView(input)) {
@ -451,9 +456,7 @@ function makeTextDecoderJS() {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'input',
['ArrayBuffer', 'ArrayBufferView']);
}
if (typeof options !== 'object') {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
}
validateArgument(options, 'object', 'options', 'Object');
if (this[kFlags] & CONVERTER_FLAGS_FLUSH) {
this[kBOMSeen] = false;
@ -496,27 +499,23 @@ function makeTextDecoderJS() {
TextDecoder.prototype,
Object.getOwnPropertyDescriptors({
get encoding() {
if (this == null || this[kDecoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
validateDecoder(this);
return this[kEncoding];
},
get fatal() {
if (this == null || this[kDecoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
validateDecoder(this);
return (this[kFlags] & CONVERTER_FLAGS_FATAL) === CONVERTER_FLAGS_FATAL;
},
get ignoreBOM() {
if (this == null || this[kDecoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
validateDecoder(this);
return (this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM) ===
CONVERTER_FLAGS_IGNORE_BOM;
},
[inspect](depth, opts) {
if (this == null || this[kDecoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
validateDecoder(this);
if (typeof depth === 'number' && depth < 0)
return opts.stylize('[Object]', 'special');
var ctor = getConstructorOf(this);