I'm trying to get a WASM version of sqlite running in cloudflare workers (in memory).
Structure, a hybrid app deployed to cloudflare pages. Builds an in memory sqlite database and serves its contents via an API.
Side Note: This is probably somewhat foolish, but I've gone down the rabbit hole
Using this wasm vita plugin code seems promising. It should allow importing wasm in cloudflare, however I'm running into a blow up with the cloudflare build
Note I've forked sql.js because the default emscripten builds blow up in cloudflare workers when trying to read various globals that don't actually exist. Adding an emscripten build argument of -s ENVIRONMENT='web' fixes this. This is tested and working with a vanilla cloudflare worker, but I'm unable to get vite to bundle it correctly
✘ [ERROR] Could not resolve "./../../_astro/sql-wasm.18fc45ef.wasm"
../../../dist/sites/acme/anvils/$server_build/chunks/pages/db_37f9fabe.mjs:1:7:
1 │ import './../../_astro/sql-wasm.18fc45ef.wasm';
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error Could not resolve "./../../_astro/sql-wasm.18fc45ef.wasm"
File:
../../../dist/sites/acme/anvils/$server_build/chunks/pages/db_37f9fabe.mjs:1:7
It seems like something is going wrong in the cloudflare adapter so that the path is not handling this temporary $server_build directory, or perhaps not moving the wasm file into the $server_build directory? I'm not seeing anything obvious looking at the @astro/cloudflare adapter code
- Any immediate ideas anyone has on what's going wrong in the cloudflare adapter?
- Any better ideas for running sqlite in the cloudflare worker? (Avoiding D1 since its alpha. Database will be pretty small)
Vite plugin to to import wasm files to cloudflare workers - GitHub - hadeeb/vite-plugin-wasm: Vite plugin to to import wasm files to cloudflare workers
sql.js is an SQL library for javascript containing a version of SQLite compiled for the web.