Skip to content

Commit

Permalink
Update locateFile to a fix critical bug in vite
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromewu committed Aug 9, 2023
1 parent 96b00d9 commit f3c67a2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
"lint:root": "eslint tests",
"build": "npm run build --workspace=packages --if-present",
"pretest": "npm run build",
"serve": "http-server -c-1 -s -p 3000 .",
"test": "server-test test:browser:server 3000 test:all",
"test:all": "npm-run-all test:*:*:*",
"test:all": "npm-run-all test:browser:*:*",
"test:browser": "mocha-headless-chrome -a enable-features=SharedArrayBuffer",
"test:browser:core:mt": "npm run test:browser -- -f http://localhost:3000/tests/ffmpeg-core-mt.test.html",
"test:browser:core:st": "npm run test:browser -- -f http://localhost:3000/tests/ffmpeg-core-st.test.html",
"test:browser:ffmpeg:mt": "npm run test:browser -- -f http://localhost:3000/tests/ffmpeg-mt.test.html",
"test:browser:ffmpeg:st": "npm run test:browser -- -f http://localhost:3000/tests/ffmpeg-st.test.html",
"test:browser:server": "http-server -c-1 -s -p 3000 .",
"test:browser:server": "npm run serve",
"test:node": "mocha --exit --bail -t 60000",
"test:node:core:mt": "npm run test:node -- --require tests/test-helper-mt.js tests/ffmpeg-core.test.js",
"test:node:core:st": "npm run test:node -- --require tests/test-helper-st.js tests/ffmpeg-core.test.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/core-mt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ffmpeg/core-mt",
"version": "0.12.1",
"version": "0.12.2",
"description": "FFmpeg WebAssembly version (multi thread)",
"main": "./dist/umd/ffmpeg-core.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ffmpeg/core",
"version": "0.12.1",
"version": "0.12.2",
"description": "FFmpeg WebAssembly version (single thread)",
"main": "./dist/umd/ffmpeg-core.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ffmpeg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ffmpeg/ffmpeg",
"version": "0.12.2",
"version": "0.12.3",
"description": "FFmpeg WebAssembly version for browser",
"main": "./dist/umd/ffmpeg.js",
"types": "./dist/esm/index.d.ts",
Expand Down
10 changes: 4 additions & 6 deletions packages/ffmpeg/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ const load = async ({

ffmpeg = await (self as WorkerGlobalScope).createFFmpegCore({
// Fix `Overload resolution failed.` when using multi-threaded ffmpeg-core.
mainScriptUrlOrBlob: coreURL,
locateFile: (path: string, prefix: string): string => {
if (path.endsWith(".wasm")) return wasmURL;
if (path.endsWith(".worker.js")) return workerURL;
return prefix + path;
},
// Encoded wasmURL and workerURL in the URL as a hack to fix locateFile issue.
mainScriptUrlOrBlob: `${coreURL}#${btoa(
JSON.stringify({ wasmURL, workerURL })
)}`,
});
ffmpeg.setLogger((data) =>
self.postMessage({ type: FFMessageType.LOG, data })
Expand Down
32 changes: 32 additions & 0 deletions src/bind/ffmpeg/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,42 @@ function reset() {
Module["timeout"] = -1;
}

/**
* In multithread version of ffmpeg.wasm, the bootstrap process is like:
* 1. Execute ffmpeg-core.js
* 2. ffmpeg-core.js spawns workers by calling `new Worker("ffmpeg-core.worker.js")`
* 3. ffmpeg-core.worker.js imports ffmpeg-core.js
* 4. ffmpeg-core.js imports ffmpeg-core.wasm
*
* It is a straightforward process when all files are in the same location.
* But when files are in different location (or Blob URL), #4 fails because
* there is no way to pass custom ffmpeg-core.wasm URL to ffmpeg-core.worker.js
* when it imports ffmpeg-core.js in #3.
*
* To fix this issue, a hack here is leveraging mainScriptUrlOrBlob variable by
* adding wasmURL and workerURL in base64 format as query string. ex:
*
* http://example.com/ffmpeg-core.js#{btoa(JSON.stringify({"wasmURL": "...", "workerURL": "..."}))}
*
* Thus, we can successfully extract custom URLs using _locateFile funciton.
*/
function _locateFile(path, prefix) {
const mainScriptUrlOrBlob = Module["mainScriptUrlOrBlob"];
if (mainScriptUrlOrBlob) {
const { wasmURL, workerURL } = JSON.parse(
atob(mainScriptUrlOrBlob.slice(mainScriptUrlOrBlob.lastIndexOf("#") + 1))
);
if (path.endsWith(".wasm")) return wasmURL;
if (path.endsWith(".worker.js")) return workerURL;
}
return prefix + path;
}

Module["stringToPtr"] = stringToPtr;
Module["stringsToPtr"] = stringsToPtr;
Module["print"] = print;
Module["printErr"] = printErr;
Module["locateFile"] = _locateFile;

Module["exec"] = exec;
Module["setLogger"] = setLogger;
Expand Down

0 comments on commit f3c67a2

Please sign in to comment.