+
Skip to content

Conversation

1natsu172
Copy link
Contributor

@1natsu172 1natsu172 commented Sep 15, 2024

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, and message type are checked strictly in the req/res listeners.

messaging-demo

  • Add CustomEventMessage demo
  • Add multiple bi-directional messaging demo

About vitest browser mode

I was annoyed by the flakey test in page.test, so I partially switched to browser mode. I understand that the window(jsdom global context) on nodeVM is common between tests, but can be made isolated when in browser 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

mermaid live editor link

Here is the understanding I have in my head and its diagram.

sequenceDiagram
    box Content-Script<br/><br/>instance1<br/>{ namespace: foo }
    participant ins1_on as onMessage
    participant ins1_send as sendMessage
    end
    box Injected/WebSite<br/><br/>instance2<br/>{ namespace: foo }
    participant ins2_on as onMessage
    participant ins2_send as sendMessage
    end
    box AnotherContext<br/><br/>instance3<br/>{ namespace: bar }
    participant ins3_on as onMessage
    participant ins3_send as sendMessage
    end

    %% namespace B
    Note over ins3_on,ins3_send: COMMUNICATION FLOW<br/>SAME AS LEFT

    par 
        ins1_send->>+ins2_on: send/type:"ping1"
        ins1_send--xins1_on: ignore own send
        ins2_on--xins2_send: ignore own res
        ins2_on->>-ins1_send: res/type:"ping1"
    end
    par 
        ins2_send->>+ins1_on: send/type:"ping2"
        ins2_send--xins2_on: ignore own send
        ins1_on--xins1_send: ignore own res
        ins1_on->>-ins2_send: res/type:"ping2"
    end

    
    %% no communicate lines
    Note over ins2_on,ins3_send: No communicate in<br/>different namespaces
Loading

Copy link
Contributor Author

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.

Comment on lines -65 to +76
res(detail);
if (
detail.namespace === namespace &&
detail.instanceId !== instanceId &&
detail.message.type === requestEvent.detail.message.type
) {
res(detail.response);
}
Copy link
Contributor Author

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?

Copy link
Owner

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 });
Copy link
Contributor Author

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.

@1natsu172
Copy link
Contributor Author

@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. 😃

Comment on lines +90 to +93
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,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@molvqingtai
Copy link

Waiting for merge....

@aklinker1 aklinker1 changed the title Fix mix up messaging fix(messaging): Fix mix up messaging in window and custom event messengers Oct 18, 2024
Copy link
Owner

@aklinker1 aklinker1 left a 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.

@aklinker1 aklinker1 changed the title fix(messaging): Fix mix up messaging in window and custom event messengers fix(messaging)!: Fix mix up messaging in window and custom event messengers Oct 18, 2024
@1natsu172
Copy link
Contributor Author

No problem, thanks for the hard review!

@aklinker1 aklinker1 merged commit 3b4e0ad into aklinker1:main Oct 19, 2024
4 checks passed
@aklinker1
Copy link
Owner

aklinker1 commented Oct 19, 2024

Released in v2.0.0

@1natsu172
Copy link
Contributor Author

@aklinker1 It seems that the published 2.0.0 contents are still in 1.4.0. There may be a problem with the build process 🤔 .
https://www.npmjs.com/package/@webext-core/messaging?activeTab=code

@aklinker1
Copy link
Owner

@1natsu172 fixed, https://github.com/aklinker1/webext-core/releases/tag/messaging-v2.0.1

@1natsu172
Copy link
Contributor Author

Thanks, confirmed that the correct update came 🚀

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.

@webext-core/messaging errors with No response when sending data back from an injected script

3 participants

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载