如何在 JavaScript 和 SvelteKit 中运行具有依赖项的工作程序

huangapple go评论119阅读模式
英文:

How can I run a worker with dependencies in javascript and sveltekit

问题

我正在尝试在我的项目中使用zxcvbn模块。对于密码长度不超过50个字符的情况,它能够正常工作。但是,当密码长度超过50个字符时,评估每个密码所需的时间太长了。

我尝试使用Worker来运行函数并在Sveltekit中更新状态。代码如下:

  1. // src/generator/+page.svelte
  2. onMount(() => {
  3. const w = new Worker("src/routes/generator/worker");
  4. w.postMessage("password");
  5. w.onmessage = function (event) {
  6. console.log(event.data);
  7. };
  8. });
  1. // src/generator/worker.ts
  2. import zxcvbn from "zxcvbn"; // 报错 "Uncaught SyntaxError: Cannot use import statement outside a module (at worker.ts:1:1)"
  3. const passwordStrength = (password: string) => {
  4. const res = zxcvbn(password);
  5. postMessage(res);
  6. };
  7. self.onmessage = (e) => {
  8. passwordStrength(e.data);
  9. };

我对Sveltekit和JavaScript/TypeScript都不太熟悉,所以欢迎任何建议。

英文:

I am trying to use the module zxcvbn in my project. It is fine for passwords up to around 50 chars. After it takes too long of a time to grade each password.

I'm trying to use workers to run the function and update the state in sveltekit. The code looks like this.

  1. // src/generator/+page.svelte
  2. onMount(() => {
  3. const w = new Worker("src/routes/generator/worker");
  4. w.postMessage("password");
  5. w.onmessage = function (event) {
  6. console.log(event.data);
  7. };
  8. });
  1. // src/generator/worker.ts
  2. import zxcvbn from "zxcvbn"; // throws error "Uncaught SyntaxError: Cannot use import statement outside a module (at worker.ts:1:1)"
  3. const passwordStrength = (password: string) => {
  4. const res = zxcvbn(password);
  5. postMessage(res);
  6. };
  7. self.onmessage = (e) => {
  8. passwordStrength(e.data);
  9. };

I'm new to Sveltekit and javascript/typescript as a whole so any suggestions are welcome.

答案1

得分: 0

你应该在每个使用的 script 标签上添加一个属性 type="module"。查看下面的示例:

  1. <script src="/path/to/your/amazing/script.js" type="module"></script>
英文:

You sould add an attribute type=&quot;module&quot; on each script tag you used. Look the example below:

  1. &lt;script src=&quot;/path/to/your/amazing/script.js&quot; type=&quot;module&quot;&gt;&lt;/script&gt;

答案2

得分: 0

I ended up importing the worker as such:

  1. // src/generator/+page.svelte
  2. import MyWorker from "src/generator/worker?worker";
  3. const w = new MyWorker();
  4. w.postMessage("password");
  5. w.onmessage = function (event) {
  6. console.log(event.data);
  7. };
  1. // src/generator/worker.ts
  2. import zxcvbn from "zxcvbn";
  3. const passwordStrength = (password: string) => {
  4. const res = zxcvbn(password);
  5. postMessage(res);
  6. };
  7. self.onmessage = (e) => {
  8. passwordStrength(e.data);
  9. };

Vite支持直接导入Worker。然后我可以毫无问题地运行它,而且不会阻塞其他JavaScript。我看到的另一种解决方案是将Worker创建为:

  1. new Worker("/my/path/workerfile?worker", {type: "module"})

免责声明:解决方案/说明可能不是100%正确,因为我仍在学习中。

英文:

I ended up importing the worker as such

  1. // src/generator/+page.svelte
  2. import MyWorker from &quot;src/generator/worker?worker&quot;;
  3. const w = new MyWorker();
  4. w.postMessage(&quot;password&quot;);
  5. w.onmessage = function (event) {
  6. console.log(event.data);
  7. };
  1. // src/generator/worker.ts
  2. import zxcvbn from &quot;zxcvbn&quot;;
  3. const passwordStrength = (password: string) =&gt; {
  4. const res = zxcvbn(password);
  5. postMessage(res);
  6. };
  7. self.onmessage = (e) =&gt; {
  8. passwordStrength(e.data);
  9. };

Vite supports importing the worker directly. Then I could run it with no problem and it wouldn't block other javascript. A different solution I saw was to make the worker like this new Worker(&quot;/my/path/workerfile?worker&quot;, {type: &quot;module&quot;})

DISCLAIMER: Solution/explanation might not be 100% correct as I am still learning.

huangapple
  • 本文由 发表于 2023年7月3日 11:19:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定