Skip to content

Commit

Permalink
remove unnecessary purely sync async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
serdnam committed Mar 4, 2022
1 parent 16b1fb0 commit e61e333
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import * as Sentry from "@sentry/node"
import * as Tracing from "@sentry/tracing"
import { Scope, Span, Transaction } from "@sentry/types"

import type {
import type {
DoneFuncWithErrOrRes,
FastifyInstance,
FastifyPluginAsync,
FastifyReply,
FastifyRequest,
HookHandlerDoneFunction,
onRequestAsyncHookHandler,
onRequestHookHandler,
preParsingAsyncHookHandler,
preParsingHookHandler,
RequestPayload
} from "fastify"
import fastifyPlugin from 'fastify-plugin'
Expand Down Expand Up @@ -281,19 +285,23 @@ export default fastifyPlugin(async function FastifySentry(fastify, options) {



const currentHookWithoutPayload = async function (
const currentHookWithoutPayload = function (
this: FastifyInstance,
req: FastifyRequest,
reply: FastifyReply) {
reply: FastifyReply,
done: HookHandlerDoneFunction) {
startSpan(req)
done()
}

const currentHookWithPayload = async function (
const currentHookWithPayload = function (
this: FastifyInstance,
req: FastifyRequest,
reply: FastifyReply,
payload: RequestPayload) {
payload: RequestPayload,
done: DoneFuncWithErrOrRes) {
startSpan(req)
done(null, payload)
}


Expand All @@ -306,12 +314,14 @@ export default fastifyPlugin(async function FastifySentry(fastify, options) {
fastify.addHook(currentHook.name as any, currentHookWithoutPayload)
}

const nextHookWithoutPayload: onRequestAsyncHookHandler = async function (req, reply) {
const nextHookWithoutPayload: onRequestHookHandler = function (req, reply, done) {
finishSpan(req)
done()
}

const nextHookWithPayload: preParsingAsyncHookHandler = async function (req, reply, payload) {
const nextHookWithPayload: preParsingHookHandler = function (req, reply, payload, done) {
finishSpan(req)
done()
}

if(hookHandlesPayload(nextHook.name)) {
Expand All @@ -321,10 +331,11 @@ export default fastifyPlugin(async function FastifySentry(fastify, options) {
}
}

fastify.addHook('onResponse', async function (req, rep) {
fastify.addHook('onResponse', function (req, rep, done) {
const tx = req[getTx]()
tx.setHttpStatus(rep.statusCode)
tx.finish()
done()
})
}

Expand Down

0 comments on commit e61e333

Please sign in to comment.