fix: embed sqlite3.wasm in lib for production builds
This commit is contained in:
@ -11,21 +11,21 @@ Worker relay is a Nostr relay built on `sqlite-wasm`
|
|||||||
```typescript
|
```typescript
|
||||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||||
|
|
||||||
// in debug mode you may need this, to map to the correct sqlite-wasm path
|
// when using Vite import the worker script directly (for production)
|
||||||
// this is needed because sqlite-wasm will otherwise look inside @snort/worker-relay directory for sqlite3.wasm
|
import WorkerVite from "@snort/worker-relay/src/worker?worker"
|
||||||
const basePath = new URL("@sqlite.org/sqlite-wasm", import.meta.url);
|
|
||||||
|
|
||||||
// internally we resolve the script path like this:
|
// in dev mode import esm module, i have no idea why it has to work like this
|
||||||
const scriptPath = new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url);
|
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 workerRelay = new WorkerRelayInterface(workerScript);
|
||||||
const relay = new WorkerRelayInterface(scriptPath.href, basePath.href);
|
|
||||||
|
|
||||||
// load sqlite database and run migrations
|
// 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
|
// 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
|
// publish a new event to the relay
|
||||||
const myEvent = {
|
const myEvent = {
|
||||||
@ -34,7 +34,7 @@ const myEvent = {
|
|||||||
content: "test",
|
content: "test",
|
||||||
tags: []
|
tags: []
|
||||||
};
|
};
|
||||||
if (await relay.event(myEvent)) {
|
if (await workerRelay.event(myEvent)) {
|
||||||
console.log("Success");
|
console.log("Success");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
import { WorkerRelayInterface } from "@snort/worker-relay";
|
import { WorkerRelayInterface } from "@snort/worker-relay";
|
||||||
|
|
||||||
// in debug mode you may need this, to map to the correct sqlite-wasm path
|
// when using Vite import the worker script directly (for production)
|
||||||
// this is needed because sqlite-wasm will otherwise look inside @snort/worker-relay directory for sqlite3.wasm
|
import WorkerVite from "@snort/worker-relay/src/worker?worker"
|
||||||
const basePath = new URL("@sqlite.org/sqlite-wasm", import.meta.url);
|
|
||||||
|
|
||||||
// internally we resolve the script path like this:
|
// in dev mode import esm module, i have no idea why it has to work like this
|
||||||
const scriptPath = new URL("@snort/worker-relay/dist/esm/worker.mjs", import.meta.url);
|
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 workerRelay = new WorkerRelayInterface(workerScript);
|
||||||
const relay = new WorkerRelayInterface(scriptPath, basePath.href);
|
|
||||||
|
|
||||||
// load sqlite database and run migrations
|
// 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
|
// 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
|
// publish a new event to the relay
|
||||||
const myEvent = {
|
const myEvent = {
|
||||||
@ -23,6 +23,6 @@ const myEvent = {
|
|||||||
content: "test",
|
content: "test",
|
||||||
tags: []
|
tags: []
|
||||||
};
|
};
|
||||||
if (await relay.event(myEvent)) {
|
if (await workerRelay.event(myEvent)) {
|
||||||
console.log("Success");
|
console.log("Success");
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@snort/worker-relay",
|
"name": "@snort/worker-relay",
|
||||||
"version": "1.0.6",
|
"version": "1.0.7",
|
||||||
"description": "A nostr relay in a service worker",
|
"description": "A nostr relay in a service worker",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
@ -10,19 +10,18 @@
|
|||||||
"author": "Kieran",
|
"author": "Kieran",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"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": [
|
"files": [
|
||||||
"src",
|
"src",
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@sqlite.org/sqlite-wasm": "^3.45.1-build1",
|
||||||
"eventemitter3": "^5.0.1",
|
"eventemitter3": "^5.0.1",
|
||||||
"uuid": "^9.0.1"
|
"uuid": "^9.0.1"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
|
||||||
"@sqlite.org/sqlite-wasm": "^3.45.1-build1"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/debug": "^4.1.12",
|
"@types/debug": "^4.1.12",
|
||||||
"@types/uuid": "^9.0.7",
|
"@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 {
|
export class WorkerRelayInterface {
|
||||||
#worker: Worker;
|
#worker: Worker;
|
||||||
#sqliteDir?: string;
|
|
||||||
#commandQueue: Map<string, (v: unknown, ports: ReadonlyArray<MessagePort>) => void> = new Map();
|
#commandQueue: Map<string, (v: unknown, ports: ReadonlyArray<MessagePort>) => void> = new Map();
|
||||||
|
|
||||||
// Command timeout
|
// Command timeout
|
||||||
@ -12,10 +11,8 @@ export class WorkerRelayInterface {
|
|||||||
/**
|
/**
|
||||||
* Interface wrapper for worker relay
|
* Interface wrapper for worker relay
|
||||||
* @param scriptPath Path to worker script or Worker script object
|
* @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) {
|
constructor(scriptPath?: string | URL | Worker) {
|
||||||
this.#sqliteDir = sqlite3Dir;
|
|
||||||
if (scriptPath instanceof Worker) {
|
if (scriptPath instanceof Worker) {
|
||||||
this.#worker = scriptPath;
|
this.#worker = scriptPath;
|
||||||
} else {
|
} else {
|
||||||
@ -39,7 +36,7 @@ export class WorkerRelayInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async init(databasePath: string) {
|
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) {
|
async event(ev: NostrEvent) {
|
||||||
|
@ -4,6 +4,9 @@ import { EventMetadata, NostrEvent, RelayHandler, RelayHandlerEvents, ReqFilter,
|
|||||||
import migrate from "./migrations";
|
import migrate from "./migrations";
|
||||||
import { debugLog } from "./debug";
|
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 {
|
export class SqliteRelay extends EventEmitter<RelayHandlerEvents> implements RelayHandler {
|
||||||
#sqlite?: Sqlite3Static;
|
#sqlite?: Sqlite3Static;
|
||||||
#log = (msg: string, ...args: Array<any>) => debugLog("SqliteRelay", msg, ...args);
|
#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
|
* Initialize the SQLite driver
|
||||||
*/
|
*/
|
||||||
async init(path: string, sqliteWasmPath?: string) {
|
async init(path: string) {
|
||||||
if (this.#sqlite) return;
|
if (this.#sqlite) return;
|
||||||
this.#sqlite = await sqlite3InitModule({
|
this.#sqlite = await sqlite3InitModule({
|
||||||
locateFile(path, prefix) {
|
locateFile: (path, prefix) => {
|
||||||
return new URL(`sqlite-wasm/jswasm/${path}`, sqliteWasmPath).href;
|
if (path === "sqlite3.wasm") {
|
||||||
|
return SqlitePath;
|
||||||
|
}
|
||||||
|
return prefix + path;
|
||||||
},
|
},
|
||||||
print: msg => this.#log(msg),
|
print: msg => this.#log(msg),
|
||||||
printErr: 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 {
|
} else {
|
||||||
relay = new InMemoryRelay();
|
relay = new InMemoryRelay();
|
||||||
}
|
}
|
||||||
const [dbPath, wasmPath] = msg.args as Array<string>;
|
const [dbPath] = msg.args as Array<string>;
|
||||||
debugLog("StartInit", dbPath, wasmPath);
|
debugLog("StartInit", dbPath);
|
||||||
await relay.init(dbPath, wasmPath);
|
await relay.init(dbPath);
|
||||||
reply(msg.id, true);
|
reply(msg.id, true);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
Reference in New Issue
Block a user