@reddb-io/browser 0.5.0 → 0.6.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 +1 -1
- package/package.json +1 -1
- package/src/index.d.ts +8 -0
- package/src/index.js +61 -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.6.0` adds local database sessions for Dart/Flutter Web and retains reusable Worker sessions. 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
|
package/package.json
CHANGED
package/src/index.d.ts
CHANGED
|
@@ -103,3 +103,11 @@ export function restoreDatabase(options: {
|
|
|
103
103
|
backup: Uint8Array
|
|
104
104
|
worker?: Worker
|
|
105
105
|
}): Promise<Database>
|
|
106
|
+
|
|
107
|
+
/** JSON-safe, token-fenced session operations for JS-interop consumers. */
|
|
108
|
+
export interface DatabaseSessionBridge {
|
|
109
|
+
/** ready, openDatabase, info, query, closeDatabase, close. Failures resolve as envelopes. */
|
|
110
|
+
invoke(method: string, params?: Record<string, unknown>): Promise<InteropResult>
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
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
|