get-db9 0.5.0 → 0.6.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.
@@ -22,9 +22,6 @@ interface HttpClient {
22
22
  /** Credential fields stored in `~/.db9/credentials` (TOML). */
23
23
  interface Credentials {
24
24
  token: string;
25
- is_anonymous?: boolean;
26
- anonymous_id?: string;
27
- anonymous_secret?: string;
28
25
  }
29
26
  /** Async credential persistence abstraction. */
30
27
  interface CredentialStore {
@@ -54,24 +51,163 @@ declare class MemoryCredentialStore implements CredentialStore {
54
51
  /** Returns a FileCredentialStore with the default path (`~/.db9/credentials`). */
55
52
  declare function defaultCredentialStore(): CredentialStore;
56
53
 
57
- /** A file or directory entry from fs9 readdir/stat API */
58
- interface Fs9FileEntry {
54
+ /** File or directory metadata returned by `stat` and `readdir`. */
55
+ interface FileInfo {
59
56
  path: string;
57
+ type: 'file' | 'dir';
60
58
  size: number;
61
- file_type: 'regular' | 'directory' | 'symlink';
62
59
  mode: number;
63
- uid: number;
64
- gid: number;
65
- atime: number;
66
- mtime: number;
67
- ctime: number;
68
- etag: string;
69
- symlink_target?: string;
70
- }
71
- /** Options for fs9 list operation */
72
- interface Fs9ListOptions {
60
+ mtime: string;
61
+ }
62
+ /** Options for listing directory contents. */
63
+ interface FsListOptions {
64
+ recursive?: boolean;
65
+ }
66
+ /** Options for removing files/directories. */
67
+ interface FsRemoveOptions {
73
68
  recursive?: boolean;
74
69
  }
70
+ /** Base shape for all WS requests. */
71
+ interface FsWsRequest {
72
+ id: string;
73
+ op: string;
74
+ [key: string]: unknown;
75
+ }
76
+ /** Successful WS response. */
77
+ interface FsWsResponse {
78
+ id: string;
79
+ ok: boolean;
80
+ data?: unknown;
81
+ error?: FsWsError;
82
+ }
83
+ /** Error detail from a failed WS response. */
84
+ interface FsWsError {
85
+ code: string;
86
+ message: string;
87
+ }
88
+ /** Auth response data. */
89
+ interface FsAuthInfo {
90
+ user: string;
91
+ tenant: string;
92
+ keyspace: string;
93
+ }
94
+ /** Options for connecting to the fs9 WebSocket server. */
95
+ interface FsConnectOptions {
96
+ /** WebSocket URL (e.g. `wss://host:5480`). */
97
+ wsUrl: string;
98
+ /** Username in `{tenant_id}.{admin_user}` format. */
99
+ username: string;
100
+ /** Admin password. */
101
+ password: string;
102
+ }
103
+ /** @deprecated Use `FileInfo` instead. */
104
+ type Fs9FileEntry = FileInfo;
105
+ /** @deprecated Use `FsListOptions` instead. */
106
+ type Fs9ListOptions = FsListOptions;
107
+ /** Server response when ready to accept streaming binary frames. */
108
+ interface StreamWriteReady {
109
+ ready: boolean;
110
+ stream_id: number;
111
+ chunk_size: number;
112
+ }
113
+
114
+ /**
115
+ * WebSocket client for the fs9 filesystem protocol.
116
+ *
117
+ * Mirrors db9-cli/src/fssh/client.rs — each public method maps to a single
118
+ * request–response round-trip over a JSON text WebSocket.
119
+ *
120
+ * For files >= 1 MB, writeFile() automatically switches to streaming mode
121
+ * using binary WebSocket frames to bypass the 2 MB JSON frame limit and
122
+ * eliminate base64 encoding overhead.
123
+ *
124
+ * Node-only: requires the `ws` package or Node 21+ native WebSocket.
125
+ */
126
+
127
+ /** Minimal WebSocket interface we depend on (compatible with `ws` package). */
128
+ interface WebSocketLike {
129
+ readonly readyState: number;
130
+ send(data: string | Buffer | Uint8Array): void;
131
+ close(code?: number, reason?: string): void;
132
+ onopen: ((ev: unknown) => void) | null;
133
+ onclose: ((ev: unknown) => void) | null;
134
+ onerror: ((ev: unknown) => void) | null;
135
+ onmessage: ((ev: {
136
+ data: unknown;
137
+ }) => void) | null;
138
+ }
139
+ /** Constructor for a W3C-compatible WebSocket. */
140
+ type WebSocketConstructor = new (url: string) => WebSocketLike;
141
+ /** Error from an fs9 WebSocket operation. */
142
+ declare class FsError extends Error {
143
+ readonly code: string;
144
+ constructor(code: string, message: string);
145
+ }
146
+ /**
147
+ * Async WebSocket client for fs9 file operations.
148
+ *
149
+ * Usage:
150
+ * ```ts
151
+ * const client = await FsClient.connect('wss://host:5480', WebSocket);
152
+ * await client.authenticate('tenant.admin', 'password');
153
+ * const entries = await client.readdir('/');
154
+ * await client.close();
155
+ * ```
156
+ */
157
+ declare class FsClient {
158
+ private ws;
159
+ private pending;
160
+ private closed;
161
+ private constructor();
162
+ /**
163
+ * Connect to an fs9 WebSocket server.
164
+ *
165
+ * @param url WebSocket URL, e.g. `wss://host:5480`
166
+ * @param WS WebSocket constructor (native or from `ws` package)
167
+ */
168
+ static connect(url: string, WS: WebSocketConstructor): Promise<FsClient>;
169
+ /** Authenticate with the server. Must be called first after connect. */
170
+ authenticate(username: string, password: string): Promise<FsAuthInfo>;
171
+ /** Get file or directory metadata. */
172
+ stat(path: string): Promise<FileInfo>;
173
+ /** List directory contents. */
174
+ readdir(path: string): Promise<FileInfo[]>;
175
+ /** Create a directory. Always recursive (mkdir -p). */
176
+ mkdir(path: string, recursive?: boolean): Promise<void>;
177
+ /** Read an entire file, returning raw bytes. */
178
+ readFile(path: string): Promise<Uint8Array>;
179
+ /**
180
+ * Write (overwrite) a file. Returns bytes written.
181
+ *
182
+ * Automatically uses streaming mode for files >= 1 MB to avoid base64
183
+ * overhead and bypass the 2 MB JSON frame limit.
184
+ */
185
+ writeFile(path: string, data: Uint8Array | ArrayBuffer | string): Promise<number>;
186
+ /** Append to a file. Returns bytes written. */
187
+ appendFile(path: string, data: Uint8Array | ArrayBuffer | string): Promise<number>;
188
+ /** Remove a file (non-recursive) or directory (recursive). */
189
+ rm(path: string, recursive?: boolean): Promise<void>;
190
+ /** Rename (move) a file or directory. */
191
+ rename(oldPath: string, newPath: string): Promise<void>;
192
+ /** Gracefully close the WebSocket connection. */
193
+ close(): Promise<void>;
194
+ /**
195
+ * Write a file using streaming mode (binary frames, no base64).
196
+ *
197
+ * Protocol:
198
+ * 1. Send streaming write request with file size
199
+ * 2. Receive ready response with stream_id and chunk_size
200
+ * 3. Send binary frames: [8-byte stream_id BE][chunk_data]
201
+ * 4. Send stream end with checksum
202
+ * 5. Receive final write confirmation
203
+ */
204
+ private writeFileStreaming;
205
+ /** Best-effort abort of an in-progress stream so the server can clean up. */
206
+ private tryAbortStream;
207
+ private sendAndRecv;
208
+ private expectOk;
209
+ private errorMessage;
210
+ }
75
211
 
76
212
  interface Endpoint {
77
213
  host: string;
@@ -260,14 +396,6 @@ interface TenantObservabilityResponse {
260
396
  summary: ObservabilitySummary;
261
397
  samples: QuerySample[];
262
398
  }
263
- interface RegisterRequest {
264
- email: string;
265
- password: string;
266
- }
267
- interface LoginRequest {
268
- email: string;
269
- password: string;
270
- }
271
399
  interface CreateDatabaseRequest {
272
400
  name: string;
273
401
  region?: string;
@@ -288,14 +416,6 @@ interface MigrationApplyRequest {
288
416
  interface BranchRequest {
289
417
  name: string;
290
418
  }
291
- interface ClaimRequest {
292
- email: string;
293
- password: string;
294
- }
295
- interface AnonymousRefreshRequest {
296
- anonymous_id: string;
297
- anonymous_secret: string;
298
- }
299
419
  interface CreateUserRequest {
300
420
  username: string;
301
421
  password: string;
@@ -310,34 +430,11 @@ interface CustomerResponse {
310
430
  created_at: string;
311
431
  status: string;
312
432
  }
313
- interface LoginResponse {
314
- token: string;
315
- expires_at: string;
316
- }
317
- interface AnonymousRegisterResponse {
318
- token: string;
319
- expires_at: string;
320
- is_anonymous: boolean;
321
- anonymous_id: string;
322
- anonymous_secret: string;
323
- }
324
- interface AnonymousRefreshResponse {
325
- token: string;
326
- expires_at: string;
327
- }
328
- interface AnonymousSecretResponse {
329
- anonymous_id: string;
330
- anonymous_secret: string;
331
- }
332
- interface ClaimResponse {
333
- id: string;
334
- email: string;
335
- claimed: boolean;
336
- }
337
433
  interface DatabaseResponse {
338
434
  id: string;
339
435
  name: string;
340
436
  state: string;
437
+ parent_database_id?: string;
341
438
  region?: string;
342
439
  endpoints?: Endpoint[];
343
440
  admin_user?: string;
@@ -396,6 +493,7 @@ interface MigrationMetadata {
396
493
  applied_at: string;
397
494
  sql_preview: string;
398
495
  }
496
+ /** @deprecated Fs9 events are not available in the WebSocket protocol. */
399
497
  interface Fs9EventEntry {
400
498
  id: string;
401
499
  type: string;
@@ -405,6 +503,7 @@ interface Fs9EventEntry {
405
503
  size?: number;
406
504
  metadata?: Record<string, unknown>;
407
505
  }
506
+ /** @deprecated Fs9 events are not available in the WebSocket protocol. */
408
507
  interface Fs9EventOptions {
409
508
  limit?: number;
410
509
  offset?: number;
@@ -421,17 +520,14 @@ interface Db9ClientOptions {
421
520
  timeout?: number;
422
521
  maxRetries?: number;
423
522
  retryDelay?: number;
523
+ /** WebSocket constructor for fs operations (native WebSocket, or `ws` package for Node 18–20). */
524
+ WebSocket?: WebSocketConstructor;
525
+ /** WebSocket port for fs9 server (default: 5480). */
526
+ wsPort?: number;
424
527
  }
425
528
  declare function createDb9Client(options?: Db9ClientOptions): {
426
529
  auth: {
427
- register: (req: RegisterRequest) => Promise<CustomerResponse>;
428
- login: (req: LoginRequest) => Promise<LoginResponse>;
429
- anonymousRegister: () => Promise<AnonymousRegisterResponse>;
430
- anonymousRefresh: (req: AnonymousRefreshRequest) => Promise<AnonymousRefreshResponse>;
431
530
  me: () => Promise<CustomerResponse>;
432
- getAnonymousSecret: () => Promise<AnonymousSecretResponse>;
433
- claim: (req: ClaimRequest) => Promise<ClaimResponse>;
434
- ensureAnonymousSecret: () => Promise<void>;
435
531
  };
436
532
  tokens: {
437
533
  list: () => Promise<TokenResponse[]>;
@@ -444,6 +540,7 @@ declare function createDb9Client(options?: Db9ClientOptions): {
444
540
  get: (databaseId: string) => Promise<DatabaseResponse>;
445
541
  delete: (databaseId: string) => Promise<MessageResponse>;
446
542
  resetPassword: (databaseId: string) => Promise<CustomerPasswordResetResponse>;
543
+ credentials: (databaseId: string) => Promise<CustomerPasswordResetResponse>;
447
544
  observability: (databaseId: string) => Promise<TenantObservabilityResponse>;
448
545
  sql: (databaseId: string, query: string) => Promise<SqlResult>;
449
546
  sqlFile: (databaseId: string, fileContent: string) => Promise<SqlResult>;
@@ -459,17 +556,35 @@ declare function createDb9Client(options?: Db9ClientOptions): {
459
556
  };
460
557
  };
461
558
  fs: {
462
- list: (dbId: string, path: string, options?: Fs9ListOptions) => Promise<Fs9FileEntry[]>;
559
+ /**
560
+ * Open a persistent WebSocket connection for multiple fs operations.
561
+ * Caller is responsible for calling `client.close()` when done.
562
+ */
563
+ connect: (dbId: string) => Promise<FsClient>;
564
+ /** List directory contents. */
565
+ list: (dbId: string, path: string) => Promise<FileInfo[]>;
566
+ /** Read a file as text (UTF-8). */
463
567
  read: (dbId: string, path: string) => Promise<string>;
464
- readBinary: (dbId: string, path: string) => Promise<ArrayBuffer>;
465
- write: (dbId: string, path: string, content: string | ArrayBuffer | Uint8Array | Blob) => Promise<void>;
466
- stat: (dbId: string, path: string) => Promise<Fs9FileEntry>;
568
+ /** Read a file as raw bytes. */
569
+ readBinary: (dbId: string, path: string) => Promise<Uint8Array>;
570
+ /** Write (overwrite) a file. Accepts string, ArrayBuffer, or Uint8Array. */
571
+ write: (dbId: string, path: string, content: string | ArrayBuffer | Uint8Array) => Promise<void>;
572
+ /** Append to a file. Returns bytes written. */
573
+ append: (dbId: string, path: string, content: string | ArrayBuffer | Uint8Array) => Promise<number>;
574
+ /** Get file or directory metadata. */
575
+ stat: (dbId: string, path: string) => Promise<FileInfo>;
576
+ /** Check if a file or directory exists. */
467
577
  exists: (dbId: string, path: string) => Promise<boolean>;
578
+ /** Create a directory (recursive by default). */
468
579
  mkdir: (dbId: string, path: string) => Promise<void>;
469
- remove: (dbId: string, path: string) => Promise<void>;
470
- events: (dbId: string, options?: Fs9EventOptions) => Promise<Fs9EventEntry[]>;
580
+ /** Remove a file or directory. */
581
+ remove: (dbId: string, path: string, opts?: {
582
+ recursive?: boolean;
583
+ }) => Promise<void>;
584
+ /** Rename (move) a file or directory. */
585
+ rename: (dbId: string, oldPath: string, newPath: string) => Promise<void>;
471
586
  };
472
587
  };
473
588
  type Db9Client = ReturnType<typeof createDb9Client>;
474
589
 
475
- export { type RegisterRequest as $, type AdminCreateUserRequest as A, type BatchCreateRequest as B, type CredentialStore as C, type DatabaseResponse as D, type Db9Client as E, type FetchFn as F, type Db9ClientOptions as G, type DumpRequest as H, type DumpResponse as I, type Endpoint as J, FileCredentialStore as K, type Fs9EventEntry as L, type Fs9EventOptions as M, type HealthResponse as N, type HttpClient as O, type HttpClientOptions as P, type ListTenantsParams as Q, type LoginRequest as R, type LoginResponse as S, MemoryCredentialStore as T, type MessageResponse as U, type MigrationApplyRequest as V, type MigrationApplyResponse as W, type MigrationMetadata as X, type ObservabilitySummary as Y, type PasswordResetResponse as Z, type QuerySample as _, type AnonymousRefreshRequest as a, type SchemaResponse as a0, type SqlErrorDetail as a1, type SqlExecuteRequest as a2, type SqlQueryRequest as a3, type SqlQueryResponse as a4, type SqlResult as a5, type TableMetadata as a6, type TenantConnectRequest as a7, type TenantConnectResponse as a8, type TenantListResponse as a9, type TenantObservabilityResponse as aa, type TenantResponse as ab, type TenantState as ac, type TenantUpdateRequest as ad, type TokenResponse as ae, type UserCreateResponse as af, type UserResponse as ag, type ViewMetadata as ah, createDb9Client as ai, defaultCredentialStore as aj, type AnonymousRefreshResponse as b, type AnonymousRegisterResponse as c, type AnonymousSecretResponse as d, type AuditLogParams as e, type AuditLogResponse as f, type BatchCreateResponse as g, type BatchDeleteRequest as h, type BatchDeleteResponse as i, type BatchItemError as j, type BatchUpdateRequest as k, type BatchUpdateResponse as l, type BranchRequest as m, type ClaimRequest as n, type ClaimResponse as o, type ColumnInfo as p, type ColumnMetadata as q, type CreateDatabaseRequest as r, type CreateTenantRequest as s, type CreateTenantResponse as t, type CreateTokenRequest as u, type CreateTokenResponse as v, type CreateUserRequest as w, type Credentials as x, type CustomerPasswordResetResponse as y, type CustomerResponse as z };
590
+ export { type MigrationMetadata as $, type AdminCreateUserRequest as A, type BatchCreateRequest as B, type CredentialStore as C, type DatabaseResponse as D, type Endpoint as E, type FetchFn as F, type Fs9EventEntry as G, type Fs9EventOptions as H, type Fs9FileEntry as I, type Fs9ListOptions as J, type FsAuthInfo as K, FsClient as L, type FsConnectOptions as M, FsError as N, type FsListOptions as O, type FsRemoveOptions as P, type FsWsError as Q, type FsWsRequest as R, type FsWsResponse as S, type HealthResponse as T, type HttpClient as U, type HttpClientOptions as V, type ListTenantsParams as W, MemoryCredentialStore as X, type MessageResponse as Y, type MigrationApplyRequest as Z, type MigrationApplyResponse as _, type AuditLogParams as a, type ObservabilitySummary as a0, type PasswordResetResponse as a1, type QuerySample as a2, type SchemaResponse as a3, type SqlErrorDetail as a4, type SqlExecuteRequest as a5, type SqlQueryRequest as a6, type SqlQueryResponse as a7, type SqlResult as a8, type StreamWriteReady as a9, type TableMetadata as aa, type TenantConnectRequest as ab, type TenantConnectResponse as ac, type TenantListResponse as ad, type TenantObservabilityResponse as ae, type TenantResponse as af, type TenantState as ag, type TenantUpdateRequest as ah, type TokenResponse as ai, type UserCreateResponse as aj, type UserResponse as ak, type ViewMetadata as al, type WebSocketConstructor as am, type WebSocketLike as an, createDb9Client as ao, defaultCredentialStore as ap, type AuditLogResponse as b, type BatchCreateResponse as c, type BatchDeleteRequest as d, type BatchDeleteResponse as e, type BatchItemError as f, type BatchUpdateRequest as g, type BatchUpdateResponse as h, type BranchRequest as i, type ColumnInfo as j, type ColumnMetadata as k, type CreateDatabaseRequest as l, type CreateTenantRequest as m, type CreateTenantResponse as n, type CreateTokenRequest as o, type CreateTokenResponse as p, type CreateUserRequest as q, type Credentials as r, type CustomerPasswordResetResponse as s, type CustomerResponse as t, type Db9Client as u, type Db9ClientOptions as v, type DumpRequest as w, type DumpResponse as x, FileCredentialStore as y, type FileInfo as z };