fix: embed sqlite3.wasm in lib for production builds
This commit is contained in:
parent
99b4d01ff7
commit
098251fee3
@ -11,30 +11,30 @@ Worker relay is a Nostr relay built on `sqlite-wasm`
|
||||
```typescript
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
|
||||
// in debug mode you may need this, to map to the correct sqlite-wasm path
|
||||
// this is needed because sqlite-wasm will otherwise look inside @snort/worker-relay directory for sqlite3.wasm
|
||||
const basePath = new URL("@sqlite.org/sqlite-wasm", import.meta.url);
|
||||
// when using Vite import the worker script directly (for production)
|
||||
import WorkerVite from "@snort/worker-relay/src/worker?worker"
|
||||
|
||||
// internally we resolve the script path like this:
|
||||
const scriptPath = new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url);
|
||||
// in dev mode import esm module, i have no idea why it has to work like this
|
||||
const workerScript = import.meta.env.DEV ?
|
||||
new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url) :
|
||||
new WorkerVite();
|
||||
|
||||
// scriptPath & basePath are optional
|
||||
const relay = new WorkerRelayInterface(scriptPath.href, basePath.href);
|
||||
const workerRelay = new WorkerRelayInterface(workerScript);
|
||||
|
||||
// load sqlite database and run migrations
|
||||
await relay.init("my-relay.db");
|
||||
await workerRelay.init("my-relay.db");
|
||||
|
||||
// Query worker relay with regular nostr REQ command
|
||||
const results = await relay.query(["REQ", "1", { kinds: [1], limit: 10 }]);
|
||||
const results = await workerRelay.query(["REQ", "1", { kinds: [1], limit: 10 }]);
|
||||
|
||||
// publish a new event to the relay
|
||||
const myEvent = {
|
||||
kind: 1,
|
||||
created_at: Math.floor(new Date().getTime() / 1000),
|
||||
content: "test",
|
||||
tags: []
|
||||
kind: 1,
|
||||
created_at: Math.floor(new Date().getTime() / 1000),
|
||||
content: "test",
|
||||
tags: []
|
||||
};
|
||||
if (await relay.event(myEvent)) {
|
||||
console.log("Success");
|
||||
if (await workerRelay.event(myEvent)) {
|
||||
console.log("Success");
|
||||
}
|
||||
```
|
||||
|
@ -1,28 +1,28 @@
|
||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||
|
||||
// in debug mode you may need this, to map to the correct sqlite-wasm path
|
||||
// this is needed because sqlite-wasm will otherwise look inside @snort/worker-relay directory for sqlite3.wasm
|
||||
const basePath = new URL("@sqlite.org/sqlite-wasm", import.meta.url);
|
||||
// when using Vite import the worker script directly (for production)
|
||||
import WorkerVite from "@snort/worker-relay/src/worker?worker"
|
||||
|
||||
// internally we resolve the script path like this:
|
||||
const scriptPath = new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url);
|
||||
// in dev mode import esm module, i have no idea why it has to work like this
|
||||
const workerScript = import.meta.env.DEV ?
|
||||
new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url) :
|
||||
new WorkerVite();
|
||||
|
||||
// scriptPath & basePath are optional
|
||||
const relay = new WorkerRelayInterface(scriptPath, basePath.href);
|
||||
const workerRelay = new WorkerRelayInterface(workerScript);
|
||||
|
||||
// load sqlite database and run migrations
|
||||
await relay.init("my-relay.db");
|
||||
await workerRelay.init("my-relay.db");
|
||||
|
||||
// Query worker relay with regular nostr REQ command
|
||||
const results = await relay.query(["REQ", "1", { kinds: [1], limit: 10 }]);
|
||||
const results = await workerRelay.query(["REQ", "1", { kinds: [1], limit: 10 }]);
|
||||
|
||||
// publish a new event to the relay
|
||||
const myEvent = {
|
||||
kind: 1,
|
||||
created_at: Math.floor(new Date().getTime() / 1000),
|
||||
content: "test",
|
||||
tags: []
|
||||
kind: 1,
|
||||
created_at: Math.floor(new Date().getTime() / 1000),
|
||||
content: "test",
|
||||
tags: []
|
||||
};
|
||||
if (await relay.event(myEvent)) {
|
||||
console.log("Success");
|
||||
if (await workerRelay.event(myEvent)) {
|
||||
console.log("Success");
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@snort/worker-relay",
|
||||
"version": "1.0.6",
|
||||
"version": "1.0.7",
|
||||
"description": "A nostr relay in a service worker",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
@ -10,19 +10,18 @@
|
||||
"author": "Kieran",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "rm -rf dist && tsc && esbuild src/worker.ts --bundle --minify --sourcemap --outdir=dist/esm --format=esm --out-extension:.js=.mjs"
|
||||
"build": "rm -rf dist && tsc && yarn build:esm",
|
||||
"build:esm": "esbuild src/worker.ts --bundle --minify --sourcemap --outdir=dist/esm --format=esm --out-extension:.js=.mjs --loader:.wasm=copy"
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
"dist"
|
||||
],
|
||||
"dependencies": {
|
||||
"@sqlite.org/sqlite-wasm": "^3.45.1-build1",
|
||||
"eventemitter3": "^5.0.1",
|
||||
"uuid": "^9.0.1"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@sqlite.org/sqlite-wasm": "^3.45.1-build1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/debug": "^4.1.12",
|
||||
"@types/uuid": "^9.0.7",
|
||||
|
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user