mirror of
https://github.com/denoland/deno.git
synced 2025-09-21 18:10:02 +00:00

Also remove a duplicated `normalizedEncoding` function. These node compatibility tests are now passing: - [test-string-decoder-end.js](https://github.com/nodejs/node/blob/v24.2.0/test/parallel/test-string-decoder-end.js) - [test-string-decoder-fuzz.js](https://github.com/nodejs/node/blob/v24.2.0/test/parallel/test-string-decoder-fuzz.js)
79 lines
2.4 KiB
TypeScript
79 lines
2.4 KiB
TypeScript
// Copyright 2018-2025 the Deno authors. MIT license.
|
|
|
|
import { primordials } from "ext:core/mod.js";
|
|
const { StringPrototypeToLowerCase } = primordials;
|
|
import type { TextEncodings } from "ext:deno_node/_utils.ts";
|
|
|
|
// https://github.com/nodejs/node/blob/a73b575304722a3682fbec3a5fb13b39c5791342/lib/internal/util.js#L252
|
|
export function normalizeEncoding(
|
|
enc: string | null,
|
|
): TextEncodings | undefined {
|
|
if (enc == null || enc === "utf8" || enc === "utf-8") return "utf8";
|
|
return slowCases(enc);
|
|
}
|
|
|
|
function slowCases(enc: string): TextEncodings | undefined {
|
|
switch (enc.length) {
|
|
case 4:
|
|
if (enc === "UTF8") return "utf8";
|
|
if (enc === "ucs2" || enc === "UCS2") return "utf16le";
|
|
enc = StringPrototypeToLowerCase(`${enc}`);
|
|
if (enc === "utf8") return "utf8";
|
|
if (enc === "ucs2") return "utf16le";
|
|
break;
|
|
case 3:
|
|
if (
|
|
enc === "hex" || enc === "HEX" ||
|
|
StringPrototypeToLowerCase(`${enc}`) === "hex"
|
|
) {
|
|
return "hex";
|
|
}
|
|
break;
|
|
case 5:
|
|
if (enc === "ascii") return "ascii";
|
|
if (enc === "ucs-2") return "utf16le";
|
|
if (enc === "UTF-8") return "utf8";
|
|
if (enc === "ASCII") return "ascii";
|
|
if (enc === "UCS-2") return "utf16le";
|
|
enc = StringPrototypeToLowerCase(`${enc}`);
|
|
if (enc === "utf-8") return "utf8";
|
|
if (enc === "ascii") return "ascii";
|
|
if (enc === "ucs-2") return "utf16le";
|
|
break;
|
|
case 6:
|
|
if (enc === "base64") return "base64";
|
|
if (enc === "latin1" || enc === "binary") return "latin1";
|
|
if (enc === "BASE64") return "base64";
|
|
if (enc === "LATIN1" || enc === "BINARY") return "latin1";
|
|
enc = StringPrototypeToLowerCase(`${enc}`);
|
|
if (enc === "base64") return "base64";
|
|
if (enc === "latin1" || enc === "binary") return "latin1";
|
|
break;
|
|
case 7:
|
|
if (
|
|
enc === "utf16le" || enc === "UTF16LE" ||
|
|
StringPrototypeToLowerCase(`${enc}`) === "utf16le"
|
|
) {
|
|
return "utf16le";
|
|
}
|
|
break;
|
|
case 8:
|
|
if (
|
|
enc === "utf-16le" || enc === "UTF-16LE" ||
|
|
StringPrototypeToLowerCase(`${enc}`) === "utf-16le"
|
|
) {
|
|
return "utf16le";
|
|
}
|
|
break;
|
|
case 9:
|
|
if (
|
|
enc === "base64url" || enc === "BASE64URL" ||
|
|
StringPrototypeToLowerCase(`${enc}`) === "base64url"
|
|
) {
|
|
return "base64url";
|
|
}
|
|
break;
|
|
default:
|
|
if (enc === "") return "utf8";
|
|
}
|
|
}
|