Skip to content

Commit f3c67a2

Browse files
committed
Update locateFile to a fix critical bug in vite
1 parent 96b00d9 commit f3c67a2

File tree

7 files changed

+52
-14
lines changed

7 files changed

+52
-14
lines changed

package-lock.json

+10-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
"lint:root": "eslint tests",
88
"build": "npm run build --workspace=packages --if-present",
99
"pretest": "npm run build",
10+
"serve": "http-server -c-1 -s -p 3000 .",
1011
"test": "server-test test:browser:server 3000 test:all",
11-
"test:all": "npm-run-all test:*:*:*",
12+
"test:all": "npm-run-all test:browser:*:*",
1213
"test:browser": "mocha-headless-chrome -a enable-features=SharedArrayBuffer",
1314
"test:browser:core:mt": "npm run test:browser -- -f http://localhost:3000/tests/ffmpeg-core-mt.test.html",
1415
"test:browser:core:st": "npm run test:browser -- -f http://localhost:3000/tests/ffmpeg-core-st.test.html",
1516
"test:browser:ffmpeg:mt": "npm run test:browser -- -f http://localhost:3000/tests/ffmpeg-mt.test.html",
1617
"test:browser:ffmpeg:st": "npm run test:browser -- -f http://localhost:3000/tests/ffmpeg-st.test.html",
17-
"test:browser:server": "http-server -c-1 -s -p 3000 .",
18+
"test:browser:server": "npm run serve",
1819
"test:node": "mocha --exit --bail -t 60000",
1920
"test:node:core:mt": "npm run test:node -- --require tests/test-helper-mt.js tests/ffmpeg-core.test.js",
2021
"test:node:core:st": "npm run test:node -- --require tests/test-helper-st.js tests/ffmpeg-core.test.js",

packages/core-mt/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ffmpeg/core-mt",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"description": "FFmpeg WebAssembly version (multi thread)",
55
"main": "./dist/umd/ffmpeg-core.js",
66
"exports": {

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ffmpeg/core",
3-
"version": "0.12.1",
3+
"version": "0.12.2",
44
"description": "FFmpeg WebAssembly version (single thread)",
55
"main": "./dist/umd/ffmpeg-core.js",
66
"exports": {

packages/ffmpeg/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ffmpeg/ffmpeg",
3-
"version": "0.12.2",
3+
"version": "0.12.3",
44
"description": "FFmpeg WebAssembly version for browser",
55
"main": "./dist/umd/ffmpeg.js",
66
"types": "./dist/esm/index.d.ts",

packages/ffmpeg/src/worker.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,10 @@ const load = async ({
7070

7171
ffmpeg = await (self as WorkerGlobalScope).createFFmpegCore({
7272
// Fix `Overload resolution failed.` when using multi-threaded ffmpeg-core.
73-
mainScriptUrlOrBlob: coreURL,
74-
locateFile: (path: string, prefix: string): string => {
75-
if (path.endsWith(".wasm")) return wasmURL;
76-
if (path.endsWith(".worker.js")) return workerURL;
77-
return prefix + path;
78-
},
73+
// Encoded wasmURL and workerURL in the URL as a hack to fix locateFile issue.
74+
mainScriptUrlOrBlob: `${coreURL}#${btoa(
75+
JSON.stringify({ wasmURL, workerURL })
76+
)}`,
7977
});
8078
ffmpeg.setLogger((data) =>
8179
self.postMessage({ type: FFMessageType.LOG, data })

src/bind/ffmpeg/bind.js

+32
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,42 @@ function reset() {
8383
Module["timeout"] = -1;
8484
}
8585

86+
/**
87+
* In multithread version of ffmpeg.wasm, the bootstrap process is like:
88+
* 1. Execute ffmpeg-core.js
89+
* 2. ffmpeg-core.js spawns workers by calling `new Worker("ffmpeg-core.worker.js")`
90+
* 3. ffmpeg-core.worker.js imports ffmpeg-core.js
91+
* 4. ffmpeg-core.js imports ffmpeg-core.wasm
92+
*
93+
* It is a straightforward process when all files are in the same location.
94+
* But when files are in different location (or Blob URL), #4 fails because
95+
* there is no way to pass custom ffmpeg-core.wasm URL to ffmpeg-core.worker.js
96+
* when it imports ffmpeg-core.js in #3.
97+
*
98+
* To fix this issue, a hack here is leveraging mainScriptUrlOrBlob variable by
99+
* adding wasmURL and workerURL in base64 format as query string. ex:
100+
*
101+
* http://example.com/ffmpeg-core.js#{btoa(JSON.stringify({"wasmURL": "...", "workerURL": "..."}))}
102+
*
103+
* Thus, we can successfully extract custom URLs using _locateFile funciton.
104+
*/
105+
function _locateFile(path, prefix) {
106+
const mainScriptUrlOrBlob = Module["mainScriptUrlOrBlob"];
107+
if (mainScriptUrlOrBlob) {
108+
const { wasmURL, workerURL } = JSON.parse(
109+
atob(mainScriptUrlOrBlob.slice(mainScriptUrlOrBlob.lastIndexOf("#") + 1))
110+
);
111+
if (path.endsWith(".wasm")) return wasmURL;
112+
if (path.endsWith(".worker.js")) return workerURL;
113+
}
114+
return prefix + path;
115+
}
116+
86117
Module["stringToPtr"] = stringToPtr;
87118
Module["stringsToPtr"] = stringsToPtr;
88119
Module["print"] = print;
89120
Module["printErr"] = printErr;
121+
Module["locateFile"] = _locateFile;
90122

91123
Module["exec"] = exec;
92124
Module["setLogger"] = setLogger;

0 commit comments

Comments
 (0)