Skip to content

Commit

Permalink
Added callback to server request hook
Browse files Browse the repository at this point in the history
Signed-off-by: marcopiraccini <[email protected]>
  • Loading branch information
marcopiraccini committed Nov 11, 2024
1 parent e51eacb commit 41de8bc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 54 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,18 @@ interceptor.close();
It's possible to set some simple **synchronous** functions as hooks:
- `onServerRequest(req)
- `onServerRequest(req, cb)`
- `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)
(which is then sent through `postMessage`, so it might be not serializable).
On `onServerRequest`, the `cb` is not supposed to be called by the hook, but the hook can
wrap/decorate it.
#### Client hooks
Expand Down
82 changes: 42 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,55 +263,57 @@ function wire ({ server: newServer, port, ...undiciOpts }) {
query: opts.query,
body: opts.body instanceof Uint8Array ? Buffer.from(opts.body) : opts.body,
}
interceptor.hooks.fireOnServerRequest(injectOpts)

const onInject = (err, res) => {
if (err) {
interceptor.hooks.fireOnServerError(injectOpts, res, err)
port.postMessage({ type: 'response', id, err })
return
}
// the cb is always fired even if there are no server request hooks set
interceptor.hooks.fireOnServerRequest(injectOpts, () => {
const onInject = (err, res) => {
if (err) {
interceptor.hooks.fireOnServerError(injectOpts, res, err)
port.postMessage({ type: 'response', id, err })
return
}

const newRes = {
headers: res.headers,
statusCode: res.statusCode,
}
const newRes = {
headers: res.headers,
statusCode: res.statusCode,
}

if (res.headers['content-type']?.indexOf('application/json')) {
if (res.headers['content-type']?.indexOf('application/json')) {
// fast path because it's utf-8, use a string
newRes.rawPayload = res.payload
} else {
newRes.rawPayload = res.payload
} else {
// slow path, buffer
newRes.rawPayload = res.rawPayload
}
newRes.rawPayload = res.rawPayload
}

const forwardRes = {
type: 'response',
id,
res: newRes,
}
interceptor.hooks.fireOnServerResponse(injectOpts, newRes)
const forwardRes = {
type: 'response',
id,
res: newRes,
}

// So we route the message back to the port
// that sent the request
this.postMessage(forwardRes)
}
interceptor.hooks.fireOnServerResponse(injectOpts, newRes)

if (!server) {
port.postMessage({
type: 'response',
id,
err: new Error('No server found for ' + injectOpts.headers.host + ' in ' + threadId),
})
// So we route the message back to the port
// that sent the request
this.postMessage(forwardRes)
}

return
}
if (!server) {
port.postMessage({
type: 'response',
id,
err: new Error('No server found for ' + injectOpts.headers.host + ' in ' + threadId),
})

if (hasInject) {
server.inject(injectOpts, onInject)
} else {
inject(server, injectOpts, onInject)
}
return
}

if (hasInject) {
server.inject(injectOpts, onInject)
} else {
inject(server, injectOpts, onInject)
}
})
} else if (msg.type === 'route') {
interceptor.route(msg.url, msg.port, false)
msg.port.on('message', onMessage)
Expand Down
25 changes: 13 additions & 12 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ class Hooks {
}
}

fireOnServerRequest (...args) {
return this.run(this.onServerRequest, ...args)
fireOnServerRequest (req, cb) {
this.run(this.onServerRequest, req, cb)
cb()
}

fireOnServerResponse (...args) {
return this.run(this.onServerResponse, ...args)
fireOnServerResponse (req, res) {
this.run(this.onServerResponse, req, res)
}

fireOnServerError (...args) {
return this.run(this.onServerError, ...args)
fireOnServerError (req, res, error) {
this.run(this.onServerError, req, res, error)
}

fireOnClientRequest (...args) {
return this.run(this.onClientRequest, ...args)
fireOnClientRequest (req, clientCtx) {
this.run(this.onClientRequest, req, clientCtx)
}

fireOnClientResponse (...args) {
return this.run(this.onClientResponse, ...args)
fireOnClientResponse (req, res, clientCtx) {
this.run(this.onClientResponse, req, res, clientCtx)
}

fireOnClientError (...args) {
return this.run(this.onClientError, ...args)
fireOnClientError (req, res, clientCtx, error) {
this.run(this.onClientError, req, res, clientCtx, error)
}
}

Expand Down

0 comments on commit 41de8bc

Please sign in to comment.