Skip to content

Commit b0274ae

Browse files
committed
feat: added Worker1 Promiser types
1 parent 18f57d4 commit b0274ae

2 files changed

Lines changed: 222 additions & 14 deletions

File tree

src/index.d.ts

Lines changed: 218 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -485,14 +485,16 @@ declare type ExecOptions = {
485485
* but clients must also refrain from using any lower-level (C-style) APIs
486486
* which might modify the statement.
487487
*/
488-
callback?: (
489-
row:
490-
| SqlValue[]
491-
| { [columnName: string]: SqlValue }
492-
| PreparedStatement
493-
| SqlValue,
494-
stmt: PreparedStatement,
495-
) => void | false;
488+
callback?:
489+
| ((
490+
row:
491+
| SqlValue[]
492+
| { [columnName: string]: SqlValue }
493+
| PreparedStatement
494+
| SqlValue,
495+
stmt: PreparedStatement,
496+
) => void | false)
497+
| string;
496498

497499
/**
498500
* If this is an array, the column names of the result set are stored in this
@@ -744,7 +746,7 @@ declare type WindowFunctionOptions = FunctionOptions & {
744746
* using `sqlite3_open` or equivalent.
745747
*
746748
* @example
747-
* ```typescript
749+
* ```ts
748750
* const db = new sqlite3.DB();
749751
* try {
750752
* db.exec([
@@ -1570,6 +1572,206 @@ declare class SQLite3Error extends Error {
15701572
/** A pointer to a location in WASM heap memory. */
15711573
declare type WasmPointer = number;
15721574

1575+
/** Common envelope for all Worker API #1 messages. */
1576+
interface Worker1MessageBusEnvelope {
1577+
/** One of: 'open', 'close', 'exec', 'export', 'config-get' */
1578+
type: string;
1579+
1580+
/**
1581+
* Optional arbitrary value. The worker will copy it as-is into response
1582+
* messages to assist in client-side dispatching.
1583+
*/
1584+
messageId?: any;
1585+
1586+
/**
1587+
* A db identifier string (returned by 'open') which tells the operation which
1588+
* database instance to work on. If not provided, the first-opened db is
1589+
* used.
1590+
*/
1591+
dbId?: string;
1592+
}
1593+
1594+
/** Worker API #1 input message envelope. */
1595+
interface Worker1InputEnvelope<
1596+
T extends string,
1597+
Args = any,
1598+
> extends Worker1MessageBusEnvelope {
1599+
type: T;
1600+
args: Args;
1601+
}
1602+
1603+
/** Worker API #1 output message envelope. */
1604+
interface Worker1OutputEnvelope<
1605+
T extends string,
1606+
Result = any,
1607+
> extends Worker1MessageBusEnvelope {
1608+
type: T;
1609+
result: Result;
1610+
}
1611+
1612+
/** Worker API #1 error response result. */
1613+
interface Worker1ErrorResult {
1614+
/** Type of the triggering operation: 'open', 'close', ... */
1615+
operation: string;
1616+
/** Error message text */
1617+
message: string;
1618+
/** The ErrorClass.name property from the thrown exception. */
1619+
errorClass: string;
1620+
/** The message object which triggered the error. */
1621+
input: any;
1622+
/** If available, a stack trace array. */
1623+
stack?: string[];
1624+
}
1625+
1626+
/** Worker API #1 error response envelope. */
1627+
interface Worker1ErrorEnvelope extends Worker1MessageBusEnvelope {
1628+
type: 'error';
1629+
result: Worker1ErrorResult;
1630+
}
1631+
1632+
/** Worker API #1 'open' arguments. */
1633+
interface Worker1OpenArgs {
1634+
/** The db filename. */
1635+
filename?: string;
1636+
/** Sqlite3_vfs name. */
1637+
vfs?: string;
1638+
}
1639+
1640+
/** Worker API #1 'open' result. */
1641+
interface Worker1OpenResult {
1642+
/** Db filename, possibly differing from the input. */
1643+
filename: string;
1644+
/** Opaque ID value for the opened db. */
1645+
dbId: string;
1646+
/** True if the given filename resides in the known-persistent storage. */
1647+
persistent: boolean;
1648+
/** Name of the VFS the "main" db is using. */
1649+
vfs: string;
1650+
}
1651+
1652+
/** Worker API #1 'close' arguments. */
1653+
interface Worker1CloseArgs {
1654+
/** If truthy, the database will be unlinked (deleted) after closing it. */
1655+
unlink?: boolean;
1656+
}
1657+
1658+
/** Worker API #1 'close' result. */
1659+
interface Worker1CloseResult {
1660+
/** Filename of closed db, or undefined if no db was closed. */
1661+
filename?: string;
1662+
}
1663+
1664+
/** Worker API #1 'exec' result. */
1665+
interface Worker1ExecResult extends ExecOptions {
1666+
/** Number of changes made by the SQL. (v3.43+) */
1667+
changeCount?: number | bigint;
1668+
/** Result of sqlite3_last_insert_rowid(). (v3.50.0+) */
1669+
lastInsertRowId?: bigint;
1670+
}
1671+
1672+
/** Worker API #1 'export' result. */
1673+
interface Worker1ExportResult {
1674+
/** The exported database as a byte array. */
1675+
byteArray: Uint8Array;
1676+
/** The db filename. */
1677+
filename: string;
1678+
/** "application/x-sqlite3" */
1679+
mimetype: string;
1680+
}
1681+
1682+
/** Worker API #1 'config-get' result. */
1683+
interface Worker1ConfigGetResult {
1684+
/** Sqlite3.version object */
1685+
version: {
1686+
libVersion: string;
1687+
libVersionNumber: number;
1688+
sourceId: string;
1689+
downloadVersion: number;
1690+
};
1691+
/** True if BigInt support is enabled. */
1692+
bigIntEnabled: boolean;
1693+
/** Result of sqlite3.capi.sqlite3_js_vfs_list() */
1694+
vfsList: string[];
1695+
}
1696+
1697+
/** Map of Worker API #1 operation types to their argument types. */
1698+
interface Worker1ArgsMap {
1699+
open: Worker1OpenArgs;
1700+
close: Worker1CloseArgs | undefined;
1701+
exec: ExecOptions | string;
1702+
export: undefined;
1703+
'config-get': undefined;
1704+
}
1705+
1706+
/** Map of Worker API #1 operation types to their result types. */
1707+
interface Worker1ResultMap {
1708+
open: Worker1OpenResult;
1709+
close: Worker1CloseResult;
1710+
exec: Worker1ExecResult;
1711+
export: Worker1ExportResult;
1712+
'config-get': Worker1ConfigGetResult;
1713+
}
1714+
1715+
/** Function type returned by Worker1PromiserFactory. */
1716+
interface Worker1Promiser {
1717+
/**
1718+
* Sends a message to the worker and returns a Promise which resolves to the
1719+
* response message.
1720+
*/
1721+
<T extends keyof Worker1ArgsMap>(
1722+
type: T,
1723+
args: Worker1ArgsMap[T],
1724+
): Promise<Worker1OutputEnvelope<T, Worker1ResultMap[T]>>;
1725+
1726+
/**
1727+
* Sends a message to the worker and returns a Promise which resolves to the
1728+
* response message.
1729+
*/
1730+
<T extends keyof Worker1ArgsMap>(
1731+
msg: Worker1InputEnvelope<T, Worker1ArgsMap[T]>,
1732+
): Promise<Worker1OutputEnvelope<T, Worker1ResultMap[T]>>;
1733+
}
1734+
1735+
/** Configuration for Worker1PromiserFactory. */
1736+
interface Worker1PromiserConfig {
1737+
/** A Worker instance or a function which returns one. */
1738+
worker?: Worker | (() => Worker);
1739+
1740+
/** Callback called when the worker is ready. */
1741+
onready?: (promiser: Worker1Promiser) => void;
1742+
1743+
/** Callback for unhandled worker messages. */
1744+
onunhandled?: (event: MessageEvent) => void;
1745+
1746+
/** Optional function to generate unique message IDs. */
1747+
generateMessageId?: (msg: any) => string;
1748+
1749+
/** Optional debug logging function. */
1750+
debug?: (...args: any[]) => void;
1751+
1752+
/** Optional error logging function (undocumented). */
1753+
onerror?: (...args: any[]) => void;
1754+
}
1755+
1756+
/** Factory for creating Worker1Promiser instances. */
1757+
interface Worker1PromiserFactory {
1758+
/** Creates a Worker1Promiser. */
1759+
(config?: Worker1PromiserConfig): Worker1Promiser;
1760+
1761+
/** Creates a Worker1Promiser from a ready callback. */
1762+
(onready: (promiser: Worker1Promiser) => void): Worker1Promiser;
1763+
1764+
/** Default configuration. */
1765+
defaultConfig: Worker1PromiserConfig;
1766+
1767+
/** V2 variant which returns a Promise that resolves to the promiser. */
1768+
v2: {
1769+
(config?: Worker1PromiserConfig): Promise<Worker1Promiser>;
1770+
(onready: (promiser: Worker1Promiser) => void): Promise<Worker1Promiser>;
1771+
defaultConfig: Worker1PromiserConfig;
1772+
};
1773+
}
1774+
15731775
declare type NullPointer = 0 | null | undefined;
15741776

15751777
declare type StructPtrMapper<T> = {
@@ -2094,6 +2296,9 @@ declare type Sqlite3Static = {
20942296
*/
20952297
initWorker1API(): void;
20962298

2299+
/** Promise-based proxy for the sqlite3 Worker API #1. */
2300+
Worker1Promiser: Worker1PromiserFactory;
2301+
20972302
installOpfsSAHPoolVfs(opts: {
20982303
/**
20992304
* If truthy (default=false) contents and filename mapping are removed from
@@ -2158,10 +2363,10 @@ declare type Sqlite3Static = {
21582363
SQLite3Error: typeof SQLite3Error;
21592364

21602365
/**
2161-
* The options with which the API was configured. Whether or not modifying
2162-
* them after the bootstrapping process will have any useful effect is
2163-
* unspecified and may change with any given version. Clients must not rely on
2164-
* that capability.
2366+
* The options with which the API was configured. Whether modifying them after
2367+
* the bootstrapping process will have any useful effect is unspecified and
2368+
* may change with any given version. Clients must not rely on that
2369+
* capability.
21652370
*/
21662371
config: {
21672372
exports: any;

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { default as sqlite3InitModule } from './bin/sqlite3-bundler-friendly.mjs';
22
import { default as sqlite3Worker1Promiser } from './bin/sqlite3-worker1-promiser.mjs';
33

4+
/** @type {import('./index.d.ts').Worker1PromiserFactory} */
5+
const typedWorker1Promiser = sqlite3Worker1Promiser;
6+
47
export default sqlite3InitModule;
58

6-
export { sqlite3Worker1Promiser };
9+
export { typedWorker1Promiser as sqlite3Worker1Promiser };

0 commit comments

Comments
 (0)