Skip to content

Commit

Permalink
Ensure browser supports workers-in-workers (GoogleChromeLabs#1325)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald authored Jan 9, 2023
1 parent 13a185d commit 5831176
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 16 deletions.
3 changes: 2 additions & 1 deletion client-tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// for comlink
"src/features/**/worker/**/*",
"src/features-worker/**/*",
"src/features/worker-utils/**/*"
"src/features/worker-utils/**/*",
"src/worker-shared/**/*"
]
}
3 changes: 2 additions & 1 deletion generic-tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"static-build/*": ["src/static-build/*"],
"client/*": ["src/client/*"],
"shared/*": ["src/shared/*"],
"features/*": ["src/features/*"]
"features/*": ["src/features/*"],
"worker-shared/*": ["src/worker-shared/*"]
}
}
}
1 change: 1 addition & 0 deletions package-lock.json

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

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,12 @@
"rollup-plugin-terser": "^7.0.2",
"serve": "^11.3.2",
"typescript": "^4.4.4",
"which": "^2.0.2"
"which": "^2.0.2",
"wasm-feature-detect": "^1.2.11"
},
"lint-staged": {
"*.{js,css,json,md,ts,tsx}": "prettier --write",
"*.{c,h,cpp,hpp}": "clang-format -i",
"*.rs": "rustfmt"
},
"dependencies": {
"wasm-feature-detect": "^1.2.11"
}
}
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default async function ({ watch }) {
'src/features-worker',
'src/features-worker-worker-bridge',
'src/sw',
'src/worker-shared',
'codecs',
]),
urlPlugin(),
Expand Down
4 changes: 2 additions & 2 deletions src/features/encoders/avif/worker/avifEncode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import type { AVIFModule } from 'codecs/avif/enc/avif_enc';
import type { EncodeOptions } from '../shared/meta';
import { initEmscriptenModule } from 'features/worker-utils';
import { threads } from 'wasm-feature-detect';
import checkThreadsSupport from 'worker-shared/supports-wasm-threads';

let emscriptenModule: Promise<AVIFModule>;

async function init() {
if (await threads()) {
if (await checkThreadsSupport()) {
const avifEncoder = await import('codecs/avif/enc/avif_enc_mt');
return initEmscriptenModule<AVIFModule>(avifEncoder.default);
}
Expand Down
5 changes: 3 additions & 2 deletions src/features/encoders/jxl/worker/jxlEncode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import type { JXLModule } from 'codecs/jxl/enc/jxl_enc';
import type { EncodeOptions } from '../shared/meta';

import { initEmscriptenModule } from 'features/worker-utils';
import { threads, simd } from 'wasm-feature-detect';
import { simd } from 'wasm-feature-detect';
import checkThreadsSupport from 'worker-shared/supports-wasm-threads';

let emscriptenModule: Promise<JXLModule>;

async function init() {
if (await threads()) {
if (await checkThreadsSupport()) {
if (await simd()) {
const jxlEncoder = await import('codecs/jxl/enc/jxl_enc_mt_simd');
return initEmscriptenModule(jxlEncoder.default);
Expand Down
4 changes: 2 additions & 2 deletions src/features/encoders/oxiPNG/worker/oxipngEncode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* limitations under the License.
*/
import { EncodeOptions } from '../shared/meta';
import { threads } from 'wasm-feature-detect';
import checkThreadsSupport from 'worker-shared/supports-wasm-threads';

async function initMT() {
const {
Expand Down Expand Up @@ -39,7 +39,7 @@ export default async function encode(
options: EncodeOptions,
): Promise<ArrayBuffer> {
if (!wasmReady) {
wasmReady = threads().then((hasThreads: boolean) =>
wasmReady = checkThreadsSupport().then((hasThreads: boolean) =>
hasThreads ? initMT() : initST(),
);
}
Expand Down
5 changes: 3 additions & 2 deletions src/features/encoders/wp2/worker/wp2Encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import type { WP2Module } from 'codecs/wp2/enc/wp2_enc';
import type { EncodeOptions } from '../shared/meta';

import { initEmscriptenModule } from 'features/worker-utils';
import { threads, simd } from 'wasm-feature-detect';
import { simd } from 'wasm-feature-detect';
import checkThreadsSupport from 'worker-shared/supports-wasm-threads';

let emscriptenModule: Promise<WP2Module>;

async function init() {
if (await threads()) {
if (await checkThreadsSupport()) {
if (await simd()) {
const wp2Encoder = await import('codecs/wp2/enc/wp2_enc_mt_simd');
return initEmscriptenModule(wp2Encoder.default);
Expand Down
5 changes: 3 additions & 2 deletions src/sw/to-cache.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { threads, simd } from 'wasm-feature-detect';
import { simd } from 'wasm-feature-detect';
import webpDataUrl from 'data-url:./tiny.webp';
import avifDataUrl from 'data-url:./tiny.avif';
import checkThreadsSupport from 'worker-shared/supports-wasm-threads';

// Give TypeScript the correct global.
declare var self: ServiceWorkerGlobalScope;
Expand Down Expand Up @@ -84,7 +85,7 @@ export const initial = ['/', ...initialJs];
export const theRest = (async () => {
const [supportsThreads, supportsSimd, supportsWebP, supportsAvif] =
await Promise.all([
threads(),
checkThreadsSupport(),
simd(),
...[webpDataUrl, avifDataUrl].map(async (dataUrl) => {
if (!self.createImageBitmap) return false;
Expand Down
17 changes: 17 additions & 0 deletions src/worker-shared/supports-wasm-threads.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { threads } from 'wasm-feature-detect';

export default async function checkThreadsSupport() {
const supportsWasmThreads = await threads();
if (!supportsWasmThreads) return false;

// Safari 16 shipped with WASM threads support, but it didn't ship with nested workers support.
// This meant Squoosh failed in Safari 16, since we call our wasm from inside a worker to begin with.

// Right now, this check is only run from a worker.
// More implementation is needed to run it from a page.
if (!('importScripts' in self)) {
throw Error('Not implemented');
}

return 'Worker' in self;
}
1 change: 1 addition & 0 deletions worker-tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"src/features/**/shared/**/*",
"src/features/worker-utils/**/*",
"src/features-worker/**/*",
"src/worker-shared/**/*",
"src/sw/**/*"
]
}

0 comments on commit 5831176

Please sign in to comment.