-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b14f093
commit 0bb49ca
Showing
17 changed files
with
942 additions
and
474 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1 @@ | ||
import { ConnectError } from '@connectrpc/connect'; | ||
import { errorToJson } from '@connectrpc/connect/protocol-connect'; | ||
import { | ||
ActionBuildRequest, | ||
ActionBuildResponse, | ||
isActionBuildRequest, | ||
isOffscreenRequest, | ||
} from '@penumbra-zone/types/internal-msg/offscreen'; | ||
|
||
chrome.runtime.onMessage.addListener((req, _sender, respond) => { | ||
if (!isOffscreenRequest(req)) { | ||
return false; | ||
} | ||
const { type, request } = req; | ||
if (isActionBuildRequest(request)) { | ||
void (async () => { | ||
try { | ||
// propagate errors that occur in unawaited promises | ||
const unhandled = Promise.withResolvers<never>(); | ||
self.addEventListener('unhandledrejection', unhandled.reject, { | ||
once: true, | ||
}); | ||
|
||
const data = await Promise.race([ | ||
spawnActionBuildWorker(request), | ||
unhandled.promise, | ||
]).finally(() => self.removeEventListener('unhandledrejection', unhandled.reject)); | ||
|
||
respond({ type, data }); | ||
} catch (e) { | ||
const error = errorToJson( | ||
// note that any given promise rejection event probably doesn't | ||
// actually involve the specific request it ends up responding to. | ||
ConnectError.from(e instanceof PromiseRejectionEvent ? e.reason : e), | ||
undefined, | ||
); | ||
respond({ type, error }); | ||
} | ||
})(); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
const spawnActionBuildWorker = (req: ActionBuildRequest) => { | ||
const { promise, resolve, reject } = Promise.withResolvers<ActionBuildResponse>(); | ||
|
||
const worker = new Worker(new URL('../wasm-build-action.ts', import.meta.url)); | ||
void promise.finally(() => worker.terminate()); | ||
|
||
const onWorkerMessage = (e: MessageEvent) => resolve(e.data as ActionBuildResponse); | ||
|
||
const onWorkerError = ({ error, filename, lineno, colno, message }: ErrorEvent) => | ||
reject( | ||
error instanceof Error | ||
? error | ||
: new Error(`Worker ErrorEvent ${filename}:${lineno}:${colno} ${message}`), | ||
); | ||
|
||
const onWorkerMessageError = (ev: MessageEvent) => reject(ConnectError.from(ev.data ?? ev)); | ||
|
||
worker.addEventListener('message', onWorkerMessage, { once: true }); | ||
worker.addEventListener('error', onWorkerError, { once: true }); | ||
worker.addEventListener('messageerror', onWorkerMessageError, { once: true }); | ||
|
||
// Send data to web worker | ||
worker.postMessage(req); | ||
|
||
return promise; | ||
}; | ||
import '@repo/chrome-offscreen-worker/entry'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { penumbraEslintConfig } from '@repo/eslint-config'; | ||
import { config, parser } from 'typescript-eslint'; | ||
|
||
export default config( | ||
{ | ||
...penumbraEslintConfig, | ||
languageOptions: { | ||
parser, | ||
parserOptions: { project: true, tsconfigRootDir: import.meta.dirname }, | ||
}, | ||
}, | ||
{ rules: { '@typescript-eslint/no-unnecessary-type-arguments': 'off' } }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "@repo/chrome-offscreen-worker", | ||
"version": "0.0.0", | ||
"type": "module", | ||
"private": true, | ||
"scripts": { | ||
"lint": "eslint src" | ||
}, | ||
"exports": { | ||
"./worker": "./src/worker.ts", | ||
"./entry": "./src/entry.ts" | ||
}, | ||
"devDependencies": { | ||
"@types/chrome": "^0.0.270" | ||
} | ||
} |
Oops, something went wrong.