LCOV - code coverage report

Current view
top level - src/_impl/web - _entry_point_uri.dart
Test
lcov.info
Date
2024-11-13
Legend
Lines
hit
not hit
Branches
taken
not taken
# not executed
HitTotalCoverage
Lines161984.2%
Functions00-
Branches00-
Each row represents a line of source code
LineBranchHitsSource code
1import 'dart:js_interop';
2
3import 'package:using/using.dart';
4import 'package:web/web.dart' as web;
5
6import '../../exceptions/squadron_error.dart';
7
8class EntryPointUri with Releasable {
911 EntryPointUri._(this.uri, {required bool revoke}) : _revoke = revoke;
10
11 final String uri;
12 final bool _revoke;
13
14 @override
151 void release() {
1611 if (_revoke) {
1711 web.URL.revokeObjectURL(uri);
18 }
1911 super.release();
201 }
21
2211 factory EntryPointUri.from(Uri workerEntrypoint) {
23 final fileName =
2411 workerEntrypoint.pathSegments.lastOrNull?.toString().toLowerCase() ??
250 '';
26
2711 if (fileName.endsWith('.js') || fileName.endsWith('.mjs')) {
28 // a JavaScript worker
2911 return EntryPointUri._(workerEntrypoint.toString(), revoke: false);
3011 } else if (fileName.endsWith('.wasm')) {
31 // blob containing the JavaScript code to load and invoke the Web Assembly worker
32 final blob = web.Blob(
3311 [wasmLoaderScript(workerEntrypoint.toString()).toJS].toJS,
34 web.BlobPropertyBag(type: 'application/javascript'),
35 );
3611 return EntryPointUri._(web.URL.createObjectURL(blob), revoke: true);
371 } else if (workerEntrypoint.isScheme('data') ||
380 workerEntrypoint.isScheme('javascript')) {
39 // something else, eg. inline JavaScript
401 return EntryPointUri._(workerEntrypoint.toString(), revoke: false);
41 } else {
420 throw SquadronErrorExt.create('Invalid entry point URI');
43 }
4411 }
45
46 static String wasmLoaderScript(String url) => '''(async function() {
47 const workerUri = new URL("${url.replaceAll('"', '\\"')}", self.location.origin).href;
48 try {
49 let dart2wasm_runtime; let moduleInstance;
50 const runtimeUri = workerUri.replaceAll('.unopt', '').replaceAll('.wasm', '.mjs');
51 try {
52 const dartModule = WebAssembly.compileStreaming(fetch(workerUri));
53 dart2wasm_runtime = await import(runtimeUri);
54 moduleInstance = await dart2wasm_runtime.instantiate(dartModule, {});
55 } catch (exception) {
56 console.error(`Failed to fetch and instantiate wasm module \${workerUri}: \${exception}`);
57 console.error('See https://dart.dev/web/wasm for more information.');
58 throw new Error(exception.message ?? 'Unknown error when instantiating worker module');
59 }
60 try {
61 await dart2wasm_runtime.invoke(moduleInstance);
62 console.log(`Succesfully loaded and invoked \${workerUri}`);
63 } catch (exception) {
64 console.error(`Exception while invoking wasm module \${workerUri}: \${exception}`);
65 throw new Error(exception.message ?? 'Unknown error when invoking worker module');
66 }
67 } catch (ex) {
68 const ts = (Date.now() - Date.UTC(2020, 1, 2)) * 1000;
69 postMessage([ts, null, ["\$sqdrn", `Failed to load Web Worker from \${workerUri}: \${ex}`, null], null, null]);
70 }
71})()''';
72}
Choose Features