Skip to content

Commit

Permalink
fixup! Simplify reply sent monitoring (#3072) (#3132)
Browse files Browse the repository at this point in the history
  • Loading branch information
sergejostir committed Jun 14, 2021
1 parent 4fb2e09 commit 7f0b9a4
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions docs/Routes.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ fastify.get('/', options, async function (request, reply) {

As you can see, we are not calling `reply.send` to send back the data to the user. You just need to return the body and you are done!

If you need it you can also send back the data to the user with `reply.send`.
If you need it you can also send back the data to the user with `reply.send`. In this case do not forget to `return reply` or `await reply` in your `async` handler or you will introduce a race condition in certain situations.
```js
fastify.get('/', options, async function (request, reply) {
var data = await getData()
var processed = await processData(data)
reply.send(processed)
return reply.send(processed)
})
```

Expand Down
12 changes: 6 additions & 6 deletions test/async-await.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test('ignore the result of the promise if reply.send is called beforehand (undef
const payload = { hello: 'world' }

server.get('/', async function awaitMyFunc (req, reply) {
reply.send(payload)
await reply.send(payload)
})

t.teardown(server.close.bind(server))
Expand All @@ -104,7 +104,7 @@ test('ignore the result of the promise if reply.send is called beforehand (objec
const payload = { hello: 'world2' }

server.get('/', async function awaitMyFunc (req, reply) {
reply.send(payload)
await reply.send(payload)
return { hello: 'world' }
})

Expand Down Expand Up @@ -139,7 +139,7 @@ test('server logs an error if reply.send is called and a value is returned via a
})

fastify.get('/', async (req, reply) => {
reply.send({ hello: 'world' })
await reply.send({ hello: 'world' })
return { hello: 'world2' }
})

Expand All @@ -160,7 +160,7 @@ test('ignore the result of the promise if reply.send is called beforehand (undef
const payload = { hello: 'world' }

server.get('/', async function awaitMyFunc (req, reply) {
reply.send(payload)
await reply.send(payload)
})

t.teardown(server.close.bind(server))
Expand All @@ -185,7 +185,7 @@ test('ignore the result of the promise if reply.send is called beforehand (objec
const payload = { hello: 'world2' }

server.get('/', async function awaitMyFunc (req, reply) {
reply.send(payload)
await reply.send(payload)
return { hello: 'world' }
})

Expand Down Expand Up @@ -478,7 +478,7 @@ test('customErrorHandler support without throwing', t => {

fastify.setErrorHandler(async (err, req, reply) => {
t.equal(err.message, 'ouch')
reply.code(401).send('kaboom')
await reply.code(401).send('kaboom')
reply.send = t.fail.bind(t, 'should not be called')
})

Expand Down
26 changes: 14 additions & 12 deletions test/hooks-async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ test('onRequest hooks should be able to block a request', t => {
const fastify = Fastify()

fastify.addHook('onRequest', async (req, reply) => {
reply.send('hello')
await reply.send('hello')
})

fastify.addHook('onRequest', async (req, reply) => {
Expand Down Expand Up @@ -224,7 +224,7 @@ test('preHandler hooks should be able to block a request', t => {
const fastify = Fastify()

fastify.addHook('preHandler', async (req, reply) => {
reply.send('hello')
await reply.send('hello')
})

fastify.addHook('preHandler', async (req, reply) => {
Expand Down Expand Up @@ -258,7 +258,7 @@ test('preValidation hooks should be able to block a request', t => {
const fastify = Fastify()

fastify.addHook('preValidation', async (req, reply) => {
reply.send('hello')
await reply.send('hello')
})

fastify.addHook('preValidation', async (req, reply) => {
Expand Down Expand Up @@ -383,7 +383,7 @@ test('preValidation hooks should handle throwing null', t => {

fastify.setErrorHandler(async (error, request, reply) => {
t.ok(error instanceof Error)
reply.send(error)
await reply.send(error)
})

fastify.addHook('preValidation', async () => {
Expand Down Expand Up @@ -434,7 +434,7 @@ test('onRequest hooks should be able to block a request (last hook)', t => {
const fastify = Fastify()

fastify.addHook('onRequest', async (req, reply) => {
reply.send('hello')
await reply.send('hello')
})

fastify.addHook('preHandler', async (req, reply) => {
Expand Down Expand Up @@ -468,7 +468,7 @@ test('preHandler hooks should be able to block a request (last hook)', t => {
const fastify = Fastify()

fastify.addHook('preHandler', async (req, reply) => {
reply.send('hello')
await reply.send('hello')
})

fastify.addHook('onSend', async (req, reply, payload) => {
Expand Down Expand Up @@ -502,8 +502,9 @@ test('onRequest respond with a stream', t => {
const stream = fs.createReadStream(process.cwd() + '/test/stream.test.js', 'utf8')
// stream.pipe(res)
// res.once('finish', resolve)
reply.send(stream)
reply.raw.once('finish', () => resolve())
reply.send(stream).then(() => {
reply.raw.once('finish', () => resolve())
})
})
})

Expand Down Expand Up @@ -551,10 +552,11 @@ test('preHandler respond with a stream', t => {
fastify.addHook('preHandler', async (req, reply) => {
return new Promise((resolve, reject) => {
const stream = fs.createReadStream(process.cwd() + '/test/stream.test.js', 'utf8')
reply.send(stream)
reply.raw.once('finish', () => {
t.equal(order.shift(), 2)
resolve()
reply.send(stream).then(() => {
reply.raw.once('finish', () => {
t.equal(order.shift(), 2)
resolve()
})
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3173,7 +3173,7 @@ test('onTimeout should be triggered', t => {
})

fastify.get('/', async (req, reply) => {
reply.send({ hello: 'world' })
await reply.send({ hello: 'world' })
})

fastify.get('/timeout', async (req, reply) => {
Expand Down Expand Up @@ -3212,7 +3212,7 @@ test('onTimeout should be triggered and socket _meta is set', t => {

fastify.get('/', async (req, reply) => {
req.raw.socket._meta = {}
reply.send({ hello: 'world' })
return reply.send({ hello: 'world' })
})

fastify.get('/timeout', async (req, reply) => {
Expand Down
4 changes: 2 additions & 2 deletions test/reply-error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ test('should not set headers or status code for custom error handler', t => {
fastify.setErrorHandler(async (err, req, res) => {
t.equal(res.statusCode, 200)
t.equal('fake-random-header' in res.headers, false)
res.code(500).send(err.message)
return res.code(500).send(err.message)
})

fastify.inject({
Expand Down Expand Up @@ -495,7 +495,7 @@ test('error thrown by custom error handler routes to default error handler', t =
t.equal('fake-random-header' in res.headers, false)
t.same(err.headers, error.headers)

res.send(newError)
return res.send(newError)
})

fastify.inject({
Expand Down
2 changes: 1 addition & 1 deletion test/route-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ function testBeforeHandlerHook (hook) {

fastify.setErrorHandler(async (error, request, reply) => {
t.same(error, myError, 'the error object throws by the user')
reply.code(500).send({ this: 'is', my: 'error' })
return reply.code(500).send({ this: 'is', my: 'error' })
})

fastify.get('/', {
Expand Down
2 changes: 1 addition & 1 deletion test/skip-reply-send.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ function testHandlerOrBeforeHandlerHook (test, hookOrHandler) {
} else {
app.addHook(hookOrHandler, async (req, reply) => {
reply.hijack()
reply.send('hello from reply.send()')
return reply.send('hello from reply.send()')
})
app.get('/', (req, reply) => t.fail('Handler should not be called'))
}
Expand Down

0 comments on commit 7f0b9a4

Please sign in to comment.