Skip to content
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

Inline Web Worker looses special characters #12117

Closed
7 tasks done
Simolation opened this issue Feb 20, 2023 · 3 comments · Fixed by #15866
Closed
7 tasks done

Inline Web Worker looses special characters #12117

Simolation opened this issue Feb 20, 2023 · 3 comments · Fixed by #15866
Labels
contribution welcome feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@Simolation
Copy link

Simolation commented Feb 20, 2023

Describe the bug

I am creating a TypeScript library using the Vite lib mode. When I use an inline Web Worker that has special characters in the code, those special characters are decoded incorrectly.

For example, I added a • character ( • ) into the Web Worker. When it is built and imported in another Vite project, the printed bullet character is â�¢ instead of • .

I observed that inside of the base64 encoded Web Worker in the build, the character is seemingly still correct, when I copy and paste the base64 text into https://www.base64decode.org/.

So, I suppose the problem is when loading and decoding the inline Web Worker using atob, as atob("IOKAoiBIZWxsbyBmcm9tIHdvcmtlcg==") is â\x80¢ Hello from worker.

Reproduction

https://stackblitz.com/edit/vitejs-vite-tyt9yp

Steps to reproduce

Run npm run install && npm run dev in the vite-project folder to start the package which imports the vite-lib. In the console log in the browser, the message the faulty message from the inline Web Worker is printed.

System Info

System:
    OS: macOS 13.2
    CPU: (8) arm64 Apple M1
    Memory: 156.81 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.19.0 - ~/.nvm/versions/node/v16.19.0/bin/node
    Yarn: 3.3.1 - ~/.yarn/bin/yarn
    npm: 8.19.3 - ~/.nvm/versions/node/v16.19.0/bin/npm
  Browsers:
    Chrome: 110.0.5481.100
    Firefox: 109.0.1
    Safari: 16.3

Used Package Manager

yarn

Logs

No response

Validations

@Simolation
Copy link
Author

Simolation commented Feb 20, 2023

As MDN states, the atob()method only handles ASCII and does not support UTF-8.

A solution on StackOverflow might be this one: https://stackoverflow.com/a/43271130/7268229
I am not sure which implications on performance this has, but it fixes the UTF-8 character decoding when injected into the worker build.

function decodeBase64(base64) {
    return new TextDecoder().decode(Uint8Array.from(atob(base64), c => c.charCodeAt(0)));
}

@zhengcling
Copy link

As MDN states, the atob()method only handles ASCII and does not support UTF-8.

A solution on StackOverflow might be this one: https://stackoverflow.com/a/43271130/7268229 I am not sure which implications on performance this has, but it fixes the UTF-8 character decoding when injected into the worker build.

function decodeBase64(base64) {
    return new TextDecoder().decode(Uint8Array.from(atob(base64), c => c.charCodeAt(0)));
}

I'm facing the same problem. How's it going? Thanks! @Simolation @sapphi-red

@sapphi-red
Copy link
Member

sapphi-red commented Oct 12, 2023

@zhengcling You can help us by rebasing #12122 on main branch and adding a test for it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contribution welcome feat: web workers p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
3 participants