MCPcopy Create free account
hub / github.com/denoland/std / encodeBase64

Function encodeBase64

encoding/unstable_base64.ts:82–102  ·  view source on GitHub ↗
(
  input: string | Uint8Array_ | ArrayBuffer,
  options: Base64Options = {},
)

Source from the content-addressed store, hash-verified

80 * ```
81 */
82export function encodeBase64(
83 input: string | Uint8Array_ | ArrayBuffer,
84 options: Base64Options = {},
85): string {
86 options.alphabet ??= "base64";
87 if (typeof input === "string") {
88 input = new TextEncoder().encode(input) as Uint8Array_;
89 } else if (input instanceof ArrayBuffer) {
90 input = new Uint8Array(input);
91 }
92 let [output, i] = detach(
93 input as Uint8Array_,
94 calcSizeBase64((input as Uint8Array_).length),
95 );
96 let o = encode(output, i, 0, alphabet[options.alphabet], padding);
97 if (options.alphabet === "base64url") {
98 o = output.indexOf(padding, o - 2);
99 if (o > 0) output = output.subarray(0, o);
100 }
101 return new TextDecoder().decode(output);
102}
103
104/**
105 * `encodeIntoBase64` takes an input source and encodes it as base64 into the

Callers

nothing calls this directly

Calls 3

detachFunction · 0.90
calcSizeBase64Function · 0.90
encodeFunction · 0.90

Tested by

no test coverage detected