-
Notifications
You must be signed in to change notification settings - Fork 12
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
Check out await-sync
- replace child process with WebWorkers
#36
Comments
I think someone else might have mentioned web workers before. I haven't used Deno or Bun but I'm not against supporting that. I think the main doubt I had was the same as what you mention:
But there's actually a clear answer to that: sync xhr has a bunch of limitations, including not allowing binary responses and (at least in the past) some cryptic, undocumented CORS restrictions. |
Oh, that brings me back in the memory lane... And now i remembered that i also wanted to try and read Blob's in a sync mannar also in the main thread in browsers too. i even have a old gist for that. const blob = new Blob(['123'])
const xhr = new XMLHttpRequest()
const url = URL.createObjectURL(blob)
// required if you need to read binary data:
xhr.overrideMimeType('text/plain; charset=x-user-defined')
xhr.open('GET', url, false)
xhr.send()
const uint8 = Uint8Array.from(xhr.response, c => c.charCodeAt(0))
const text = new TextDecoder().decode(uint8)
const arrayBuffer = uint8.buffer
const json = JSON.parse(text) the trick is to override mime type and use a charset that isn't recognised. then xhr won't try to be smart and change the response, so you will actually get back the raw bytes. |
ironically MDN says it's bad to use sync
And yet they have a tutorial on how to get around it here: Receiving binary data in older browsers
|
to-sync
- replace child process with WebWorkersawaitSync
- replace child process with WebWorkers
awaitSync
- replace child process with WebWorkersawait-sync
- replace child process with WebWorkers
I've implemented the trick for binary data from MDN that you mentioned, it's released in |
I just created this async-to-sync package today that i named await-sync
it utilize web workers instead of spawning sync processes and data is transfered synchronous over
SharedArrayBuffer
so less code needs to be copied over.Therefore it's also more compatible with other enviorments like Deno, Bun, and also Web Workers.
it's as simple as just doing:
if you would use this instead then your package would have the possibility of being able to run in Deno, Bun.js and also Web Workers. but why web workers when you sync xhr...? that's a good question...
The text was updated successfully, but these errors were encountered: