Skip to content

Commit

Permalink
Web: show WASM/SharedArrayBuffer status on debug console
Browse files Browse the repository at this point in the history
  • Loading branch information
espresso3389 committed Feb 13, 2025
1 parent 6784197 commit 7784068
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"COLORSPACE",
"COLORTYPE",
"COMBOBOX",
"credentialless",
"Cupertino",
"dartify",
"Dests",
Expand Down
31 changes: 31 additions & 0 deletions lib/src/web/pdf.js.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<void> ensurePdfjsInitialized() async {
if (_pdfjsInitialized) return;
await _dummyJsSyncContext.synchronized(() async {
Expand All @@ -295,6 +312,20 @@ Future<void> 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 =
Expand Down

0 comments on commit 7784068

Please sign in to comment.