@delali/sirannon-db 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +14 -0
- package/README.md +418 -0
- package/dist/chunk-VI4UP4RR.mjs +417 -0
- package/dist/client/index.d.ts +223 -0
- package/dist/client/index.mjs +479 -0
- package/dist/core/index.d.ts +295 -0
- package/dist/core/index.mjs +1346 -0
- package/dist/protocol-BX1H-_Mz.d.ts +104 -0
- package/dist/server/index.d.ts +103 -0
- package/dist/server/index.mjs +808 -0
- package/dist/sirannon-BJ8Yd1Uf.d.ts +148 -0
- package/dist/types-DArCObcu.d.ts +186 -0
- package/package.json +87 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { E as ExecuteResult } from './types-DArCObcu.js';
|
|
2
|
+
|
|
3
|
+
/** Body for POST /db/:id/query */
|
|
4
|
+
interface QueryRequest {
|
|
5
|
+
sql: string;
|
|
6
|
+
params?: Record<string, unknown> | unknown[];
|
|
7
|
+
}
|
|
8
|
+
/** Body for POST /db/:id/execute */
|
|
9
|
+
interface ExecuteRequest {
|
|
10
|
+
sql: string;
|
|
11
|
+
params?: Record<string, unknown> | unknown[];
|
|
12
|
+
}
|
|
13
|
+
/** A single statement within a transaction batch. */
|
|
14
|
+
interface TransactionStatement {
|
|
15
|
+
sql: string;
|
|
16
|
+
params?: Record<string, unknown> | unknown[];
|
|
17
|
+
}
|
|
18
|
+
/** Body for POST /db/:id/transaction */
|
|
19
|
+
interface TransactionRequest {
|
|
20
|
+
statements: TransactionStatement[];
|
|
21
|
+
}
|
|
22
|
+
/** Response for a successful query. */
|
|
23
|
+
interface QueryResponse {
|
|
24
|
+
rows: Record<string, unknown>[];
|
|
25
|
+
}
|
|
26
|
+
/** Response for a successful execute. */
|
|
27
|
+
interface ExecuteResponse {
|
|
28
|
+
changes: number;
|
|
29
|
+
lastInsertRowId: number | string;
|
|
30
|
+
}
|
|
31
|
+
/** Response for a successful transaction. */
|
|
32
|
+
interface TransactionResponse {
|
|
33
|
+
results: ExecuteResponse[];
|
|
34
|
+
}
|
|
35
|
+
/** Standard error response envelope. */
|
|
36
|
+
interface ErrorResponse {
|
|
37
|
+
error: {
|
|
38
|
+
code: string;
|
|
39
|
+
message: string;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** Inbound WS message types. */
|
|
43
|
+
type WSClientMessage = WSSubscribeMessage | WSUnsubscribeMessage | WSQueryMessage | WSExecuteMessage;
|
|
44
|
+
interface WSSubscribeMessage {
|
|
45
|
+
type: 'subscribe';
|
|
46
|
+
id: string;
|
|
47
|
+
table: string;
|
|
48
|
+
filter?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
interface WSUnsubscribeMessage {
|
|
51
|
+
type: 'unsubscribe';
|
|
52
|
+
id: string;
|
|
53
|
+
}
|
|
54
|
+
interface WSQueryMessage {
|
|
55
|
+
type: 'query';
|
|
56
|
+
id: string;
|
|
57
|
+
sql: string;
|
|
58
|
+
params?: Record<string, unknown> | unknown[];
|
|
59
|
+
}
|
|
60
|
+
interface WSExecuteMessage {
|
|
61
|
+
type: 'execute';
|
|
62
|
+
id: string;
|
|
63
|
+
sql: string;
|
|
64
|
+
params?: Record<string, unknown> | unknown[];
|
|
65
|
+
}
|
|
66
|
+
/** Outbound WS message types. */
|
|
67
|
+
type WSServerMessage = WSSubscribedMessage | WSUnsubscribedMessage | WSChangeMessage | WSResultMessage | WSErrorMessage;
|
|
68
|
+
interface WSSubscribedMessage {
|
|
69
|
+
type: 'subscribed';
|
|
70
|
+
id: string;
|
|
71
|
+
}
|
|
72
|
+
interface WSUnsubscribedMessage {
|
|
73
|
+
type: 'unsubscribed';
|
|
74
|
+
id: string;
|
|
75
|
+
}
|
|
76
|
+
interface WSChangeMessage {
|
|
77
|
+
type: 'change';
|
|
78
|
+
id: string;
|
|
79
|
+
event: {
|
|
80
|
+
type: 'insert' | 'update' | 'delete';
|
|
81
|
+
table: string;
|
|
82
|
+
row: Record<string, unknown>;
|
|
83
|
+
oldRow?: Record<string, unknown>;
|
|
84
|
+
seq: string;
|
|
85
|
+
timestamp: number;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
interface WSResultMessage {
|
|
89
|
+
type: 'result';
|
|
90
|
+
id: string;
|
|
91
|
+
data: QueryResponse | ExecuteResponse;
|
|
92
|
+
}
|
|
93
|
+
interface WSErrorMessage {
|
|
94
|
+
type: 'error';
|
|
95
|
+
id: string;
|
|
96
|
+
error: {
|
|
97
|
+
code: string;
|
|
98
|
+
message: string;
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** Convert an ExecuteResult (with possible bigint) to a JSON-safe response. */
|
|
102
|
+
declare function toExecuteResponse(result: ExecuteResult): ExecuteResponse;
|
|
103
|
+
|
|
104
|
+
export { type ErrorResponse as E, type QueryRequest as Q, type TransactionRequest as T, type WSChangeMessage as W, type ExecuteRequest as a, type ExecuteResponse as b, type QueryResponse as c, type TransactionResponse as d, type TransactionStatement as e, type WSClientMessage as f, type WSErrorMessage as g, type WSExecuteMessage as h, type WSQueryMessage as i, type WSResultMessage as j, type WSServerMessage as k, type WSSubscribeMessage as l, type WSSubscribedMessage as m, type WSUnsubscribeMessage as n, type WSUnsubscribedMessage as o, toExecuteResponse as t };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export { E as ErrorResponse, a as ExecuteRequest, b as ExecuteResponse, Q as QueryRequest, c as QueryResponse, T as TransactionRequest, d as TransactionResponse, e as TransactionStatement, W as WSChangeMessage, f as WSClientMessage, g as WSErrorMessage, h as WSExecuteMessage, i as WSQueryMessage, j as WSResultMessage, k as WSServerMessage, l as WSSubscribeMessage, m as WSSubscribedMessage, n as WSUnsubscribeMessage, o as WSUnsubscribedMessage, t as toExecuteResponse } from '../protocol-BX1H-_Mz.js';
|
|
2
|
+
import { S as Sirannon } from '../sirannon-BJ8Yd1Uf.js';
|
|
3
|
+
import { S as ServerOptions, W as WSHandlerOptions } from '../types-DArCObcu.js';
|
|
4
|
+
import 'better-sqlite3';
|
|
5
|
+
|
|
6
|
+
declare class SirannonServer {
|
|
7
|
+
private app;
|
|
8
|
+
private listenSocket;
|
|
9
|
+
private readonly host;
|
|
10
|
+
private readonly port;
|
|
11
|
+
private readonly cors;
|
|
12
|
+
private readonly onRequestHook;
|
|
13
|
+
private readonly sirannon;
|
|
14
|
+
private readonly wsHandler;
|
|
15
|
+
constructor(sirannon: Sirannon, options?: ServerOptions);
|
|
16
|
+
listen(): Promise<void>;
|
|
17
|
+
close(): Promise<void>;
|
|
18
|
+
get listeningPort(): number;
|
|
19
|
+
private registerRoutes;
|
|
20
|
+
private registerWebSocketRoute;
|
|
21
|
+
private withCors;
|
|
22
|
+
private wrapDbRoute;
|
|
23
|
+
}
|
|
24
|
+
declare function createServer(sirannon: Sirannon, options?: ServerOptions): SirannonServer;
|
|
25
|
+
|
|
26
|
+
/** Transport-agnostic WebSocket connection. */
|
|
27
|
+
interface WSConnection {
|
|
28
|
+
send(data: string): void;
|
|
29
|
+
close(code?: number, reason?: string): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Manages WebSocket connections, routes messages to databases, and integrates
|
|
33
|
+
* with CDC for real-time change subscriptions.
|
|
34
|
+
*
|
|
35
|
+
* Designed to be transport-agnostic: any WebSocket implementation can drive
|
|
36
|
+
* this handler by calling handleOpen/handleMessage/handleClose with a
|
|
37
|
+
* WSConnection adapter.
|
|
38
|
+
*/
|
|
39
|
+
declare class WSHandler {
|
|
40
|
+
private readonly sirannon;
|
|
41
|
+
private readonly maxPayloadLength;
|
|
42
|
+
private readonly connections;
|
|
43
|
+
private readonly cdcContexts;
|
|
44
|
+
private closed;
|
|
45
|
+
constructor(sirannon: Sirannon, options?: WSHandlerOptions);
|
|
46
|
+
/**
|
|
47
|
+
* Register a new WebSocket connection for the given database.
|
|
48
|
+
*
|
|
49
|
+
* Sends an error and closes the connection if the database does not exist,
|
|
50
|
+
* is closed, or the handler itself has been shut down.
|
|
51
|
+
*/
|
|
52
|
+
handleOpen(conn: WSConnection, databaseId: string): void;
|
|
53
|
+
/**
|
|
54
|
+
* Process an incoming WebSocket message.
|
|
55
|
+
*
|
|
56
|
+
* Validates the JSON payload, extracts the message type and correlation ID,
|
|
57
|
+
* and routes to the appropriate handler.
|
|
58
|
+
*/
|
|
59
|
+
handleMessage(conn: WSConnection, data: string): void;
|
|
60
|
+
/**
|
|
61
|
+
* Clean up after a WebSocket connection closes.
|
|
62
|
+
*
|
|
63
|
+
* Unsubscribes all active CDC subscriptions for this connection and
|
|
64
|
+
* releases the per-database CDC context if no subscribers remain.
|
|
65
|
+
*/
|
|
66
|
+
handleClose(conn: WSConnection): void;
|
|
67
|
+
/** Number of active WebSocket connections. */
|
|
68
|
+
get connectionCount(): number;
|
|
69
|
+
/**
|
|
70
|
+
* Shut down the handler.
|
|
71
|
+
*
|
|
72
|
+
* Unsubscribes all CDC subscriptions, closes all WebSocket connections
|
|
73
|
+
* with code 1001 (Going Away), and releases all CDC resources.
|
|
74
|
+
*/
|
|
75
|
+
close(): void;
|
|
76
|
+
private handleQuery;
|
|
77
|
+
private handleExecute;
|
|
78
|
+
private handleSubscribe;
|
|
79
|
+
private handleUnsubscribe;
|
|
80
|
+
/**
|
|
81
|
+
* Get or create a CDC context for the given database.
|
|
82
|
+
*
|
|
83
|
+
* Opens a separate better-sqlite3 connection for trigger installation and
|
|
84
|
+
* change polling. WAL mode and a 5-second busy timeout are set so that
|
|
85
|
+
* the CDC connection coexists with the Database class's connection pool.
|
|
86
|
+
*/
|
|
87
|
+
private ensureCDC;
|
|
88
|
+
/**
|
|
89
|
+
* Release CDC resources for a database if no subscribers remain.
|
|
90
|
+
*
|
|
91
|
+
* Stops the polling loop and closes the dedicated CDC connection.
|
|
92
|
+
*/
|
|
93
|
+
private maybeCleanupCDC;
|
|
94
|
+
private isValidParams;
|
|
95
|
+
private send;
|
|
96
|
+
private sendError;
|
|
97
|
+
private sendSirannonError;
|
|
98
|
+
private sendChange;
|
|
99
|
+
}
|
|
100
|
+
/** Create a transport-agnostic WebSocket handler bound to a Sirannon registry. */
|
|
101
|
+
declare function createWSHandler(sirannon: Sirannon, options?: WSHandlerOptions): WSHandler;
|
|
102
|
+
|
|
103
|
+
export { SirannonServer, type WSConnection, WSHandler, createServer, createWSHandler };
|