fix: embed sqlite3.wasm in lib for production builds
This commit is contained in:
4
packages/worker-relay/src/custom.d.ts
vendored
Normal file
4
packages/worker-relay/src/custom.d.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
declare module "*.wasm" {
|
||||
const value: string;
|
||||
export default value;
|
||||
}
|
@ -3,7 +3,6 @@ import { v4 as uuid } from "uuid";
|
||||
|
||||
export class WorkerRelayInterface {
|
||||
#worker: Worker;
|
||||
#sqliteDir?: string;
|
||||
#commandQueue: Map<string, (v: unknown, ports: ReadonlyArray<MessagePort>) => void> = new Map();
|
||||
|
||||
// Command timeout
|
||||
@ -12,10 +11,8 @@ export class WorkerRelayInterface {
|
||||
/**
|
||||
* Interface wrapper for worker relay
|
||||
* @param scriptPath Path to worker script or Worker script object
|
||||
* @param sqlite3Dir Directory to search for sqlite3 depends
|
||||
*/
|
||||
constructor(scriptPath?: string | URL | Worker, sqlite3Dir?: string) {
|
||||
this.#sqliteDir = sqlite3Dir;
|
||||
constructor(scriptPath?: string | URL | Worker) {
|
||||
if (scriptPath instanceof Worker) {
|
||||
this.#worker = scriptPath;
|
||||
} else {
|
||||
@ -39,7 +36,7 @@ export class WorkerRelayInterface {
|
||||
}
|
||||
|
||||
async init(databasePath: string) {
|
||||
return await this.#workerRpc<Array<string>, boolean>("init", [databasePath, this.#sqliteDir ?? ""]);
|
||||
return await this.#workerRpc<Array<string | undefined>, boolean>("init", [databasePath]);
|
||||
}
|
||||
|
||||
async event(ev: NostrEvent) {
|
||||
|
@ -4,6 +4,9 @@ import { EventMetadata, NostrEvent, RelayHandler, RelayHandlerEvents, ReqFilter,
|
||||
import migrate from "./migrations";
|
||||
import { debugLog } from "./debug";
|
||||
|
||||
// import wasm file directly, this needs to be copied from https://sqlite.org/download.html
|
||||
import SqlitePath from "./sqlite3.wasm";
|
||||
|
||||
export class SqliteRelay extends EventEmitter<RelayHandlerEvents> implements RelayHandler {
|
||||
#sqlite?: Sqlite3Static;
|
||||
#log = (msg: string, ...args: Array<any>) => debugLog("SqliteRelay", msg, ...args);
|
||||
@ -13,11 +16,14 @@ export class SqliteRelay extends EventEmitter<RelayHandlerEvents> implements Rel
|
||||
/**
|
||||
* Initialize the SQLite driver
|
||||
*/
|
||||
async init(path: string, sqliteWasmPath?: string) {
|
||||
async init(path: string) {
|
||||
if (this.#sqlite) return;
|
||||
this.#sqlite = await sqlite3InitModule({
|
||||
locateFile(path, prefix) {
|
||||
return new URL(`sqlite-wasm/jswasm/${path}`, sqliteWasmPath).href;
|
||||
locateFile: (path, prefix) => {
|
||||
if (path === "sqlite3.wasm") {
|
||||
return SqlitePath;
|
||||
}
|
||||
return prefix + path;
|
||||
},
|
||||
print: msg => this.#log(msg),
|
||||
printErr: msg => this.#log(msg)
|
||||
|
BIN
packages/worker-relay/src/sqlite3.wasm
Normal file
BIN
packages/worker-relay/src/sqlite3.wasm
Normal file
Binary file not shown.
@ -80,9 +80,9 @@ globalThis.onmessage = async ev => {
|
||||
} else {
|
||||
relay = new InMemoryRelay();
|
||||
}
|
||||
const [dbPath, wasmPath] = msg.args as Array<string>;
|
||||
debugLog("StartInit", dbPath, wasmPath);
|
||||
await relay.init(dbPath, wasmPath);
|
||||
const [dbPath] = msg.args as Array<string>;
|
||||
debugLog("StartInit", dbPath);
|
||||
await relay.init(dbPath);
|
||||
reply(msg.id, true);
|
||||
});
|
||||
break;
|
||||
|
Reference in New Issue
Block a user