byte-encodings
Version:
Utilities for encoding and decoding common formats like hex, base64, and varint. Ported from Deno's @std/encoding.
2 lines (1 loc) • 1.03 kB
JavaScript
import{a as o,b as A,c as y,d as i,e as b}from"./chunk-KSXH2JGP.js";import{a as l}from"./chunk-JF3YMNGM.js";function U(e,r={}){r.alphabet??="base64",typeof e=="string"?e=new TextEncoder().encode(e):e instanceof ArrayBuffer&&(e=new Uint8Array(e));let[a,n]=l(e,y(e.length)),t=i(a,n,0,o[r.alphabet],61);return r.alphabet==="base64url"&&(t=a.indexOf(61,t-2),t>0&&(a=a.subarray(0,t))),new TextDecoder().decode(a)}function _(e,r,a={}){a.alphabet??="base64",typeof e=="string"?e=new TextEncoder().encode(e):e instanceof ArrayBuffer&&(e=new Uint8Array(e));let n=y(e.length);if(r.length<n)throw new RangeError("Cannot encode input as base64: Output too small");r=r.subarray(0,n);let t=n-e.length;r.set(e,t);let f=i(r,t,0,o[a.alphabet],61);if(a.alphabet==="base64url"){let c=r.indexOf(61,f-2);if(c>0)return c}return f}function B(e,r={}){return r.alphabet??="base64",typeof e=="string"&&(e=new TextEncoder().encode(e)),e.subarray(0,b(e,0,0,A[r.alphabet],61))}export{y as calcSizeBase64,B as decodeBase64,U as encodeBase64,_ as encodeIntoBase64};