-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cross-document error propagation (#755)
- Loading branch information
1 parent
74d9a83
commit 77ae311
Showing
15 changed files
with
201 additions
and
132 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,53 +1,51 @@ | ||
import type { JsonValue } from '@bufbuild/protobuf'; | ||
import { ConnectError } from '@connectrpc/connect'; | ||
import { errorToJson } from '@connectrpc/connect/protocol-connect'; | ||
import { | ||
ActionBuildRequest, | ||
ActionBuildResponse, | ||
isActionBuildRequest, | ||
isOffscreenRequest, | ||
} from '@penumbra-zone/types/src/internal-msg/offscreen'; | ||
|
||
chrome.runtime.onMessage.addListener((req, _sender, respond) => { | ||
if (!isOffscreenRequest(req)) return false; | ||
if (isActionBuildRequest(req.request)) { | ||
const { type, request } = req; | ||
const { type, request } = req; | ||
if (isActionBuildRequest(request)) { | ||
void (async () => { | ||
const response = spawnWorker(request); | ||
const res = await response | ||
.then(data => ({ type, data })) | ||
.catch((e: Error) => ({ | ||
try { | ||
const data = await spawnActionBuildWorker(request); | ||
respond({ type, data }); | ||
} catch (e) { | ||
respond({ | ||
type, | ||
error: `Offscreen: ${e.message}`, | ||
})); | ||
respond(res); | ||
error: errorToJson(ConnectError.from(e), undefined), | ||
}); | ||
} | ||
})(); | ||
return true; | ||
} | ||
return true; | ||
return false; | ||
}); | ||
|
||
const spawnWorker = (req: ActionBuildRequest): Promise<JsonValue> => { | ||
return new Promise((resolve, reject) => { | ||
const worker = new Worker(new URL('../wasm-build-action.ts', import.meta.url)); | ||
const spawnActionBuildWorker = (req: ActionBuildRequest) => { | ||
const worker = new Worker(new URL('../wasm-build-action.ts', import.meta.url)); | ||
return new Promise<ActionBuildResponse>((resolve, reject) => { | ||
const onWorkerMessage = (e: MessageEvent) => resolve(e.data as ActionBuildResponse); | ||
|
||
const onWorkerMessage = (e: MessageEvent) => { | ||
resolve(e.data as JsonValue); | ||
worker.removeEventListener('error', onWorkerError); | ||
worker.terminate(); | ||
}; | ||
|
||
const onWorkerError = (ev: ErrorEvent) => { | ||
const { filename, lineno, colno, message } = ev; | ||
const onWorkerError = ({ error, filename, lineno, colno, message }: ErrorEvent) => | ||
reject( | ||
ev.error instanceof Error | ||
? ev.error | ||
error instanceof Error | ||
? error | ||
: new Error(`Worker ErrorEvent ${filename}:${lineno}:${colno} ${message}`), | ||
); | ||
worker.removeEventListener('message', onWorkerMessage); | ||
worker.terminate(); | ||
}; | ||
|
||
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); | ||
}); | ||
}).finally(() => worker.terminate()); | ||
}; |
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
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
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
Oops, something went wrong.