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

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

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

问题

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

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

// src/generator/+page.svelte

onMount(() => {
    const w = new Worker("src/routes/generator/worker");
    w.postMessage("password");
    w.onmessage = function (event) {
      console.log(event.data);
    };
  });
// src/generator/worker.ts

import zxcvbn from "zxcvbn";    // 报错 "Uncaught SyntaxError: Cannot use import statement outside a module (at worker.ts:1:1)"

const passwordStrength = (password: string) => {
  const res = zxcvbn(password);
  postMessage(res);
};

self.onmessage = (e) => {
  passwordStrength(e.data);
};

我对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.

// src/generator/+page.svelte

onMount(() => {
    const w = new Worker("src/routes/generator/worker");
    w.postMessage("password");
    w.onmessage = function (event) {
      console.log(event.data);
    };
  });
// src/generator/worker.ts

import zxcvbn from "zxcvbn";    // throws error "Uncaught SyntaxError: Cannot use import statement outside a module (at worker.ts:1:1)"

const passwordStrength = (password: string) => {
  const res = zxcvbn(password);
  postMessage(res);
};

self.onmessage = (e) => {
  passwordStrength(e.data);
};

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

答案1

得分: 0

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

<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:

&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:

// src/generator/+page.svelte

import MyWorker from "src/generator/worker?worker";

const w = new MyWorker();
w.postMessage("password");
w.onmessage = function (event) {
   console.log(event.data);
};
// src/generator/worker.ts

import zxcvbn from "zxcvbn";

const passwordStrength = (password: string) => {
  const res = zxcvbn(password);
  postMessage(res);
};

self.onmessage = (e) => {
  passwordStrength(e.data);
};

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

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

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

英文:

I ended up importing the worker as such

// src/generator/+page.svelte

import MyWorker from &quot;src/generator/worker?worker&quot;;


const w = new MyWorker();
w.postMessage(&quot;password&quot;);
w.onmessage = function (event) {
   console.log(event.data);
};
// src/generator/worker.ts

import zxcvbn from &quot;zxcvbn&quot;;

const passwordStrength = (password: string) =&gt; {
  const res = zxcvbn(password);
  postMessage(res);
};

self.onmessage = (e) =&gt; {
  passwordStrength(e.data);
};

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:

确定