Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina committed May 21, 2024
1 parent 70ef344 commit 3f718ed
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ const reply = memo('fastify.reply')
const logger = memo('fastify.logger')

const fastifyAsyncForge = fp(function (fastify, opts, next) {
const store = create()

fastify.decorate('runInAsyncScope', function (fn) {
return store.run(fn)
const store = create()
return store.run(() => {
app.set(this)
logger.set(this.log)
return fn()
})
})

fastify.addHook('onRequest', function (req, res, next) {
Expand All @@ -28,7 +31,7 @@ const fastifyAsyncForge = fp(function (fastify, opts, next) {
})
})

store.run(() => {
create(() => {
app.set(fastify)
logger.set(fastify.log)
next()
Expand Down
70 changes: 67 additions & 3 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,49 @@ test('basic helpers', async (t) => {
await p.completed
})

test('basic helpers without start', async (t) => {
test('support in following plugin', async (t) => {
const p = tspl(t, { plan: 7 })
const fastify = Fastify()

fastify.register(fastifyAsyncForge)

fastify.register(async function (fastify, _opts) {
p.strictEqual(app(), Object.getPrototypeOf(fastify))
p.strictEqual(request(), undefined)
})

fastify.get('/', async function (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
return { hello: 'world' }
})

const res = await fastify.inject({
method: 'GET',
url: '/'
})

p.strictEqual(res.statusCode, 200)

await p.completed
})

test('support in following plugin with await ', async (t) => {
const p = tspl(t, { plan: 9 })
const fastify = Fastify()

await fastify.register(fastifyAsyncForge)

p.throws(logger)
p.throws(app)
fastify.register(async function (fastify, _opts) {
p.throws(app)
p.throws(request)
fastify.runInAsyncScope(() => {
p.strictEqual(app(), fastify)
p.strictEqual(request(), undefined)
})
})

fastify.get('/', async function (_request, _reply) {
p.strictEqual(app(), this)
Expand Down Expand Up @@ -148,3 +183,32 @@ test('onRequest hook added before registration fails', async (t) => {

await p.completed
})

test('basic helpers with start', async (t) => {
const p = tspl(t, { plan: 7 })
const fastify = Fastify()

await fastify.register(fastifyAsyncForge)

fastify.runInAsyncScope(() => {
p.strictEqual(logger(), fastify.log)
p.strictEqual(app(), fastify)
})

fastify.get('/', async function (_request, _reply) {
p.strictEqual(app(), this)
p.strictEqual(request(), _request)
p.strictEqual(reply(), _reply)
p.strictEqual(logger(), _request.log)
return { hello: 'world' }
})

const res = await fastify.inject({
method: 'GET',
url: '/'
})

p.strictEqual(res.statusCode, 200)

await p.completed
})

0 comments on commit 3f718ed

Please sign in to comment.