@reddb-io/browser 0.5.0 → 0.7.0
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 +20 -1
- package/package.json +1 -1
- package/src/index.d.ts +13 -0
- package/src/index.js +66 -0
- package/src/wasm/reddb_wasm.d.ts +4 -0
- package/src/wasm/reddb_wasm.js +29 -0
- package/src/wasm/reddb_wasm_bg.wasm +0 -0
- package/src/wasm/reddb_wasm_bg.wasm.d.ts +2 -0
- package/src/worker.js +9 -0
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ RedDB server, account, CDN or external API. Applications bundle the JS, worker
|
|
|
12
12
|
and WASM assets and make them available locally or through their own offline
|
|
13
13
|
application cache. Sync is optional and must never gate local reads or writes.
|
|
14
14
|
|
|
15
|
-
Version `0.
|
|
15
|
+
Version `0.7.0` adds persisted catalog inspection and read-only, host-scoped SELECT, retaining local Dart/Flutter Web database sessions and reusable Workers. Native `.rdb` backup export and restore remain available. It exposes local table SELECT, CREATE TABLE, INSERT, UPDATE, DELETE
|
|
16
16
|
and explicit BEGIN/COMMIT/ROLLBACK transactions through `openDatabase`.
|
|
17
17
|
Other models, synchronization, conflict
|
|
18
18
|
resolution and forwarding between tabs remain pending. The browser package
|
|
@@ -504,3 +504,22 @@ internal fixtures remain separate from application entrypoints.
|
|
|
504
504
|
pending intent, identity separation, offline assets and the storage-status UI.
|
|
505
505
|
5. Validate two-device sync and engine conformance, Safari/iOS on real devices,
|
|
506
506
|
quota/failure cases, bundle size, startup, memory and query performance.
|
|
507
|
+
|
|
508
|
+
## Embedded explorers and scoped reads
|
|
509
|
+
|
|
510
|
+
`database.catalog()` returns the persisted columns `table_name`, `column_name`,
|
|
511
|
+
`data_type` and `nullable`. It reads real contracts, including empty tables.
|
|
512
|
+
|
|
513
|
+
```ts
|
|
514
|
+
const scopes = [{ table: 'files', field: 'tenant_id', value: 'campaign-1' }]
|
|
515
|
+
const result = await database.readQuery('SELECT name, body FROM files', [], scopes)
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
The host supplies the allowed table and equality predicate. The canonical engine
|
|
519
|
+
filters source records before the caller's WHERE, ordering and limit. Only SELECT
|
|
520
|
+
is admitted; SQL cannot remove this filter or address an unlisted table. Empty
|
|
521
|
+
scopes deny all tables. This is a local inspection boundary, not a server user
|
|
522
|
+
session or RLS. The browser's file owner can still use the ordinary full SQL API.
|
|
523
|
+
Catalogs and scoped reads honor the same I/O health fence; an aborted explicit
|
|
524
|
+
transaction must roll back first. Source filtering uses canonical record
|
|
525
|
+
materialization and may temporarily allocate one record for each candidate.
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export interface RuntimeInfo {
|
|
|
16
16
|
readonly transactions: boolean
|
|
17
17
|
readonly queryParameters?: boolean
|
|
18
18
|
readonly databaseBackups?: boolean
|
|
19
|
+
readonly databaseInspection?: boolean
|
|
19
20
|
readonly sync: boolean
|
|
20
21
|
}
|
|
21
22
|
}
|
|
@@ -70,6 +71,10 @@ export function openArtifact(options: {
|
|
|
70
71
|
export type QueryParameter = null | boolean | number | string | bigint
|
|
71
72
|
|
|
72
73
|
export interface Database extends Runtime {
|
|
74
|
+
/** Persisted catalog columns: table_name, column_name, data_type, nullable. */
|
|
75
|
+
catalog(): Promise<{ columns: string[]; rows: unknown[][] }>
|
|
76
|
+
/** SELECT only, over the host's allowed tables and equality-filtered source rows. */
|
|
77
|
+
readQuery(sql: string, params: readonly QueryParameter[], scopes: readonly {table: string; field: string; value: QueryParameter}[]): Promise<{ columns: string[]; rows: unknown[][] }>
|
|
73
78
|
/** SELECT and table SQL mutations; mutations return an affectedRows column. */
|
|
74
79
|
query(sql: string, params?: readonly QueryParameter[]): Promise<{ columns: string[]; rows: unknown[][] }>
|
|
75
80
|
/** Complete native .rdb image of committed state, at most 256 MiB. Pending SQL changes are excluded. */
|
|
@@ -103,3 +108,11 @@ export function restoreDatabase(options: {
|
|
|
103
108
|
backup: Uint8Array
|
|
104
109
|
worker?: Worker
|
|
105
110
|
}): Promise<Database>
|
|
111
|
+
|
|
112
|
+
/** JSON-safe, token-fenced session operations for JS-interop consumers. */
|
|
113
|
+
export interface DatabaseSessionBridge {
|
|
114
|
+
/** ready, openDatabase, info, query, closeDatabase, close. Failures resolve as envelopes. */
|
|
115
|
+
invoke(method: string, params?: Record<string, unknown>): Promise<InteropResult>
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function createInteropDatabaseSession(options?: { worker?: Worker }): DatabaseSessionBridge
|
package/src/index.js
CHANGED
|
@@ -231,6 +231,67 @@ export async function createDatabaseSession({ worker } = {}) {
|
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
+
/** JSON-safe session boundary for Dart and other JS-interop hosts. */
|
|
235
|
+
export function createInteropDatabaseSession(options = {}) {
|
|
236
|
+
let ready
|
|
237
|
+
try { ready = createDatabaseSession(options) }
|
|
238
|
+
catch (error) { ready = Promise.reject(error) }
|
|
239
|
+
ready.catch(() => {})
|
|
240
|
+
let database
|
|
241
|
+
let handle = 0
|
|
242
|
+
let fileClosing
|
|
243
|
+
let closed = false
|
|
244
|
+
return {
|
|
245
|
+
async invoke(method, params = {}) {
|
|
246
|
+
try {
|
|
247
|
+
if (method === 'close' || (method === 'closeDatabase' && closed)) {
|
|
248
|
+
closed = true
|
|
249
|
+
await (await ready).close()
|
|
250
|
+
return { ok: true, result: null }
|
|
251
|
+
}
|
|
252
|
+
if (closed) throw new BrowserError('CLOSED', 'This database session is closed')
|
|
253
|
+
const session = await ready
|
|
254
|
+
if (closed) throw new BrowserError('CLOSED', 'This database session is closed')
|
|
255
|
+
if (!params || typeof params !== 'object' || Array.isArray(params)) {
|
|
256
|
+
throw new BrowserError('INVALID_ARGUMENT', 'Expected an argument object')
|
|
257
|
+
}
|
|
258
|
+
let result
|
|
259
|
+
switch (method) {
|
|
260
|
+
case 'ready': break
|
|
261
|
+
case 'openDatabase':
|
|
262
|
+
database = await session.openDatabase({ name: params.name, create: params.create ?? false })
|
|
263
|
+
fileClosing = undefined
|
|
264
|
+
result = ++handle
|
|
265
|
+
break
|
|
266
|
+
case 'closeDatabase':
|
|
267
|
+
// A repeated close of an old handle cannot release the current owner.
|
|
268
|
+
if (params.handle === handle) {
|
|
269
|
+
if (database) {
|
|
270
|
+
const releasing = database
|
|
271
|
+
database = undefined
|
|
272
|
+
fileClosing = releasing.close()
|
|
273
|
+
}
|
|
274
|
+
await fileClosing
|
|
275
|
+
}
|
|
276
|
+
break
|
|
277
|
+
case 'info':
|
|
278
|
+
case 'query':
|
|
279
|
+
if (!database || params.handle !== handle) throw new BrowserError('CLOSED', 'This database handle is closed')
|
|
280
|
+
result = method === 'info' ? await database.info() : await database.query(params.sql, params.params)
|
|
281
|
+
break
|
|
282
|
+
default: throw new BrowserError('INVALID_ARGUMENT', 'Unknown database operation')
|
|
283
|
+
}
|
|
284
|
+
return { ok: true, result: result ?? null }
|
|
285
|
+
} catch (error) {
|
|
286
|
+
return { ok: false, error: {
|
|
287
|
+
code: typeof error.code === 'string' ? error.code : 'RUNTIME_ERROR',
|
|
288
|
+
message: error.message || String(error),
|
|
289
|
+
} }
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
234
295
|
/** Restore a native .rdb backup to a new, exclusively owned OPFS filename. */
|
|
235
296
|
export async function restoreDatabase(options) {
|
|
236
297
|
const backup = options?.backup
|
|
@@ -243,6 +304,11 @@ export async function restoreDatabase(options) {
|
|
|
243
304
|
function databaseMethods(client) {
|
|
244
305
|
return {
|
|
245
306
|
...runtimeMethods(client),
|
|
307
|
+
catalog: () => client.call('catalog'),
|
|
308
|
+
readQuery(sql, params, scopes) {
|
|
309
|
+
if (typeof sql !== 'string' || !Array.isArray(params) || params.length > MAX_QUERY_PARAMETERS || !Array.isArray(scopes) || scopes.length > 64) return Promise.reject(new BrowserError('INVALID_ARGUMENT', 'Expected SQL, parameters and at most 64 inspection scopes'))
|
|
310
|
+
return client.call('readQuery', { sql, params, scopes })
|
|
311
|
+
},
|
|
246
312
|
query(sql, params) {
|
|
247
313
|
if (typeof sql !== 'string') return Promise.reject(new BrowserError('INVALID_ARGUMENT', 'Expected an RQL string'))
|
|
248
314
|
if (params === undefined) return client.call('query', { sql })
|
package/src/wasm/reddb_wasm.d.ts
CHANGED
|
@@ -29,11 +29,13 @@ export class BrowserDatabase {
|
|
|
29
29
|
private constructor();
|
|
30
30
|
free(): void;
|
|
31
31
|
[Symbol.dispose](): void;
|
|
32
|
+
catalog(): any;
|
|
32
33
|
checkHealthy(): void;
|
|
33
34
|
static create(handle: any, name: string, now_ms: number): BrowserDatabase;
|
|
34
35
|
static open(handle: any, name: string): BrowserDatabase;
|
|
35
36
|
query(sql: string, now_ms: number): any;
|
|
36
37
|
queryWithParams(sql: string, params: Array<any>, now_ms: number): any;
|
|
38
|
+
readQuery(sql: string, params: Array<any>, scopes: Array<any>): any;
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
/**
|
|
@@ -61,11 +63,13 @@ export interface InitOutput {
|
|
|
61
63
|
readonly browserartifact_open: (a: any, b: number, c: number) => [number, number, number];
|
|
62
64
|
readonly browserartifact_readSnapshot: (a: number) => [number, number, number, number];
|
|
63
65
|
readonly browserartifact_readWal: (a: number) => [number, number, number];
|
|
66
|
+
readonly browserdatabase_catalog: (a: number) => [number, number, number];
|
|
64
67
|
readonly browserdatabase_checkHealthy: (a: number) => [number, number];
|
|
65
68
|
readonly browserdatabase_create: (a: any, b: number, c: number, d: number) => [number, number, number];
|
|
66
69
|
readonly browserdatabase_open: (a: any, b: number, c: number) => [number, number, number];
|
|
67
70
|
readonly browserdatabase_query: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
68
71
|
readonly browserdatabase_queryWithParams: (a: number, b: number, c: number, d: any, e: number) => [number, number, number];
|
|
72
|
+
readonly browserdatabase_readQuery: (a: number, b: number, c: number, d: any, e: any) => [number, number, number];
|
|
69
73
|
readonly executeScalar: (a: number, b: number) => [number, number, number];
|
|
70
74
|
readonly runtimeInfo: () => [number, number, number];
|
|
71
75
|
readonly validateQuery: (a: number, b: number) => [number, number];
|
package/src/wasm/reddb_wasm.js
CHANGED
|
@@ -142,6 +142,16 @@ export class BrowserDatabase {
|
|
|
142
142
|
const ptr = this.__destroy_into_raw();
|
|
143
143
|
wasm.__wbg_browserdatabase_free(ptr, 0);
|
|
144
144
|
}
|
|
145
|
+
/**
|
|
146
|
+
* @returns {any}
|
|
147
|
+
*/
|
|
148
|
+
catalog() {
|
|
149
|
+
const ret = wasm.browserdatabase_catalog(this.__wbg_ptr);
|
|
150
|
+
if (ret[2]) {
|
|
151
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
152
|
+
}
|
|
153
|
+
return takeFromExternrefTable0(ret[0]);
|
|
154
|
+
}
|
|
145
155
|
checkHealthy() {
|
|
146
156
|
const ret = wasm.browserdatabase_checkHealthy(this.__wbg_ptr);
|
|
147
157
|
if (ret[1]) {
|
|
@@ -206,6 +216,21 @@ export class BrowserDatabase {
|
|
|
206
216
|
}
|
|
207
217
|
return takeFromExternrefTable0(ret[0]);
|
|
208
218
|
}
|
|
219
|
+
/**
|
|
220
|
+
* @param {string} sql
|
|
221
|
+
* @param {Array<any>} params
|
|
222
|
+
* @param {Array<any>} scopes
|
|
223
|
+
* @returns {any}
|
|
224
|
+
*/
|
|
225
|
+
readQuery(sql, params, scopes) {
|
|
226
|
+
const ptr0 = passStringToWasm0(sql, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
227
|
+
const len0 = WASM_VECTOR_LEN;
|
|
228
|
+
const ret = wasm.browserdatabase_readQuery(this.__wbg_ptr, ptr0, len0, params, scopes);
|
|
229
|
+
if (ret[2]) {
|
|
230
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
231
|
+
}
|
|
232
|
+
return takeFromExternrefTable0(ret[0]);
|
|
233
|
+
}
|
|
209
234
|
}
|
|
210
235
|
if (Symbol.dispose) BrowserDatabase.prototype[Symbol.dispose] = BrowserDatabase.prototype.free;
|
|
211
236
|
|
|
@@ -287,6 +312,10 @@ function __wbg_get_imports() {
|
|
|
287
312
|
const ret = arg0.getSize();
|
|
288
313
|
return ret;
|
|
289
314
|
}, arguments); },
|
|
315
|
+
__wbg_get_41476db20fef99a8: function() { return handleError(function (arg0, arg1) {
|
|
316
|
+
const ret = Reflect.get(arg0, arg1);
|
|
317
|
+
return ret;
|
|
318
|
+
}, arguments); },
|
|
290
319
|
__wbg_get_unchecked_be562b1421656321: function(arg0, arg1) {
|
|
291
320
|
const ret = arg0[arg1 >>> 0];
|
|
292
321
|
return ret;
|
|
Binary file
|
|
@@ -10,11 +10,13 @@ export const browserartifact_generation: (a: number) => [number, number, number,
|
|
|
10
10
|
export const browserartifact_open: (a: any, b: number, c: number) => [number, number, number];
|
|
11
11
|
export const browserartifact_readSnapshot: (a: number) => [number, number, number, number];
|
|
12
12
|
export const browserartifact_readWal: (a: number) => [number, number, number];
|
|
13
|
+
export const browserdatabase_catalog: (a: number) => [number, number, number];
|
|
13
14
|
export const browserdatabase_checkHealthy: (a: number) => [number, number];
|
|
14
15
|
export const browserdatabase_create: (a: any, b: number, c: number, d: number) => [number, number, number];
|
|
15
16
|
export const browserdatabase_open: (a: any, b: number, c: number) => [number, number, number];
|
|
16
17
|
export const browserdatabase_query: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
17
18
|
export const browserdatabase_queryWithParams: (a: number, b: number, c: number, d: any, e: number) => [number, number, number];
|
|
19
|
+
export const browserdatabase_readQuery: (a: number, b: number, c: number, d: any, e: any) => [number, number, number];
|
|
18
20
|
export const executeScalar: (a: number, b: number) => [number, number, number];
|
|
19
21
|
export const runtimeInfo: () => [number, number, number];
|
|
20
22
|
export const validateQuery: (a: number, b: number) => [number, number];
|
package/src/worker.js
CHANGED
|
@@ -163,6 +163,15 @@ async function execute(method, params) {
|
|
|
163
163
|
}
|
|
164
164
|
if (!artifact && !database) throw error('CLOSED', 'The file is not open')
|
|
165
165
|
if (database) {
|
|
166
|
+
if (method === 'catalog') {
|
|
167
|
+
if (typeof database.catalog !== 'function') throw error('VERSION_MISMATCH', 'WASM catalog inspection is missing')
|
|
168
|
+
return database.catalog()
|
|
169
|
+
}
|
|
170
|
+
if (method === 'readQuery') {
|
|
171
|
+
if (typeof params.sql !== 'string' || !Array.isArray(params.params) || params.params.length > MAX_QUERY_PARAMETERS || !Array.isArray(params.scopes) || params.scopes.length > 64) throw error('INVALID_ARGUMENT', 'Expected SQL, parameters and at most 64 inspection scopes')
|
|
172
|
+
if (typeof database.readQuery !== 'function') throw error('VERSION_MISMATCH', 'WASM scoped reads are missing')
|
|
173
|
+
return database.readQuery(params.sql, params.params, params.scopes)
|
|
174
|
+
}
|
|
166
175
|
if (method === 'exportBackup') {
|
|
167
176
|
try { return exportBackup() }
|
|
168
177
|
catch (failure) {
|