Skip to content
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

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ function validateDecoder(obj) {
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
}

function validateArgument(prop, expected, propName) {
if (typeof prop !== expected.toLowerCase())
throw new errors.Error('ERR_INVALID_ARG_TYPE', propName, expected);
function validateArgument(prop, expected, propName, expectedName) {
if (typeof prop !== expected)
throw new errors.Error('ERR_INVALID_ARG_TYPE', propName, expectedName);
}

function validateArgumentBuffer(prop, propName) {
if (!isArrayBuffer(prop) || !isArrayBufferView(prop))
throw new errors.Error('ERR_INVALID_ARG_TYPE', propName,
['ArrayBuffer', 'ArrayBufferView']);
}

const CONVERTER_FLAGS_FLUSH = 0x1;
Expand Down Expand Up @@ -341,7 +347,7 @@ const { hasConverter, TextDecoder } =
makeTextDecoderJS();

function hasTextDecoder(encoding = 'utf-8') {
validateArgument(encoding, 'string', 'encoding');
validateArgument(encoding, 'string', 'encoding', 'string');
return hasConverter(getEncodingFromLabel(encoding));
}

Expand All @@ -355,7 +361,7 @@ function makeTextDecoderICU() {
class TextDecoder {
constructor(encoding = 'utf-8', options = {}) {
encoding = `${encoding}`;
validateArgument(options, 'Object', 'options');
validateArgument(options, 'object', 'options', 'Object');

const enc = getEncodingFromLabel(encoding);
if (enc === undefined)
Expand All @@ -380,13 +386,12 @@ function makeTextDecoderICU() {

decode(input = empty, options = {}) {
validateDecoder(this);
if (isArrayBuffer(input)) {
validateArgumentBuffer(input, 'input');

if (isArrayBuffer(input))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in a second isArrayBuffer check and is definitely not desirable. Please keep that check as it was before (as in: remove validateArgumentBuffer again).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.

input = lazyBuffer().from(input);
} else if (!isArrayBufferView(input)) {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'input',
['ArrayBuffer', 'ArrayBufferView']);
}
validateArgument(options, 'Object', 'options');

validateArgument(options, 'object', 'options', 'Object');

var flags = 0;
if (options !== null)
Expand Down Expand Up @@ -423,7 +428,7 @@ function makeTextDecoderJS() {
class TextDecoder {
constructor(encoding = 'utf-8', options = {}) {
encoding = `${encoding}`;
validateArgument(options, 'Object', 'options');
validateArgument(options, 'object', 'options', 'Object');

const enc = getEncodingFromLabel(encoding);
if (enc === undefined || !hasConverter(enc))
Expand All @@ -447,16 +452,11 @@ function makeTextDecoderJS() {

decode(input = empty, options = {}) {
validateDecoder(this);
if (isArrayBuffer(input)) {
validateArgumentBuffer(input, 'input');

if (isArrayBuffer(input))
input = lazyBuffer().from(input);
} else if (isArrayBufferView(input)) {
input = lazyBuffer().from(input.buffer, input.byteOffset,
input.byteLength);
} else {
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'input',
['ArrayBuffer', 'ArrayBufferView']);
}
validateArgument(options, 'Object', 'options');
validateArgument(options, 'object', 'options', 'Object');

if (this[kFlags] & CONVERTER_FLAGS_FLUSH) {
this[kBOMSeen] = false;
Expand Down