Skip to content

Commit 8d1c944

Browse files
authored
feat: added Worker1 Promiser types (#137)
* feat: added Worker1 Promiser types * feat: added missing promiser types * feat: added stricter types
1 parent ccf0b99 commit 8d1c944

2 files changed

Lines changed: 249 additions & 11 deletions

File tree

src/index.d.ts

Lines changed: 245 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,16 @@ export type ExecOptions = {
483483
* but clients must also refrain from using any lower-level (C-style) APIs
484484
* which might modify the statement.
485485
*/
486-
callback?: (
487-
row: SqlValue[] | Record<string, SqlValue> | PreparedStatement | SqlValue,
488-
stmt: PreparedStatement,
489-
) => void | false;
486+
callback?:
487+
| ((
488+
row:
489+
| SqlValue[]
490+
| Record<string, SqlValue>
491+
| PreparedStatement
492+
| SqlValue,
493+
stmt: PreparedStatement,
494+
) => void | false)
495+
| string;
490496

491497
/**
492498
* If this is an array, the column names of the result set are stored in this
@@ -738,7 +744,7 @@ export type WindowFunctionOptions = FunctionOptions & {
738744
* using `sqlite3_open` or equivalent.
739745
*
740746
* @example
741-
* ```typescript
747+
* ```ts
742748
* const db = new sqlite3.DB();
743749
* try {
744750
* db.exec([
@@ -1552,7 +1558,7 @@ export type SAHPoolUtil = {
15521558
/** Exception class for reporting WASM-side allocation errors. */
15531559
export class WasmAllocError extends Error {
15541560
constructor(message: string);
1555-
toss: any;
1561+
toss: (...args: unknown[]) => never;
15561562
}
15571563

15581564
/** Exception class used primarily by the oo1 API. */
@@ -1566,6 +1572,232 @@ export type WasmPointer = number;
15661572

15671573
export type NullPointer = 0 | null | undefined;
15681574

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?: unknown;
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 = unknown,
1598+
> extends Worker1MessageBusEnvelope {
1599+
type: T;
1600+
args?: Args;
1601+
/** Timestamp set by the promiser before posting a message. */
1602+
departureTime?: number;
1603+
}
1604+
1605+
/** Worker API #1 output message envelope. */
1606+
interface Worker1OutputEnvelope<
1607+
T extends string,
1608+
Result = unknown,
1609+
> extends Worker1MessageBusEnvelope {
1610+
type: T;
1611+
result: Result;
1612+
}
1613+
1614+
/**
1615+
* Worker API #1 per-row callback payload for promiser `exec()` callback
1616+
* functions.
1617+
*/
1618+
interface Worker1ExecRowMessage {
1619+
/** Internally synthesized callback message type. */
1620+
type: `${string}:row`;
1621+
/** Current row value in the shape implied by `rowMode`. */
1622+
row?: SqlValue[] | Record<string, SqlValue> | SqlValue;
1623+
/** 1-based row number, or null as end-of-result-set sentinel. */
1624+
rowNumber: number | null;
1625+
/** Column names populated when requested by options. */
1626+
columnNames?: string[];
1627+
}
1628+
1629+
/** Worker API #1 exec options accepted by the promiser wrapper. */
1630+
type Worker1ExecArgs = Omit<ExecOptions, 'callback'> & {
1631+
/**
1632+
* Promiser-specific callback mode. String callback IDs are not accepted by
1633+
* the promiser wrapper.
1634+
*/
1635+
callback?: (row: Worker1ExecRowMessage) => void;
1636+
};
1637+
1638+
/** Worker API #1 error response result. */
1639+
interface Worker1ErrorResult {
1640+
/** Type of the triggering operation: 'open', 'close', ... */
1641+
operation: string;
1642+
/** Error message text */
1643+
message: string;
1644+
/** The ErrorClass.name property from the thrown exception. */
1645+
errorClass: string;
1646+
/** The message object which triggered the error. */
1647+
input: Worker1InputEnvelope<string, unknown>;
1648+
/** If available, a stack trace array. */
1649+
stack?: string[];
1650+
}
1651+
1652+
/** Worker API #1 error response envelope. */
1653+
interface Worker1ErrorEnvelope extends Worker1MessageBusEnvelope {
1654+
type: 'error';
1655+
result: Worker1ErrorResult;
1656+
}
1657+
1658+
/** Worker API #1 'open' arguments. */
1659+
interface Worker1OpenArgs {
1660+
/** The db filename. */
1661+
filename?: string;
1662+
/** Sqlite3_vfs name. */
1663+
vfs?: string;
1664+
}
1665+
1666+
/** Worker API #1 'open' result. */
1667+
interface Worker1OpenResult {
1668+
/** Db filename, possibly differing from the input. */
1669+
filename: string;
1670+
/** Opaque ID value for the opened db. */
1671+
dbId: string;
1672+
/** True if the given filename resides in the known-persistent storage. */
1673+
persistent: boolean;
1674+
/** Name of the VFS the "main" db is using. */
1675+
vfs: string;
1676+
}
1677+
1678+
/** Worker API #1 'close' arguments. */
1679+
interface Worker1CloseArgs {
1680+
/** If truthy, the database will be unlinked (deleted) after closing it. */
1681+
unlink?: boolean;
1682+
}
1683+
1684+
/** Worker API #1 'close' result. */
1685+
interface Worker1CloseResult {
1686+
/** Filename of closed db, or undefined if no db was closed. */
1687+
filename?: string;
1688+
}
1689+
1690+
/** Worker API #1 'exec' result. */
1691+
interface Worker1ExecResult extends ExecOptions {
1692+
/** Number of changes made by the SQL. (v3.43+) */
1693+
changeCount?: number | bigint;
1694+
/** Result of sqlite3_last_insert_rowid(). (v3.50.0+) */
1695+
lastInsertRowId?: bigint;
1696+
}
1697+
1698+
/** Worker API #1 'export' result. */
1699+
interface Worker1ExportResult {
1700+
/** The exported database as a byte array. */
1701+
byteArray: Uint8Array;
1702+
/** The db filename. */
1703+
filename: string;
1704+
/** "application/x-sqlite3" */
1705+
mimetype: string;
1706+
}
1707+
1708+
/** Worker API #1 'config-get' result. */
1709+
interface Worker1ConfigGetResult {
1710+
/** Sqlite3.version object */
1711+
version: {
1712+
libVersion: string;
1713+
libVersionNumber: number;
1714+
sourceId: string;
1715+
downloadVersion: number;
1716+
};
1717+
/** True if BigInt support is enabled. */
1718+
bigIntEnabled: boolean;
1719+
/** Result of sqlite3.capi.sqlite3_js_vfs_list() */
1720+
vfsList: string[];
1721+
}
1722+
1723+
/** Map of Worker API #1 operation types to their argument types. */
1724+
interface Worker1ArgsMap {
1725+
open: Worker1OpenArgs;
1726+
close: Worker1CloseArgs | undefined;
1727+
exec: Worker1ExecArgs | string;
1728+
export: undefined;
1729+
'config-get': undefined;
1730+
}
1731+
1732+
/** Map of Worker API #1 operation types to their result types. */
1733+
interface Worker1ResultMap {
1734+
open: Worker1OpenResult;
1735+
close: Worker1CloseResult;
1736+
exec: Worker1ExecResult;
1737+
export: Worker1ExportResult;
1738+
'config-get': Worker1ConfigGetResult;
1739+
}
1740+
1741+
/** Function type returned by Worker1PromiserFactory. */
1742+
interface Worker1Promiser {
1743+
/**
1744+
* Sends a message to the worker and returns a Promise which resolves to the
1745+
* response message.
1746+
*/
1747+
<T extends keyof Worker1ArgsMap>(
1748+
type: T,
1749+
args: Worker1ArgsMap[T],
1750+
): Promise<Worker1OutputEnvelope<T, Worker1ResultMap[T]>>;
1751+
1752+
/**
1753+
* Sends a message to the worker and returns a Promise which resolves to the
1754+
* response message.
1755+
*/
1756+
<T extends keyof Worker1ArgsMap>(
1757+
msg: Worker1InputEnvelope<T, Worker1ArgsMap[T]>,
1758+
): Promise<Worker1OutputEnvelope<T, Worker1ResultMap[T]>>;
1759+
}
1760+
1761+
/** Configuration for Worker1PromiserFactory. */
1762+
interface Worker1PromiserConfig {
1763+
/** A Worker instance or a function which returns one. */
1764+
worker?: Worker | (() => Worker);
1765+
1766+
/** Callback called when the worker is ready. */
1767+
onready?: (promiser: Worker1Promiser) => void | Promise<void>;
1768+
1769+
/** Callback for unhandled worker messages. */
1770+
onunhandled?: (event: MessageEvent) => void;
1771+
1772+
/** Optional function to generate unique message IDs. */
1773+
generateMessageId?: (msg: Worker1InputEnvelope<string, unknown>) => string;
1774+
1775+
/** Optional debug logging function. */
1776+
debug?: (...args: unknown[]) => void;
1777+
1778+
/** Optional error logging function (undocumented). */
1779+
onerror?: (...args: unknown[]) => void;
1780+
}
1781+
1782+
/** Factory for creating Worker1Promiser instances. */
1783+
interface Worker1PromiserFactory {
1784+
/** Creates a Worker1Promiser. */
1785+
(config?: Worker1PromiserConfig): Worker1Promiser;
1786+
1787+
/** Creates a Worker1Promiser from a ready callback. */
1788+
(onready: (promiser: Worker1Promiser) => void): Worker1Promiser;
1789+
1790+
/** Default configuration. */
1791+
defaultConfig: Worker1PromiserConfig;
1792+
1793+
/** V2 variant which returns a Promise that resolves to the promiser. */
1794+
v2: {
1795+
(config?: Worker1PromiserConfig): Promise<Worker1Promiser>;
1796+
(onready: (promiser: Worker1Promiser) => void): Promise<Worker1Promiser>;
1797+
defaultConfig: Worker1PromiserConfig;
1798+
};
1799+
}
1800+
15691801
export type StructPtrMapper<T> = {
15701802
StructType: T;
15711803
/**
@@ -2089,6 +2321,9 @@ export type Sqlite3Static = {
20892321
*/
20902322
initWorker1API(): void;
20912323

2324+
/** Promise-based proxy for the sqlite3 Worker API #1. */
2325+
Worker1Promiser: Worker1PromiserFactory;
2326+
20922327
installOpfsSAHPoolVfs(opts: {
20932328
/**
20942329
* If truthy (default=false) contents and filename mapping are removed from
@@ -2153,10 +2388,10 @@ export type Sqlite3Static = {
21532388
SQLite3Error: typeof SQLite3Error;
21542389

21552390
/**
2156-
* The options with which the API was configured. Whether or not modifying
2157-
* them after the bootstrapping process will have any useful effect is
2158-
* unspecified and may change with any given version. Clients must not rely on
2159-
* that capability.
2391+
* The options with which the API was configured. Whether modifying them after
2392+
* the bootstrapping process will have any useful effect is unspecified and
2393+
* may change with any given version. Clients must not rely on that
2394+
* capability.
21602395
*/
21612396
config: {
21622397
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)