From b0b204571b69ebf69be06ed5f2bb10da90f18c0e Mon Sep 17 00:00:00 2001 From: "Timothy O. Peters" Date: Sun, 28 Jan 2018 19:25:56 +0100 Subject: [PATCH] lib: extract validation functions PR-URL: https://github.com/nodejs/node/pull/18421 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- lib/internal/encoding.js | 59 ++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index 763ee42426b..fa178f3a8c7 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -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);