@jini-ai/db 0.6.17 → 0.6.19

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/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.6.19
2
+
3
+ - Redact PostgreSQL idle-pool failure diagnostics without changing pool recovery.
4
+
5
+ ### Patch Changes
6
+
7
+ - Preserve the clear missing-directory error, including its path, when SQLite backup space checks fail with ENOENT. Other filesystem errors propagate unchanged.
8
+
1
9
  ## 0.6.17
2
10
 
3
11
  ### Patch Changes
@@ -49,7 +49,12 @@ export declare class UnsupportedCapabilityError extends Error {
49
49
  readonly transport: StorageTransport;
50
50
  constructor(capability: StorageCapability, transport: StorageTransport);
51
51
  }
52
+ declare const storageKernelSchema: unique symbol;
52
53
  export interface StorageKernel<DB> {
54
+ /** Type-only schema witness: retain DB independently of Kysely's structural method
55
+ * signatures so stores reject kernels missing required tables.
56
+ * Optional so existing host implementations need no runtime property. */
57
+ readonly [storageKernelSchema]?: DB;
53
58
  readonly dialect: StorageDialect;
54
59
  readonly transport: StorageTransport;
55
60
  readonly capabilities: StorageCapabilities;
@@ -1,3 +1,5 @@
1
+ import { inspect } from 'node:util';
2
+ import { redactSecrets } from '@jini-ai/core';
1
3
  import { Kysely, PostgresDialect } from "kysely";
2
4
  import { PG_PARSERS } from "../../core/index.js";
3
5
  import { buildKernel, postgresLockKey } from "../index.js";
@@ -25,7 +27,7 @@ export function openPostgresKernel(required, optional = {}) {
25
27
  // An idle connection dropped by the server (restart, failover, pg_terminate_backend, a pooler's
26
28
  // idle cut) is evicted by the pool and reopened on next use; without a listener it would crash the
27
29
  // process.
28
- pool.on("error", (err) => console.warn(`[postgres] idle pool connection dropped: ${err.message}`));
30
+ pool.on("error", (err) => console.warn('[postgres] idle pool connection dropped:', redactSecrets({ input: inspect(err) }, { redactPii: false })));
29
31
  const base = new Kysely({ dialect: new PostgresDialect({ pool }) });
30
32
  return buildKernel({
31
33
  dialect: "postgres",
@@ -8,7 +8,8 @@ export declare class DatabaseDiskSpaceError extends Error {
8
8
  requiredBytes: number;
9
9
  });
10
10
  }
11
- /** Check the destination filesystem before allocating a full copy. Source/statfs errors propagate.
11
+ /** Check the destination filesystem before allocating a full copy. Missing destination directories
12
+ * get the backup API's clear error; source errors and other statfs errors propagate unchanged.
12
13
  * The reserve leaves room for the live DB's WAL and journal during the operation.
13
14
  * @complexity O(1), two metadata reads; no database bytes are read.
14
15
  * @example await assertDatabaseCopySpace({ sourcePath: '/site/content.db', destinationPath: '/site/copy.db' }); */
@@ -13,13 +13,21 @@ export class DatabaseDiskSpaceError extends Error {
13
13
  this.name = 'DatabaseDiskSpaceError';
14
14
  }
15
15
  }
16
- /** Check the destination filesystem before allocating a full copy. Source/statfs errors propagate.
16
+ /** Check the destination filesystem before allocating a full copy. Missing destination directories
17
+ * get the backup API's clear error; source errors and other statfs errors propagate unchanged.
17
18
  * The reserve leaves room for the live DB's WAL and journal during the operation.
18
19
  * @complexity O(1), two metadata reads; no database bytes are read.
19
20
  * @example await assertDatabaseCopySpace({ sourcePath: '/site/content.db', destinationPath: '/site/copy.db' }); */
20
21
  export async function assertDatabaseCopySpace({ sourcePath, destinationPath }, _options = {}) {
21
22
  const sourceBytes = sourcePath === ':memory:' ? 0 : (await fs.stat(sourcePath)).size;
22
- const space = await fs.statfs(path.dirname(path.resolve(destinationPath)));
23
+ const directory = path.dirname(path.resolve(destinationPath));
24
+ const space = await fs.statfs(directory).catch((error) => {
25
+ // Preserve the backup API's diagnostic now that preflight reaches the filesystem first.
26
+ if (error instanceof Error && 'code' in error && error.code === 'ENOENT') {
27
+ throw new TypeError(`Cannot save backup because the directory does not exist: ${directory}`, { cause: error });
28
+ }
29
+ throw error;
30
+ });
23
31
  const freeBytes = space.bavail * space.bsize;
24
32
  const requiredBytes = Math.ceil(sourceBytes * 1.2) + DISK_RESERVE_BYTES;
25
33
  if (freeBytes < requiredBytes)
@@ -42,9 +42,18 @@ export class SqliteDbOpsAdapter {
42
42
  async captureRestorePoint(required, options = {}) {
43
43
  const watermarkAtCapture = await this.readWatermark();
44
44
  const targetDir = options.directory ?? (this.filePath === IN_MEMORY ? os.tmpdir() : path.dirname(this.filePath));
45
- const artifactRef = path.join(targetDir, restorePointFilename({ scopeId: required.scopeId, watermarkAtCapture, timestamp: this.now() }));
46
- await assertDatabaseCopySpace({ sourcePath: this.filePath, destinationPath: artifactRef });
47
- await this.connection.backup(artifactRef);
45
+ const timestamp = this.now();
46
+ const candidate = path.join(targetDir, restorePointFilename({ scopeId: required.scopeId, watermarkAtCapture, timestamp }));
47
+ await assertDatabaseCopySpace({ sourcePath: this.filePath, destinationPath: candidate });
48
+ const artifactRef = await reserveRestorePoint({ targetDir, scopeId: required.scopeId, watermarkAtCapture, timestamp });
49
+ try {
50
+ await this.connection.backup(artifactRef);
51
+ }
52
+ catch (error) {
53
+ // Only our exclusive reservation may be removed; a prior capture must survive a failed copy.
54
+ await fs.rm(artifactRef, { force: true }).catch(() => undefined);
55
+ throw error;
56
+ }
48
57
  return { artifactRef, watermarkAtCapture };
49
58
  }
50
59
  /**
@@ -81,4 +90,24 @@ export class SqliteDbOpsAdapter {
81
90
  return { restartRequired: true };
82
91
  }
83
92
  }
93
+ /** Reserve before backup: SQLite otherwise overwrites an existing same-millisecond snapshot.
94
+ * Exclusive create also arbitrates between different processes. Advance the filename stamp only
95
+ * on collision, preserving the existing artifact-name contract and host retention matchers.
96
+ * @complexity O(c) metadata operations for c collisions; no database bytes are copied here.
97
+ */
98
+ async function reserveRestorePoint(required, _options = {}) {
99
+ for (let timestamp = required.timestamp;; timestamp++) {
100
+ const artifactRef = path.join(required.targetDir, restorePointFilename({ ...required, timestamp }));
101
+ try {
102
+ const reservation = await fs.open(artifactRef, 'wx', 0o600);
103
+ await reservation.close();
104
+ return artifactRef;
105
+ }
106
+ catch (error) {
107
+ if (error instanceof Error && 'code' in error && error.code === 'EEXIST')
108
+ continue;
109
+ throw error;
110
+ }
111
+ }
112
+ }
84
113
  //# sourceMappingURL=db-ops.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jini-ai/db",
3
- "version": "0.6.17",
3
+ "version": "0.6.19",
4
4
  "description": "Database ports, connection helpers, ownership, isolated Kysely kernels and migrations. All peers are optional; drivers are injected.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -33,6 +33,7 @@
33
33
  "src/kernel/store-copy.ts",
34
34
  "src/kernel/turn-lock.ts",
35
35
  "src/kernel/typegen.ts",
36
+ "src/sqlite/backup-policy.ts",
36
37
  "src/sqlite/checked-state.ts"
37
38
  ]
38
39
  },
@@ -210,7 +211,7 @@
210
211
  },
211
212
  "dependencies": {
212
213
  "kysely": "^0.29.6",
213
- "@jini-ai/core": "^0.6.17"
214
+ "@jini-ai/core": "^0.6.19"
214
215
  },
215
216
  "peerDependencies": {
216
217
  "better-sqlite3": "^13.0.0",
@@ -226,8 +227,8 @@
226
227
  "better-sqlite3": "13.0.3",
227
228
  "kysely": "0.29.6",
228
229
  "pg": "8.23.0",
229
- "@jini-ai/platform": "0.6.17",
230
- "@jini-ai/core": "0.6.17"
230
+ "@jini-ai/core": "0.6.19",
231
+ "@jini-ai/platform": "0.6.19"
231
232
  },
232
233
  "peerDependenciesMeta": {
233
234
  "better-sqlite3": {