MCPcopy Create free account
hub / github.com/denoland/celld / transcode

Function transcode

crates/celld/js/node_buffer.js:734–766  ·  view source on GitHub ↗
(source, fromEnc, toEnc)

Source from the content-addressed store, hash-verified

732 // '?', like Node's ICU-backed transcode. The 128 MiB cap mirrors
733 // Workerd's isolate external-memory limit.
734 transcode(source, fromEnc, toEnc) {
735 const src = __u8(source);
736 if (!src) throw __bufType("source",
737 "an instance of Buffer or Uint8Array", source);
738 const from = __normEnc(fromEnc);
739 const to = __normEnc(toEnc);
740 const ok = ["ascii", "latin1", "utf8", "utf16le"];
741 if (!from || !ok.includes(from)) throw __badEnc(fromEnc);
742 if (!to || !ok.includes(to)) throw __badEnc(toEnc);
743 if (src.length >= 134217728)
744 throw new RangeError("Cannot transcode a buffer this large");
745 if (from === "utf16le" && src.length % 2 !== 0)
746 throw __bufErr(RangeError, "ERR_INVALID_ARG_VALUE",
747 "Unable to transcode buffer");
748 let s;
749 if (from === "utf8" && to === "utf16le") {
750 try {
751 s = new TextDecoder("utf-8", { fatal: true }).decode(src);
752 } catch {
753 throw new TypeError("Unable to transcode buffer");
754 }
755 } else if (from === "utf8") {
756 s = new TextDecoder().decode(src); // replacement characters
757 } else {
758 s = __decodeStr(src, from);
759 }
760 if (to === "ascii" || to === "latin1") {
761 const max = to === "ascii" ? 0x7f : 0xff;
762 s = Array.from(s,
763 (c) => c.charCodeAt(0) > max ? "?" : c).join("");
764 }
765 return new NodeBuffer(__encodeStr(s, to));
766 },
767 Blob: globalThis.Blob,
768 File: globalThis.File,
769 atob: globalThis.atob,

Callers

nothing calls this directly

Calls 11

__u8Function · 0.85
__bufTypeFunction · 0.85
__normEncFunction · 0.85
__badEncFunction · 0.85
__bufErrFunction · 0.85
__decodeStrFunction · 0.85
__encodeStrFunction · 0.85
includesMethod · 0.80
decodeMethod · 0.80
joinMethod · 0.80
fromMethod · 0.45

Tested by

no test coverage detected