diff options
author | Satya Rohith <me@satyarohith.com> | 2024-10-02 13:53:14 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-02 08:23:14 +0000 |
commit | 32c12787361b65bbc55a7b9c1fe43689cb0a8b98 (patch) | |
tree | 047f47b5146b9192d74bd8ef1d9af9d183b7aad1 /ext/node/polyfills/internal | |
parent | 620e6b43a66c2af44ae4aea62417af408309f61c (diff) |
feat(ext/node): buffer.transcode() (#25972)
Closes https://github.com/denoland/deno/issues/25911
Diffstat (limited to 'ext/node/polyfills/internal')
-rw-r--r-- | ext/node/polyfills/internal/buffer.mjs | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/ext/node/polyfills/internal/buffer.mjs b/ext/node/polyfills/internal/buffer.mjs index 6e43a4903..6687f7394 100644 --- a/ext/node/polyfills/internal/buffer.mjs +++ b/ext/node/polyfills/internal/buffer.mjs @@ -6,7 +6,7 @@ // deno-lint-ignore-file prefer-primordials import { core } from "ext:core/mod.js"; -import { op_is_ascii, op_is_utf8 } from "ext:core/ops"; +import { op_is_ascii, op_is_utf8, op_transcode } from "ext:core/ops"; import { TextDecoder, TextEncoder } from "ext:deno_web/08_text_encoding.js"; import { codes } from "ext:deno_node/internal/error_codes.ts"; @@ -32,7 +32,11 @@ import { import { normalizeEncoding } from "ext:deno_node/internal/util.mjs"; import { validateBuffer } from "ext:deno_node/internal/validators.mjs"; import { isUint8Array } from "ext:deno_node/internal/util/types.ts"; -import { ERR_INVALID_STATE, NodeError } from "ext:deno_node/internal/errors.ts"; +import { + ERR_INVALID_STATE, + genericNodeError, + NodeError, +} from "ext:deno_node/internal/errors.ts"; import { forgivingBase64Encode, forgivingBase64UrlEncode, @@ -2598,6 +2602,48 @@ export function isAscii(input) { ], input); } +export function transcode(source, fromEnco, toEnco) { + if (!isUint8Array(source)) { + throw new codes.ERR_INVALID_ARG_TYPE( + "source", + ["Buffer", "Uint8Array"], + source, + ); + } + if (source.length === 0) { + return Buffer.alloc(0); + } + const code = "U_ILLEGAL_ARGUMENT_ERROR"; + const illegalArgumentError = genericNodeError( + `Unable to transcode Buffer [${code}]`, + { code: code, errno: 1 }, + ); + fromEnco = normalizeEncoding(fromEnco); + toEnco = normalizeEncoding(toEnco); + if (!fromEnco || !toEnco) { + throw illegalArgumentError; + } + // Return the provided source when transcode is not required + // for the from/to encoding pair. + const returnSource = fromEnco === toEnco || + fromEnco === "ascii" && toEnco === "utf8" || + fromEnco === "ascii" && toEnco === "latin1"; + if (returnSource) { + return Buffer.from(source); + } + + try { + const result = op_transcode(new Uint8Array(source), fromEnco, toEnco); + return Buffer.from(result, toEnco); + } catch (err) { + if (err.message.includes("Unable to transcode Buffer")) { + throw illegalArgumentError; + } else { + throw err; + } + } +} + export default { atob, btoa, @@ -2610,4 +2656,5 @@ export default { kMaxLength, kStringMaxLength, SlowBuffer, + transcode, }; |