Skip to content

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

Closed
@my-lalex

Description

@my-lalex

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. 😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions