packageDocumentation module hidden export const UNKNOWN_ERROR = 'UNKNOWN_ERROR' ; hidden export const NOT_IMPLEMENTED = 'NOT_IMPLEMENTED' ; hidden export const MISSING_NEW = 'MISSING_NEW' ; hidden export const CALL_EXCEPTION = 'CALL_EXCEPTION' ; hidden export const INVALID_ARGUMENT = 'INVALID_ARGUMENT' ; hidden export const MISSING_ARGUMENT = 'MISSING_ARGUMENT' ; hidden export const UNEXPECTED_ARGUMENT = 'UNEXPECTED_ARGUMENT' ; hidden export const NUMERIC_FAULT = 'NUMERIC_FAULT' ; hidden export const INSUFFICIENT_FUNDS = 'INSUFFICIENT_FUNDS' ; hidden export const NONCE_EXPIRED = 'NONCE_EXPIRED' ; hidden export const REPLACEMENT_UNDERPRICED = 'REPLACEMENT_UNDERPRICED' ; hidden export const UNSUPPORTED_OPERATION = 'UNSUPPORTED_OPERATION' ; hidden let _permanentCensorErrors = false ; hidden let _censorErrors = false ; hidden export function throwError ( message , code , params ) { if ( _censorErrors ) { throw new Error ( 'unknown error' ) ; } if ( ! code ) { code = UNKNOWN_ERROR ; } if ( ! params ) { params = { } ; } const messageDetails = [ ] ; Object . keys ( params ) . forEach ( ( key ) => { try { messageDetails . push ( key + '=' + JSON . stringify ( params [ key ] ) ) ; } catch ( error ) { messageDetails . push ( key + '=' + JSON . stringify ( params [ key ] . toString ( ) ) ) ; } } ) ; messageDetails . push ( 'version=' + '#version' ) ; const reason = message ; if ( messageDetails . length ) { message += ' (' + messageDetails . join ( ', ' ) + ')' ; } const error = new Error ( message ) ; error . reason = reason ; error . code = code ; Object . keys ( params ) . forEach ( ( key ) => { error [ key ] = params [ key ] ; } ) ; throw error ; } hidden export function checkNew ( self , kind ) { if ( ! ( self instanceof kind ) ) { throwError ( 'missing new' , MISSING_NEW , { name : kind . name } ) ; } } hidden export function checkArgumentCount ( count , expectedCount , suffix ? ) { if ( ! suffix ) { suffix = '' ; } if ( count < expectedCount ) { throwError ( 'missing argument' + suffix , MISSING_ARGUMENT , { count , expectedCount , } ) ; } if ( count > expectedCount ) { throwError ( 'too many arguments' + suffix , UNEXPECTED_ARGUMENT , { count , expectedCount , } ) ; } } hidden export function setCensorship ( censorship , permanent ? ) { if ( _permanentCensorErrors ) { throwError ( 'error censorship permanent' , UNSUPPORTED_OPERATION , { operation : 'setCensorship' , } ) ; } _censorErrors = ! ! censorship ; _permanentCensorErrors = ! ! permanent ; } hidden export function checkNormalize ( ) { try { [ 'NFD' , 'NFC' , 'NFKD' , 'NFKC' ] . forEach ( ( form ) => { try { 'test' . normalize ( form ) ; } catch ( error ) { throw new Error ( 'missing ' + form ) ; } } ) ; if ( String . fromCharCode ( 0xe9 ) . normalize ( 'NFD' ) !== String . fromCharCode ( 0x65 , 0x0301 ) ) { throw new Error ( 'broken implementation' ) ; } } catch ( error ) { throwError ( 'platform missing String.prototype.normalize' , UNSUPPORTED_OPERATION , { operation : 'String.prototype.normalize' , form : error . message , } ) ; } } hidden const LogLevels = { debug : 1 , default : 2 , info : 2 , warn : 3 , error : 4 , off : 5 , } ; hidden let LogLevel = LogLevels . default ; hidden export function setLogLevel ( logLevel ) { const level = LogLevels [ logLevel ] ; if ( level == null ) { warn ( 'invliad log level - ' + logLevel ) ; return ; } LogLevel = level ; } hidden function log ( logLevel , args ) { if ( LogLevel > LogLevels [ logLevel ] ) { return ; } console . log . apply ( console , args ) ; } hidden export function warn ( ... args ) { log ( 'warn' , args ) ; } hidden export function info ( ... args ) { log ( 'info' , args ) ; }