Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add "request" to response events #1993

Merged
merged 4 commits into from
Jan 25, 2024
Merged

fix: add "request" to response events #1993

merged 4 commits into from
Jan 25, 2024

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Jan 24, 2024

This is actually a feature. I've added the request argument to the response events by mistake, trying to keep things consistent. But I've never implemented the thing.

@@ -30,6 +30,8 @@ export const createRequestListener = (
const request = parseWorkerRequest(message.payload)
const requestCloneForLogs = request.clone()

context.requests.set(requestId, request.clone())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what I feel in particular about this approach but I've been meaning to provide in-memory requestId/request storage for some time now. This will simplify the usage of life-cycle events.

@@ -103,6 +103,7 @@ export interface SetupWorkerInternalContext {
worker: ServiceWorker | null
registration: ServiceWorkerRegistration | null
requestHandlers: Array<RequestHandler>
requests: Map<string, Request>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would very much like this to be a WeakMap so we wouldn't have to clean it up but then to access the request instance it'd have to be a WeakMap<Request, string>, iterating over which would be rather inefficient.

@mattcosta7, what do you think about this? Is it worth rewriting this as WeakMap?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm

I think we could, but it might be a bit complicated, and not worth the effort?

I think we would need to WeakMap<Request, {requestId, RequestClone> to really make that change, which might be ok, but it's pretty straightforward to cleanup, and should generally be small enough at any point in time to not be an issue?

@@ -30,6 +30,8 @@ export const createRequestListener = (
const request = parseWorkerRequest(message.payload)
const requestCloneForLogs = request.clone()

context.requests.set(requestId, request.clone())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also means +1 request clone per request.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few important notes:

  1. This is always called once per 1 request.
  2. Request resolution hasn't started yet so there's no cache.

We can technically make this the earliest clone to prevent the first request handler from cloning this request again. I think this makes sense. Wdyt, @mattcosta7?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this first request clone to RequestHandler.cache so no request handlers would have to clone the request.

It's still a bit of an overhead since now we are always making 1 clone for every outgoing request (in the browser). Previously, only the first request handler would make the clone. I'd say this is not a concern since request cloning was done in the parsing phase, and the only scenario when this actually changes anything is if you have 0 request handlers (than, indeed, no cloning is needed).

But we still have to clone the request even in that scenario so you can access it in the response:* life-cycle events.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think this is definitely worth it, and the cost seems low enough that it shouldn't be a concern

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now request handlers won't clone requests at all, I think. Any outgoing request will be cloned here, and then request handlers will reuse the clone from cache.

@kettanaito kettanaito merged commit bad537f into main Jan 25, 2024
11 checks passed
@kettanaito kettanaito deleted the fix/request-argument branch January 25, 2024 12:16
@kettanaito
Copy link
Member Author

Released: v2.1.5 🎉

This has been released in v2.1.5!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"request" in worker.events.on('response:mocked') is always null
2 participants