|
| 1 | +import { parentPort, workerData } from 'node:worker_threads'; |
| 2 | +import * as path from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import * as Comlink from 'comlink'; |
| 5 | +import Database, { type Statement } from 'better-sqlite3'; |
| 6 | +import { |
| 7 | + isSqliteWorkerData, |
| 8 | + type SqliteRunResult, |
| 9 | + type ISqliteWorkerApi, |
| 10 | + type SqliteWorkerRunnerDbParams, |
| 11 | + type SqliteWorkerQueryParams, |
| 12 | + type SqliteWorkerStatementHandle |
| 13 | +} from './protocol.ts'; |
| 14 | +import { nodeEndpoint } from './utils/index.ts'; |
| 15 | +import { assertString, assertStringArray } from '../utils/index.ts'; |
| 16 | + |
| 17 | +declare const __filename: string | undefined; |
| 18 | + |
| 19 | +function all<TRow>(statement: Statement<unknown[], TRow>, params?: SqliteWorkerQueryParams): TRow[] { |
| 20 | + return params === undefined ? statement.all() : statement.all(params); |
| 21 | +} |
| 22 | + |
| 23 | +function get<TRow>(statement: Statement<unknown[], TRow>, params?: SqliteWorkerQueryParams): TRow | undefined { |
| 24 | + return params === undefined ? statement.get() : statement.get(params); |
| 25 | +} |
| 26 | + |
| 27 | +function run(statement: Statement, params?: SqliteWorkerQueryParams): SqliteRunResult { |
| 28 | + return params === undefined ? statement.run() : statement.run(params); |
| 29 | +} |
| 30 | + |
| 31 | +/** @internal */ |
| 32 | +export function resolveCurrentFileLocationFromStack(stack = new Error().stack) { |
| 33 | + const stackFilename = stack?.match(/\((file:\/\/[^)]+SqliteWorkerRunner\.js):\d+:\d+\)/)?.[1]; |
| 34 | + if (!stackFilename) |
| 35 | + throw new Error('Worker location could not be resolved from Error stack, pass sqliteWorkerRunnerLocation'); |
| 36 | + |
| 37 | + return fileURLToPath(stackFilename); |
| 38 | +} |
| 39 | + |
| 40 | +export class SqliteWorkerRunner implements ISqliteWorkerApi { |
| 41 | + |
| 42 | + static get location() { |
| 43 | + if (typeof __filename !== 'undefined' && path.isAbsolute(__filename)) |
| 44 | + return __filename; |
| 45 | + |
| 46 | + /* istanbul ignore next -- exercised by ESM consumers, not ts-jest's CJS transform */ |
| 47 | + return resolveCurrentFileLocationFromStack(); |
| 48 | + } |
| 49 | + |
| 50 | + readonly #db; |
| 51 | + #nextStatementHandle = 1; |
| 52 | + readonly #statements = new Map<SqliteWorkerStatementHandle, Statement<unknown[], unknown>>(); |
| 53 | + |
| 54 | + constructor(dbParams: SqliteWorkerRunnerDbParams) { |
| 55 | + assertString(dbParams.location, 'dbParams.location'); |
| 56 | + |
| 57 | + this.#db = new Database(dbParams.location, { |
| 58 | + readonly: true, |
| 59 | + fileMustExist: true |
| 60 | + }); |
| 61 | + |
| 62 | + if (dbParams.pragmas?.length) { |
| 63 | + assertStringArray(dbParams.pragmas, 'dbParams.pragmas'); |
| 64 | + |
| 65 | + for (const pragma of dbParams.pragmas) |
| 66 | + this.#db.pragma(pragma); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + all<TRow>(sql: string, params?: SqliteWorkerQueryParams): TRow[] { |
| 71 | + const statement = this.#db.prepare<unknown[], TRow>(sql); |
| 72 | + return all(statement, params); |
| 73 | + } |
| 74 | + |
| 75 | + get<TRow>(sql: string, params?: SqliteWorkerQueryParams): TRow | undefined { |
| 76 | + const statement = this.#db.prepare<unknown[], TRow>(sql); |
| 77 | + return get(statement, params); |
| 78 | + } |
| 79 | + |
| 80 | + run(sql: string, params?: SqliteWorkerQueryParams): SqliteRunResult { |
| 81 | + const statement = this.#db.prepare(sql); |
| 82 | + return run(statement, params); |
| 83 | + } |
| 84 | + |
| 85 | + prepare(sql: string): SqliteWorkerStatementHandle { |
| 86 | + const handle = this.#nextStatementHandle++; |
| 87 | + this.#statements.set(handle, this.#db.prepare(sql)); |
| 88 | + |
| 89 | + return handle; |
| 90 | + } |
| 91 | + |
| 92 | + /** @internal */ |
| 93 | + allPrepared<TRow>(handle: SqliteWorkerStatementHandle, params?: SqliteWorkerQueryParams): TRow[] { |
| 94 | + return all(this.#getStatement<TRow>(handle), params); |
| 95 | + } |
| 96 | + |
| 97 | + /** @internal */ |
| 98 | + getPrepared<TRow>(handle: SqliteWorkerStatementHandle, params?: SqliteWorkerQueryParams): TRow | undefined { |
| 99 | + return get(this.#getStatement<TRow>(handle), params); |
| 100 | + } |
| 101 | + |
| 102 | + /** @internal */ |
| 103 | + runPrepared(handle: SqliteWorkerStatementHandle, params?: SqliteWorkerQueryParams): SqliteRunResult { |
| 104 | + return run(this.#getStatement(handle), params); |
| 105 | + } |
| 106 | + |
| 107 | + #getStatement<TRow>(handle: SqliteWorkerStatementHandle): Statement<unknown[], TRow> { |
| 108 | + const statement = this.#statements.get(handle); |
| 109 | + if (!statement) |
| 110 | + throw new Error(`SQLite worker statement '${handle}' does not exist`); |
| 111 | + |
| 112 | + return statement as Statement<unknown[], TRow>; |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/* istanbul ignore next -- this branch runs inside the spawned worker process */ |
| 117 | +if (parentPort) { |
| 118 | + if (!isSqliteWorkerData(workerData)) |
| 119 | + throw new Error('workerData does not contain SQLite worker db parameters'); |
| 120 | + |
| 121 | + const runner = new SqliteWorkerRunner(workerData.db); |
| 122 | + |
| 123 | + parentPort.postMessage({ type: 'ready' }); |
| 124 | + Comlink.expose(runner, nodeEndpoint(parentPort)); |
| 125 | +} |
0 commit comments