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 check for message listeners #1436

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension/src/background/helpers/dataStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export const addIsNonSSLEnabled = async () => {

if (!storageVersion || semver.lt(storageVersion, "4.2.0")) {
await localStore.setItem(IS_NON_SSL_ENABLED_ID, false);
await migrateDataStorageVersion("4.2.0)");
await migrateDataStorageVersion("4.2.0");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

noticed this issue with migrations, so adding the fix while i'm here

}
};

Expand Down
9 changes: 0 additions & 9 deletions extension/src/background/helpers/message.ts

This file was deleted.

17 changes: 11 additions & 6 deletions extension/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
migrateSorobanRpcUrlNetworkDetails,
versionedMigration,
} from "./helpers/dataStorage";
import { isExternalMessage, isResponse } from "./helpers/message";

export const initContentScriptMessageListener = () => {
browser?.runtime?.onMessage?.addListener((message) => {
Expand All @@ -36,16 +35,22 @@ export const initExtensionMessageListener = () => {
// todo this is kinda ugly
const req = request as ExternalRequest | Response;
let res;
if (isResponse(req) && Object.values(SERVICE_TYPES).includes(req.type)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

the isResponse vs isExternalMessage checks didn't quite work because they were keying off of messageId, which actually is only an optional field on the type Response. We only include it on requests that come through freighter-api and requests that come through localhost when you're in fullscreen mode in dev.

Unfortunately this meant requests that were coming from the extension itself weren't making it to the appropriate handler.

The type casting is a little ugly, but accomplishes routing the requests to the appropriate handler. The comment // todo this is kinda ugly definitely still stands. I'll take another shot at cleaning this up in the next release


if (Object.values(SERVICE_TYPES).includes(req.type as SERVICE_TYPES)) {
// eslint-disable-next-line
res = await popupMessageListener(req, sessionStore);
res = await popupMessageListener(req as Response, sessionStore);
}
if (
isExternalMessage(req) &&
Object.values(EXTERNAL_SERVICE_TYPES).includes(req.type)
Object.values(EXTERNAL_SERVICE_TYPES).includes(
req.type as EXTERNAL_SERVICE_TYPES,
)
) {
// eslint-disable-next-line
res = await freighterApiMessageListener(req, sender, sessionStore);
res = await freighterApiMessageListener(
req as ExternalRequest,
sender,
sessionStore,
);
}

return res;
Expand Down