import { Buffer } from 'buffer' ; import jconv from 'jconv' ; export async function sleep ( msec ) { await new Promise ( resolve => setTimeout ( resolve , msec ) ) ; } export function withTimeout < T > ( timeoutInMs , cb ) { return new Promise ( async ( resolve , reject ) => { let timer = setTimeout ( ( ) => { reject ( new Error ( `Operation timed out` ) ) ; } , timeoutInMs ) ; let result = await cb ( ) ; clearTimeout ( timer ) ; resolve ( result ) ; } ) ; } export function assert ( condition , message ? ) { if ( condition ) { return ; } message = message || 'no message provided' ; throw new Error ( `Assertion failed: ${ message }` ) ; } export function assertBigInt ( value , message ? ) { if ( typeof value === 'bigint' ) { return value as bigint ; } throw assert ( false , `Expected BigInt type - ${ message }` ) ; } export function assertNumber ( value , message ? ) { if ( typeof value === 'number' ) { return value as number ; } throw assert ( false , `Expected number type - ${ message }` ) ; } export function assertString ( value , message ? ) { if ( typeof value === 'string' ) { return value as string ; } throw assert ( false , `Expected string type - ${ message }` ) ; } export function assertUint8Array ( value , message ? ) { if ( value instanceof Uint8Array ) { return value as Uint8Array ; } throw assert ( false , `Expected Uint8Array type - ${ message }` ) ; } export function stringToCharCodeArray ( str ) { let result = new Array ( str . length ) ; for ( let i = 0 ; i < str . length ; i ++ ) { result [ i ] = str . charCodeAt ( i ) ; } return result ; } export function arrayBuffersAreEqual ( a , b ) { return dataViewsAreEqual ( new DataView ( a ) , new DataView ( b ) ) ; } export function dataViewsAreEqual ( a , b ) { if ( a . byteLength !== b . byteLength ) return false ; for ( let i = 0 ; i < a . byteLength ; i ++ ) { if ( a . getUint8 ( i ) !== b . getUint8 ( i ) ) return false ; } return true ; } export function isBigEndian ( ) { return new Uint8Array ( new Uint32Array ( [ 0x12345678 ] ) . buffer ) [ 0 ] === 0x12 ; } export function hexEncode ( str ) { let hex ; let result = '' ; for ( let i = 0 ; i < str . length ; i ++ ) { hex = str . charCodeAt ( i ) . toString ( 16 ) ; result += ( '0' + hex ) . slice ( - 2 ) ; } return result ; } export function arrayBufferToBinaryString ( ab ) { let src = new Uint8Array ( ab ) ; return uint8arrayToBinaryString ( src ) ; } export function uint8arrayToBinaryString ( u8a ) { let dst = Array ( u8a . length ) ; u8a . forEach ( ( c , i ) => ( dst [ i ] = String . fromCharCode ( c ) ) ) ; return dst . join ( '' ) ; } export function concatUint8Arrays ( ... args ) { let totalLength = 0 ; for ( let a of args ) { totalLength += a . length ; } let res = new Uint8Array ( totalLength ) ; let offset = 0 ; for ( let a of args ) { res . set ( a , offset ) ; offset += a . length ; } return res ; } export function concatArrayBuffers ( buffer1 , buffer2 ) { var tmp = new Uint8Array ( buffer1 . byteLength + buffer2 . byteLength ) ; tmp . set ( new Uint8Array ( buffer1 ) , 0 ) ; tmp . set ( new Uint8Array ( buffer2 ) , buffer1 . byteLength ) ; return tmp . buffer ; } function wordToByteArray ( word , length ) { let ba = [ ] , xFF = 0xff ; if ( length > 0 ) ba . push ( word >>> 24 ) ; if ( length > 1 ) ba . push ( ( word >>> 16 ) & xFF ) ; if ( length > 2 ) ba . push ( ( word >>> 8 ) & xFF ) ; if ( length > 3 ) ba . push ( word & xFF ) ; return ba ; } export function wordArrayToByteArray ( wordArray , length = wordArray . sigBytes ) { let res = new Uint8Array ( length ) ; let bytes ; let i = 0 ; let offset = 0 ; while ( length > 0 ) { bytes = wordToByteArray ( wordArray . words [ i ] , Math . min ( 4 , length ) ) ; res . set ( bytes , offset ) ; length -= bytes . length ; offset += bytes . length ; i ++ ; } return res ; } export function timeToFrames ( time ) { assert ( time . length === 4 ) ; return ( ( time [ 0 ] * 60 + time [ 1 ] ) * 60 + time [ 2 ] ) * 512 + time [ 3 ] ; } export function pad ( str , pad ) { return ( pad + str ) . slice ( - pad . length ) ; } export function formatTimeFromFrames ( value , frames = true ) { let f = value % 512 ; value = ( value - f ) / 512 ; let s = value % 60 ; value = ( value - s ) / 60 ; let m = value % 60 ; value = ( value - m ) / 60 ; let h = value ; return `${ pad ( h , '00' ) }:${ pad ( m , '00' ) }:${ pad ( s , '00' ) }` + ( frames ? `+${ pad ( f , '000' ) }` : `` ) ; } export function halfWidthToFullWidthRange ( range ) { const mappings = { '0' : '０' , '1' : '１' , '2' : '２' , '3' : '３' , '4' : '４' , '5' : '５' , '6' : '６' , '7' : '７' , '8' : '８' , '9' : '９' , '-' : '－' , '/' : '／' , ';' : '；' , } ; return range . split ( '' ) . map ( n => mappings [ n ] ?? '' ) . join ( '' ) ; } export function sanitizeTrackTitle ( title ) { return encodeURIComponent ( title ) ; } export function encodeToSJIS ( utf8String ) { return jconv . encode ( utf8String , 'SJIS' ) ; } export function decodeFromSJIS ( sjisBuffer ) { return jconv . decode ( Buffer . from ( sjisBuffer ) , 'SJIS' ) ; } export function getLengthAfterEncodingToSJIS ( utf8String ) { return encodeToSJIS ( utf8String ) . length ; }