javascript - 我需要导入/需要一个 Node 模块 - 但它包含一个大于 4kb 的 wasm 文件。我的应用程序是一个 Electron 应用程序。我怎样才能使这项工作?

标签 javascript node.js typescript electron node-modules

我正在尝试导入 essentia.js。运行时,任何导入模块的尝试都会导致

"WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread."


我试着做一个 worker ,但意识到你不能导入 Node 模块。
如果 wasm 文件大于 4kb,是否可以处理 npm 导入?我该怎么做?
作为引用,我正在尝试在 Electron 应用程序上使用 essentia.js 中的函数。我通过 cmd 成功地用 node test.js 运行了一个测试函数;没有意识到它会在 Electron 上失败。
Essentia.js 有一个教程,他们说:

Note: You shouldn't import the essentia-wasm.module.js on the main thread. The ideal way is to use it along with the AudioWorklet design pattern or with WebWorkers. Also, it is recommended to use local files instead of CDN in production. https://mtg.github.io/essentia.js/docs/api/tutorial-1.%20Getting%20Started.html


但是,我不知道该怎么做。我一直在试图理解我必须做什么,但它需要接受很多。
我的目标是调用 Essentia 函数进行 bpm 和键分析 - 但我对多线程非常陌生。
例如,我需要 essentia.js-core - 但这个文件需要 wasm 文件。如何准备 wasm 文件以便我可以使用其他文件?这可能吗?
回想起来, Electron 可能不是最好的设计选择——但我们选择了熟悉。我知道 nodeJS 不是问题——事实上这个应用程序本质上是在 chrome 浏览器上运行的。

最佳答案

electron 可以在其网络 worker 中支持 Node 模块。
在 webPreferences 中,确保设置了 nodeIntegrationInWorker: true。
例如

Main.mainWindow = new Main.BrowserWindow({ width: 800, height: 600, webPreferences: {nodeIntegration: true, enableRemoteModule: true, nodeIntegrationInWorker: true} });
完成此操作后,您可能需要 worker.js 文件中的 Node 模块
您还必须确保您的 worker 的类型是这样的:
const worker = new Worker('./someWorker.js', { type: 'module' });
然后我可以执行以下操作
   await worker.postMessage(audioData);

   worker.onmessage = ({ data }) => {
      console.log(`page got message: ${data}`);      
   };
在我的工作文件中执行以下操作
essentia = require('essentia.js');

addEventListener('message', e => {
  console.log(e.data);

  let audio = e.data.channelData[0];
  let inputSignalVector = essentia.arrayToVector(audio);
  let key = essentia.KeyExtractor(inputSignalVector);

  console.log("working");

  postMessage(key);

});
在我的工作人员中,我使用 Node 模块来估计我传递的音频缓冲区的 key 。这是 Electron worker 需要 Node 模块的证明。

关于javascript - 我需要导入/需要一个 Node 模块 - 但它包含一个大于 4kb 的 wasm 文件。我的应用程序是一个 Electron 应用程序。我怎样才能使这项工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66359591/

相关文章:

javascript - 不能 .join() 函数参数 - TypeError : undefined is not a function

javascript - MooTools将json数据注入(inject)到select中在IE中是向后的

html - Angular 5 FormGroup 重置不会重置验证器

typescript - 真正的setTimeout类型

javascript - 当我们使用 Jquery/Javascript 单击下一个和上一个按钮时动态添加图像

javascript - 可靠的 html5 输入类型处理

node.js - Multer:在不同的文件夹中上传不同的文件类型

node.js - 如何使用 react 前端点击 Electron 将文件下载到系统

node.js - 在 MongoDB 中使用变量作为字段名时,我可以在嵌入式文档上使用 '$set' 吗?

angular2 TypeError : self. _el_11 不是函数