lib/app.js
function handleIntentSuccess(req, res, next, data, intentObj) {
    let took = Date.now() - req.startAt,
      status = 200,
      isDone = false;
    /* IF we already have a status code, we just end the rqeuest right here. */
    try {
      if (typeof res.statusCode !== 'number') {
        res.status(status);
      }
      if (res.headersSent) {
        isDone = true;
      }
    } catch (e) {
    }

    if (isDone) {
      try {
        res.end();
      } catch (e) {
      }
    } else {
      // We're sending a string or HTML
      let contentType = res.get('content-type');
      if (typeof data === 'string') {
        if (!contentType) {
          let isHtml = data.indexOf("DOCTYPE ") !== -1 || data.indexOf("<html") !== -1 || data.indexOf("</") !== -1 || data.indexOf("/>") !== -1;
          if (isHtml) {
            res.type('html');
          } else {
            res.type('text');
          }
        }
        res.end(data);
      } else if (data instanceof Buffer) {
        // we have a buffer, possibly a download occurring
        try {
          if (!contentType) {
            res.set({
              'Content-Type': 'application/octet-stream'
            });
          }
          res.send(data);
        } catch (e) {
          console.error('Thorin.transport.http: failed to send buffer to response.');
          console.debug(e);
        }
      } else if (typeof data === 'object' && data != null) {
        try {
          try {
            data.result = data.result.toJSON();
          } catch (e) {
          }
          if (req.exposeType === false && data.type) {
            delete data.type;
          }
          try {
            data = this.parseIntentJson(null, data, req, intentObj);
          } catch (e) {
          }
          data = JSON.stringify(data);
          res.header('content-type', 'application/json');
          res.end(data);
        } catch (e) {
          console.error('Thorin.transport.http: failed to handleIntentSuccess', e);
          try {
            res.end();
          } catch (e) {
          }
        }
      } else {
        res.end(data);
      }
    }
    if (req._hasDebug !== false) {
      let logMsg = '[ENDED ' + req.uniqueId + "] - ";
      logMsg += req.action + ' ';
      logMsg += "(" + req.method.toUpperCase() + ' ' + req.originalUrl.substr(0, 64) + ') ';
      logMsg += '= ' + status + ' ';
      logMsg += '(' + took + 'ms)';
      logger('trace', logMsg);
    }

  }
function handleIntentSuccess(req, res, next, data, intentObj) {
    let took = Date.now() - req.startAt,
      status = 200,
      isDone = false;
    /* IF we already have a status code, we just end the rqeuest right here. */
    try {
      if (typeof res.statusCode !== 'number') {
        res.status(status);
      }
      if (res.headersSent) {
        isDone = true;
      }
    } catch (e) {
    }

    if (isDone) {
      try {
        res.end();
      } catch (e) {
      }
    } else {
      // We're sending a string or HTML
      let contentType = res.get('content-type');
      if (typeof data === 'string') {
        if (!contentType) {
          let isHtml = data.indexOf("DOCTYPE ") !== -1 || data.indexOf("<html") !== -1 || data.indexOf("</") !== -1 || data.indexOf("/>") !== -1;
          if (isHtml) {
            res.type('html');
          } else {
            res.type('text');
          }
        }
        res.end(data);
      } else if (data instanceof Buffer) {
        // we have a buffer, possibly a download occurring
        try {
          if (!contentType) {
            res.set({
              'Content-Type': 'application/octet-stream'
            });
          }
          res.send(data);
        } catch (e) {
          console.error('Thorin.transport.http: failed to send buffer to response.');
          console.debug(e);
        }
      } else if (typeof data === 'object' && data != null) {
        try {
          try {
            data.result = data.result.toJSON();
          } catch (e) {
          }
          if (req.exposeType === false && data.type) {
            delete data.type;
          }
          try {
            data = this.parseIntentJson(null, data, req, intentObj);
          } catch (e) {
          }
          data = JSON.stringify(data);
          res.header('content-type', 'application/json');
          res.end(data);
        } catch (e) {
          console.error('Thorin.transport.http: failed to handleIntentSuccess', e);
          try {
            res.end();
          } catch (e) {
          }
        }
      } else {
        res.end(data);
      }
    }
    if (req._hasDebug !== false) {
      let logMsg = '[ENDED ' + req.uniqueId + "] - ";
      logMsg += req.action + ' ';
      logMsg += "(" + req.method.toUpperCase() + ' ' + req.originalUrl.substr(0, 64) + ') ';
      logMsg += '= ' + status + ' ';
      logMsg += '(' + took + 'ms)';
      logger('trace', logMsg);
    }

  }
function handleIntentSuccess ( req , res , next , data , intentObj ) { let took = Date . now ( ) - req . startAt , status = 200 , isDone = false ; /* IF we already have a status code, we just end the rqeuest right here. */ try { if ( typeof res . statusCode !== 'number' ) { res . status ( status ) ; } if ( res . headersSent ) { isDone = true ; } } catch ( e ) { } if ( isDone ) { try { res . end ( ) ; } catch ( e ) { } } else { // We're sending a string or HTML let contentType = res . get ( 'content-type' ) ; if ( typeof data === 'string' ) { if ( ! contentType ) { let isHtml = data . indexOf ( "DOCTYPE " ) !== - 1 || data . indexOf ( "<html" ) !== - 1 || data . indexOf ( "</" ) !== - 1 || data . indexOf ( "/>" ) !== - 1 ; if ( isHtml ) { res . type ( 'html' ) ; } else { res . type ( 'text' ) ; } } res . end ( data ) ; } else if ( data instanceof Buffer ) { // we have a buffer, possibly a download occurring try { if ( ! contentType ) { res . set ( { 'Content-Type' : 'application/octet-stream' } ) ; } res . send ( data ) ; } catch ( e ) { console . error ( 'Thorin.transport.http: failed to send buffer to response.' ) ; console . debug ( e ) ; } } else if ( typeof data === 'object' && data != null ) { try { try { data . result = data . result . toJSON ( ) ; } catch ( e ) { } if ( req . exposeType === false && data . type ) { delete data . type ; } try { data = this . parseIntentJson ( null , data , req , intentObj ) ; } catch ( e ) { } data = JSON . stringify ( data ) ; res . header ( 'content-type' , 'application/json' ) ; res . end ( data ) ; } catch ( e ) { console . error ( 'Thorin.transport.http: failed to handleIntentSuccess' , e ) ; try { res . end ( ) ; } catch ( e ) { } } } else { res . end ( data ) ; } } if ( req . _hasDebug !== false ) { let logMsg = '[ENDED ' + req . uniqueId + "] - " ; logMsg += req . action + ' ' ; logMsg += "(" + req . method . toUpperCase ( ) + ' ' + req . originalUrl . substr ( 0 , 64 ) + ') ' ; logMsg += '= ' + status + ' ' ; logMsg += '(' + took + 'ms)' ; logger ( 'trace' , logMsg ) ; } }
/*
Handles the actual success of the request's result.
/ * Handles the actual success of the request s result .