UNPKG

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.97 kB
var c=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var m=Object.prototype.hasOwnProperty;var h=(r,e)=>{for(var n in e)c(r,n,{get:e[n],enumerable:!0})},g=(r,e,n,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of _(e))!m.call(r,o)&&o!==n&&c(r,o,{get:()=>e[o],enumerable:!(t=x(e,o))||t.enumerable});return r};var l=r=>g(c({},"__esModule",{value:!0}),r);var b={};h(b,{calcSizeHex:()=>i,decodeHex:()=>p,encodeHex:()=>w,encodeIntoHex:()=>f});module.exports=l(b);var a=new TextEncoder().encode("0123456789abcdef"),y=new Uint8Array(128).fill(16);a.forEach((r,e)=>y[r]=e);new TextEncoder().encode("ABCDEF").forEach((r,e)=>y[r]=e+10);function i(r){return r*2}function s(r,e,n,t){for(;e<r.length;++e){let o=r[e];r[n++]=t[o>>4],r[n++]=t[o&15]}return n}function d(r,e,n,t){if((r.length-n)%2===1)throw new RangeError(`Cannot decode input as hex: Length (${r.length-n}) must be divisible by 2`);for(e+=1;e<r.length;e+=2)r[n++]=A(r[e-1],t)<<4|A(r[e],t);return n}function A(r,e){let n=e[r]??16;if(n===16)throw new TypeError(`Cannot decode input as hex: Invalid character (${String.fromCharCode(r)})`);return n}function U(r,e){let n=r.length;if(r.byteOffset){let t=new Uint8Array(r.buffer);t.set(r),r=t.subarray(0,n)}return r=new Uint8Array(r.buffer.transfer(e)),r.set(r.subarray(0,n),e-n),[r,e-n]}function w(r){typeof r=="string"?r=new TextEncoder().encode(r):r instanceof ArrayBuffer&&(r=new Uint8Array(r));let[e,n]=U(r,i(r.length));return s(e,n,0,a),new TextDecoder().decode(e)}function f(r,e){typeof r=="string"?r=new TextEncoder().encode(r):r instanceof ArrayBuffer&&(r=new Uint8Array(r));let n=i(r.length);if(e.length<n)throw new RangeError("Cannot encode input as hex: Output too small");e=e.subarray(0,n);let t=n-r.length;return e.set(r,t),s(e,t,0,a)}function p(r){return typeof r=="string"&&(r=new TextEncoder().encode(r)),r.subarray(0,d(r,0,0,y))}0&&(module.exports={calcSizeHex,decodeHex,encodeHex,encodeIntoHex});