Skip to content

Commit

Permalink
added context for client hooks
Browse files Browse the repository at this point in the history
Signed-off-by: marcopiraccini <[email protected]>
  • Loading branch information
marcopiraccini committed Nov 6, 2024
1 parent 2dbd008 commit a289a6b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,15 @@ interceptor.close();
It's possible to set some simple **synchronous** functions as hooks:
- `onClientRequest(req)`
- `onClientResponse(req, res)`
- `onClientError(req, res, error)`
- `onServerRequest(req)`
- `onServerRequest(req)
- `onServerResponse(req, res)`
- `onServerError(req, res, error)`
- `onClientRequest(req, clientCtx)`
- `onClientResponse(req, res, clientCtx)`
- `onClientError(req, res, clientCtx, error)`

The `clientCtx` is used to pass through hooks calls objects which cannot be set on request
(which is then sent through `postMessage`, so it might be not serializable)

#### Client hooks

Expand Down
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function createThreadInterceptor (opts) {

delete newOpts.dispatcher

hooks.fireOnClientRequest(newOpts)
// We use it as client context where hooks can add non-serializable properties
const clientCtx = {}
hooks.fireOnClientRequest(newOpts, clientCtx)

if (newOpts.body?.[Symbol.asyncIterator]) {
collectBodyAndDispatch(newOpts, handler).then(() => {
Expand Down Expand Up @@ -89,11 +91,11 @@ function createThreadInterceptor (opts) {
clearTimeout(handle)

if (err) {
hooks.fireOnClientError(newOpts, res, err)
hooks.fireOnClientError(newOpts, res, clientCtx, err)
handler.onError(err)
return
}
hooks.fireOnClientResponse(newOpts, res)
hooks.fireOnClientResponse(newOpts, res, clientCtx)

const headers = []
for (const [key, value] of Object.entries(res.headers)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const supportedHooks = [
'onServerError',
'onClientRequest',
'onClientResponse',
'onClientError']
'onClientError'
]

class Hooks {
onServerRequest = []
Expand Down
32 changes: 29 additions & 3 deletions test/hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ test('hooks - onClientError', async (t) => {

const interceptor = createThreadInterceptor({
domain: '.local',
onClientError: (_req, _rep, error) => {
onClientError: (_req, _rep, _ctx, error) => {
hookCalled = error
}
})
Expand All @@ -198,11 +198,11 @@ test('hooks - multiple onClientErrors', async (t) => {
t.after(() => worker.terminate())
const hookCalled = []

const onClientError1 = (_req, _rep, error) => {
const onClientError1 = (_req, _rep, _ctx, error) => {
hookCalled.push({ error1: error.message })
}

const onClientError2 = (_req, _rep, error) => {
const onClientError2 = (_req, _rep, _ctx, error) => {
hookCalled.push({ error2: error.message })
}

Expand Down Expand Up @@ -347,3 +347,29 @@ test('hooks - request propagation between onClientRequest and onClientResponse',
strictEqual(statusCode, 200)
deepStrictEqual(hookCalledClient, 'propagated')
})

test('hooks - context propagation between onClientRequest and onClientResponse', async (t) => {
const worker = new Worker(join(__dirname, 'fixtures', 'worker1.js'))
t.after(() => worker.terminate())
let hookCalledClient

const interceptor = createThreadInterceptor({
domain: '.local',
onClientRequest: (_req, ctx) => {
ctx.data = 'propagated'
},
onClientResponse: (req, _res, ctx) => {
hookCalledClient = ctx.data
}
})
interceptor.route('myserver', worker)

const agent = new Agent().compose(interceptor)

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

strictEqual(statusCode, 200)
deepStrictEqual(hookCalledClient, 'propagated')
})

0 comments on commit a289a6b

Please sign in to comment.