lib/middleware.js
/**
   * Invoke the application callback and get the initial set of instructions
   */
Invoke the application callback and get the initial set of instructions
Invoke the application callback and get the initial set of instructions
async function invokeWebCallback(req, res, next) {
    const logger = req.locals.logger;
    const {rootSpan, application:app} = req.locals;
    let span;
    try {
      if (app.tasks) {
        app.tasks = normalizeJambones(logger, app.tasks).map((tdata) => makeTask(logger, tdata));
        if (0 === app.tasks.length) throw new Error('no application provided');
        return next();
      }
      const params = Object.assign(['POST', 'WS'].includes(app.call_hook.method) ? {sip: req.msg} : {},
        req.locals.callInfo);
      const obj = rootSpan.startChildSpan('performAppWebhook');
      span = obj.span;
      const json = await app.requestor.request('session:new', app.call_hook, params);
      app.tasks = normalizeJambones(logger, json).map((tdata) => makeTask(logger, tdata));
      span.setAttributes({
        'http.statusCode': 200,
        'app.tasks': listTaskNames(app.tasks)
      });
      span.end();
      if (0 === app.tasks.length) throw new Error('no application provided');
      next();
    } catch (err) {
      span?.setAttributes({webhookStatus: err.statusCode});
      span?.end();
      logger.info({err}, `Error retrieving or parsing application: ${err?.message}`);
      res.send(480, {headers: {'X-Reason': err?.message || 'unknown'}});
      app.requestor.close();
    }
  }
async function invokeWebCallback ( req , res , next ) { const logger = req . locals . logger ; const { rootSpan , application : app } = req . locals ; let span ; try { if ( app . tasks ) { app . tasks = normalizeJambones ( logger , app . tasks ) . map ( ( tdata ) => makeTask ( logger , tdata ) ) ; if ( 0 === app . tasks . length ) throw new Error ( 'no application provided' ) ; return next ( ) ; } const params = Object . assign ( [ 'POST' , 'WS' ] . includes ( app . call_hook . method ) ? { sip : req . msg } : { } , req . locals . callInfo ) ; const obj = rootSpan . startChildSpan ( 'performAppWebhook' ) ; span = obj . span ; const json = await app . requestor . request ( 'session:new' , app . call_hook , params ) ; app . tasks = normalizeJambones ( logger , json ) . map ( ( tdata ) => makeTask ( logger , tdata ) ) ; span . setAttributes ( { 'http.statusCode' : 200 , 'app.tasks' : listTaskNames ( app . tasks ) } ) ; span . end ( ) ; if ( 0 === app . tasks . length ) throw new Error ( 'no application provided' ) ; next ( ) ; } catch ( err ) { span ?. setAttributes ( { webhookStatus : err . statusCode } ) ; span ?. end ( ) ; logger . info ( { err } , ` ${ err ?. message } ` ) ; res . send ( 480 , { headers : { 'X-Reason' : err ?. message || 'unknown' } } ) ; app . requestor . close ( ) ; } }
Invoke the application callback and get the initial set of instructions
Invoke the application callback and get the initial set of instructions
/* retrieve the application to execute for this inbound call */