UNPKG

api-service-core

Version:

NodeJS api-service

60 lines 2.32 kB
"use strict"; /************************************************************************* * * Troven CONFIDENTIAL * __________________ * * (c) 2017-2019 Troven Pty Ltd * All Rights Reserved. * * NOTICE: All information contained herein is, and remains * the property of Troven Pty Ltd and its licensors, * if any. The intellectual and technical concepts contained * herein are proprietary to Troven Pty Ltd * and its suppliers and may be covered by International and Regional Patents, * patents in process, and are protected by trade secret or copyright law. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from Troven Pty Ltd. */ Object.defineProperty(exports, "__esModule", { value: true }); const uuidv5 = require("uuid/v5"); const os = require("os"); /** * Request UUID * ------------ * Feature generates a UUIDv5 and inject into X-REQUEST-UUID header * * @type {{name: string, title: string, description: string, options: {header: string}, fn: module.exports.fn}} */ class request_uuid { constructor() { this.name = "api.request_uuid"; this.title = "Inject a X-REQUEST-UUID into request and response headers"; this.options = { header: "X-REQUEST-UUID" }; this.header = function () { return this.options.header; }; } fn(operation, options) { let context = operation.context; let seed = os.hostname() + Date.now(); return function (req, res, next) { let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; let request_uuid = uuidv5(seed + ip, uuidv5.DNS); // inject a header into both request & response req.header(this.options.header, request_uuid); res.header(this.options.header, request_uuid); // auditing - defaults to true if (!(options.audit === false)) { context.audit({ uuid: context.uuid, request_uuid: request_uuid, code: "api:policy:request_uuid", message: req.method + " " + req.path }); } // continue processing request ... next(); }; } } exports.default = request_uuid; //# sourceMappingURL=request_uuid.js.map