@hop-top/kit 0.5.0-alpha.12 → 0.5.0-alpha.13
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 +29 -0
- package/README.md +5 -5
- package/dist/bus-D2bAOPYz.d.ts +55 -0
- package/dist/bus.d.ts +1 -0
- package/dist/bus.js +13 -0
- package/dist/chunk-74E6RNOI.js +82 -0
- package/dist/chunk-B7XTFDBB.js +17 -0
- package/dist/chunk-CAONBSPO.js +497 -0
- package/dist/chunk-F7RSL4N4.js +88 -0
- package/dist/chunk-I6XDPSXQ.js +75 -0
- package/dist/chunk-IDM6YZXN.js +204 -0
- package/dist/chunk-VKQCOVKI.js +880 -0
- package/dist/cli.js +3 -3
- package/dist/index-BqZ7WGQf.d.ts +731 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +27 -13
- package/dist/upgrade/drivers/configfile.d.ts +45 -0
- package/dist/upgrade/drivers/configfile.js +78 -0
- package/dist/upgrade/drivers/fsdir.d.ts +61 -0
- package/dist/upgrade/drivers/fsdir.js +170 -0
- package/dist/upgrade/drivers/postgres.d.ts +121 -0
- package/dist/upgrade/drivers/postgres.js +195 -0
- package/dist/upgrade/drivers/sqlite.d.ts +101 -0
- package/dist/upgrade/drivers/sqlite.js +241 -0
- package/dist/upgrade/index.d.ts +2 -0
- package/dist/upgrade/index.js +73 -0
- package/dist/uri-IJ_AoY3d.d.ts +144 -0
- package/dist/uri.d.ts +1 -0
- package/dist/uri.js +37 -0
- package/dist/version-DEJ3pbiO.d.ts +10 -0
- package/dist/wizard-Bf_qHc31.d.ts +159 -0
- package/dist/wizard.d.ts +1 -0
- package/dist/wizard.js +39 -0
- package/package.json +74 -15
- package/skills/hop-top-kit-ts/.claude-plugin/plugin.json +19 -0
- package/skills/hop-top-kit-ts/SKILL.md +117 -0
- package/skills/hop-top-kit-ts/assets/snippets/mcp/mcp.expected +2 -0
- package/skills/hop-top-kit-ts/assets/snippets/mcp/mcp.ts +20 -0
- package/skills/hop-top-kit-ts/assets/snippets/quickstart/quickstart.expected +13 -0
- package/skills/hop-top-kit-ts/assets/snippets/quickstart/quickstart.ts +26 -0
- package/skills/hop-top-kit-ts/gym.json +5 -0
- package/skills/hop-top-kit-ts/references/ai-and-agents.md +45 -0
- package/skills/hop-top-kit-ts/references/cli-and-console.md +63 -0
- package/skills/hop-top-kit-ts/references/coverage.md +56 -0
- package/skills/hop-top-kit-ts/references/guardrails-and-trust.md +60 -0
- package/skills/hop-top-kit-ts/references/other-ports.md +31 -0
- package/skills/hop-top-kit-ts/references/serving-and-transports.md +49 -0
- package/skills/hop-top-kit-ts/scripts/detect-port.sh +168 -0
- package/src/index.test.ts +48 -2
- package/src/index.ts +6 -1
- package/src/published-types.test.ts +8 -0
- package/src/serve.stopped-order.test.ts +156 -0
- package/src/upgrade/README.md +42 -0
- package/src/upgrade/backup.ts +174 -0
- package/src/upgrade/conformance.test.ts +310 -0
- package/src/upgrade/document.ts +229 -0
- package/src/upgrade/driver.ts +66 -0
- package/src/upgrade/drivers/README.md +47 -0
- package/src/upgrade/drivers/configfile.test.ts +112 -0
- package/src/upgrade/drivers/configfile.ts +107 -0
- package/src/upgrade/drivers/fsdir.test.ts +188 -0
- package/src/upgrade/drivers/fsdir.ts +216 -0
- package/src/upgrade/drivers/isolation.test.ts +18 -0
- package/src/upgrade/drivers/postgres.integration.test.ts +395 -0
- package/src/upgrade/drivers/postgres.test.ts +272 -0
- package/src/upgrade/drivers/postgres.ts +284 -0
- package/src/upgrade/drivers/sqlite.integration.test.ts +492 -0
- package/src/upgrade/drivers/sqlite.ts +300 -0
- package/src/upgrade/drivers/version.ts +21 -0
- package/src/upgrade/errors.ts +117 -0
- package/src/upgrade/events.ts +117 -0
- package/src/{upgrade.test.ts → upgrade/index.test.ts} +1 -1
- package/src/{upgrade.ts → upgrade/index.ts} +20 -0
- package/src/upgrade/migration.ts +89 -0
- package/src/upgrade/migrator.test.ts +391 -0
- package/src/upgrade/migrator.ts +527 -0
- package/src/upgrade/options.ts +59 -0
- package/src/upgrade/semver.ts +90 -0
- package/test/skill-snippets.test.ts +60 -0
- package/test/upgrade/conformance-migrator.ts +439 -0
- package/test/upgrade/conformance-support.ts +44 -0
- package/test/upgrade/driver-support.ts +44 -0
- package/test/upgrade/pg-container.ts +64 -0
- package/test/upgrade/pg-race-worker.ts +48 -0
- package/test/upgrade/race-helper.ts +36 -0
- package/test/upgrade/sqlite-support.ts +41 -0
- package/test/upgrade/testdata/go-fsdir-mydata.tar.gz +0 -0
- package/vitest.config.ts +26 -0
- package/dist/chunk-X4E5QGEB.js +0 -171
- package/dist/upgrade-BJ2z6Oqf.d.ts +0 -156
- package/dist/upgrade.d.ts +0 -1
- package/dist/upgrade.js +0 -9
- package/dist/{chunk-MFVUENZR.js → chunk-KV7FYEAF.js} +2 -2
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
var _chunkI6XDPSXQjs = require('../../chunk-I6XDPSXQ.js');
|
|
6
|
+
require('../../chunk-R5U7XKVJ.js');
|
|
7
|
+
|
|
8
|
+
// src/upgrade/drivers/postgres.ts
|
|
9
|
+
var TABLE_RE = /^[a-zA-Z_][a-zA-Z0-9_]{0,63}$/;
|
|
10
|
+
var SQL_LOCK = "SELECT pg_advisory_lock(hashtextextended($1, 0))";
|
|
11
|
+
var SQL_UNLOCK = "SELECT pg_advisory_unlock(hashtextextended($1, 0))";
|
|
12
|
+
var SQL_XACT_LOCK = "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))";
|
|
13
|
+
var SQL_TRY_LOCK = "SELECT pg_try_advisory_lock(hashtextextended($1, 0))";
|
|
14
|
+
var lockKey = (table, name) => `kit-upgrade:${table}:${name}`;
|
|
15
|
+
var ddlKey = (table) => `kit-upgrade:${table}:ddl`;
|
|
16
|
+
var createTableSql = (table) => `CREATE TABLE IF NOT EXISTS ${table} (schema_name TEXT PRIMARY KEY, version TEXT NOT NULL DEFAULT '')`;
|
|
17
|
+
var versionSql = (table) => `SELECT version FROM ${table} WHERE schema_name = $1`;
|
|
18
|
+
var upsertSql = (table) => `INSERT INTO ${table} (schema_name, version) VALUES ($1, $2) ON CONFLICT (schema_name) DO UPDATE SET version = EXCLUDED.version`;
|
|
19
|
+
var POLL_MS = 50;
|
|
20
|
+
function validateTable(name) {
|
|
21
|
+
if (typeof name !== "string" || !TABLE_RE.test(name)) {
|
|
22
|
+
throw new Error(`invalid table name: ${JSON.stringify(name)}`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
var firstValue = (r) => {
|
|
26
|
+
const row = r.rows[0];
|
|
27
|
+
return row === void 0 ? void 0 : Object.values(row)[0];
|
|
28
|
+
};
|
|
29
|
+
async function discard(c) {
|
|
30
|
+
try {
|
|
31
|
+
if (typeof c.release === "function") c.release(true);
|
|
32
|
+
else if (typeof c.end === "function") await c.end();
|
|
33
|
+
} catch (e2) {
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async function finish(c) {
|
|
37
|
+
if (typeof c.release === "function") c.release();
|
|
38
|
+
else if (typeof c.end === "function") await c.end();
|
|
39
|
+
}
|
|
40
|
+
function pause(ms, signal) {
|
|
41
|
+
return new Promise((resolve) => {
|
|
42
|
+
const done = () => {
|
|
43
|
+
clearTimeout(t);
|
|
44
|
+
_optionalChain([signal, 'optionalAccess', _ => _.removeEventListener, 'call', _2 => _2("abort", done)]);
|
|
45
|
+
resolve();
|
|
46
|
+
};
|
|
47
|
+
const t = setTimeout(done, ms);
|
|
48
|
+
_optionalChain([signal, 'optionalAccess', _3 => _3.addEventListener, 'call', _4 => _4("abort", done, { once: true })]);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
var PostgresDriver = class {
|
|
52
|
+
/** @internal use {@link openPostgresDriver}. */
|
|
53
|
+
constructor(o) {
|
|
54
|
+
this.o = o;
|
|
55
|
+
this.storesVersion = true;
|
|
56
|
+
}
|
|
57
|
+
name() {
|
|
58
|
+
return this.o.name;
|
|
59
|
+
}
|
|
60
|
+
/** The stored version; no row means `""`. */
|
|
61
|
+
async version() {
|
|
62
|
+
try {
|
|
63
|
+
const r = await this.o.db.query(versionSql(this.o.table), [this.o.name]);
|
|
64
|
+
const v = _optionalChain([r, 'access', _5 => _5.rows, 'access', _6 => _6[0], 'optionalAccess', _7 => _7["version"]]);
|
|
65
|
+
return typeof v === "string" ? v : "";
|
|
66
|
+
} catch (e) {
|
|
67
|
+
throw new Error(`postgres upgrade: version: ${_chunkI6XDPSXQjs.errorText.call(void 0, e)}`, { cause: e });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** Upserts the version row for this schema. */
|
|
71
|
+
async setVersion(version) {
|
|
72
|
+
try {
|
|
73
|
+
await this.o.db.query(upsertSql(this.o.table), [this.o.name, version]);
|
|
74
|
+
} catch (e) {
|
|
75
|
+
throw new Error(`postgres upgrade: set version: ${_chunkI6XDPSXQjs.errorText.call(void 0, e)}`, { cause: e });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** Kit does not back up a server database: the run goes on without one. */
|
|
79
|
+
async backup(_dest) {
|
|
80
|
+
throw new (0, _chunkI6XDPSXQjs.BackupUnsupportedError)("postgres upgrade: backup unsupported");
|
|
81
|
+
}
|
|
82
|
+
/** Nothing is restored: a rollback that needs it ends "not restored". */
|
|
83
|
+
async restore(_src) {
|
|
84
|
+
throw new (0, _chunkI6XDPSXQjs.BackupUnsupportedError)("postgres upgrade: restore unsupported");
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Takes the session-level advisory lock on one dedicated connection and
|
|
88
|
+
* holds that connection until the returned unlock runs. Waits by polling
|
|
89
|
+
* `pg_try_advisory_lock` (about every 50 ms) until acquired or `signal`
|
|
90
|
+
* aborts; an aborted wait rejects with `abortError(signal)` and the
|
|
91
|
+
* connection is destroyed, so nothing is held. Unlock runs
|
|
92
|
+
* `pg_advisory_unlock` on that same connection regardless of `signal`;
|
|
93
|
+
* when it fails or answers `false` the connection is destroyed, which
|
|
94
|
+
* ends the session and the lock.
|
|
95
|
+
*
|
|
96
|
+
* A pool with `options.max === 1` is refused (the lock holds its only
|
|
97
|
+
* connection and the run's queries would wait for ever).
|
|
98
|
+
*/
|
|
99
|
+
async lock(signal) {
|
|
100
|
+
if (_optionalChain([this, 'access', _8 => _8.o, 'access', _9 => _9.db, 'access', _10 => _10.options, 'optionalAccess', _11 => _11.max]) === 1) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
"postgres upgrade: lock needs a pool of at least two connections: the lock holds one for the whole run (a pool with max 1 would deadlock)"
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
if (_optionalChain([signal, 'optionalAccess', _12 => _12.aborted])) throw _chunkI6XDPSXQjs.abortError.call(void 0, signal);
|
|
106
|
+
let client;
|
|
107
|
+
try {
|
|
108
|
+
client = await this.o.connect();
|
|
109
|
+
} catch (e) {
|
|
110
|
+
throw new Error(`postgres upgrade: lock: ${_chunkI6XDPSXQjs.errorText.call(void 0, e)}`, { cause: e });
|
|
111
|
+
}
|
|
112
|
+
const key = lockKey(this.o.table, this.o.name);
|
|
113
|
+
try {
|
|
114
|
+
for (; ; ) {
|
|
115
|
+
if (_optionalChain([signal, 'optionalAccess', _13 => _13.aborted])) throw _chunkI6XDPSXQjs.abortError.call(void 0, signal);
|
|
116
|
+
const r = await client.query(SQL_TRY_LOCK, [key]);
|
|
117
|
+
if (firstValue(r) === true) break;
|
|
118
|
+
await pause(POLL_MS, signal);
|
|
119
|
+
}
|
|
120
|
+
if (_optionalChain([signal, 'optionalAccess', _14 => _14.aborted])) throw _chunkI6XDPSXQjs.abortError.call(void 0, signal);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
await discard(client);
|
|
123
|
+
if (_optionalChain([signal, 'optionalAccess', _15 => _15.aborted])) throw _chunkI6XDPSXQjs.abortError.call(void 0, signal);
|
|
124
|
+
throw new Error(`postgres upgrade: lock: ${_chunkI6XDPSXQjs.errorText.call(void 0, e)}`, { cause: e });
|
|
125
|
+
}
|
|
126
|
+
return this.unlocker(client, key);
|
|
127
|
+
}
|
|
128
|
+
unlocker(client, key) {
|
|
129
|
+
let result;
|
|
130
|
+
return () => _nullishCoalesce(result, () => ( (result = this.release(client, key))));
|
|
131
|
+
}
|
|
132
|
+
async release(client, key) {
|
|
133
|
+
let err;
|
|
134
|
+
try {
|
|
135
|
+
const r = await client.query(SQL_UNLOCK, [key]);
|
|
136
|
+
if (firstValue(r) !== true) err = new Error("lock was not held by this session");
|
|
137
|
+
} catch (e) {
|
|
138
|
+
err = e;
|
|
139
|
+
}
|
|
140
|
+
if (err !== void 0) {
|
|
141
|
+
await discard(client);
|
|
142
|
+
throw new Error(`postgres upgrade: unlock: ${_chunkI6XDPSXQjs.errorText.call(void 0, err)}`, { cause: err });
|
|
143
|
+
}
|
|
144
|
+
await finish(client);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
async function openPostgresDriver(opts) {
|
|
148
|
+
validateTable(opts.table);
|
|
149
|
+
if (!opts.db || typeof opts.db.query !== "function") {
|
|
150
|
+
throw new Error("postgres upgrade: db must have query(text, params)");
|
|
151
|
+
}
|
|
152
|
+
if (typeof opts.connect !== "function") {
|
|
153
|
+
throw new Error("postgres upgrade: connect must return a dedicated client");
|
|
154
|
+
}
|
|
155
|
+
const o = { ...opts, name: _nullishCoalesce(opts.name, () => ( "postgres")) };
|
|
156
|
+
try {
|
|
157
|
+
await ensureTable(o);
|
|
158
|
+
} catch (e) {
|
|
159
|
+
throw new Error(`postgres upgrade: migrate: ${_chunkI6XDPSXQjs.errorText.call(void 0, e)}`, { cause: e });
|
|
160
|
+
}
|
|
161
|
+
return new PostgresDriver(o);
|
|
162
|
+
}
|
|
163
|
+
async function ensureTable(o) {
|
|
164
|
+
const client = await o.connect();
|
|
165
|
+
try {
|
|
166
|
+
await client.query("BEGIN");
|
|
167
|
+
await client.query(SQL_XACT_LOCK, [ddlKey(o.table)]);
|
|
168
|
+
await client.query(createTableSql(o.table));
|
|
169
|
+
await client.query("COMMIT");
|
|
170
|
+
} catch (e) {
|
|
171
|
+
try {
|
|
172
|
+
await client.query("ROLLBACK");
|
|
173
|
+
} catch (e3) {
|
|
174
|
+
await discard(client);
|
|
175
|
+
throw e;
|
|
176
|
+
}
|
|
177
|
+
await finish(client);
|
|
178
|
+
throw e;
|
|
179
|
+
}
|
|
180
|
+
await finish(client);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
exports.PostgresDriver = PostgresDriver; exports.SQL_LOCK = SQL_LOCK; exports.SQL_TRY_LOCK = SQL_TRY_LOCK; exports.SQL_UNLOCK = SQL_UNLOCK; exports.SQL_XACT_LOCK = SQL_XACT_LOCK; exports.createTableSql = createTableSql; exports.ddlKey = ddlKey; exports.lockKey = lockKey; exports.openPostgresDriver = openPostgresDriver; exports.upsertSql = upsertSql; exports.validateTable = validateTable; exports.versionSql = versionSql;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import Database from 'better-sqlite3';
|
|
2
|
+
import { D as DriverOptions } from '../../version-DEJ3pbiO.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A {@link SchemaDriver} for a SQLite database file, ported from Go
|
|
6
|
+
* `core/upgrade/driver/sqlite`, over `better-sqlite3`.
|
|
7
|
+
*
|
|
8
|
+
* Two modes. Path only: `new SqliteDriver(dbPath)` opens its own
|
|
9
|
+
* short-lived connection for `backup` and swaps the file on `restore`.
|
|
10
|
+
* Handle: `new SqliteDriver(dbPath, { db })` backs up through the caller's
|
|
11
|
+
* `Database`, never closes it, and cannot restore (the file cannot be
|
|
12
|
+
* replaced under a live connection): `restore` throws
|
|
13
|
+
* {@link BackupUnsupportedError} and the backup stays for manual recovery.
|
|
14
|
+
*
|
|
15
|
+
* The lock is `BEGIN EXCLUSIVE` on a separate database `.lock.db` beside
|
|
16
|
+
* the version file, because Node has no `flock`. It excludes other
|
|
17
|
+
* connections and processes using this driver, but does NOT interoperate
|
|
18
|
+
* with the `flock` on `.lock` that the Go, Python and Rust drivers take: a
|
|
19
|
+
* TypeScript and a non-TypeScript process migrating one file do not
|
|
20
|
+
* exclude each other.
|
|
21
|
+
*
|
|
22
|
+
* Imports `better-sqlite3`: import this module only where it is used
|
|
23
|
+
* (`@hop-top/kit/upgrade/drivers/sqlite`); the main entry never loads it.
|
|
24
|
+
*
|
|
25
|
+
* @packageDocumentation
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/** Options of a {@link SqliteDriver}. */
|
|
29
|
+
interface SqliteOptions extends DriverOptions {
|
|
30
|
+
/** The schema name (default `"sqlite"`). */
|
|
31
|
+
name?: string;
|
|
32
|
+
/**
|
|
33
|
+
* The caller's open connection. `backup` runs on it and leaves it open;
|
|
34
|
+
* `restore` throws {@link BackupUnsupportedError}. `dbPath` then only
|
|
35
|
+
* names the backup file (and may be empty).
|
|
36
|
+
*/
|
|
37
|
+
db?: Database.Database;
|
|
38
|
+
}
|
|
39
|
+
/** The lock database, in the version file's directory. */
|
|
40
|
+
declare const LOCK_FILE = ".lock.db";
|
|
41
|
+
/**
|
|
42
|
+
* Backs up, restores and locks one SQLite database file.
|
|
43
|
+
*
|
|
44
|
+
* A `SchemaDriver`; not declared with `implements` so the bundled declarations
|
|
45
|
+
* keep `SchemaDriver` out of a chunk shared with the root barrel (the tests
|
|
46
|
+
* check the fit).
|
|
47
|
+
*/
|
|
48
|
+
declare class SqliteDriver {
|
|
49
|
+
private readonly dbPath;
|
|
50
|
+
private readonly schema;
|
|
51
|
+
private readonly db?;
|
|
52
|
+
private readonly opts;
|
|
53
|
+
constructor(dbPath: string, options?: SqliteOptions);
|
|
54
|
+
name(): string;
|
|
55
|
+
version(): Promise<string>;
|
|
56
|
+
/** The Migrator writes the version file; nothing to do here. */
|
|
57
|
+
setVersion(): void;
|
|
58
|
+
/** The backup file's name: the database file's base name, or `<schema>.db` without a path. */
|
|
59
|
+
private backupName;
|
|
60
|
+
/**
|
|
61
|
+
* Writes a consistent snapshot to `<dest>/<database file name>` with
|
|
62
|
+
* `VACUUM INTO`, which reads through the WAL: rows committed but not yet
|
|
63
|
+
* checkpointed are in it. Path only: a missing database file is
|
|
64
|
+
* "nothing to back up" (no file, no error); the driver opens a
|
|
65
|
+
* connection of its own and closes it. With a caller handle the snapshot
|
|
66
|
+
* runs on it, left open; it must not be inside a transaction.
|
|
67
|
+
*/
|
|
68
|
+
backup(dest: string): Promise<void>;
|
|
69
|
+
/** Snapshots the database to `file`, bound as a parameter so no path needs quoting. */
|
|
70
|
+
private vacuumInto;
|
|
71
|
+
/**
|
|
72
|
+
* Replaces the database file with the backup in `src`, then removes the
|
|
73
|
+
* replaced database's `-wal`, `-shm` and `-journal` files.
|
|
74
|
+
*
|
|
75
|
+
* Precondition: no open handle on the file, in this process or another;
|
|
76
|
+
* a connection left open keeps using the replaced file and may recreate
|
|
77
|
+
* its -wal beside the restored one. With a caller handle it restores
|
|
78
|
+
* nothing and throws {@link BackupUnsupportedError}; the backup stays.
|
|
79
|
+
*
|
|
80
|
+
* The backup is copied to a temporary file beside the database, synced
|
|
81
|
+
* and renamed over it, so a failure before the rename leaves the
|
|
82
|
+
* database and its sidecars untouched. An error after the rename means
|
|
83
|
+
* the database already holds the backup; a sidecar named in the error
|
|
84
|
+
* must be removed before the database is opened.
|
|
85
|
+
*/
|
|
86
|
+
restore(src: string): Promise<void>;
|
|
87
|
+
/** `<data dir>/<schema>`: where the version file and the lock database live. */
|
|
88
|
+
private stateDir;
|
|
89
|
+
/**
|
|
90
|
+
* Holds `BEGIN EXCLUSIVE` on `<version dir>/.lock.db` (the directory is
|
|
91
|
+
* created if needed) until the returned function runs, retrying about
|
|
92
|
+
* every 50 ms while another connection holds it. When `signal` aborts
|
|
93
|
+
* while waiting it rejects with `abortError(signal)` and holds nothing.
|
|
94
|
+
* The retry loop is asynchronous (the busy timeout is 0), so a wait
|
|
95
|
+
* never blocks the event loop. The file is never deleted. Does not
|
|
96
|
+
* interoperate with the other ports' `flock` on `.lock`.
|
|
97
|
+
*/
|
|
98
|
+
lock(signal?: AbortSignal): Promise<() => void>;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { LOCK_FILE, SqliteDriver, type SqliteOptions };
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunkB7XTFDBBjs = require('../../chunk-B7XTFDBB.js');
|
|
5
|
+
require('../../chunk-IDM6YZXN.js');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
var _chunkI6XDPSXQjs = require('../../chunk-I6XDPSXQ.js');
|
|
10
|
+
require('../../chunk-4UE5TCMQ.js');
|
|
11
|
+
require('../../chunk-R5U7XKVJ.js');
|
|
12
|
+
|
|
13
|
+
// src/upgrade/drivers/sqlite.ts
|
|
14
|
+
var _fs = require('fs');
|
|
15
|
+
var _crypto = require('crypto');
|
|
16
|
+
var _path = require('path'); var path = _interopRequireWildcard(_path);
|
|
17
|
+
var _bettersqlite3 = require('better-sqlite3'); var _bettersqlite32 = _interopRequireDefault(_bettersqlite3);
|
|
18
|
+
var SIDECARS = ["-wal", "-shm", "-journal"];
|
|
19
|
+
var BUSY_TIMEOUT_MS = 5e3;
|
|
20
|
+
var LOCK_FILE = ".lock.db";
|
|
21
|
+
var LOCK_POLL_MS = 50;
|
|
22
|
+
var isMissing = (e) => e.code === "ENOENT";
|
|
23
|
+
var msg = (e) => e instanceof Error ? e.message : String(e);
|
|
24
|
+
var isBusy = (e) => {
|
|
25
|
+
const code = e.code;
|
|
26
|
+
return typeof code === "string" && (code.startsWith("SQLITE_BUSY") || code.startsWith("SQLITE_LOCKED"));
|
|
27
|
+
};
|
|
28
|
+
var SqliteDriver = class {
|
|
29
|
+
constructor(dbPath, options = {}) {
|
|
30
|
+
this.dbPath = dbPath;
|
|
31
|
+
this.schema = _nullishCoalesce(options.name, () => ( "sqlite"));
|
|
32
|
+
this.db = options.db;
|
|
33
|
+
this.opts = { dataDir: options.dataDir, tool: options.tool };
|
|
34
|
+
}
|
|
35
|
+
name() {
|
|
36
|
+
return this.schema;
|
|
37
|
+
}
|
|
38
|
+
version() {
|
|
39
|
+
return _chunkB7XTFDBBjs.versionOf.call(void 0, this.opts, this.schema);
|
|
40
|
+
}
|
|
41
|
+
/** The Migrator writes the version file; nothing to do here. */
|
|
42
|
+
setVersion() {
|
|
43
|
+
}
|
|
44
|
+
/** The backup file's name: the database file's base name, or `<schema>.db` without a path. */
|
|
45
|
+
backupName() {
|
|
46
|
+
return this.dbPath !== "" ? path.basename(this.dbPath) : this.schema + ".db";
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Writes a consistent snapshot to `<dest>/<database file name>` with
|
|
50
|
+
* `VACUUM INTO`, which reads through the WAL: rows committed but not yet
|
|
51
|
+
* checkpointed are in it. Path only: a missing database file is
|
|
52
|
+
* "nothing to back up" (no file, no error); the driver opens a
|
|
53
|
+
* connection of its own and closes it. With a caller handle the snapshot
|
|
54
|
+
* runs on it, left open; it must not be inside a transaction.
|
|
55
|
+
*/
|
|
56
|
+
async backup(dest) {
|
|
57
|
+
if (this.db === void 0) {
|
|
58
|
+
try {
|
|
59
|
+
await _fs.promises.stat(this.dbPath);
|
|
60
|
+
} catch (e) {
|
|
61
|
+
if (isMissing(e)) return;
|
|
62
|
+
throw new Error(`stat db: ${msg(e)}`, { cause: e });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
await _fs.promises.mkdir(dest, { recursive: true, mode: 488 });
|
|
67
|
+
} catch (e) {
|
|
68
|
+
throw new Error(`create backup dir: ${msg(e)}`, { cause: e });
|
|
69
|
+
}
|
|
70
|
+
const target = path.join(dest, this.backupName());
|
|
71
|
+
const tmp = target + ".tmp";
|
|
72
|
+
try {
|
|
73
|
+
await _fs.promises.rm(tmp, { force: true });
|
|
74
|
+
await _fs.promises.rm(target, { force: true });
|
|
75
|
+
} catch (e) {
|
|
76
|
+
throw new Error(`clear backup file: ${msg(e)}`, { cause: e });
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
this.vacuumInto(tmp);
|
|
80
|
+
await _fs.promises.rename(tmp, target);
|
|
81
|
+
} catch (e) {
|
|
82
|
+
await _fs.promises.rm(tmp, { force: true });
|
|
83
|
+
throw new Error(`backup db: ${msg(e)}`, { cause: e });
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/** Snapshots the database to `file`, bound as a parameter so no path needs quoting. */
|
|
87
|
+
vacuumInto(file) {
|
|
88
|
+
if (this.db !== void 0) {
|
|
89
|
+
if (this.db.inTransaction) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
"the caller's connection has an open transaction; VACUUM INTO cannot run inside one: commit or roll it back first"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
this.db.prepare("VACUUM INTO ?").run(file);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const own = new (0, _bettersqlite32.default)(this.dbPath, { fileMustExist: true, timeout: BUSY_TIMEOUT_MS });
|
|
98
|
+
try {
|
|
99
|
+
own.prepare("VACUUM INTO ?").run(file);
|
|
100
|
+
} finally {
|
|
101
|
+
own.close();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Replaces the database file with the backup in `src`, then removes the
|
|
106
|
+
* replaced database's `-wal`, `-shm` and `-journal` files.
|
|
107
|
+
*
|
|
108
|
+
* Precondition: no open handle on the file, in this process or another;
|
|
109
|
+
* a connection left open keeps using the replaced file and may recreate
|
|
110
|
+
* its -wal beside the restored one. With a caller handle it restores
|
|
111
|
+
* nothing and throws {@link BackupUnsupportedError}; the backup stays.
|
|
112
|
+
*
|
|
113
|
+
* The backup is copied to a temporary file beside the database, synced
|
|
114
|
+
* and renamed over it, so a failure before the rename leaves the
|
|
115
|
+
* database and its sidecars untouched. An error after the rename means
|
|
116
|
+
* the database already holds the backup; a sidecar named in the error
|
|
117
|
+
* must be removed before the database is opened.
|
|
118
|
+
*/
|
|
119
|
+
async restore(src) {
|
|
120
|
+
if (this.db !== void 0) {
|
|
121
|
+
throw new (0, _chunkI6XDPSXQjs.BackupUnsupportedError)(
|
|
122
|
+
`restore ${this.dbPath} under a caller-owned handle: upgrade: backup unsupported`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
const backup = path.join(src, this.backupName());
|
|
126
|
+
try {
|
|
127
|
+
await _fs.promises.stat(backup);
|
|
128
|
+
} catch (e) {
|
|
129
|
+
throw new Error(`open backup: ${msg(e)}`, { cause: e });
|
|
130
|
+
}
|
|
131
|
+
const dir = path.dirname(this.dbPath);
|
|
132
|
+
try {
|
|
133
|
+
await _fs.promises.mkdir(dir, { recursive: true, mode: 488 });
|
|
134
|
+
} catch (e) {
|
|
135
|
+
throw new Error(`create db dir: ${msg(e)}`, { cause: e });
|
|
136
|
+
}
|
|
137
|
+
let mode = 384;
|
|
138
|
+
try {
|
|
139
|
+
mode = (await _fs.promises.stat(this.dbPath)).mode & 511;
|
|
140
|
+
} catch (e2) {
|
|
141
|
+
}
|
|
142
|
+
const tmp = `${this.dbPath}.restore-${_crypto.randomBytes.call(void 0, 6).toString("hex")}`;
|
|
143
|
+
try {
|
|
144
|
+
await _fs.promises.copyFile(backup, tmp, _fs.promises.constants.COPYFILE_EXCL);
|
|
145
|
+
await _fs.promises.chmod(tmp, mode);
|
|
146
|
+
await syncFile(tmp);
|
|
147
|
+
await _fs.promises.rename(tmp, this.dbPath);
|
|
148
|
+
} catch (e) {
|
|
149
|
+
await _fs.promises.rm(tmp, { force: true });
|
|
150
|
+
throw new Error(`restore db: ${msg(e)}`, { cause: e });
|
|
151
|
+
}
|
|
152
|
+
const errs = [];
|
|
153
|
+
for (const suffix of SIDECARS) {
|
|
154
|
+
const p = this.dbPath + suffix;
|
|
155
|
+
try {
|
|
156
|
+
await _fs.promises.rm(p);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
if (!isMissing(e)) errs.push(`remove stale ${p}: ${msg(e)}`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (process.platform !== "win32") {
|
|
162
|
+
try {
|
|
163
|
+
await syncFile(dir);
|
|
164
|
+
} catch (e) {
|
|
165
|
+
errs.push(`fsync dir ${dir}: ${msg(e)}`);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (errs.length > 0) throw new Error(`restore: ${this.dbPath} replaced: ${errs.join("; ")}`);
|
|
169
|
+
}
|
|
170
|
+
/** `<data dir>/<schema>`: where the version file and the lock database live. */
|
|
171
|
+
stateDir() {
|
|
172
|
+
return path.join(_chunkB7XTFDBBjs.resolveDataDir.call(void 0, this.opts), this.schema);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Holds `BEGIN EXCLUSIVE` on `<version dir>/.lock.db` (the directory is
|
|
176
|
+
* created if needed) until the returned function runs, retrying about
|
|
177
|
+
* every 50 ms while another connection holds it. When `signal` aborts
|
|
178
|
+
* while waiting it rejects with `abortError(signal)` and holds nothing.
|
|
179
|
+
* The retry loop is asynchronous (the busy timeout is 0), so a wait
|
|
180
|
+
* never blocks the event loop. The file is never deleted. Does not
|
|
181
|
+
* interoperate with the other ports' `flock` on `.lock`.
|
|
182
|
+
*/
|
|
183
|
+
async lock(signal) {
|
|
184
|
+
const dir = this.stateDir();
|
|
185
|
+
try {
|
|
186
|
+
await _fs.promises.mkdir(dir, { recursive: true, mode: 488 });
|
|
187
|
+
} catch (e) {
|
|
188
|
+
throw new Error(`create lock dir: ${msg(e)}`, { cause: e });
|
|
189
|
+
}
|
|
190
|
+
const file = path.join(dir, LOCK_FILE);
|
|
191
|
+
const conn = new (0, _bettersqlite32.default)(file, { timeout: 0 });
|
|
192
|
+
try {
|
|
193
|
+
for (; ; ) {
|
|
194
|
+
if (_optionalChain([signal, 'optionalAccess', _ => _.aborted])) throw _chunkI6XDPSXQjs.abortError.call(void 0, signal);
|
|
195
|
+
try {
|
|
196
|
+
conn.exec("BEGIN EXCLUSIVE");
|
|
197
|
+
break;
|
|
198
|
+
} catch (e) {
|
|
199
|
+
if (!isBusy(e)) throw new Error(`lock ${file}: ${msg(e)}`, { cause: e });
|
|
200
|
+
}
|
|
201
|
+
await sleep(LOCK_POLL_MS, signal);
|
|
202
|
+
}
|
|
203
|
+
} catch (e) {
|
|
204
|
+
conn.close();
|
|
205
|
+
throw e;
|
|
206
|
+
}
|
|
207
|
+
let released = false;
|
|
208
|
+
return () => {
|
|
209
|
+
if (released) return;
|
|
210
|
+
released = true;
|
|
211
|
+
try {
|
|
212
|
+
if (conn.inTransaction) conn.exec("ROLLBACK");
|
|
213
|
+
} finally {
|
|
214
|
+
conn.close();
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
function sleep(ms, signal) {
|
|
220
|
+
return new Promise((resolve) => {
|
|
221
|
+
const done = () => {
|
|
222
|
+
clearTimeout(timer);
|
|
223
|
+
_optionalChain([signal, 'optionalAccess', _2 => _2.removeEventListener, 'call', _3 => _3("abort", done)]);
|
|
224
|
+
resolve();
|
|
225
|
+
};
|
|
226
|
+
const timer = setTimeout(done, ms);
|
|
227
|
+
_optionalChain([signal, 'optionalAccess', _4 => _4.addEventListener, 'call', _5 => _5("abort", done, { once: true })]);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
async function syncFile(p) {
|
|
231
|
+
const h = await _fs.promises.open(p, "r");
|
|
232
|
+
try {
|
|
233
|
+
await h.sync();
|
|
234
|
+
} finally {
|
|
235
|
+
await h.close();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
exports.LOCK_FILE = LOCK_FILE; exports.SqliteDriver = SqliteDriver;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { A as AppliedMigration, B as BackupUnsupportedError, C as CanceledError, a as Checker, b as CheckerOptions, D as DEFAULT_MIGRATION_TOPICS, c as DocApplied, d as DocError, e as DocKind, f as DocOptions, g as DocReport, h as DocStep, E as EventPublisher, J as JsonObject, M as Migration, j as MigrationPayload, k as MigrationRunPayload, l as MigrationTopics, m as Migrator, n as MigratorOptions, N as NotRestoredError, P as PUBLISH_SOURCE, R as Registry, S as SchemaDriver, o as SchemaStatus, p as Semver, U as Unlock, q as UpgradeCheckError, r as UpgradeCheckResult, s as UpgradeError, V as VerifyError, t as abortError, u as compareSemver, v as compareVersions, w as createChecker, x as defaultDataDir, y as defaultRegistry, z as errorText, F as isBackupUnsupported, G as isCanceled, H as isNotRestored, I as isVerifyError, K as migrateDocument, L as migrateDocumentValue, O as parseSemver, Q as readVersionFile, T as registerMigration, W as resetRegistryForTest, X as topicsFromPrefix, Y as trimV, Z as writeVersionFile } from '../index-BqZ7WGQf.js';
|
|
2
|
+
import '../bus-D2bAOPYz.js';
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
var _chunkVKQCOVKIjs = require('../chunk-VKQCOVKI.js');
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
var _chunkIDM6YZXNjs = require('../chunk-IDM6YZXN.js');
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
var _chunkI6XDPSXQjs = require('../chunk-I6XDPSXQ.js');
|
|
38
|
+
require('../chunk-74E6RNOI.js');
|
|
39
|
+
require('../chunk-4UE5TCMQ.js');
|
|
40
|
+
require('../chunk-R5U7XKVJ.js');
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
exports.BackupUnsupportedError = _chunkI6XDPSXQjs.BackupUnsupportedError; exports.CanceledError = _chunkI6XDPSXQjs.CanceledError; exports.DEFAULT_MIGRATION_TOPICS = _chunkVKQCOVKIjs.DEFAULT_MIGRATION_TOPICS; exports.DocError = _chunkVKQCOVKIjs.DocError; exports.Migrator = _chunkVKQCOVKIjs.Migrator; exports.NotRestoredError = _chunkI6XDPSXQjs.NotRestoredError; exports.PUBLISH_SOURCE = _chunkVKQCOVKIjs.PUBLISH_SOURCE; exports.Registry = _chunkVKQCOVKIjs.Registry; exports.UpgradeCheckError = _chunkVKQCOVKIjs.UpgradeCheckError; exports.UpgradeError = _chunkI6XDPSXQjs.UpgradeError; exports.VerifyError = _chunkI6XDPSXQjs.VerifyError; exports.abortError = _chunkI6XDPSXQjs.abortError; exports.compareSemver = _chunkIDM6YZXNjs.compareSemver; exports.compareVersions = _chunkIDM6YZXNjs.compareVersions; exports.createChecker = _chunkVKQCOVKIjs.createChecker; exports.defaultDataDir = _chunkIDM6YZXNjs.defaultDataDir; exports.defaultRegistry = _chunkVKQCOVKIjs.defaultRegistry; exports.errorText = _chunkI6XDPSXQjs.errorText; exports.isBackupUnsupported = _chunkI6XDPSXQjs.isBackupUnsupported; exports.isCanceled = _chunkI6XDPSXQjs.isCanceled; exports.isNotRestored = _chunkI6XDPSXQjs.isNotRestored; exports.isVerifyError = _chunkI6XDPSXQjs.isVerifyError; exports.migrateDocument = _chunkVKQCOVKIjs.migrateDocument; exports.migrateDocumentValue = _chunkVKQCOVKIjs.migrateDocumentValue; exports.parseSemver = _chunkIDM6YZXNjs.parseSemver; exports.readVersionFile = _chunkIDM6YZXNjs.readVersionFile; exports.registerMigration = _chunkVKQCOVKIjs.registerMigration; exports.resetRegistryForTest = _chunkVKQCOVKIjs.resetRegistryForTest; exports.topicsFromPrefix = _chunkVKQCOVKIjs.topicsFromPrefix; exports.trimV = _chunkIDM6YZXNjs.trimV; exports.writeVersionFile = _chunkIDM6YZXNjs.writeVersionFile;
|