diff --git a/bindings/wasm/html/index.html b/bindings/wasm/html/index.html new file mode 100644 index 000000000..0097d61f8 --- /dev/null +++ b/bindings/wasm/html/index.html @@ -0,0 +1,9 @@ + + +
+ + + diff --git a/bindings/wasm/limbo-opfs-test.html b/bindings/wasm/html/limbo-opfs-test.html similarity index 100% rename from bindings/wasm/limbo-opfs-test.html rename to bindings/wasm/html/limbo-opfs-test.html diff --git a/bindings/wasm/limbo-test.html b/bindings/wasm/html/limbo-test.html similarity index 100% rename from bindings/wasm/limbo-test.html rename to bindings/wasm/html/limbo-test.html diff --git a/bindings/wasm/index.html b/bindings/wasm/index.html deleted file mode 100644 index f3246b19e..000000000 --- a/bindings/wasm/index.html +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/bindings/wasm/lib.rs b/bindings/wasm/lib.rs index 949df3516..c01b9e986 100644 --- a/bindings/wasm/lib.rs +++ b/bindings/wasm/lib.rs @@ -48,7 +48,6 @@ impl Database { #[wasm_bindgen] pub fn exec(&self, _sql: &str) { let _res = self.conn.execute(_sql).unwrap(); - // Statement::new(RefCell::new(stmt), false) } #[wasm_bindgen] diff --git a/bindings/wasm/scripts/build b/bindings/wasm/scripts/build index e39ff6ae7..09691006c 100755 --- a/bindings/wasm/scripts/build +++ b/bindings/wasm/scripts/build @@ -1,5 +1,6 @@ #!/bin/bash +# get target as argument from cli, defaults to nodejs if no argument is supplied TARGET=${1:-nodejs} FEATURE="nodejs" diff --git a/bindings/wasm/src/limbo-worker.js b/bindings/wasm/src/limbo-worker.js index f91cf6115..d8598303f 100644 --- a/bindings/wasm/src/limbo-worker.js +++ b/bindings/wasm/src/limbo-worker.js @@ -28,7 +28,7 @@ initAll().then(() => { break; } case "exec": { - console.log(e.data.sql); + log(e.data.sql); db.exec(e.data.sql); self.postMessage({ type: "success", op: "exec" }); break; @@ -53,3 +53,22 @@ initAll().then(() => { self.postMessage({ type: "error", error: error.toString() }); }); +// logLevel: +// +// 0 = no logging output +// 1 = only errors +// 2 = warnings and errors +// 3 = debug, warnings, and errors +const logLevel = 1; + +const loggers = { + 0: console.error.bind(console), + 1: console.warn.bind(console), + 2: console.log.bind(console), +}; +const logImpl = (level, ...args) => { + if (logLevel > level) loggers[level]("OPFS asyncer:", ...args); +}; +const log = (...args) => logImpl(2, ...args); +const warn = (...args) => logImpl(1, ...args); +const error = (...args) => logImpl(0, ...args); diff --git a/bindings/wasm/src/opfs-sync-proxy.js b/bindings/wasm/src/opfs-sync-proxy.js index 2bc1a9105..6bac0a759 100644 --- a/bindings/wasm/src/opfs-sync-proxy.js +++ b/bindings/wasm/src/opfs-sync-proxy.js @@ -8,9 +8,9 @@ let nextFd = 1; self.postMessage("ready"); onmessage = async (e) => { - console.log("handle message: ", e.data); + log("handle message: ", e.data); if (e.data.cmd === "init") { - console.log("init"); + log("init"); transferBuffer = e.data.transferBuffer; statusBuffer = e.data.statusBuffer; @@ -33,7 +33,7 @@ self.onerror = (error) => { }; function handleCommand(msg) { - console.log(`handle message: ${msg.cmd}`); + log(`handle message: ${msg.cmd}`); switch (msg.cmd) { case "open": return handleOpen(msg.path); @@ -74,10 +74,10 @@ function handleRead(fd, offset, size) { const handle = handles.get(fd); const readBuffer = new ArrayBuffer(size); const readSize = handle.read(readBuffer, { at: offset }); - console.log("opfssync read: size: ", readBuffer.byteLength); + log("opfssync read: size: ", readBuffer.byteLength); const tmp = new Uint8Array(readBuffer); - console.log("opfssync read buffer: ", [...tmp]); + log("opfssync read buffer: ", [...tmp]); transferArray.set(tmp); @@ -85,8 +85,8 @@ function handleRead(fd, offset, size) { } function handleWrite(fd, buffer, offset) { - console.log("opfssync buffer size:", buffer.byteLength); - console.log("opfssync write buffer: ", [...buffer]); + log("opfssync buffer size:", buffer.byteLength); + log("opfssync write buffer: ", [...buffer]); const handle = handles.get(fd); const size = handle.write(buffer, { at: offset }); return { success: true, length: size }; @@ -107,10 +107,30 @@ function sendResult(result) { if (result?.fd) { statusView.setInt32(4, result.fd, true); } else { - console.log("opfs-sync-proxy: result.length: ", result.length); + log("opfs-sync-proxy: result.length: ", result.length); statusView.setInt32(4, result?.length || 0, true); } Atomics.store(statusArray, 0, 1); Atomics.notify(statusArray, 0); } + +// logLevel: +// +// 0 = no logging output +// 1 = only errors +// 2 = warnings and errors +// 3 = debug, warnings, and errors +const logLevel = 1; + +const loggers = { + 0: console.error.bind(console), + 1: console.warn.bind(console), + 2: console.log.bind(console), +}; +const logImpl = (level, ...args) => { + if (logLevel > level) loggers[level]("OPFS asyncer:", ...args); +}; +const log = (...args) => logImpl(2, ...args); +const warn = (...args) => logImpl(1, ...args); +const error = (...args) => logImpl(0, ...args); diff --git a/bindings/wasm/src/opfs-worker.js b/bindings/wasm/src/opfs-worker.js index dd58d3899..b9a1c0ee3 100644 --- a/bindings/wasm/src/opfs-worker.js +++ b/bindings/wasm/src/opfs-worker.js @@ -55,47 +55,3 @@ onmessage = async function (e) { }; console.log("opfs-worker.js"); -// checkCompatibility(); - -// // In VFS class -// this.worker.onerror = (error) => { -// console.error("Worker stack:", error.error?.stack || error.message); -// }; - -// checkCompatibility(); -// -// async function checkCompatibility() { -// console.log("begin check compatibility"); -// // OPFS API check -// if (!("storage" in navigator && "getDirectory" in navigator.storage)) { -// throw new Error("OPFS API not supported"); -// } -// -// // SharedArrayBuffer support check -// if (typeof SharedArrayBuffer === "undefined") { -// throw new Error("SharedArrayBuffer not supported"); -// } -// -// // Atomics API check -// if (typeof Atomics === "undefined") { -// throw new Error("Atomics API not supported"); -// } -// -// // Permission check for OPFS -// try { -// const root = await navigator.storage.getDirectory(); -// await root.getFileHandle("test.txt", { create: true }); -// } catch (e) { -// console.log(e); -// console.log("throwing OPFS permission Denied"); -// throw new Error("OPFS permission denied"); -// } -// -// // Cross-Origin-Isolation check for SharedArrayBuffer -// if (!crossOriginIsolated) { -// throw new Error("Cross-Origin-Isolation required for SharedArrayBuffer"); -// } -// -// console.log("done check compatibility"); -// return true; -// } diff --git a/bindings/wasm/src/opfs.js b/bindings/wasm/src/opfs.js index 104cf3cfc..01c6d2175 100644 --- a/bindings/wasm/src/opfs.js +++ b/bindings/wasm/src/opfs.js @@ -32,7 +32,7 @@ class VFS { initWorker() { return new Promise((resolve) => { this.worker.addEventListener("message", (e) => { - console.log("eventListener: ", e.data); + log("eventListener: ", e.data); resolve(); }, { once: true }); @@ -50,8 +50,8 @@ class VFS { Atomics.wait(this.statusArray, 0, 0); const result = this.statusView.getInt32(4, true); - console.log("opfs.js open result: ", result); - console.log("opfs.js open result type: ", typeof result); + log("opfs.js open result: ", result); + log("opfs.js open result type: ", typeof result); return result; } @@ -86,7 +86,7 @@ class VFS { new Uint8Array(this.transferBuffer, 0, readSize), bytesRead, ); - console.log("opfs pread buffer: ", [...buffer]); + log("opfs pread buffer: ", [...buffer]); bytesRead += readSize; if (readSize < chunkSize) break; @@ -96,7 +96,7 @@ class VFS { } pwrite(fd, buffer, offset) { - console.log("write buffer size: ", buffer.byteLength); + log("write buffer size: ", buffer.byteLength); Atomics.store(this.statusArray, 0, 0); this.worker.postMessage({ cmd: "write", @@ -106,7 +106,7 @@ class VFS { }); Atomics.wait(this.statusArray, 0, 0); - console.log( + log( "opfs pwrite length statusview: ", this.statusView.getInt32(4, true), ); @@ -119,8 +119,8 @@ class VFS { Atomics.wait(this.statusArray, 0, 0); const result = this.statusView.getInt32(4, true); - console.log("opfs.js size result: ", result); - console.log("opfs.js size result type: ", typeof result); + log("opfs.js size result: ", result); + log("opfs.js size result type: ", typeof result); return BigInt(result); } @@ -131,4 +131,24 @@ class VFS { } } +// logLevel: +// +// 0 = no logging output +// 1 = only errors +// 2 = warnings and errors +// 3 = debug, warnings, and errors +const logLevel = 1; + +const loggers = { + 0: console.error.bind(console), + 1: console.warn.bind(console), + 2: console.log.bind(console), +}; +const logImpl = (level, ...args) => { + if (logLevel > level) loggers[level]("OPFS asyncer:", ...args); +}; +const log = (...args) => logImpl(2, ...args); +const warn = (...args) => logImpl(1, ...args); +const error = (...args) => logImpl(0, ...args); + export { VFS }; diff --git a/bindings/wasm/src/web-vfs.js b/bindings/wasm/src/web-vfs.js index 4ba0cce4a..a24bc75d5 100644 --- a/bindings/wasm/src/web-vfs.js +++ b/bindings/wasm/src/web-vfs.js @@ -4,10 +4,7 @@ export class VFS { } open(path, flags) { - const result = self.vfs.open(path); - consol.log("webvfs open result: ", result); - consol.log("webvfs open result type: ", typeof result); - return result; + return self.vfs.open(path); } close(fd) { diff --git a/bindings/wasm/test/setup.js b/bindings/wasm/test/setup.js index 3ac2a64fc..9e22bb660 100644 --- a/bindings/wasm/test/setup.js +++ b/bindings/wasm/test/setup.js @@ -1,4 +1,4 @@ -import { afterEach, beforeEach } from "vitest"; +import { afterAll, afterEach, beforeAll, beforeEach } from "vitest"; import { chromium } from "playwright"; import { createServer } from "vite"; @@ -7,8 +7,7 @@ let context; let page; let server; -beforeEach(async () => { - // Start Vite dev server +beforeAll(async () => { server = await createServer({ configFile: "./vite.config.js", root: ".", @@ -17,17 +16,20 @@ beforeEach(async () => { }, }); await server.listen(); - browser = await chromium.launch(); +}); + +beforeEach(async () => { context = await browser.newContext(); page = await context.newPage(); - globalThis.__page__ = page; }); afterEach(async () => { await context.close(); +}); + +afterAll(async () => { await browser.close(); await server.close(); }); - diff --git a/bindings/wasm/test/test.html b/bindings/wasm/test/test.html deleted file mode 100644 index ad2534032..000000000 --- a/bindings/wasm/test/test.html +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - diff --git a/bindings/wasm/vite.config.js b/bindings/wasm/vite.config.js index 0e84f547f..e92b7f354 100644 --- a/bindings/wasm/vite.config.js +++ b/bindings/wasm/vite.config.js @@ -2,6 +2,8 @@ import { defineConfig } from "vite"; import wasm from "vite-plugin-wasm"; export default defineConfig({ + publicDir: "./html", + root: ".", plugins: [wasm()], test: { globals: true, @@ -28,7 +30,4 @@ export default defineConfig({ }, }, }, - // worker: { - // format: "es", - // }, }); diff --git a/core/lib.rs b/core/lib.rs index e79a1bf98..5ef48e742 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -35,7 +35,6 @@ use storage::sqlite3_ondisk::{DatabaseHeader, DATABASE_HEADER_SIZE}; pub use storage::wal::WalFile; pub use storage::wal::WalFileShared; use util::parse_schema_rows; -// use web_sys::console; // Add to dependencies in Cargo.toml use translate::select::prepare_select_plan; use types::OwnedValue; @@ -81,8 +80,6 @@ impl Database { pub fn open_file(io: Arc