Skip to content

Commit

Permalink
feat: add onClientResponseEnd hook (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-tymoshenko authored Dec 25, 2024
1 parent 1c34992 commit d764868
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ It's possible to set some simple **synchronous** functions as hooks:
- `onServerError(req, res, error)`
- `onClientRequest(req, clientCtx)`
- `onClientResponse(req, res, clientCtx)`
- `onClientResponseEnd(req, res, clientCtx)`
- `onClientError(req, res, clientCtx, error)`
The `clientCtx` is used to pass through hooks calls objects which cannot be set on request
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ function createThreadInterceptor (opts) {

handler.onResponseData(controller, res.rawPayload)
handler.onResponseEnd(controller, [])

hooks.fireOnClientResponseEnd(newOpts, res, clientCtx)
}))

return true
Expand Down
6 changes: 6 additions & 0 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const supportedHooks = [
'onServerError',
'onClientRequest',
'onClientResponse',
'onClientResponseEnd',
'onClientError'
]

Expand All @@ -17,6 +18,7 @@ class Hooks {
onServerError = noop
onClientRequest = noop
onClientResponse = noop
onClientResponseEnd = noop
onClientError = noop

constructor (opts) {
Expand Down Expand Up @@ -55,6 +57,10 @@ class Hooks {
return this.onClientResponse(req, res, ctx)
}

fireOnClientResponseEnd (req, res, ctx) {
return this.onClientResponseEnd(req, res, ctx)
}

fireOnClientError (req, res, ctx, error) {
return this.onClientError(req, res, ctx, error)
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/server-hooks/worker-server-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ wire({
},
onServerResponse: (req, res) => {
const { dataInRequest } = req
console.log('onServerReponse called:', dataInRequest)
console.log('onServerResponse called:', dataInRequest)
}
})
24 changes: 23 additions & 1 deletion test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ test('hooks - onClientResponse', async (t) => {
deepStrictEqual(hookCalled, '{"hello":"world"}')
})

test('hooks - onClientResponseEnd', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js'))
t.after(() => worker.terminate())
let hookCalled = null

const interceptor = createThreadInterceptor({
domain: '.local',
onClientResponseEnd: (_req, res) => {
hookCalled = Buffer.from(res.rawPayload).toString()
}
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)
const { statusCode } = await request('http://myserver.local', {
dispatcher: agent,
})

strictEqual(statusCode, 200)
deepStrictEqual(hookCalled, '{"hello":"world"}')
})

test('hooks - onClientError', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'error.js'))
t.after(() => worker.terminate())
Expand Down Expand Up @@ -226,7 +248,7 @@ test('hooks - request propagation between onServerRequest and onServerResponse',
lines.push(line)
})
await sleep(300)
deepStrictEqual(lines, ['onServerRequest called {"method":"GET","url":"/","headers":{"host":"myserver.local"}}', 'onServerReponse called: propagated'])
deepStrictEqual(lines, ['onServerRequest called {"method":"GET","url":"/","headers":{"host":"myserver.local"}}', 'onServerResponse called: propagated'])
})

test('hooks - request propagation between onClientRequest and onClientResponse', async (t) => {
Expand Down

0 comments on commit d764868

Please sign in to comment.