From 1eb657ab764a3597e909c61855550d80ff33cee4 Mon Sep 17 00:00:00 2001 From: Andrey Pechkurov <37772591+puzpuzpuz@users.noreply.github.com> Date: Fri, 12 Jun 2020 20:31:39 +0300 Subject: [PATCH] Migrate from factory function to single instance of Fastify plugin (#34) Also fixes misprints in readme --- README.md | 14 +++++----- index.d.ts | 4 +-- samples/fastify.winston.js | 2 +- src/rtracer.js | 56 +++++++++++++++++++------------------- tests/fastify.test.js | 2 +- 5 files changed, 38 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 740789a..d950074 100644 --- a/README.md +++ b/README.md @@ -87,18 +87,18 @@ Use the plugin provided by the library: const fastify = require('fastify')() const rTracer = require('cls-rtracer') -// any third party middleware that does not need access to request ids goes here +// any third party plugin that does not need access to request ids goes here // ... -fastify.use(rTracer.fastifyPlugin()) -// optionally, you can override default middleware config: -// fastify.use(rTracer.fastifyPlugin({ +fastify.register(rTracer.fastifyPlugin) +// optionally, you can override default plugin config: +// fastify.register(rTracer.fastifyPlugin, { // useHeader: true, // headerName: 'X-Your-Request-Header', // useFastifyRequestId: true // })) -// all code in middlewares, starting from here, has access to request ids +// all code in plugins or handlers, starting from here, has access to request ids ``` Obtain request id in middlewares on the incoming request: @@ -204,12 +204,12 @@ const init = async () => { // ... await server.register({ - plugin: rtracer.hapiPlugin + plugin: rTracer.hapiPlugin }) // optionally, you can override default middleware config: // await server.register({ - // plugin: rtracer.hapiPlugin, + // plugin: rTracer.hapiPlugin, // options: { // useHeader: true, // headerName: 'X-Your-Request-Header' diff --git a/index.d.ts b/index.d.ts index ee98766..13574f4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -31,10 +31,8 @@ export declare const expressMiddleware: ( ) => void export declare const fastifyPlugin: ( - options?: IFastifyOptions, -) => ( fastify: any, - options: any, + options: IFastifyOptions, done: (err?: any) => void, ) => void diff --git a/samples/fastify.winston.js b/samples/fastify.winston.js index 9075cbc..8bff43b 100644 --- a/samples/fastify.winston.js +++ b/samples/fastify.winston.js @@ -28,7 +28,7 @@ const logger = createLogger({ const Fastify = require('fastify') const app = new Fastify() -app.register(rTracer.fastifyPlugin()) +app.register(rTracer.fastifyPlugin) app.get('/', async (request, reply) => { logger.info('Starting request handling') diff --git a/src/rtracer.js b/src/rtracer.js index 5048fa1..e709cdb 100644 --- a/src/rtracer.js +++ b/src/rtracer.js @@ -42,7 +42,7 @@ const expressMiddleware = ({ } /** - * Generates a request tracer plugin for Fastify. + * Request tracer plugin for Fastify. * * @param {Object} options possible options * @param {boolean} options.useHeader respect request header flag @@ -52,34 +52,34 @@ const expressMiddleware = ({ * @param {boolean} options.useFastifyRequestId respect Fastify request id flag * (default: `false`) */ -const fastifyPlugin = ({ - useHeader = false, - headerName = 'X-Request-Id', - useFastifyRequestId = false -} = {}) => { - const plugin = (fastify, _, next) => { - fastify.addHook('onRequest', (request, reply, done) => { - let requestId - if (useHeader) { - requestId = request.headers[headerName.toLowerCase()] - } - if (useFastifyRequestId) { - requestId = requestId || request.id - } - requestId = requestId || uuidv1() +const fastifyPlugin = (fastify, options, next) => { + const { + useHeader = false, + headerName = 'X-Request-Id', + useFastifyRequestId = false + } = options + + fastify.addHook('onRequest', (request, reply, done) => { + let requestId + if (useHeader) { + requestId = request.headers[headerName.toLowerCase()] + } + if (useFastifyRequestId) { + requestId = requestId || request.id + } + requestId = requestId || uuidv1() - als.run(requestId, () => { - wrapHttpEmitters(request.raw, reply.res) - done() - }) + als.run(requestId, () => { + wrapHttpEmitters(request.raw, reply.res) + done() }) - next() - } - plugin[Symbol.for('skip-override')] = true - plugin[Symbol.for('fastify.display-name')] = pluginName - return plugin + }) + next() } +fastifyPlugin[Symbol.for('skip-override')] = true +fastifyPlugin[Symbol.for('fastify.display-name')] = pluginName + /** * Generates a request tracer middleware for Koa v2. * @@ -138,7 +138,7 @@ const koaV1Middleware = ({ } /** - * A request tracer plugin for Hapi. + * Request tracer plugin for Hapi. * * @type {{once: boolean, name: string, register: hapiPlugin.register}} */ @@ -181,8 +181,8 @@ const runWithId = (fn, id) => { } /** - * Returns request tracer id or `undefined` in case if the call is made from - * an outside CLS context. + * Returns request tracer id or `undefined` in case if the call + * is made outside of the CLS context. */ const id = () => als.getStore() diff --git a/tests/fastify.test.js b/tests/fastify.test.js index 056ca75..3eb2f2f 100644 --- a/tests/fastify.test.js +++ b/tests/fastify.test.js @@ -13,7 +13,7 @@ const types = [pluginType, middlewareType] const register = (type, app, options) => { switch (type) { case pluginType: - return app.register(rTracer.fastifyPlugin(options)) + return app.register(rTracer.fastifyPlugin, options) case middlewareType: return app.use(rTracer.fastifyMiddleware(options)) default: