async function handleEvents(eventGate, conf, req, res) {

    // If empty body, return 400 now.
    if (_.isEmpty(req.body)) {
        res.statusMessage = 'Must provide JSON encoded events in request body.';
        req.logger.log('warn/events', res.statusMessage);
        res.status(400);
        res.end();
        return;
    }

    // Make sure events is an array, even if we were given only one event.
    // For possible usage of future
    const events = _.isArray(req.body) ? req.body : [req.body];

    // Provide this conf and request context to eventGate.process
    const context = {
        req,
        conf
    };

    // TODO: bikeshed this query param name.
    // If the requester wants a hasty response, return now!
    if (req.query.hasty) {
        res.statusMessage = `${events.length} events hastily received.`;
        res.status(204);
        res.end();
    }

    let results;
    try {
        // Process events (validate and produce)
        results = await eventGate.process(events, context);
    } catch (err) {
        // Error and end response now if we encounter anything unexpected.
        // This probably shouldn't happen, as eventGate.process should catch Errors
        // and reform them into error EventStatuses.
        res.statusMessage =
            `Encountered an unexpected error while processing events: ${err.message}`;
        req.logger.log('error/events', { error: err, message: res.statusMessage });
        res.status(500);
        res.end();
        throw err;
    }

    // Respond with appropriate HTTP status based on status of processing all events.
    const successCount = results.success.length;
    const invalidCount = results.invalid.length;
    const errorCount   = results.error.length;
    const failureCount = results.invalid.length + results.error.length;

    if (failureCount === 0) {
        // No failures, all events produced successfully: 204
        const statusMessage =
            `All ${successCount} out of ${events.length} events were accepted.`;
        req.logger.log('debug/events', statusMessage);

        // Only set response if it hasn't yet finished,
        // i.e. hasty response was not requested.
        if (!res.finished) {
            res.statusMessage = statusMessage;
            res.status(204);
            res.end();
        }
    } else if (invalidCount === events.length) {
        // All events were invalid: 400
        const statusMessage = `${invalidCount} out of ${events.length} ` +
            `events were invalid and not accepted.`;
        req.logger.log(
            'warn/events',
            { invalid: results.invalid, message: statusMessage }
        );

        if (!res.finished) {
            res.statusMessage = statusMessage;
            res.status(400);
            res.json({ invalid: results.invalid });
        }
    } else if (failureCount !== events.length) {
        // Some successes, but also some failures (invalid or errored): 207
        const statusMessage = `${results.success.length} out of ${events.length} ` +
            `events were accepted, but ${failureCount} failed (${invalidCount} ` +
            `invalid and ${errorCount} errored).`;

        req.logger.log(
            'warn/events',
            { error: results.error, invalid: results.invalid, message: statusMessage }
        );

        if (!res.finished) {
            req.logger.log('info/events', 'FINISHING REQ');

            res.statusMessage = statusMessage;
            res.status(207);
            res.json({ invalid: results.invalid, error: results.error });
        }
    } else {
        // All events had some failure eith at least one error
        // (some might have been invalid): 500
        const statusMessage = `${failureCount} out of ${events.length} ` +
            `events had failures and were not accepted. (${invalidCount} ` +
            `invalid and ${errorCount} errored).`;
        req.logger.log(
            'error/events',
            { errors: results.error, message: statusMessage }
        );

        if (!res.finished) {
            res.statusMessage = statusMessage;
            res.status(500);
            res.json({ invalid: results.invalid, error: results.error });
        }
    }
}
/**
 * Handles incoming JSON events in req.body with the EventGate instance.
 * @param {EventGate} eventGate
 * @param {Object} conf config object to provide in eventGate.process context.
 * @param {http.ClientRequest} req
 * @param {http.ServerResponse} res
 */
Handles incoming JSON events in req.body with the EventGate instance.
@param {EventGate} eventGate
@param {Object} conf config object to provide in eventGate.process context.
Handles incoming JSON events in req . body with the EventGate instance . @param { EventGate } eventGate @param { Object } conf config object to provide in eventGate . process context .
async function handleEvents(eventGate, conf, req, res) {
    if (_.isEmpty(req.body)) {
        res.statusMessage = 'Must provide JSON encoded events in request body.';
        req.logger.log('warn/events', res.statusMessage);
        res.status(400);
        res.end();
        return;
    }
    const events = _.isArray(req.body) ? req.body : [req.body];
    const context = {
        req,
        conf
    };
    if (req.query.hasty) {
        res.statusMessage = `${events.length} events hastily received.`;
        res.status(204);
        res.end();
    }
    let results;
    try {
        results = await eventGate.process(events, context);
    } catch (err) {
        res.statusMessage =
            `Encountered an unexpected error while processing events: ${err.message}`;
        req.logger.log('error/events', { error: err, message: res.statusMessage });
        res.status(500);
        res.end();
        throw err;
    }
    const successCount = results.success.length;
    const invalidCount = results.invalid.length;
    const errorCount   = results.error.length;
    const failureCount = results.invalid.length + results.error.length;
    if (failureCount === 0) {
        const statusMessage =
            `All ${successCount} out of ${events.length} events were accepted.`;
        req.logger.log('debug/events', statusMessage);
        if (!res.finished) {
            res.statusMessage = statusMessage;
            res.status(204);
            res.end();
        }
    } else if (invalidCount === events.length) {
        const statusMessage = `${invalidCount} out of ${events.length} ` +
            `events were invalid and not accepted.`;
        req.logger.log(
            'warn/events',
            { invalid: results.invalid, message: statusMessage }
        );
        if (!res.finished) {
            res.statusMessage = statusMessage;
            res.status(400);
            res.json({ invalid: results.invalid });
        }
    } else if (failureCount !== events.length) {
        const statusMessage = `${results.success.length} out of ${events.length} ` +
            `events were accepted, but ${failureCount} failed (${invalidCount} ` +
            `invalid and ${errorCount} errored).`;
        req.logger.log(
            'warn/events',
            { error: results.error, invalid: results.invalid, message: statusMessage }
        );
        if (!res.finished) {
            req.logger.log('info/events', 'FINISHING REQ');
            res.statusMessage = statusMessage;
            res.status(207);
            res.json({ invalid: results.invalid, error: results.error });
        }
    } else {
        const statusMessage = `${failureCount} out of ${events.length} ` +
            `events had failures and were not accepted. (${invalidCount} ` +
            `invalid and ${errorCount} errored).`;
        req.logger.log(
            'error/events',
            { errors: results.error, message: statusMessage }
        );
        if (!res.finished) {
            res.statusMessage = statusMessage;
            res.status(500);
            res.json({ invalid: results.invalid, error: results.error });
        }
    }
}
async function handleEvents ( eventGate , conf , req , res ) { if ( _ . isEmpty ( req . body ) ) { res . statusMessage = 'Must provide JSON encoded events in request body.' ; req . logger . log ( 'warn/events' , res . statusMessage ) ; res . status ( 400 ) ; res . end ( ) ; return ; } const events = _ . isArray ( req . body ) ? req . body : [ req . body ] ; const context = { req , conf } ; if ( req . query . hasty ) { res . statusMessage = ` ${ events . length } ` ; res . status ( 204 ) ; res . end ( ) ; } let results ; try { results = await eventGate . process ( events , context ) ; } catch ( err ) { res . statusMessage = ` ${ err . message } ` ; req . logger . log ( 'error/events' , { error : err , message : res . statusMessage } ) ; res . status ( 500 ) ; res . end ( ) ; throw err ; } const successCount = results . success . length ; const invalidCount = results . invalid . length ; const errorCount = results . error . length ; const failureCount = results . invalid . length + results . error . length ; if ( failureCount === 0 ) { const statusMessage = ` ${ successCount } ${ events . length } ` ; req . logger . log ( 'debug/events' , statusMessage ) ; if ( ! res . finished ) { res . statusMessage = statusMessage ; res . status ( 204 ) ; res . end ( ) ; } } else if ( invalidCount === events . length ) { const statusMessage = ` ${ invalidCount } ${ events . length } ` + ` ` ; req . logger . log ( 'warn/events' , { invalid : results . invalid , message : statusMessage } ) ; if ( ! res . finished ) { res . statusMessage = statusMessage ; res . status ( 400 ) ; res . json ( { invalid : results . invalid } ) ; } } else if ( failureCount !== events . length ) { const statusMessage = ` ${ results . success . length } ${ events . length } ` + ` ${ failureCount } ${ invalidCount } ` + ` ${ errorCount } ` ; req . logger . log ( 'warn/events' , { error : results . error , invalid : results . invalid , message : statusMessage } ) ; if ( ! res . finished ) { req . logger . log ( 'info/events' , 'FINISHING REQ' ) ; res . statusMessage = statusMessage ; res . status ( 207 ) ; res . json ( { invalid : results . invalid , error : results . error } ) ; } } else { const statusMessage = ` ${ failureCount } ${ events . length } ` + ` ${ invalidCount } ` + ` ${ errorCount } ` ; req . logger . log ( 'error/events' , { errors : results . error , message : statusMessage } ) ; if ( ! res . finished ) { res . statusMessage = statusMessage ; res . status ( 500 ) ; res . json ( { invalid : results . invalid , error : results . error } ) ; } } }
Handles incoming JSON events in req.body with the EventGate instance.
Handles incoming JSON events in req . body with the EventGate instance .
// If empty body, return 400 now. // Make sure events is an array, even if we were given only one event. // For possible usage of future // Provide this conf and request context to eventGate.process // TODO: bikeshed this query param name. // If the requester wants a hasty response, return now! // Process events (validate and produce) // Error and end response now if we encounter anything unexpected. // This probably shouldn't happen, as eventGate.process should catch Errors // and reform them into error EventStatuses. // Respond with appropriate HTTP status based on status of processing all events. // No failures, all events produced successfully: 204 // Only set response if it hasn't yet finished, // i.e. hasty response was not requested. // All events were invalid: 400 // Some successes, but also some failures (invalid or errored): 207 // All events had some failure eith at least one error // (some might have been invalid): 500