Skip to content

Commit

Permalink
meet adr 006
Browse files Browse the repository at this point in the history
  • Loading branch information
turbocrime committed Jul 30, 2024
1 parent ba19606 commit 7f47e9c
Show file tree
Hide file tree
Showing 15 changed files with 2,115 additions and 3,043 deletions.
6 changes: 1 addition & 5 deletions apps/extension/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
"content_scripts": [
{
"matches": ["https://*/*"],
"js": [
"injected-connection-port.js",
"injected-disconnect-listener.js",
"injected-request-listener.js"
],
"js": ["injected-page-listener.js"],
"run_at": "document_start"
},
{
Expand Down
44 changes: 0 additions & 44 deletions apps/extension/src/content-scripts/injected-connection-port.ts

This file was deleted.

10 changes: 0 additions & 10 deletions apps/extension/src/content-scripts/injected-disconnect-listener.ts

This file was deleted.

61 changes: 61 additions & 0 deletions apps/extension/src/content-scripts/injected-page-listener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
PraxMessage,
isPraxConnectMessageEvent,
isPraxDisconnectMessageEvent,
} from './message-event';
import { PraxConnection } from '../message/prax';
import { PenumbraRequestFailure } from '@penumbra-zone/client/error';
import { CRSessionClient } from '@penumbra-zone/transport-chrome/session-client';

const endMessage = { [PRAX]: PraxConnection.End } satisfies PraxMessage<PraxConnection.End>;

let port: MessagePort | undefined;

const failureMessage = (failure?: unknown): PraxMessage<PenumbraRequestFailure> => {
if (typeof failure === 'string' && failure in PenumbraRequestFailure) {
return { [PRAX]: failure };
} else {
console.error('Bad response', failure);
return { [PRAX]: PenumbraRequestFailure.BadResponse };

Check failure on line 19 in apps/extension/src/content-scripts/injected-page-listener.ts

View workflow job for this annotation

GitHub Actions / Lint

Unsafe assignment of an error typed value
}
};

window.addEventListener('message', (ev: MessageEvent<unknown>) => {
if (ev.origin === window.origin) {
void (async () => {
// any response to these messages only indicates failure.
let failure: PenumbraRequestFailure | undefined;

if (isPraxConnectMessageEvent(ev)) {
try {
failure = await chrome.runtime.sendMessage(PraxConnection.Connect);
} catch (e) {
console.error(e);
failure = PenumbraRequestFailure.NotHandled;

Check failure on line 34 in apps/extension/src/content-scripts/injected-page-listener.ts

View workflow job for this annotation

GitHub Actions / Lint

Unsafe assignment of an error typed value
}

if (failure == null) {
port ??= CRSessionClient.init(PRAX);
window.postMessage({ [PRAX]: port } satisfies PraxMessage<MessagePort>, '/', [port]);
}
} else if (isPraxDisconnectMessageEvent(ev)) {
port = undefined;

try {
failure = await chrome.runtime.sendMessage(PraxConnection.Disconnect);
} catch (e) {
console.error(e);
failure = PenumbraRequestFailure.NotHandled;

Check failure on line 48 in apps/extension/src/content-scripts/injected-page-listener.ts

View workflow job for this annotation

GitHub Actions / Lint

Unsafe assignment of an error typed value
}

if (failure == null) {
window.postMessage(endMessage, '/');
}
}

if (failure != null) {
window.postMessage(failureMessage(failure), '/');
}
})();
}
});
Loading

0 comments on commit 7f47e9c

Please sign in to comment.