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

eval'ing generated code in web worker defined as module is trying to execute commonJS require statements in browser #9987

Open
Lorthiz opened this issue Oct 23, 2024 · 0 comments

Comments

@Lorthiz
Copy link

Lorthiz commented Oct 23, 2024

💬 RFC

I have been looking for similar issue, but it seems to be a pretty niche area so I wanted to discuss it here. I will try to explain it as best as I can. I am working on a fork of Kaitai Web IDE. It's environment where you can describe binary formats using YAML, then that yaml is sent to compiler that generates parsers for those binary formats in requested language. Web IDE also has functionality to get that generated code and execute it to parse data in the browser.

🔦 Context

So the workflow looks like this:

  1. User writes grammar for a binray format(YAML)
  2. That grammar is compiled to JS code(module with UMD like header)
  3. Generated code gets executed in the browser to parse data and display it for the user in form of Object tree and HexViewer;

The 3rd point is executed in WebWorker, so web workers recives binary(arrayBuffer) data and generated Parsers code to execute that code on data and return parsed data. It was working fine as long as I just used plain JS file

const codeExecutorWorker = new Worker(new URL("data-url:./CodeExecution/KaitaiCodeWorker.ts", import.meta.url));

Now I wanted to add more logic to it, do some code splitting and the best way to do it is to define that worker as a module so I can use import statements, static typing and all the Typescript goodies so I just added {type: 'module'} the way it was recommended in Parcel docs:

const codeExecutorWorker = new Worker(new URL("data-url:./CodeExecution/KaitaiCodeWorker.ts", import.meta.url), {type: "module"});

And from this point onward, Worker is trying to execute CommonJS requires in UMD header from generated code.
The example header looks like this:

(function (root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['kaitai-struct/KaitaiStream', './DosDatetime'], factory);
  } else if (typeof module === 'object' && module.exports) {
    module.exports = factory(require('kaitai-struct/KaitaiStream'), require('./DosDatetime'));
  } else {
    root.Zip = factory(root.KaitaiStream, root.DosDatetime);
  }
}(typeof self !== 'undefined' ? self : this, function (KaitaiStream, DosDatetime) {
  ....defining Zip class...
  return Zip;
})();
return Zip;
}));

Is there a way to tell Parcel that this specific worker should not be able to execute CommonJS modules(should not have exports object present during execution) and how can I do it?

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

1 participant