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

Function encodeIntoBase64

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

Source from the content-addressed store, hash-verified

138 * ```
139 */
140export function encodeIntoBase64(
141 input: string | Uint8Array_ | ArrayBuffer,
142 output: Uint8Array_,
143 options: Base64Options = {},
144): number {
145 options.alphabet ??= "base64";
146 if (typeof input === "string") {
147 input = new TextEncoder().encode(input) as Uint8Array_;
148 } else if (input instanceof ArrayBuffer) {
149 input = new Uint8Array(input);
150 }
151 const min = calcSizeBase64((input as Uint8Array_).length);
152 if (output.length < min) {
153 throw new RangeError("Cannot encode input as base64: Output too small");
154 }
155 output = output.subarray(0, min);
156 const i = min - (input as Uint8Array_).length;
157 output.set(input as Uint8Array_, i);
158 const o = encode(output, i, 0, alphabet[options.alphabet], padding);
159 if (options.alphabet === "base64url") {
160 const i = output.indexOf(padding, o - 2);
161 if (i > 0) return i;
162 }
163 return o;
164}
165
166/**
167 * `decodeBase64` takes an input source and decodes it into a

Callers 1

Calls 3

calcSizeBase64Function · 0.90
encodeFunction · 0.90
setMethod · 0.65

Tested by

no test coverage detected