Skip to content

Commit

Permalink
fix: Unknown encoding when undefined is used (#3252)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcandeia authored Jan 3, 2025
1 parent 16853b4 commit 9ce173d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
7 changes: 5 additions & 2 deletions src/node/internal/internal_buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import {
isUint8Array,
} from 'node-internal:internal_types';

import { normalizeEncoding } from 'node-internal:internal_utils';
import {
normalizeEncoding,
getEncodingOps,
} from 'node-internal:internal_utils';

import { validateString } from 'node-internal:validators';

Expand Down Expand Up @@ -648,7 +651,7 @@ Buffer.prototype.toString = function toString(
return '';
}

const normalizedEncoding = normalizeEncoding(`${encoding}`);
const normalizedEncoding = getEncodingOps(encoding);
if (normalizedEncoding === undefined) {
throw new ERR_UNKNOWN_ENCODING(`${encoding}`);
}
Expand Down
6 changes: 4 additions & 2 deletions src/node/internal/internal_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ export function normalizeEncoding(enc?: string): Encoding | undefined {
enc === 'UTF-8'
)
return UTF8;
return slowCases(enc);
return getEncodingOps(enc);
}

export function slowCases(enc: unknown): Encoding | undefined {
export function getEncodingOps(enc: unknown): Encoding | undefined {
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
enc += '';
// @ts-expect-error TS18046 TS complains about unknown can not have length.
switch (enc.length) {
case 4:
Expand Down
23 changes: 15 additions & 8 deletions src/workerd/api/node/tests/buffer-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,24 @@

import {
deepStrictEqual,
ok,
notDeepStrictEqual,
notStrictEqual,
ok,
strictEqual,
throws,
} from 'node:assert';
import util from 'node:util';

import {
Buffer,
File,
SlowBuffer,
kMaxLength,
kStringMaxLength,
constants,
isAscii,
isUtf8,
kMaxLength,
kStringMaxLength,
transcode,
File,
} from 'node:buffer';

import * as buffer from 'node:buffer';
Expand Down Expand Up @@ -5693,9 +5693,14 @@ export const toString = {
strictEqual(Buffer.from('666f6f', encoding).toString(encoding), '666f6f');
});

// default utf-8 if undefined
strictEqual(Buffer.from('utf-8').toString(), 'utf-8');

const invalidEncodings = new Array(10)
.fill(0)
.map((_, i) => String(i + 1).repeat(i + 1));
// Invalid encodings
for (let i = 1; i < 10; i++) {
const encoding = String(i).repeat(i);
for (const encoding of [...invalidEncodings, null]) {
const error = {
code: 'ERR_UNKNOWN_ENCODING',
name: 'TypeError',
Expand Down Expand Up @@ -5815,7 +5820,8 @@ export const toStringRange = {
},
{
name: 'TypeError',
}
},
'toString() with 0 and null as the encoding should have thrown'
);

throws(
Expand All @@ -5824,7 +5830,8 @@ export const toStringRange = {
},
{
name: 'TypeError',
}
},
'toString() with null encoding should have thrown'
);
},
};
Expand Down

0 comments on commit 9ce173d

Please sign in to comment.