From c1cadd31d131d9964ba83d60b73f924393b30f9e Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Sat, 6 Apr 2024 13:32:38 +0200 Subject: [PATCH] Test build for Emscripten PR 21701 --- CHANGELOG.md | 5 +++++ Dockerfile | 2 +- README.md | 7 +++---- build.sh | 2 -- package.json | 4 ++-- src/CMakeLists.txt | 2 +- src/locatefile-cors-pre.js | 16 ---------------- src/workaround-cors-pre.js | 7 +++++++ 8 files changed, 19 insertions(+), 26 deletions(-) delete mode 100644 src/locatefile-cors-pre.js create mode 100644 src/workaround-cors-pre.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 04c2bf62b..06196793d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Uses libvips v8.15.2, compiled with Emscripten v3.1.57. +### Changed + +- Inline worker scripts (`*.worker.js`) into the main output file. + [emscripten-core/emscripten#21701](https://github.com/emscripten-core/emscripten/pull/21701) + ## [v0.0.8] - 2024-03-17 Uses libvips v8.15.2, compiled with Emscripten v3.1.56. diff --git a/Dockerfile b/Dockerfile index f382f56f6..debd83e5b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ RUN \ # Emscripten patches RUN \ - curl -Ls https://github.com/emscripten-core/emscripten/compare/3.1.57...kleisauke:wasm-vips-3.1.57.patch | patch -p1 -d $EMSDK/upstream/emscripten && \ + curl -Ls https://github.com/emscripten-core/emscripten/compare/3.1.57...kleisauke:wasm-vips-3.1.57-pthread_inline.patch | patch -p1 -d $EMSDK/upstream/emscripten && \ emcc --clear-cache && embuilder build sysroot --force # Rust diff --git a/README.md b/README.md index d7b2d1fa0..4de134900 100644 --- a/README.md +++ b/README.md @@ -48,14 +48,13 @@ yarn add wasm-vips ### Browser -Requires `vips.js`, `vips.wasm` and `vips.worker.js` to be served from -the same directory. Additionally, when using ES6 modules, it's necessary -to also serve the `vips-es6.js` and `vips-es6.worker.js` files. +Requires `vips.js` (or `vips-es6.js`) and `vips.wasm` to be served from +the same directory. Since wasm-vips requires [the `SharedArrayBuffer` API]( https://caniuse.com/sharedarraybuffer), the website needs to opt-in to a cross-origin isolated state, by serving the following HTTP headers on -both the main document and worker script (`*.worker.js`): +both the main document and `vips*.js` script: ```http Cross-Origin-Embedder-Policy: require-corp diff --git a/build.sh b/build.sh index a004a9bc6..bd58dc2a8 100755 --- a/build.sh +++ b/build.sh @@ -529,9 +529,7 @@ node --version # Omit -es6 suffix from Node.js files mv $SOURCE_DIR/lib/vips-node-es6.mjs $SOURCE_DIR/lib/vips-node.mjs - mv $SOURCE_DIR/lib/vips-node-es6.worker.mjs $SOURCE_DIR/lib/vips-node.worker.mjs sed -i 's/vips-node-es6/vips-node/g' $SOURCE_DIR/lib/vips-node.mjs - sed -i 's/vips-node-es6/vips-node/g' $SOURCE_DIR/lib/vips-node.worker.mjs # Print the target features section echo -n "Used Wasm features: " diff --git a/package.json b/package.json index 3f0bfc87b..a23426fd5 100644 --- a/package.json +++ b/package.json @@ -65,8 +65,8 @@ "ignore": [ "src/closure-externs/wasm-vips.js", "src/fixed-threadpool-web.js", - "src/locatefile-cors-pre.js", - "src/modules-pre.js" + "src/modules-pre.js", + "src/workaround-cors-pre.js" ], "env": [ "browser", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 31a1e8e82..930a7c875 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -166,7 +166,7 @@ endif() if ("web" IN_LIST ENVIRONMENT) set(WEB_CORS_WORKAROUND - --pre-js=${CMAKE_CURRENT_SOURCE_DIR}/locatefile-cors-pre.js + --pre-js=${CMAKE_CURRENT_SOURCE_DIR}/workaround-cors-pre.js ) set(WEB_FIXED_THREADPOOL --js-library=${CMAKE_CURRENT_SOURCE_DIR}/fixed-threadpool-web.js diff --git a/src/locatefile-cors-pre.js b/src/locatefile-cors-pre.js deleted file mode 100644 index 0affb705e..000000000 --- a/src/locatefile-cors-pre.js +++ /dev/null @@ -1,16 +0,0 @@ -// https://stackoverflow.com/q/25458104 -if (Module['workaroundCors']) { - var workerObjectURL; - - Module['locateFile'] = Module['locateFile'] || function (fileName, scriptDirectory) { - var url = scriptDirectory + fileName; - if (url.endsWith('.worker.js')) { - if (workerObjectURL) return workerObjectURL; - workerObjectURL = URL.createObjectURL(new Blob( - [`importScripts('${url}');`], - {'type': 'application/javascript'})); - return workerObjectURL; - } - return url; - } -} diff --git a/src/workaround-cors-pre.js b/src/workaround-cors-pre.js new file mode 100644 index 000000000..5fbf55af0 --- /dev/null +++ b/src/workaround-cors-pre.js @@ -0,0 +1,7 @@ +// https://stackoverflow.com/q/25458104 +if (Module['workaroundCors']) { + Module['mainScriptUrlOrBlob'] = Module['mainScriptUrlOrBlob'] || + URL.createObjectURL(new Blob( + [`importScripts('${_scriptName}');`], + {'type': 'application/javascript'})); +}