@scriptdb/client 1.1.0 → 1.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/README.md CHANGED
@@ -373,7 +373,7 @@ The CLI reads configuration from `~/.scriptdb/config.json`:
373
373
 
374
374
  ## Changelog
375
375
 
376
- ### 1.1.0 (2025-01-16)
376
+ ### 1.1.1 (2025-01-16)
377
377
 
378
378
  **Added**
379
379
  - Native `scriptdb logs` command to view real-time logs
@@ -0,0 +1,192 @@
1
+ import * as tls from 'tls';
2
+
3
+ type Action = 'script-code' | 'save-db' | 'remove-db' | 'rename-db' | 'login' | 'logout' | 'get-info' | 'list-dbs' | 'create-db' | 'update-db' | 'get-db' | 'shell-command' | 'string';
4
+ interface DataOptions {
5
+ code?: string | any;
6
+ databaseName?: string | any;
7
+ token?: string | any;
8
+ username?: string | any;
9
+ password?: string | any;
10
+ newName?: string | any;
11
+ }
12
+ interface Logger {
13
+ debug?: (...args: any[]) => void;
14
+ info?: (...args: any[]) => void;
15
+ warn?: (...args: any[]) => void;
16
+ error?: (...args: any[]) => void;
17
+ }
18
+ interface ClientOptions {
19
+ secure?: boolean;
20
+ logger?: Logger;
21
+ requestTimeout?: number;
22
+ socketTimeout?: number;
23
+ retries?: number;
24
+ retryDelay?: number;
25
+ tlsOptions?: tls.TlsOptions;
26
+ frame?: 'ndjson' | 'length-prefix';
27
+ preferLengthPrefix?: boolean;
28
+ maxPending?: number;
29
+ maxQueue?: number;
30
+ maxMessageSize?: number;
31
+ signing?: {
32
+ secret: string;
33
+ algorithm?: string;
34
+ };
35
+ stringify?: (obj: any) => string;
36
+ username?: string;
37
+ password?: string;
38
+ tokenRefresh?: () => Promise<{
39
+ token: string;
40
+ expiresAt?: number;
41
+ }>;
42
+ }
43
+ interface Message {
44
+ id?: number;
45
+ action?: Action;
46
+ command?: string;
47
+ message?: string;
48
+ data?: DataOptions;
49
+ token?: string;
50
+ signature?: string;
51
+ payload?: any;
52
+ }
53
+ declare class ScriptDBClient {
54
+ private options;
55
+ private socketTimeout;
56
+ private maxMessageSize;
57
+ private _mask;
58
+ private _maskArgs;
59
+ private logger?;
60
+ private secure;
61
+ private requestTimeout;
62
+ private retries;
63
+ private retryDelay;
64
+ private frame;
65
+ private uri;
66
+ private protocolName;
67
+ private username;
68
+ private password;
69
+ private host;
70
+ private port;
71
+ private database;
72
+ private client;
73
+ private buffer;
74
+ private _nextId;
75
+ private _pending;
76
+ private _maxPending;
77
+ private _maxQueue;
78
+ private _pendingQueue;
79
+ private _connected;
80
+ private _authenticating;
81
+ private token;
82
+ private _currentRetries;
83
+ private tokenExpiry;
84
+ private _destroyed;
85
+ private _reconnectTimer;
86
+ private signing;
87
+ private _stringify;
88
+ private ready;
89
+ private _resolveReadyFn;
90
+ private _rejectReadyFn;
91
+ private _connecting;
92
+ private _authPendingId;
93
+ /**
94
+ * Create a new client. Do NOT auto-connect in constructor — call connect()
95
+ * @param uri - Connection URI
96
+ * @param options - Client options
97
+ * @param options.secure - use TLS
98
+ * @param options.logger - { debug, info, warn, error }
99
+ * @param options.requestTimeout - Request timeout in ms
100
+ * @param options.retries - Reconnection retries
101
+ * @param options.retryDelay - Initial retry delay in ms
102
+ * @param options.tlsOptions - Passed to tls.connect when secure
103
+ */
104
+ constructor(uri: string, options?: ClientOptions);
105
+ _createReady(): void;
106
+ _resolveReady(value: any): void;
107
+ _rejectReady(err: Error): void;
108
+ /**
109
+ * Check if connected
110
+ */
111
+ get connected(): boolean;
112
+ /**
113
+ * Connect to server and authenticate. Returns a Promise that resolves when authenticated.
114
+ */
115
+ connect(): Promise<any>;
116
+ _setupListeners(): void;
117
+ _buildFinalBuffer(payloadBase: Message, id: number): Buffer<ArrayBuffer>;
118
+ _handleMessage(msg: Message): void;
119
+ authenticate(): Promise<any>;
120
+ _write(buf: any): Promise<unknown>;
121
+ _processQueue(): Promise<void>;
122
+ /**
123
+ * Execute a command. Supports concurrent requests via request id mapping.
124
+ * Returns a Promise resolved with response.data or rejected on ERROR/timeout.
125
+ */
126
+ execute(payload: Message): Promise<unknown>;
127
+ /**
128
+ * Attempt to refresh token using provided tokenRefresh option if token expired.
129
+ * options.tokenRefresh should be async function that returns { token, expiresAt }
130
+ */
131
+ _maybeRefreshToken(): Promise<boolean>;
132
+ destroy(): void;
133
+ _handleDisconnect(err: any): void;
134
+ close(): void;
135
+ /**
136
+ * Disconnect (alias for close)
137
+ */
138
+ disconnect(): void;
139
+ /**
140
+ * Send request helper (for public API methods)
141
+ */
142
+ sendRequest(action: Action, data?: any): Promise<any>;
143
+ /**
144
+ * Login with username and password
145
+ */
146
+ login(username: string, password: string): Promise<any>;
147
+ /**
148
+ * Logout from server
149
+ */
150
+ logout(): Promise<any>;
151
+ /**
152
+ * List all databases
153
+ */
154
+ listDatabases(): Promise<any>;
155
+ /**
156
+ * Create a new database
157
+ */
158
+ createDatabase(name: string): Promise<any>;
159
+ /**
160
+ * Remove a database
161
+ */
162
+ removeDatabase(name: string): Promise<any>;
163
+ /**
164
+ * Rename a database
165
+ */
166
+ renameDatabase(oldName: string, newName: string): Promise<any>;
167
+ /**
168
+ * Execute code in a database
169
+ */
170
+ run(code: string, databaseName: string): Promise<any>;
171
+ /**
172
+ * Save a database to disk
173
+ */
174
+ saveDatabase(databaseName: string): Promise<any>;
175
+ /**
176
+ * Update database metadata
177
+ */
178
+ updateDatabase(databaseName: string, data: any): Promise<any>;
179
+ /**
180
+ * Get server information
181
+ */
182
+ getInfo(): Promise<any>;
183
+ /**
184
+ * Execute shell command on server
185
+ */
186
+ executeShell(command: string): Promise<{
187
+ stdout: string;
188
+ stderr: string;
189
+ }>;
190
+ }
191
+
192
+ export { ScriptDBClient, ScriptDBClient as default };
@@ -0,0 +1,192 @@
1
+ import * as tls from 'tls';
2
+
3
+ type Action = 'script-code' | 'save-db' | 'remove-db' | 'rename-db' | 'login' | 'logout' | 'get-info' | 'list-dbs' | 'create-db' | 'update-db' | 'get-db' | 'shell-command' | 'string';
4
+ interface DataOptions {
5
+ code?: string | any;
6
+ databaseName?: string | any;
7
+ token?: string | any;
8
+ username?: string | any;
9
+ password?: string | any;
10
+ newName?: string | any;
11
+ }
12
+ interface Logger {
13
+ debug?: (...args: any[]) => void;
14
+ info?: (...args: any[]) => void;
15
+ warn?: (...args: any[]) => void;
16
+ error?: (...args: any[]) => void;
17
+ }
18
+ interface ClientOptions {
19
+ secure?: boolean;
20
+ logger?: Logger;
21
+ requestTimeout?: number;
22
+ socketTimeout?: number;
23
+ retries?: number;
24
+ retryDelay?: number;
25
+ tlsOptions?: tls.TlsOptions;
26
+ frame?: 'ndjson' | 'length-prefix';
27
+ preferLengthPrefix?: boolean;
28
+ maxPending?: number;
29
+ maxQueue?: number;
30
+ maxMessageSize?: number;
31
+ signing?: {
32
+ secret: string;
33
+ algorithm?: string;
34
+ };
35
+ stringify?: (obj: any) => string;
36
+ username?: string;
37
+ password?: string;
38
+ tokenRefresh?: () => Promise<{
39
+ token: string;
40
+ expiresAt?: number;
41
+ }>;
42
+ }
43
+ interface Message {
44
+ id?: number;
45
+ action?: Action;
46
+ command?: string;
47
+ message?: string;
48
+ data?: DataOptions;
49
+ token?: string;
50
+ signature?: string;
51
+ payload?: any;
52
+ }
53
+ declare class ScriptDBClient {
54
+ private options;
55
+ private socketTimeout;
56
+ private maxMessageSize;
57
+ private _mask;
58
+ private _maskArgs;
59
+ private logger?;
60
+ private secure;
61
+ private requestTimeout;
62
+ private retries;
63
+ private retryDelay;
64
+ private frame;
65
+ private uri;
66
+ private protocolName;
67
+ private username;
68
+ private password;
69
+ private host;
70
+ private port;
71
+ private database;
72
+ private client;
73
+ private buffer;
74
+ private _nextId;
75
+ private _pending;
76
+ private _maxPending;
77
+ private _maxQueue;
78
+ private _pendingQueue;
79
+ private _connected;
80
+ private _authenticating;
81
+ private token;
82
+ private _currentRetries;
83
+ private tokenExpiry;
84
+ private _destroyed;
85
+ private _reconnectTimer;
86
+ private signing;
87
+ private _stringify;
88
+ private ready;
89
+ private _resolveReadyFn;
90
+ private _rejectReadyFn;
91
+ private _connecting;
92
+ private _authPendingId;
93
+ /**
94
+ * Create a new client. Do NOT auto-connect in constructor — call connect()
95
+ * @param uri - Connection URI
96
+ * @param options - Client options
97
+ * @param options.secure - use TLS
98
+ * @param options.logger - { debug, info, warn, error }
99
+ * @param options.requestTimeout - Request timeout in ms
100
+ * @param options.retries - Reconnection retries
101
+ * @param options.retryDelay - Initial retry delay in ms
102
+ * @param options.tlsOptions - Passed to tls.connect when secure
103
+ */
104
+ constructor(uri: string, options?: ClientOptions);
105
+ _createReady(): void;
106
+ _resolveReady(value: any): void;
107
+ _rejectReady(err: Error): void;
108
+ /**
109
+ * Check if connected
110
+ */
111
+ get connected(): boolean;
112
+ /**
113
+ * Connect to server and authenticate. Returns a Promise that resolves when authenticated.
114
+ */
115
+ connect(): Promise<any>;
116
+ _setupListeners(): void;
117
+ _buildFinalBuffer(payloadBase: Message, id: number): Buffer<ArrayBuffer>;
118
+ _handleMessage(msg: Message): void;
119
+ authenticate(): Promise<any>;
120
+ _write(buf: any): Promise<unknown>;
121
+ _processQueue(): Promise<void>;
122
+ /**
123
+ * Execute a command. Supports concurrent requests via request id mapping.
124
+ * Returns a Promise resolved with response.data or rejected on ERROR/timeout.
125
+ */
126
+ execute(payload: Message): Promise<unknown>;
127
+ /**
128
+ * Attempt to refresh token using provided tokenRefresh option if token expired.
129
+ * options.tokenRefresh should be async function that returns { token, expiresAt }
130
+ */
131
+ _maybeRefreshToken(): Promise<boolean>;
132
+ destroy(): void;
133
+ _handleDisconnect(err: any): void;
134
+ close(): void;
135
+ /**
136
+ * Disconnect (alias for close)
137
+ */
138
+ disconnect(): void;
139
+ /**
140
+ * Send request helper (for public API methods)
141
+ */
142
+ sendRequest(action: Action, data?: any): Promise<any>;
143
+ /**
144
+ * Login with username and password
145
+ */
146
+ login(username: string, password: string): Promise<any>;
147
+ /**
148
+ * Logout from server
149
+ */
150
+ logout(): Promise<any>;
151
+ /**
152
+ * List all databases
153
+ */
154
+ listDatabases(): Promise<any>;
155
+ /**
156
+ * Create a new database
157
+ */
158
+ createDatabase(name: string): Promise<any>;
159
+ /**
160
+ * Remove a database
161
+ */
162
+ removeDatabase(name: string): Promise<any>;
163
+ /**
164
+ * Rename a database
165
+ */
166
+ renameDatabase(oldName: string, newName: string): Promise<any>;
167
+ /**
168
+ * Execute code in a database
169
+ */
170
+ run(code: string, databaseName: string): Promise<any>;
171
+ /**
172
+ * Save a database to disk
173
+ */
174
+ saveDatabase(databaseName: string): Promise<any>;
175
+ /**
176
+ * Update database metadata
177
+ */
178
+ updateDatabase(databaseName: string, data: any): Promise<any>;
179
+ /**
180
+ * Get server information
181
+ */
182
+ getInfo(): Promise<any>;
183
+ /**
184
+ * Execute shell command on server
185
+ */
186
+ executeShell(command: string): Promise<{
187
+ stdout: string;
188
+ stderr: string;
189
+ }>;
190
+ }
191
+
192
+ export { ScriptDBClient, ScriptDBClient as default };