From 7784068e01ac2e9136eb54333f26edb66d48a400 Mon Sep 17 00:00:00 2001 From: Takashi Kawasaki Date: Thu, 13 Feb 2025 14:21:31 +0900 Subject: [PATCH] Web: show WASM/SharedArrayBuffer status on debug console --- .vscode/settings.json | 1 + lib/src/web/pdf.js.dart | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9b70043..bd8fec3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -26,6 +26,7 @@ "COLORSPACE", "COLORTYPE", "COMBOBOX", + "credentialless", "Cupertino", "dartify", "Dests", diff --git a/lib/src/web/pdf.js.dart b/lib/src/web/pdf.js.dart index d0f15ef..976e7d2 100644 --- a/lib/src/web/pdf.js.dart +++ b/lib/src/web/pdf.js.dart @@ -4,8 +4,10 @@ library; import 'dart:js_interop'; +import 'dart:js_interop_unsafe'; import 'dart:typed_data'; +import 'package:flutter/material.dart'; import 'package:synchronized/extension.dart'; import 'package:web/web.dart' as web; @@ -286,6 +288,21 @@ final _dummyJsSyncContext = {}; bool _pdfjsInitialized = false; +/// Whether SharedArrayBuffer is supported. +/// +/// It actually means whether Flutter Web can take advantage of multiple threads or not. +/// +/// See [Support for WebAssembly (Wasm) - Serve the built output with an HTTP server](https://docs.flutter.dev/platform-integration/web/wasm#serve-the-built-output-with-an-http-server) +bool _determineWhetherSharedArrayBufferSupportedOrNot() { + try { + return web.window.hasProperty('SharedArrayBuffer'.toJS).toDart; + } catch (e) { + return false; + } +} + +final bool _isSharedArrayBufferSupported = _determineWhetherSharedArrayBufferSupportedOrNot(); + Future ensurePdfjsInitialized() async { if (_pdfjsInitialized) return; await _dummyJsSyncContext.synchronized(() async { @@ -295,6 +312,20 @@ Future ensurePdfjsInitialized() async { return; } + const isRunningWithWasm = bool.fromEnvironment('dart.tool.dart2wasm'); + debugPrint( + 'pdfrx Web status:\n' + '- Running WASM: $isRunningWithWasm\n' + '- SharedArrayBuffer: $_isSharedArrayBufferSupported', + ); + if (isRunningWithWasm && !_isSharedArrayBufferSupported) { + debugPrint( + 'WARNING: SharedArrayBuffer is not enabled and WASM is running in single thread mode. Enable SharedArrayBuffer by setting the following HTTP header on your server:\n' + ' Cross-Origin-Embedder-Policy: require-corp|credentialless\n' + ' Cross-Origin-Opener-Policy: same-origin\n', + ); + } + final pdfJsSrc = PdfJsConfiguration.configuration?.pdfJsSrc ?? _pdfjsUrl; try { final script =