Skip to content

Commit

Permalink
Migrate from factory function to single instance of Fastify plugin (#34)
Browse files Browse the repository at this point in the history
Also fixes misprints in readme
  • Loading branch information
puzpuzpuz authored Jun 12, 2020
1 parent c60a663 commit 1eb657a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down
4 changes: 1 addition & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion samples/fastify.winston.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
56 changes: 28 additions & 28 deletions src/rtracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down Expand Up @@ -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}}
*/
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion tests/fastify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 1eb657a

Please sign in to comment.