@hulla/db-sqlite 0.3.2 → 0.3.3

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
@@ -19,6 +19,8 @@ an application glob compiled by [`@hulla/db/build`](https://github.com/hulladev/
19
19
  combine shared/app handlers using ordinary exports.
20
20
 
21
21
  The browser and Expo entrypoints accept `open`, which supplies a dedicated driver.
22
+ Expo can use the optional `expo-crypto` peer for secure UUID generation when
23
+ Web Crypto is unavailable.
22
24
  Readonly replicas require explicit source URL bindings. Offline mutations queue
23
25
  server calls with mandatory device-side conflict callbacks and optional optimistic
24
26
  feedback. Native Rust SQLite storage and SQLite managed HTTP hosting remain work.
package/dist/browser.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type { BrowserSQLiteConnection } from "./descriptor.ts";
2
1
  export { wasmSQLiteDriver } from "./driver.ts";
3
2
  export type { SQLiteWasmConnection, SQLiteDriver } from "./driver.ts";
4
3
  export type { BrowserSQLiteConnection as SQLiteConnection } from "./descriptor.ts";
5
- export declare const sqlite: import("./descriptor.ts").SQLiteFactory<BrowserSQLiteConnection>;
4
+ export declare const sqlite: import("./descriptor.ts").SQLiteFactory<import("./descriptor.ts").BrowserSQLiteConnection>;
package/dist/browser.js CHANGED
@@ -1,9 +1,3 @@
1
- import { sqliteFactory } from "./descriptor.js";
2
- import { openSQLiteDatabase } from "./runtime.js";
1
+ import { portableSQLite } from "./portable.js";
3
2
  export { wasmSQLiteDriver } from "./driver.js";
4
- export const sqlite = sqliteFactory(async (schema, settings, initialize) => {
5
- if (!settings || typeof settings !== "object" || Reflect.ownKeys(settings).some(key => !["open", "randomBytes", "transactionTimeoutMs"].includes(key))
6
- || settings.filename !== undefined || typeof settings.open !== "function")
7
- throw new TypeError("Browser SQLite requires open: () => a dedicated SQLite driver");
8
- return openSQLiteDatabase(schema, await settings.open(), settings, initialize);
9
- });
3
+ export const sqlite = portableSQLite();
@@ -1,11 +1,13 @@
1
1
  import type { Database, DatabaseEngine, SchemaDefinition, ConnectedDatabase, SourceConnection } from "@hulla/db";
2
2
  import { type DatabaseOptions } from "@hulla/db/engine";
3
3
  import type { FunctionSource } from "@hulla/db";
4
- import type { SourcesOf } from "@hulla/db/internal/engine";
4
+ import { type SourcesOf } from "@hulla/db/internal/engine";
5
5
  import type { SQLiteDriver } from "./driver.ts";
6
6
  interface SQLiteSettings {
7
7
  readonly randomBytes?: (length: number) => Uint8Array;
8
8
  readonly transactionTimeoutMs?: number;
9
+ /** Override which driver errors get one startup retry, or disable it. */
10
+ readonly startupRetry?: false | ((error: unknown) => boolean);
9
11
  }
10
12
  /** Each instance owns a fresh dedicated driver connection. */
11
13
  export type BrowserSQLiteConnection = SQLiteSettings & {
@@ -1,9 +1,41 @@
1
1
  import { createDatabase, registerDatabaseFactory } from "@hulla/db/engine";
2
+ import { DatabaseError } from "@hulla/db/internal/engine";
3
+ function isLockResult(code) {
4
+ if (typeof code !== "number" || !Number.isInteger(code) || code < 0 || code > 0x7fff_ffff)
5
+ return false;
6
+ const primary = code & 0xff;
7
+ return primary === 5 || primary === 6;
8
+ }
9
+ function isLockError(error) {
10
+ if (!(error instanceof Error))
11
+ return false;
12
+ const { code, errcode, errno, resultCode } = error;
13
+ if (typeof code === "string" && /^SQLITE_(?:BUSY|LOCKED)(?:_|$)/.test(code))
14
+ return true;
15
+ if (isLockResult(errcode ?? errno ?? resultCode))
16
+ return true;
17
+ // Expo reports SQLite's result code in the message of its native error.
18
+ if (code !== "ERR_INTERNAL_SQLITE_ERROR")
19
+ return false;
20
+ const result = /\bError code (\d+)\b/.exec(error.message);
21
+ return result !== null && isLockResult(Number(result[1]));
22
+ }
2
23
  export function sqliteDescriptor(connect) {
24
+ const open = (schema, settings, initialize) => {
25
+ const policy = settings?.startupRetry;
26
+ if (policy !== undefined && policy !== false && typeof policy !== "function")
27
+ throw new TypeError("SQLite startupRetry must be false or a function");
28
+ return connect(schema, settings, initialize);
29
+ };
3
30
  return Object.freeze({
4
31
  name: "sqlite", replicaStorage: true, serialTransactions: true,
5
- connect: (schema, settings) => connect(schema, settings, false),
6
- initialize: (schema, settings) => connect(schema, settings, true),
32
+ connect: (schema, settings) => open(schema, settings, false),
33
+ initialize: (schema, settings) => open(schema, settings, true),
34
+ retryStartup(error, settings) {
35
+ if (error instanceof DatabaseError || settings?.startupRetry === false)
36
+ return false;
37
+ return typeof settings?.startupRetry === "function" ? settings.startupRetry(error) === true : isLockError(error);
38
+ },
7
39
  });
8
40
  }
9
41
  export function sqliteFactory(connect) {
package/dist/expo.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { sqlite } from "./browser.ts";
2
1
  export { expoSQLiteDriver } from "./driver.ts";
3
2
  export type { ExpoSQLiteConnection, SQLiteDriver } from "./driver.ts";
4
3
  export type { BrowserSQLiteConnection as SQLiteConnection } from "./descriptor.ts";
4
+ export declare const sqlite: import("./descriptor.ts").SQLiteFactory<import("./descriptor.ts").BrowserSQLiteConnection>;
package/dist/expo.js CHANGED
@@ -1,2 +1,16 @@
1
- export { sqlite } from "./browser.js";
1
+ /// <reference path="./expo-crypto.d.ts" />
2
+ import { portableSQLite } from "./portable.js";
2
3
  export { expoSQLiteDriver } from "./driver.js";
4
+ export const sqlite = portableSQLite(async () => {
5
+ try {
6
+ const { getRandomValues } = await import("expo-crypto");
7
+ if (typeof getRandomValues !== "function")
8
+ throw new TypeError("expo-crypto requires getRandomValues");
9
+ return length => getRandomValues(new Uint8Array(length));
10
+ }
11
+ catch (cause) {
12
+ return () => {
13
+ throw new TypeError("Install expo-crypto for secure SQLite UUID generation, or supply randomBytes", { cause });
14
+ };
15
+ }
16
+ });
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { sqliteFactory } from "./descriptor.js";
2
2
  export const sqlite = sqliteFactory(async (schema, settings, initialize) => {
3
- if (!settings || typeof settings !== "object" || Reflect.ownKeys(settings).some(key => !["filename", "open", "randomBytes", "transactionTimeoutMs"].includes(key))
3
+ if (!settings || typeof settings !== "object" || Reflect.ownKeys(settings).some(key => !["filename", "open", "randomBytes", "transactionTimeoutMs", "startupRetry"].includes(key))
4
4
  || (settings.filename === undefined) === (settings.open === undefined))
5
5
  throw new TypeError("SQLite requires exactly one of filename or open");
6
6
  let driver;
@@ -0,0 +1,2 @@
1
+ import { type BrowserSQLiteConnection } from "./descriptor.ts";
2
+ export declare function portableSQLite(resolveRandomBytes?: () => Promise<NonNullable<BrowserSQLiteConnection["randomBytes"]>>): import("./descriptor.ts").SQLiteFactory<BrowserSQLiteConnection>;
@@ -0,0 +1,13 @@
1
+ import { sqliteFactory } from "./descriptor.js";
2
+ import { openSQLiteDatabase } from "./runtime.js";
3
+ export function portableSQLite(resolveRandomBytes) {
4
+ return sqliteFactory(async (schema, settings, initialize) => {
5
+ if (!settings || typeof settings !== "object" || Reflect.ownKeys(settings).some(key => !["open", "randomBytes", "transactionTimeoutMs", "startupRetry"].includes(key))
6
+ || settings.filename !== undefined || typeof settings.open !== "function")
7
+ throw new TypeError("Browser SQLite requires open: () => a dedicated SQLite driver");
8
+ const options = resolveRandomBytes && settings.randomBytes === undefined && typeof globalThis.crypto?.getRandomValues !== "function"
9
+ ? { ...settings, randomBytes: await resolveRandomBytes() }
10
+ : settings;
11
+ return openSQLiteDatabase(schema, await settings.open(), options, initialize);
12
+ });
13
+ }
package/dist/runtime.js CHANGED
@@ -1,8 +1,9 @@
1
- import { registerClientStorage, registerStorageExecution, registerStorageChanges, transactionDatabase } from "@hulla/db/internal/engine";
1
+ import { failStorageStartup, registerClientStorage, registerStorageExecution, registerStorageChanges, transactionDatabase } from "@hulla/db/internal/engine";
2
2
  import { SQLiteEngine, sqliteFailure } from "./engine.js";
3
3
  import { sqliteClientStorage } from "./client-storage.js";
4
4
  /** Open schema-typed local storage. The driver belongs exclusively to the returned handle. */
5
5
  export async function openSQLiteDatabase(schema, driver, options = {}, initialize = false) {
6
+ const failOpen = (error) => failStorageStartup(error, () => driver.close());
6
7
  let engine;
7
8
  const timeoutMs = options.transactionTimeoutMs ?? 5000;
8
9
  try {
@@ -11,8 +12,7 @@ export async function openSQLiteDatabase(schema, driver, options = {}, initializ
11
12
  engine = new SQLiteEngine(driver, schema, options.randomBytes);
12
13
  }
13
14
  catch (error) {
14
- await driver.close();
15
- throw error;
15
+ return failOpen(error);
16
16
  }
17
17
  let closed = false, userTransaction = false, queued = 0;
18
18
  let tail = Promise.resolve();
@@ -62,8 +62,7 @@ export async function openSQLiteDatabase(schema, driver, options = {}, initializ
62
62
  storage = sqliteClientStorage(engine, schema, handler => enqueue(handler, true), notify);
63
63
  }
64
64
  catch (error) {
65
- await driver.close();
66
- throw error;
65
+ return failOpen(error);
67
66
  }
68
67
  try {
69
68
  if (initialize)
@@ -72,8 +71,7 @@ export async function openSQLiteDatabase(schema, driver, options = {}, initializ
72
71
  await engine.verify();
73
72
  }
74
73
  catch (error) {
75
- await driver.close();
76
- throw error;
74
+ return failOpen(error);
77
75
  }
78
76
  function guard(operation) {
79
77
  if (typeof operation.table === "string" && storage.isReplica(operation.table)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hulla/db-sqlite",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "type": "module",
5
5
  "description": "SQLite application instances and platform adapters for Hulla schemas",
6
6
  "exports": {
@@ -55,7 +55,13 @@
55
55
  "LICENSE"
56
56
  ],
57
57
  "peerDependencies": {
58
- "@hulla/db": "^0.3.2"
58
+ "@hulla/db": "^0.3.3",
59
+ "expo-crypto": "*"
60
+ },
61
+ "peerDependenciesMeta": {
62
+ "expo-crypto": {
63
+ "optional": true
64
+ }
59
65
  },
60
66
  "engines": {
61
67
  "node": ">=22.18.0"