diff --git a/README.md b/README.md index 6c51471..89e1c3c 100644 --- a/README.md +++ b/README.md @@ -155,7 +155,7 @@ 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)` @@ -163,7 +163,10 @@ It's possible to set some simple **synchronous** functions as hooks: - `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 diff --git a/index.js b/index.js index 8222c00..34c7839 100644 --- a/index.js +++ b/index.js @@ -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) diff --git a/lib/hooks.js b/lib/hooks.js index 8195754..b9d44fd 100644 --- a/lib/hooks.js +++ b/lib/hooks.js @@ -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) } }