Skip to content

rozzilla/fastify-asyncforge

 
 

Repository files navigation

fastify-asyncforge

Provide easy AsyncLocalStorage magic to your Fastify apps. It's based on asyncforge.

Install

npm i fastify fastify-asyncforge

Usage

// app.mjs
import fastify from 'fastify'
import doWork from './do-work.mjs'
import asyncforge, { logger } from 'fastify-asyncforge'

const app = fastify({
  logger: true
})
await app.register(asyncforge)

app.runInAsyncScope(() => {
  logger().info('hello')
})

app.decorate('foo', 'bar')
app.decorateRequest('a')
app.decorateReply('b')

app.addHook('onRequest', async function (req, reply) {
  req.a = 'a'
  reply.b = 'b'
})

app.get('/', async function (request, reply) {
  doWork()
  return { hello: 'world' }
})

app.listen({ port: 3000 })

// do-work.mjs
import { logger, app, request, reply } from 'fastify-asyncforge'

export default function doWork () {
  const log = logger()
  log.info({
    foo: app().foo,
    a: request().a,
    b: reply().b
  }, 'doing work')
}

.enterWith()

In case .runInAsyncScope() is incovenient, you can use .enterWith()

import fastify from 'fastify'
import asyncforge from 'fastify-asyncforge'

const fastify = Fastify()

// Calling .enterWith() is necessary or `asyncforge.app()` will throw
fastify.enterWith()
asyncforge.app()

Notice

Note that you cannot wrap .enterWith() inside an async function, as it will not work. If you are interested in knowing more, read nodejs/node#53037.

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 83.2%
  • TypeScript 16.8%