import { ErrorMessage , loggerMiddleware } from '@celo/phone-number-privacy-common' ; import Logger from 'bunyan' ; import * as functions from 'firebase-functions' ; import { performance , PerformanceObserver } from 'perf_hooks' ; import config , { FORNO_ALFAJORES , VERSION } from './config' ; import { handleGetContactMatches } from './match-making/get-contact-matches' ; import { handleGetBlindedMessageSig } from './signing/get-threshold-signature' ; require ( 'dotenv' ) . config ( ) ; async function meterResponse ( handler , req , res , endpoint ? ) { if ( ! res . locals ) { res . locals = { } ; } const logger = loggerMiddleware ( req , res ) ; logger . fields . endpoint = endpoint ; logger . info ( { req : req . body } , 'Request received' ) ; const eventLoopLagMeasurementStart = Date . now ( ) ; setTimeout ( ( ) => { const eventLoopLag = Date . now ( ) - eventLoopLagMeasurementStart ; logger . info ( { eventLoopLag } , 'Measure event loop lag' ) ; } ) ; const startMark = `Begin ${ handler . name }` ; const endMark = `End ${ handler . name }` ; const entryName = `${ handler . name } latency` ; const obs = new PerformanceObserver ( ( list ) => { const entry = list . getEntriesByName ( entryName ) [ 0 ] ; if ( entry ) { logger . info ( { latency : entry } , 'e2e response latency measured' ) ; } } ) ; obs . observe ( { entryTypes : [ 'measure' ] , buffered : true } ) ; performance . mark ( startMark ) ; await handler ( req , res ) . then ( ( ) => { logger . info ( { res } , 'Response sent' ) ; } ) . catch ( ( err ) => { logger . error ( ErrorMessage . UNKNOWN_ERROR ) ; logger . error ( err ) ; } ) ; performance . mark ( endMark ) ; performance . measure ( entryName , startMark , endMark ) ; performance . clearMarks ( ) ; obs . disconnect ( ) ; } export const getBlindedMessageSig = functions . region ( 'us-central1' , 'europe-west3' ) . runWith ( { minInstances : config . blockchain . provider === FORNO_ALFAJORES ? 0 : 3 , } ) . https . onRequest ( async ( req , res ) => meterResponse ( handleGetBlindedMessageSig , req , res , '/getBlindedMessageSig' ) ) ; export const getContactMatches = functions . region ( 'us-central1' , 'europe-west3' ) . runWith ( { minInstances : config . blockchain . provider === FORNO_ALFAJORES ? 0 : 3 , } ) . https . onRequest ( async ( req , res ) => meterResponse ( handleGetContactMatches , req , res , '/getContactMatches' ) ) ; export const status = functions . region ( 'us-central1' , 'europe-west3' ) . https . onRequest ( async ( _request , response ) => { response . status ( 200 ) . json ( { version : VERSION , } ) ; } ) ;