Repository navigation
fix(shell-bson-parser): drop web-worker package #934
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { WorkerHandle, WorkerCallbacks } from './worker-types.js'; | ||
|
|
||
| export function createBrowserWorker( | ||
| source: string, | ||
| { onMessage, onError }: WorkerCallbacks, | ||
| ): WorkerHandle { | ||
| // Compass Web injects whatwg-url for bare URL references; it lacks blob APIs. | ||
| // Use the native constructor for blob creation and revocation. | ||
| const blobUrl = globalThis.URL.createObjectURL( | ||
| new Blob([source], { type: 'text/javascript' }), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recently learned that this is allowed even when you're running node with eval disabled... neat! but concerning? I don't know... nothing to action here lol |
||
| ); | ||
| try { | ||
| const worker = new globalThis.Worker(blobUrl); | ||
| worker.addEventListener('message', (event: MessageEvent) => | ||
| onMessage(event.data), | ||
| ); | ||
| worker.addEventListener('error', (event: ErrorEvent) => | ||
| onError(new Error(event.message || 'Worker error')), | ||
| ); | ||
| worker.addEventListener('messageerror', () => | ||
| onError(new Error('Worker message could not be deserialized')), | ||
| ); | ||
|
|
||
| return { | ||
| postMessage: (message) => worker.postMessage(message), | ||
| terminate: () => { | ||
| worker.terminate(); | ||
| globalThis.URL.revokeObjectURL(blobUrl); | ||
| }, | ||
| }; | ||
| } catch (err) { | ||
| globalThis.URL.revokeObjectURL(blobUrl); | ||
| throw err; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import type { Worker as NodeWorker } from 'worker_threads'; | ||
| import type { WorkerHandle, WorkerCallbacks } from './worker-types.js'; | ||
|
|
||
| export function createNodeWorker( | ||
| source: string, | ||
| { onMessage, onError }: WorkerCallbacks, | ||
| ): WorkerHandle { | ||
| // getBuiltinModule keeps bundlers from rewriting or polyfilling the import. | ||
| const threads = | ||
| typeof process !== 'undefined' && process.getBuiltinModule | ||
| ? process.getBuiltinModule('worker_threads') | ||
| : undefined; | ||
| if (!threads) { | ||
| throw new Error( | ||
| '@mongodb-js/shell-bson-parser needs a global Worker or process.getBuiltinModule', | ||
| ); | ||
|
Comment on lines
+14
to
+16
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well in this case the global wouldn't help this code work right? |
||
| } | ||
|
|
||
| // The worker bundle talks to worker_threads' parentPort itself. | ||
| const worker: NodeWorker = new threads.Worker(source, { eval: true }); | ||
| // Fails pending requests right away if the thread dies, instead of waiting | ||
| // for their timeout. Also fires after terminate(), ignored by the client. | ||
| worker.on('exit', (code: number) => | ||
| onError(new Error(`Worker exited with code ${code}`)), | ||
| ); | ||
| worker.on('message', onMessage); | ||
| worker.on('error', (err: Error) => | ||
| onError(new Error(err?.message || 'Worker error')), | ||
| ); | ||
| worker.on('messageerror', () => | ||
| onError(new Error('Worker message could not be deserialized')), | ||
| ); | ||
| // An idle worker should not keep the host process alive. Must come after | ||
| // the listeners, since adding a 'message' listener re-refs the port. | ||
| worker.unref(); | ||
|
|
||
| return { | ||
| postMessage: (message) => worker.postMessage(message), | ||
| terminate: () => void worker.terminate(), | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to be so explicit about the globalThis? same for Worker?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, although i closed this PR, I still have it in the code already. This is for compass-web. We resolve url to whatwg-url and that does not have blob-url methods. This breaks for compass-web and i had to go this route for that.
What would be the better alternate here?