import * as base64js from "base64-js" ; import { assert } from "./assert" ; param arr param encoding returns export function Uint8ArrayToString ( arr , encoding ? ) { switch ( encoding ) { case "base64" : { return base64js . fromByteArray ( arr ) ; } case "utf8" : case "utf-8" : case undefined : { return new TextDecoder ( ) . decode ( arr ) ; } default : { throw new Error ( "invalid/unsupported encoding" ) ; } } } param encoding export const stringToBuffer = ( input , encoding ) : ArrayBufferLike => IsoBuffer . from ( input , encoding ) . buffer ; param blob param encoding returns export const bufferToString = ( blob , encoding ) : string => IsoBuffer . from ( blob ) . toString ( encoding ) ; param obj export function isArrayBuffer ( obj ) { const maybe = obj as ( Partial < ArrayBuffer > & Partial < Uint8Array > ) | undefined ; return obj instanceof ArrayBuffer || ( typeof maybe === "object" && maybe !== null && typeof maybe . byteLength === "number" && typeof maybe . slice === "function" && maybe . byteOffset === undefined && maybe . buffer === undefined ) ; } /**
 * Minimal implementation of Buffer for our usages in the browser environment.
 */ export class IsoBuffer extends Uint8Array { param encoding public toString ( encoding ? ) { return Uint8ArrayToString ( this , encoding ) ; } param value param encodingOrOffset param length static from ( value , encodingOrOffset ? , length ? ) { if ( typeof value === "string" ) { return IsoBuffer . fromString ( value , encodingOrOffset as string | undefined ) ; } else if ( value !== null && typeof value === "object" && isArrayBuffer ( value . buffer ) ) { assert ( value . byteOffset === 0 , 0x000 ) ; assert ( value . byteLength === value . buffer . byteLength , 0x001 ) ; return IsoBuffer . fromArrayBuffer ( value . buffer , encodingOrOffset as number | undefined , length ) ; } else if ( isArrayBuffer ( value ) ) { return IsoBuffer . fromArrayBuffer ( value , encodingOrOffset as number | undefined , length ) ; } else { throw new TypeError ( ) ; } } static fromArrayBuffer ( arrayBuffer , byteOffset ? , byteLength ? ) { const offset = byteOffset ?? 0 ; const validLength = byteLength ?? arrayBuffer . byteLength - offset ; if ( offset < 0 || offset > arrayBuffer . byteLength || validLength < 0 || validLength + offset > arrayBuffer . byteLength ) { throw new RangeError ( ) ; } return new IsoBuffer ( arrayBuffer , offset , validLength ) ; } static fromString ( str , encoding ? ) { switch ( encoding ) { case "base64" : { const sanitizedString = this . sanitizeBase64 ( str ) ; const encoded = base64js . toByteArray ( sanitizedString ) ; return new IsoBuffer ( encoded . buffer ) ; } case "utf8" : case "utf-8" : case undefined : { const encoded = new TextEncoder ( ) . encode ( str ) ; return new IsoBuffer ( encoded . buffer ) ; } default : { throw new Error ( "invalid/unsupported encoding" ) ; } } } static isBuffer ( obj ) { throw new Error ( "unimplemented" ) ; } param str private static sanitizeBase64 ( str ) { let sanitizedStr = str ; sanitizedStr = sanitizedStr . split ( "=" ) [ 0 ] ; sanitizedStr = sanitizedStr . replace ( /[^\w+-/]/g , "" ) ; if ( sanitizedStr . length % 4 !== 0 ) { const paddingArray = [ "" , "===" , "==" , "=" ] ; sanitizedStr += paddingArray [ sanitizedStr . length % 4 ] ; } return sanitizedStr ; } }