Repository navigation
chore(bson-parsing): integration with compass #910
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
393f9ba
b0d3693
78bc42e
77a8173
a8822fb
dd3bdde
75b942a
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 |
|---|---|---|
|
|
@@ -100,5 +100,3 @@ const query = await parse('{ a: 1 }'); | |
|
|
||
| terminateWorker(); | ||
| ``` | ||
|
|
||
| This package is ESM only. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,33 +33,32 @@ const pending = new Map< | |
| } | ||
| >(); | ||
|
|
||
| const isNodeEnv = | ||
| typeof window === 'undefined' && | ||
| typeof process !== 'undefined' && | ||
| !!process.versions?.node; | ||
|
|
||
| async function getWorkerScriptUrl(): Promise<string> { | ||
| if (process.env.TEST_WORKER_SCRIPT_URL) { | ||
| return new URL( | ||
| process.env.TEST_WORKER_SCRIPT_URL, | ||
| import.meta.url, | ||
| ).toString(); | ||
| } | ||
| if (isNodeEnv) { | ||
| return new URL('./worker.js', import.meta.url).toString(); | ||
| /** Exported for tests */ | ||
| export async function getWorkerScriptUrl(): Promise<string> { | ||
| const override = | ||
| typeof process !== 'undefined' | ||
| ? process.env.TEST_WORKER_SCRIPT_URL | ||
| : undefined; | ||
| if (override) { | ||
| return new URL(override, import.meta.url).toString(); | ||
| } | ||
|
|
||
| // On browser env we want to fetch and blob so that the worker | ||
| // script can run on the web running locally. | ||
| const scriptUrl = new URL('./worker.js', import.meta.url); | ||
| if (scriptUrl.protocol === 'file:') { | ||
| return scriptUrl.toString(); | ||
| } | ||
| const response = await fetch(scriptUrl); | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Failed to fetch shell-bson-parser worker script: ${response.status} ${response.statusText}`, | ||
| ); | ||
| } | ||
| const code = await response.text(); | ||
| blobUrl = URL.createObjectURL(new Blob([code], { type: 'text/javascript' })); | ||
| // Compass Web injects whatwg-url for bare URL references; it lacks blob APIs. | ||
| // Use the native constructor for blob creation and revocation. | ||
| blobUrl = globalThis.URL.createObjectURL( | ||
|
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. Is |
||
| new Blob([code], { type: 'text/javascript' }), | ||
| ); | ||
| return blobUrl; | ||
| } | ||
|
|
||
|
|
@@ -73,7 +72,10 @@ function createWorker(): Promise<Worker> { | |
|
|
||
| workerPromise = (async () => { | ||
| const scriptUrl = await getWorkerScriptUrl(); | ||
| const newWorker = new WebWorker(scriptUrl, { type: 'module' }); | ||
| const newWorker = new WebWorker( | ||
| scriptUrl, | ||
| typeof globalThis.Worker === 'function' ? undefined : { type: 'module' }, | ||
| ); | ||
| const onMessageHandler = (event: MessageEvent<WorkerResponse>) => { | ||
| const response = event.data; | ||
| const entry = pending.get(response.id); | ||
|
|
@@ -153,7 +155,7 @@ export function terminateWorker( | |
| reason: Error = new Error('Worker terminated'), | ||
| ): void { | ||
| if (worker) worker.terminate(); | ||
| if (blobUrl) URL.revokeObjectURL(blobUrl); | ||
| if (blobUrl) globalThis.URL.revokeObjectURL(blobUrl); | ||
|
Member
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. Why are we adding this back?
Collaborator
Author
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. Yes, this showed in the integration with compass-web. There we alias |
||
|
|
||
| worker = null; | ||
| workerPromise = null; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,21 @@ | ||
| // Avoiding the import of these two functions, | ||
| // adding them here to keep this file self-contained | ||
| function unmarkBSON(value) { | ||
| function untrackBSON(value) { | ||
| return value.data; | ||
| } | ||
| function markBSON(value) { | ||
| function trackBSON(value) { | ||
| return { data: value, bsonTypes: new Map() }; | ||
| } | ||
| self.onmessage = (event) => { | ||
| const { id, args } = event.data; | ||
| const [delayMs] = unmarkBSON(args); | ||
| const [delayMs] = untrackBSON(args); | ||
| const start = Date.now(); | ||
| while (Date.now() - start < delayMs) { | ||
| // noop | ||
| } | ||
| self.postMessage({ | ||
| id, | ||
| ok: true, | ||
| result: markBSON('done'), | ||
| result: trackBSON('done'), | ||
| }); | ||
| }; |
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.
Should we test this
if file:path?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.
added in 9c6fcdd