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

Cannot use in web worker (and quick fix) #34

Open
my-lalex opened this issue Dec 26, 2024 · 1 comment
Open

Cannot use in web worker (and quick fix) #34

my-lalex opened this issue Dec 26, 2024 · 1 comment

Comments

@my-lalex
Copy link

The JS main file uses if (typeof window === 'undefined') to check if we're in a browser or Node.js environment...
In a web worker, the window property doesn't exists, juste like the global object (used by Node.js)
And the same test is used in the init function to load the WASM bytecode so just like previously, it doens't work in web workers...

As a workaround, I updated and rebuilt the library after

  • removing condition to the first polyfill bloc and using globalThis instead of global to
!globalThis.crypto && (globalThis.crypto = {
  async getRandomValues(b) {
    const { randomFillSync } = await import('crypto');
    randomFillSync(b);
  }
});
!globalThis.performance && (globalThis.performance = {
  now() {
    const [sec, nsec] = process.hrtime();
    return sec * 1000 + nsec / 1000000;
  }
});
  • testing the fs.readFileSyncexistence in the initmain function and keep on using globalThis
if (fs && fs.readFileSync) {
    globalThis.excelize = {};
    const fs = await import('fs');
    buffer = pako.ungzip(fs.readFileSync(wasmPath));
  } else {
    globalThis.excelize = {};
    buffer = pako.ungzip(await (await fetch(wasmPath)).arrayBuffer());
  }

It's not the cleaner way to fix this issue and I don't have much time to spend on this but hey... it works this way. 😅

@xuri
Copy link
Owner

xuri commented Dec 30, 2024

Thanks for your feedback, if you can create a PR for this, that's would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants