import os from 'os' ; import { ErrorCode , errorWithCode } from './errors' ; const pkg = require ( '../package.json' ) ; function escape ( s ) { return s . replace ( '/' , ':' ) . replace ( ' ' , '_' ) ; } export const errorCodes = { PROMISE_TIMEOUT : ErrorCode . PromiseTimeout , } ; param ms param promise export function promiseTimeout < T > ( ms , promise ) { let id ; const timeout = new Promise < never > ( ( _resolve , reject ) => { id = setTimeout ( ( ) => { clearTimeout ( id ) ; reject ( errorWithCode ( new Error ( 'Promise timed out' ) , ErrorCode . PromiseTimeout ) ) ; } , ms ) ; } ) ; return Promise . race ( [ promise as Promise < T > , timeout , ] ) . then ( ( value ) => { clearTimeout ( id ) ; return value ; } ) ; } export function packageIdentifier ( addons = { } ) { const identifierMap = { [ pkg . name ] : pkg . version , [ os . platform ( ) ] : os . release ( ) , node : process . version . replace ( 'v' , '' ) , ... addons , } ; return Object . keys ( identifierMap ) . reduce ( ( acc , k ) => `${ acc } ${ escape ( k ) }/${ escape ( identifierMap [ k ] ) }` , '' ) ; } param x export function isFalsy ( x ) { return x === 0 || x === '' || x === null || x === undefined || ( typeof x === 'number' && Number . isNaN ( x ) ) ; }