import { EventEmitter } from 'events' ; import { JsonRpcMiddleware , PendingJsonRpcResponse } from 'json-rpc-engine' ; import { ethErrors } from 'eth-rpc-errors' ; export type Maybe < T > = Partial < T > | null | undefined ; export type ConsoleLike = Pick < Console , 'log' | 'warn' | 'error' | 'debug' | 'info' | 'trace' > ; param log returns export function createErrorMiddleware ( log ) { return ( req , res , next ) => { if ( typeof req . method !== 'string' || ! req . method ) { res . error = ethErrors . rpc . invalidRequest ( { message : `The request 'method' must be a non-empty string.` , data : req , } ) ; } next ( ( done ) => { const { error } = res ; if ( ! error ) { return done ( ) ; } log . error ( `MetaMask - RPC Error: ${ error . message }` , error ) ; return done ( ) ; } ) ; } ; } export const getRpcPromiseCallback = ( resolve , reject , unwrapResult = true ) => ( error , response ) : void => { if ( error || response . error ) { reject ( error || response . error ) ; } else { ! unwrapResult || Array . isArray ( response ) ? resolve ( response ) : resolve ( response . result ) ; } } ; param log param remoteLabel param error param emitter export function logStreamDisconnectWarning ( log , remoteLabel , error , emitter ) { let warningMsg = `MetaMask: Lost connection to "${ remoteLabel }".` ; if ( error ?. stack ) { warningMsg += `\n${ error . stack }` ; } log . warn ( warningMsg ) ; if ( emitter && emitter . listenerCount ( 'error' ) > 0 ) { emitter . emit ( 'error' , warningMsg ) ; } } export const NOOP = ( ) => undefined ; export const EMITTED_NOTIFICATIONS = [ 'eth_subscription' , ] ;