-
Notifications
You must be signed in to change notification settings - Fork 37
fix(messaging)!: Fix mix up messaging in window and custom event messengers #70
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Switch page.test
to browser mode test.
res(detail); | ||
if ( | ||
detail.namespace === namespace && | ||
detail.instanceId !== instanceId && | ||
detail.message.type === requestEvent.detail.message.type | ||
) { | ||
res(detail.response); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it a mistake that unlike windowMessage, customEventMessage has been unguarded so far? Is there a particular reason I don't know about?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably a bug, but could be related to how window.postMessage
sends the message to all frames in the window.
removeAdditionalListeners.push(() => | ||
window.removeEventListener(RESPONSE_EVENT, responseListener), | ||
); | ||
window.addEventListener(RESPONSE_EVENT, responseListener, { once: true }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, too, I don't understand why it has been once: true
so far.
@aklinker1 PR is ready for review. But there's really no need hurry. Because at least in my project, I'm resuming work on this content with a local patching. 😃 |
const reqDetail = { message, namespace, instanceId }; | ||
const requestEvent = new CustomEvent(REQUEST_EVENT, { | ||
// @ts-expect-error not exist cloneInto types because implemented only in Firefox. | ||
detail: typeof cloneInto !== 'undefined' ? cloneInto(reqDetail, window) : reqDetail, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a problem with CustomEvent
in Firefox that causes an error when sharing a message object.
Waiting for merge.... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, sorry it took a while to get to!
I will do a major release for this change.
No problem, thanks for the hard review! |
Released in v2.0.0 |
@aklinker1 It seems that the published |
Thanks, confirmed that the correct update came 🚀 |
fix #57
Firstly
It may be hard to review, so take your time.
Also, if approved, it might be safer to make it a major bump. Because the messaging path is narrowed and pseudo-channeled, it is possible that a user's application, through which the message previously happened to pass, could be broken.
Changes
message
First, has a unique instance ID. This is to check whether the message came from its own instance or not. Then,
namespace
,instanceId
, andmessage type
are checked strictly in the req/res listeners.messaging-demo
About vitest
browser mode
I was annoyed by the flakey test in
page.test
, so I partially switched tobrowser mode
. I understand that thewindow
(jsdom global context) on nodeVM is common between tests, but can be made isolated when inbrowser mode
.I really wanted to split the test environment nicely with workspace configs like in this example, but I gave up because of a bug that broke the existing unit test. The bug that cjs mocking (
webextension-polyfill
) fails when using browser mode together. They seem to have reworked the whole mock mechanism.This Issue (#48) may have been resolved. But the link is already dead, I don't know which test Flakey was testing at the time.
Diagrams
Here is the understanding I have in my head and its diagram.