-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: extract out encoding validation functions #18421
Changes from 2 commits
f06ac13
8f4a276
b83472d
9ee5f96
077c258
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
if (typeof prop !== expected && !isArrayBufferView(prop)) | ||
throw new errors.Error('ERR_INVALID_ARG_TYPE', propName, expected); | ||
} | ||
|
||
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'); | ||
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By combining the check for the type with the second There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BridgeAR the |
||
|
||
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'); | ||
|
||
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'); | ||
|
||
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'); | ||
|
||
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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not only a extraction but it changes the validation itself as it also validates that the input is not an
ArrayBufferView
. I would like to keep it as is.