simolus3 / sqlite3.dart

sqlite3 bindings for Dart
MIT License
200 stars 70 forks source link

Web: DriftRemoteException WebAssembly.instantiateStreaming is not a function #187

Closed gokhanmeteerturk closed 7 months ago

gokhanmeteerturk commented 8 months ago

I'm having an error on mobile devices when browser is 'Chrome Mobile iOS 92', 'Mobile Safari 13.1', 'Samsung Internet 22' etc:

DriftRemoteException
NoSuchMethodError: method not found: 'instantiateStreaming'
(self.WebAssembly.instantiateStreaming is not a function.
(In 'self.WebAssembly.instantiateStreaming(a,o)',
'self.WebAssembly.instantiateStreaming' is undefined)
)

I know that JavaScript built-in: WebAssembly: instantiateStreaming is not supported by those browsers: https://caniuse.com/?search=instantiatestreaming

But I'm not sure why my drift setup uses instantiateStreaming or if this is expected for those browsers.

When checked with https://drift.simonbinder.eu/web/ it says:

WasmStorageImplementation.unsafeIndexedDb

I'm using:

   final db = await WasmDatabase.open(
      databaseName: 'todo-app',
      sqlite3Uri: Uri.parse('sqlite3.wasm'),
      driftWorkerUri: Uri.parse('drift_worker.js'),
    );

Headers:

  Cross-Origin-Opener-Policy: same-origin
  Cross-Origin-Embedder-Policy: require-corp

drift versions: tried with 2.11 and 2.12.2

Using WasmStorageImplementation.unsafeIndexedDb due to unsupported browser features: {MissingBrowserFeature.sharedWorkers, MissingBrowserFeature.fileSystemAccess, MissingBrowserFeature.sharedArrayBuffers}

simolus3 commented 8 months ago

Thanks for the report! The sqlite3 Dart package, which drift uses internally to open the database, uses WebAssembly.instantiateStreaming to download the sqlite3 engine.

I have added a fallback, but I don't have access to older browsers to try it out. Could you replace your drift worker with https://storage.googleapis.com/simon-public-euw3/assets/drift_worker.js and see if that fixes the issue on those browsers?

gokhanmeteerturk commented 8 months ago

Thank you for the fallback. Unfortunately there seems to be a problem with it. Now I have this error message from DriftRemoteException:

Error: WebAssembly.Module doesn't parse at byte 241: invalid opcode 192, in function at index 85

(I'm using browserstack to test it right now. Apparently it has free plan for open source projects)

simolus3 commented 7 months ago

WebAssembly.Module doesn't parse at byte 241: invalid opcode 192

This is because llvm uses sign-extension features by default, which are not available in the WebAssembly MVP version supported by these old browsers. I've tried transforming these with binaryen, but then these browsers complain about imported functions using 64-bit ints, something we need to implement custom file systems. The binaryen pass supposed to lower those crashed for me, so it seems like there's nothing I could do about it.

WebAssembly is a new web technology and we support recent versions of major browsers. That already took a lot of work, expanding this to support even older versions (which sometimes have buggy implementations as well) seems infeasible to me.