import { CppCas } from './binding' ; import { DurabilityLevel } from './generaltypes' ; import parseDuration from 'parse-duration' ; import * as qs from 'querystring' ; category export type Cas = CppCas ; category export interface NodeCallback < T > { ( err , result ) : void ; } internal export class PromiseHelper { internal static wrapAsync < T , U extends Promise < T > > ( fn , callback ? ) { if ( callback ) { const prom = fn ( ) ; prom . then ( ( res ) => callback ( null , res ) ) . catch ( ( err ) => callback ( err , null ) ) ; return prom ; } return fn ( ) ; } internal static wrap < T > ( fn , callback ? ) { const prom = new Promise ( ( resolve , reject ) => { fn ( ( err , res ) => { if ( err ) { reject ( err as Error ) ; } else { resolve ( res as T ) ; } } ) ; } ) ; if ( callback ) { prom . then ( ( res ) => callback ( null , res ) ) . catch ( ( err ) => callback ( err , null ) ) ; } return prom ; } } internal export class CompoundTimeout { private _start : [ number , number ] ; private _timeout : number | undefined ; internal constructor ( timeout ) { this . _start = process . hrtime ( ) ; this . _timeout = timeout ; } internal left ( ) { if ( this . _timeout === undefined ) { return undefined ; } const period = process . hrtime ( this . _start ) ; const periodMs = period [ 0 ] * 1e3 + period [ 1 ] / 1e6 ; if ( periodMs > this . _timeout ) { return 0 ; } return this . _timeout - periodMs ; } internal expired ( ) { const timeLeft = this . left ( ) ; if ( timeLeft === undefined ) { return false ; } return timeLeft <= 0 ; } } internal export function msToGoDurationStr ( ms ? ) { if ( ms === undefined ) { return undefined ; } return `${ ms }ms` ; } internal export function goDurationStrToMs ( str ? ) { if ( str === undefined ) { return undefined ; } const duration = parseDuration ( str ) ; if ( duration === null ) { return undefined ; } return duration ; } internal export function duraLevelToNsServerStr ( level ) { if ( level === undefined ) { return undefined ; } if ( typeof level === 'string' ) { return level as string ; } if ( level === DurabilityLevel . None ) { return 'none' ; } else if ( level === DurabilityLevel . Majority ) { return 'majority' ; } else if ( level === DurabilityLevel . MajorityAndPersistOnMaster ) { return 'majorityAndPersistActive' ; } else if ( level === DurabilityLevel . PersistToMajority ) { return 'persistToMajority' ; } else { throw new Error ( 'invalid durability level specified' ) ; } } internal export function nsServerStrToDuraLevel ( level ) { if ( level === undefined ) { return DurabilityLevel . None ; } if ( level === 'none' ) { return DurabilityLevel . None ; } else if ( level === 'majority' ) { return DurabilityLevel . Majority ; } else if ( level === 'majorityAndPersistActive' ) { return DurabilityLevel . MajorityAndPersistOnMaster ; } else if ( level === 'persistToMajority' ) { return DurabilityLevel . PersistToMajority ; } else { throw new Error ( 'invalid durability level string' ) ; } } internal export function cbQsStringify ( values , options ? ) { const cbValues = { } ; for ( const i in values ) { if ( values [ i ] === undefined ) { } else if ( typeof values [ i ] === 'boolean' ) { if ( options && options . boolAsString ) { cbValues [ i ] = values [ i ] ? 'true' : 'false' ; } else { cbValues [ i ] = values [ i ] ? 1 : 0 ; } } else { cbValues [ i ] = values [ i ] ; } } return qs . stringify ( cbValues ) ; }