@solidactions/cli 3.39.0 → 3.40.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 +3 -0
- package/dist/command-manifest.json +14 -1
- package/dist/commands/database.js +506 -28
- package/dist/index.js +3 -1
- package/dist/utils/database-pull-guidance.js +20 -0
- package/dist/utils/database-replica-cache.js +432 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -110,6 +110,7 @@ solidactions database exec analytics "DELETE FROM events WHERE expired = 1" --ye
|
|
|
110
110
|
solidactions database dump analytics backup.sql --yes
|
|
111
111
|
solidactions database pull analytics .solidactions/databases/analytics.db --yes
|
|
112
112
|
solidactions database pull analytics --writable
|
|
113
|
+
solidactions database pull analytics --no-cache
|
|
113
114
|
solidactions database import analytics backup.sql --yes
|
|
114
115
|
solidactions database import analytics backup.sql --resume <checkpoint> --yes
|
|
115
116
|
solidactions database push analytics complete.db --yes
|
|
@@ -133,6 +134,8 @@ solidactions database versions metrics --limit 20 --json
|
|
|
133
134
|
solidactions database rollback metrics 2 --yes --json
|
|
134
135
|
```
|
|
135
136
|
|
|
137
|
+
`database pull` fetches only what changed since your last pull of the same database and keeps a hidden cache per database in `~/.solidactions/cache/database-replicas`; use `--no-cache` to skip it. Inside a workflow or function, query the database directly instead of pulling (`ctx.vars` with `createDatabaseClient`). Pull only for local analysis, keep and reuse one copy, and never pull per test or per preview.
|
|
138
|
+
|
|
136
139
|
`--json` produces machine-readable output where supported. `delete`, `exec`,
|
|
137
140
|
`dump` overwrites, `pull` overwrites, `drop-table`, and `import` prompt before
|
|
138
141
|
changing data; use `--yes` only when that change is already approved. A
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"schema_version": 1,
|
|
3
3
|
"cli_name": "solidactions",
|
|
4
|
-
"cli_version": "3.
|
|
4
|
+
"cli_version": "3.40.0",
|
|
5
5
|
"global_options": [
|
|
6
6
|
{
|
|
7
7
|
"flags": "-V, --version",
|
|
@@ -953,6 +953,19 @@
|
|
|
953
953
|
"negated": false,
|
|
954
954
|
"hidden": false
|
|
955
955
|
},
|
|
956
|
+
{
|
|
957
|
+
"flags": "--no-cache",
|
|
958
|
+
"long": "--no-cache",
|
|
959
|
+
"short": null,
|
|
960
|
+
"description": "Do not read or write the local replica cache for this pull",
|
|
961
|
+
"required": false,
|
|
962
|
+
"value_required": false,
|
|
963
|
+
"value_optional": false,
|
|
964
|
+
"variadic": false,
|
|
965
|
+
"negated": true,
|
|
966
|
+
"hidden": false,
|
|
967
|
+
"default": true
|
|
968
|
+
},
|
|
956
969
|
{
|
|
957
970
|
"flags": "-h, --help",
|
|
958
971
|
"long": "--help",
|
|
@@ -94,6 +94,8 @@ const database_sql_import_runner_1 = require("../utils/database-sql-import-runne
|
|
|
94
94
|
const table_1 = require("../utils/table");
|
|
95
95
|
const database_download_1 = require("../utils/database-download");
|
|
96
96
|
const database_ingest_digest_1 = require("../utils/database-ingest-digest");
|
|
97
|
+
const database_pull_guidance_1 = require("../utils/database-pull-guidance");
|
|
98
|
+
const database_replica_cache_1 = require("../utils/database-replica-cache");
|
|
97
99
|
function defaultConfirmation(message) {
|
|
98
100
|
const input = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
99
101
|
return new Promise((resolve) => {
|
|
@@ -133,6 +135,8 @@ function resolveDependencies(dependencies) {
|
|
|
133
135
|
setTimeout(resolve, milliseconds);
|
|
134
136
|
})),
|
|
135
137
|
zstdAvailable: dependencies.zstdAvailable ?? (0, database_download_1.zstdDecompressAvailable)(),
|
|
138
|
+
replicaCacheRoot: dependencies.replicaCacheRoot,
|
|
139
|
+
env: dependencies.env ?? process.env,
|
|
136
140
|
put: dependencies.put ?? axios_1.default.put,
|
|
137
141
|
};
|
|
138
142
|
}
|
|
@@ -664,7 +668,10 @@ async function removeOwnedFiles(filesystem, paths) {
|
|
|
664
668
|
}
|
|
665
669
|
}
|
|
666
670
|
function pullSidecars(temp) {
|
|
667
|
-
|
|
671
|
+
// The real database host's sync client also leaves `${temp}-info` (68 bytes) beside the
|
|
672
|
+
// replica; it must be owned here or `finalizeReplica` renames only the main file and the
|
|
673
|
+
// hidden `-info` stays beside the user's copy (cli#266).
|
|
674
|
+
return [`${temp}-wal`, `${temp}-shm`, `${temp}-journal`, `${temp}-client_wal_index`, `${temp}-info`];
|
|
668
675
|
}
|
|
669
676
|
async function anyPathExists(filesystem, candidates) {
|
|
670
677
|
for (const candidate of candidates) {
|
|
@@ -1363,12 +1370,16 @@ async function databaseDumpWithConfig(name, file, options, config, dependencies
|
|
|
1363
1370
|
throw freshSafeFileError(error);
|
|
1364
1371
|
}
|
|
1365
1372
|
}
|
|
1366
|
-
async function databaseWritablePullWithConfig(name, destination, options, config, io) {
|
|
1373
|
+
async function databaseWritablePullWithConfig(name, destination, options, config, io, row) {
|
|
1367
1374
|
const target = resolveDatabaseDestination(io, destination, path_1.default.join('.solidactions', 'databases', `${safeDatabaseStem(name)}.db`));
|
|
1368
1375
|
let temp;
|
|
1369
1376
|
let reservationHandle;
|
|
1370
1377
|
let ownsTemp = false;
|
|
1371
1378
|
let ownsSidecars = false;
|
|
1379
|
+
// With a cache (cli#262) the libSQL replica lives in the cache and `temp` stays the open reservation.
|
|
1380
|
+
let cache;
|
|
1381
|
+
let cacheOrigin = null;
|
|
1382
|
+
let reserved;
|
|
1372
1383
|
let client;
|
|
1373
1384
|
let localCreateClient;
|
|
1374
1385
|
let credentialDeadline;
|
|
@@ -1389,42 +1400,59 @@ async function databaseWritablePullWithConfig(name, destination, options, config
|
|
|
1389
1400
|
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database file operation failed.');
|
|
1390
1401
|
}
|
|
1391
1402
|
};
|
|
1392
|
-
const openAttachedClient = async (initial) => {
|
|
1403
|
+
const openAttachedClient = async (initial, preloaded) => {
|
|
1404
|
+
const replicaPath = cache?.replicaPath ?? temp;
|
|
1405
|
+
// The cache module checks its own folders (`<root>/<key>`, `data`); a symlinked ancestor of the
|
|
1406
|
+
// cache root (a linked home) is the user's setup, so only the replica file itself is checked there (I3).
|
|
1407
|
+
const assertSafeReplicaAncestors = async () => {
|
|
1408
|
+
if (!cache)
|
|
1409
|
+
await assertNoSymlinkComponents(io.filesystem, replicaPath);
|
|
1410
|
+
};
|
|
1393
1411
|
if (!initial) {
|
|
1394
|
-
await
|
|
1395
|
-
const beforeMint = await lstatIfPresent(io.filesystem,
|
|
1412
|
+
await assertSafeReplicaAncestors();
|
|
1413
|
+
const beforeMint = await lstatIfPresent(io.filesystem, replicaPath);
|
|
1396
1414
|
if (!beforeMint || beforeMint.isSymbolicLink() || !beforeMint.isFile()) {
|
|
1397
1415
|
throw new database_data_plane_1.DatabaseOperationError('unsafe_destination', 'The database replica temporary path is unsafe.');
|
|
1398
1416
|
}
|
|
1399
1417
|
}
|
|
1400
|
-
let loaded;
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1418
|
+
let loaded = preloaded;
|
|
1419
|
+
if (!loaded) {
|
|
1420
|
+
try {
|
|
1421
|
+
loaded = await (0, database_client_support_1.loadDatabaseClientBeforeMint)(() => (0, database_data_plane_1.requestDatabaseAccess)(config, name, 'write', requestDependencies(io)), { loadClient: io.loadClient });
|
|
1422
|
+
}
|
|
1423
|
+
catch (error) {
|
|
1424
|
+
const safeError = freshSafeFileError(error);
|
|
1425
|
+
if (!initial && error instanceof database_data_plane_1.DatabaseOperationError) {
|
|
1426
|
+
throw new WritableRenewalAttemptError(safeError, true);
|
|
1427
|
+
}
|
|
1428
|
+
throw safeError;
|
|
1408
1429
|
}
|
|
1409
|
-
throw safeError;
|
|
1410
1430
|
}
|
|
1411
1431
|
const access = validateWritableAccess(loaded.access, io.now(), io.monotonicNow());
|
|
1412
1432
|
const createClient = loaded.createClient;
|
|
1413
|
-
|
|
1433
|
+
// Another server answered the renewal mint: the cache replica is not its copy (plan D3, I6).
|
|
1434
|
+
if (cache && !initial && (0, database_replica_cache_1.syncOriginOf)(access.url) !== cacheOrigin) {
|
|
1435
|
+
throw new WritableRenewalAttemptError(new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database file operation failed.'), true);
|
|
1436
|
+
}
|
|
1437
|
+
if (initial && !cache) {
|
|
1414
1438
|
await handoffReplicaReservation(io, temp, reservationHandle);
|
|
1415
1439
|
reservationHandle = undefined;
|
|
1416
1440
|
ownsTemp = false;
|
|
1417
1441
|
}
|
|
1418
|
-
await
|
|
1419
|
-
const beforeCreate = await lstatIfPresent(io.filesystem,
|
|
1420
|
-
if ((initial && beforeCreate !== null)
|
|
1442
|
+
await assertSafeReplicaAncestors();
|
|
1443
|
+
const beforeCreate = await lstatIfPresent(io.filesystem, replicaPath);
|
|
1444
|
+
if ((initial && !cache && beforeCreate !== null)
|
|
1445
|
+
// A warm cache replica is a regular file the session continues (review finding 8b). A non-regular
|
|
1446
|
+
// one was wiped before this open; one appearing since is a race and is refused.
|
|
1447
|
+
|| (initial && cache !== undefined && beforeCreate !== null
|
|
1448
|
+
&& (beforeCreate.isSymbolicLink() || !beforeCreate.isFile()))
|
|
1421
1449
|
|| (!initial && (!beforeCreate || beforeCreate.isSymbolicLink() || !beforeCreate.isFile()))) {
|
|
1422
1450
|
throw new database_data_plane_1.DatabaseOperationError('unsafe_destination', 'The database replica temporary path is unsafe.');
|
|
1423
1451
|
}
|
|
1424
1452
|
let opened;
|
|
1425
1453
|
try {
|
|
1426
1454
|
opened = createClient({
|
|
1427
|
-
url: (0, url_1.pathToFileURL)(
|
|
1455
|
+
url: (0, url_1.pathToFileURL)(replicaPath).href,
|
|
1428
1456
|
syncUrl: access.url,
|
|
1429
1457
|
authToken: access.token,
|
|
1430
1458
|
intMode: 'string',
|
|
@@ -1452,7 +1480,7 @@ async function databaseWritablePullWithConfig(name, destination, options, config
|
|
|
1452
1480
|
catch {
|
|
1453
1481
|
syncFailed = true;
|
|
1454
1482
|
}
|
|
1455
|
-
const replicaStat = await lstatIfPresent(io.filesystem,
|
|
1483
|
+
const replicaStat = await lstatIfPresent(io.filesystem, replicaPath);
|
|
1456
1484
|
if (replicaStat?.isFile() && !replicaStat.isSymbolicLink())
|
|
1457
1485
|
ownsTemp = true;
|
|
1458
1486
|
if (syncFailed
|
|
@@ -1472,6 +1500,28 @@ async function databaseWritablePullWithConfig(name, destination, options, config
|
|
|
1472
1500
|
localCreateClient = createClient;
|
|
1473
1501
|
credentialDeadline = access.deadline;
|
|
1474
1502
|
};
|
|
1503
|
+
const discardReservation = async () => {
|
|
1504
|
+
if (reservationHandle) {
|
|
1505
|
+
try {
|
|
1506
|
+
await reservationHandle.close();
|
|
1507
|
+
}
|
|
1508
|
+
catch {
|
|
1509
|
+
// Cleanup below remains scoped to the exclusively reserved path.
|
|
1510
|
+
}
|
|
1511
|
+
reservationHandle = undefined;
|
|
1512
|
+
}
|
|
1513
|
+
// With a cache the temp is ours only while it is still the reserved inode.
|
|
1514
|
+
if (temp && ownsTemp && reserved)
|
|
1515
|
+
ownsTemp = await isReservedFile(io, temp, reserved);
|
|
1516
|
+
if (temp) {
|
|
1517
|
+
await removeOwnedFiles(io.filesystem, [
|
|
1518
|
+
...(ownsTemp ? [temp] : []),
|
|
1519
|
+
...(ownsSidecars ? pullSidecars(temp) : []),
|
|
1520
|
+
]);
|
|
1521
|
+
}
|
|
1522
|
+
ownsTemp = false;
|
|
1523
|
+
ownsSidecars = false;
|
|
1524
|
+
};
|
|
1475
1525
|
try {
|
|
1476
1526
|
if (!await confirmFileOverwrite(target, options, io))
|
|
1477
1527
|
return;
|
|
@@ -1483,7 +1533,77 @@ async function databaseWritablePullWithConfig(name, destination, options, config
|
|
|
1483
1533
|
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database replica temporary files already exist.');
|
|
1484
1534
|
}
|
|
1485
1535
|
ownsSidecars = true;
|
|
1486
|
-
|
|
1536
|
+
let first;
|
|
1537
|
+
if (row && cacheEligible(io, options)) {
|
|
1538
|
+
// The first mint comes before the cache: its URL host is part of the cache identity (review finding 1).
|
|
1539
|
+
const minted = await (0, database_client_support_1.loadDatabaseClientBeforeMint)(() => (0, database_data_plane_1.requestDatabaseAccess)(config, name, 'write', requestDependencies(io)), { loadClient: io.loadClient });
|
|
1540
|
+
const outcome = await (0, database_replica_cache_1.acquireReplicaCache)({
|
|
1541
|
+
root: io.replicaCacheRoot,
|
|
1542
|
+
identity: cacheIdentity(config, name, row, minted.access),
|
|
1543
|
+
now: io.now,
|
|
1544
|
+
});
|
|
1545
|
+
if (outcome.kind === 'skipped') {
|
|
1546
|
+
const note = cacheSkipNote(outcome.reason);
|
|
1547
|
+
if (note !== undefined)
|
|
1548
|
+
io.stderr(note);
|
|
1549
|
+
}
|
|
1550
|
+
else {
|
|
1551
|
+
cache = outcome.cache;
|
|
1552
|
+
cacheOrigin = (0, database_replica_cache_1.syncOriginOf)(minted.access.url);
|
|
1553
|
+
first = minted;
|
|
1554
|
+
const reservedStat = await reservationHandle.stat();
|
|
1555
|
+
reserved = { dev: reservedStat.dev, ino: reservedStat.ino };
|
|
1556
|
+
}
|
|
1557
|
+
}
|
|
1558
|
+
/** Wipes the cache; when the wipe itself fails, drops the cache (lock released) so the session runs uncached (I3). */
|
|
1559
|
+
const wipeOrDropCache = async () => {
|
|
1560
|
+
try {
|
|
1561
|
+
await cache.wipe();
|
|
1562
|
+
return true;
|
|
1563
|
+
}
|
|
1564
|
+
catch {
|
|
1565
|
+
await cache.release();
|
|
1566
|
+
cache = undefined;
|
|
1567
|
+
reserved = undefined;
|
|
1568
|
+
return false;
|
|
1569
|
+
}
|
|
1570
|
+
};
|
|
1571
|
+
if (cache) {
|
|
1572
|
+
// A replica that is a symlink or not a regular file is a cache fault: wipe and open cold (I3).
|
|
1573
|
+
const existingReplica = await lstatIfPresent(io.filesystem, cache.replicaPath);
|
|
1574
|
+
if (existingReplica && (existingReplica.isSymbolicLink() || !existingReplica.isFile())) {
|
|
1575
|
+
await wipeOrDropCache();
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
if (!cache) {
|
|
1579
|
+
await openAttachedClient(true);
|
|
1580
|
+
}
|
|
1581
|
+
else {
|
|
1582
|
+
const startedWarm = cache.warm;
|
|
1583
|
+
try {
|
|
1584
|
+
await openAttachedClient(true, first);
|
|
1585
|
+
}
|
|
1586
|
+
catch (error) {
|
|
1587
|
+
if (!(error instanceof database_data_plane_1.DatabaseOperationError) || error.code !== 'upstream_unavailable')
|
|
1588
|
+
throw error;
|
|
1589
|
+
if (!startedWarm) {
|
|
1590
|
+
// A cold cache that cannot open or sync (a full disk, an unopenable cache folder) must not fail
|
|
1591
|
+
// a session the uncached path would open: drop the cache and open uncached, once (I3).
|
|
1592
|
+
await cache.wipe().catch(() => undefined);
|
|
1593
|
+
await cache.release();
|
|
1594
|
+
cache = undefined;
|
|
1595
|
+
reserved = undefined;
|
|
1596
|
+
await openAttachedClient(true);
|
|
1597
|
+
}
|
|
1598
|
+
else if (await wipeOrDropCache()) {
|
|
1599
|
+
// A warm replica the server will not continue: wipe it and open cold, once (review finding 8b).
|
|
1600
|
+
await openAttachedClient(true, first);
|
|
1601
|
+
}
|
|
1602
|
+
else {
|
|
1603
|
+
await openAttachedClient(true);
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
}
|
|
1487
1607
|
io.stdout(WRITABLE_WARNING);
|
|
1488
1608
|
inputSession = createWritableInput(io);
|
|
1489
1609
|
const accumulator = new database_sql_import_1.DatabaseSqlStatementAccumulator();
|
|
@@ -1597,9 +1717,39 @@ async function databaseWritablePullWithConfig(name, destination, options, config
|
|
|
1597
1717
|
}
|
|
1598
1718
|
if (sessionFailure && !publishAfterFailure)
|
|
1599
1719
|
throw sessionFailure;
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1720
|
+
if (cache) {
|
|
1721
|
+
try {
|
|
1722
|
+
await snapshotCacheReplica(io, localCreateClient, cache, { temp, handle: reservationHandle });
|
|
1723
|
+
}
|
|
1724
|
+
catch {
|
|
1725
|
+
// The session's writes are on the server already (readYourWrites, offline: false):
|
|
1726
|
+
// replay nothing, publish a read-only copy through the uncached pull (review finding 8a).
|
|
1727
|
+
await cache.wipe().catch(() => undefined);
|
|
1728
|
+
await discardReservation();
|
|
1729
|
+
io.stderr("The live session's writes are saved. Publishing a read-only copy instead.");
|
|
1730
|
+
// `io` is the resolved dependency object, which satisfies DatabaseCommandDependencies.
|
|
1731
|
+
await databasePullWithConfig(name, destination, { yes: true, cache: false, skipWorkflowWarning: true }, config, io);
|
|
1732
|
+
if (sessionFailure)
|
|
1733
|
+
throw sessionFailure;
|
|
1734
|
+
return;
|
|
1735
|
+
}
|
|
1736
|
+
reservationHandle = undefined;
|
|
1737
|
+
await publishSnapshot(io, temp, target, reserved);
|
|
1738
|
+
ownsTemp = false;
|
|
1739
|
+
ownsSidecars = false;
|
|
1740
|
+
try {
|
|
1741
|
+
await cache.markSynced();
|
|
1742
|
+
}
|
|
1743
|
+
catch {
|
|
1744
|
+
// The file is published; a cache whose meta.json could not be written is not trusted again (D5).
|
|
1745
|
+
await cache.wipe().catch(() => undefined);
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
else {
|
|
1749
|
+
await finalizeReplica(io, temp, target, localCreateClient);
|
|
1750
|
+
ownsTemp = false;
|
|
1751
|
+
ownsSidecars = false;
|
|
1752
|
+
}
|
|
1603
1753
|
if (sessionFailure)
|
|
1604
1754
|
throw sessionFailure;
|
|
1605
1755
|
io.stdout(`Database replica saved to ${displayDestination(io, target)}.`);
|
|
@@ -1621,6 +1771,167 @@ async function databaseWritablePullWithConfig(name, destination, options, config
|
|
|
1621
1771
|
// Cleanup below remains scoped to this invocation's replica.
|
|
1622
1772
|
}
|
|
1623
1773
|
}
|
|
1774
|
+
await discardReservation();
|
|
1775
|
+
throw freshSafeFileError(error);
|
|
1776
|
+
}
|
|
1777
|
+
finally {
|
|
1778
|
+
await cache?.release();
|
|
1779
|
+
}
|
|
1780
|
+
}
|
|
1781
|
+
/** A cache problem inside a cached pull: the cache is wiped and the uncached path runs (plan D5, invariant I3). */
|
|
1782
|
+
class ReplicaCacheFallback extends Error {
|
|
1783
|
+
}
|
|
1784
|
+
const SNAPSHOT_COPY_CHUNK_BYTES = 1024 * 1024;
|
|
1785
|
+
function cacheEligible(io, options) {
|
|
1786
|
+
return options.cache !== false && typeof io.replicaCacheRoot === 'string' && io.replicaCacheRoot !== '';
|
|
1787
|
+
}
|
|
1788
|
+
function cacheSkipNote(reason) {
|
|
1789
|
+
if (reason === 'busy') {
|
|
1790
|
+
return 'Another pull of this database is using the local cache; downloading a full copy instead.';
|
|
1791
|
+
}
|
|
1792
|
+
if (reason === 'rewritten_often') {
|
|
1793
|
+
return 'This database is replaced often, so its local cache is off for now; downloading a full copy.';
|
|
1794
|
+
}
|
|
1795
|
+
return undefined;
|
|
1796
|
+
}
|
|
1797
|
+
function cacheIdentity(config, name, row, access) {
|
|
1798
|
+
return {
|
|
1799
|
+
host: config.host,
|
|
1800
|
+
workspaceId: config.workspaceId ?? '',
|
|
1801
|
+
name,
|
|
1802
|
+
databaseId: row.id ?? null,
|
|
1803
|
+
createdAt: row.kind === 'duckdb_file' ? null : row.created_at ?? null,
|
|
1804
|
+
syncOrigin: (0, database_replica_cache_1.syncOriginOf)(access.url),
|
|
1805
|
+
};
|
|
1806
|
+
}
|
|
1807
|
+
function isQuickCheckOk(result) {
|
|
1808
|
+
return Array.isArray(result?.rows) && result.rows.length === 1 && result.rows[0]?.[0] === 'ok';
|
|
1809
|
+
}
|
|
1810
|
+
/** Runs `operation` on a credential-free local client of `file` (no `syncUrl`) and always closes it. */
|
|
1811
|
+
async function withLocalSnapshotClient(io, createClient, file, operation) {
|
|
1812
|
+
let client;
|
|
1813
|
+
let closePromise;
|
|
1814
|
+
const closeClient = () => {
|
|
1815
|
+
closePromise ??= (0, database_data_plane_1.runDatabaseClientOperationWithDeadline)(async () => { await client?.close(); }, undefined, io.dataPlaneTimeoutMs);
|
|
1816
|
+
return closePromise;
|
|
1817
|
+
};
|
|
1818
|
+
try {
|
|
1819
|
+
client = createClient({ url: (0, url_1.pathToFileURL)(file).href, intMode: 'string' });
|
|
1820
|
+
await operation((sql) => (0, database_data_plane_1.runDatabaseClientOperationWithDeadline)(() => client.execute(sql), closeClient, io.dataPlaneTimeoutMs));
|
|
1821
|
+
}
|
|
1822
|
+
finally {
|
|
1823
|
+
if (client)
|
|
1824
|
+
await closeClient();
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
/** Copies the snapshot's bytes into the still-open reservation; never unlinks or recreates it by path. */
|
|
1828
|
+
async function copySnapshotIntoReservation(io, snapshotPath, handle) {
|
|
1829
|
+
const source = await io.filesystem.open(snapshotPath, 'r');
|
|
1830
|
+
let snapshotSize;
|
|
1831
|
+
let copied = 0;
|
|
1832
|
+
try {
|
|
1833
|
+
snapshotSize = (await source.stat()).size;
|
|
1834
|
+
await handle.truncate(0);
|
|
1835
|
+
const buffer = Buffer.alloc(SNAPSHOT_COPY_CHUNK_BYTES);
|
|
1836
|
+
for (;;) {
|
|
1837
|
+
const { bytesRead } = await source.read(buffer, 0, buffer.byteLength, copied);
|
|
1838
|
+
if (bytesRead === 0)
|
|
1839
|
+
break;
|
|
1840
|
+
let offset = 0;
|
|
1841
|
+
while (offset < bytesRead) {
|
|
1842
|
+
const { bytesWritten } = await handle.write(buffer, offset, bytesRead - offset, copied + offset);
|
|
1843
|
+
if (bytesWritten <= 0)
|
|
1844
|
+
throw new ReplicaCacheFallback('The snapshot copy made no progress.');
|
|
1845
|
+
offset += bytesWritten;
|
|
1846
|
+
}
|
|
1847
|
+
copied += bytesRead;
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
finally {
|
|
1851
|
+
await source.close();
|
|
1852
|
+
}
|
|
1853
|
+
await handle.sync();
|
|
1854
|
+
if (copied !== snapshotSize || (await handle.stat()).size !== snapshotSize) {
|
|
1855
|
+
throw new ReplicaCacheFallback('The snapshot copy is incomplete.');
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
/**
|
|
1859
|
+
* Turns a synced cache replica into the reserved temp's bytes, all through the real local client:
|
|
1860
|
+
* `PRAGMA quick_check` on the replica (review N1: `VACUUM INTO` rebuilds the free list, so only
|
|
1861
|
+
* this check sees damage there), `VACUUM INTO` the cache's own `snapshotPath`, `PRAGMA quick_check`
|
|
1862
|
+
* on the snapshot (review finding 3), then a copy into the still-open reservation handle (review
|
|
1863
|
+
* finding 12), which it closes. The reserved file's `{ dev, ino }` is the caller's `handle.stat()`
|
|
1864
|
+
* (the same open handle, so the same inode) and goes to `publishSnapshot`. Any failure throws
|
|
1865
|
+
* `ReplicaCacheFallback`.
|
|
1866
|
+
*/
|
|
1867
|
+
async function snapshotCacheReplica(io, createClient, cache, reservation) {
|
|
1868
|
+
try {
|
|
1869
|
+
await withLocalSnapshotClient(io, createClient, cache.replicaPath, async (execute) => {
|
|
1870
|
+
if (!isQuickCheckOk(await execute('PRAGMA quick_check'))) {
|
|
1871
|
+
throw new ReplicaCacheFallback('The cache replica failed PRAGMA quick_check.');
|
|
1872
|
+
}
|
|
1873
|
+
await execute(`VACUUM INTO '${cache.snapshotPath.replaceAll("'", "''")}'`);
|
|
1874
|
+
});
|
|
1875
|
+
await withLocalSnapshotClient(io, createClient, cache.snapshotPath, async (execute) => {
|
|
1876
|
+
if (!isQuickCheckOk(await execute('PRAGMA quick_check'))) {
|
|
1877
|
+
throw new ReplicaCacheFallback('The snapshot failed PRAGMA quick_check.');
|
|
1878
|
+
}
|
|
1879
|
+
});
|
|
1880
|
+
await copySnapshotIntoReservation(io, cache.snapshotPath, reservation.handle);
|
|
1881
|
+
await reservation.handle.close();
|
|
1882
|
+
await cache.discardSnapshot();
|
|
1883
|
+
}
|
|
1884
|
+
catch (error) {
|
|
1885
|
+
if (error instanceof ReplicaCacheFallback)
|
|
1886
|
+
throw error;
|
|
1887
|
+
throw new ReplicaCacheFallback('The cache snapshot could not be made.');
|
|
1888
|
+
}
|
|
1889
|
+
}
|
|
1890
|
+
/** Publishes the reserved temp (the snapshot's bytes) only while it is still the reserved inode. */
|
|
1891
|
+
async function publishSnapshot(io, temp, target, expected) {
|
|
1892
|
+
await assertNoSymlinkComponents(io.filesystem, temp);
|
|
1893
|
+
const stat = await lstatIfPresent(io.filesystem, temp);
|
|
1894
|
+
if (!stat
|
|
1895
|
+
|| stat.isSymbolicLink()
|
|
1896
|
+
|| !stat.isFile()
|
|
1897
|
+
|| stat.dev !== expected.dev
|
|
1898
|
+
|| stat.ino !== expected.ino) {
|
|
1899
|
+
throw new database_data_plane_1.DatabaseOperationError('unsafe_destination', 'The database replica temporary path is unsafe.');
|
|
1900
|
+
}
|
|
1901
|
+
await removeOwnedFiles(io.filesystem, pullSidecars(temp));
|
|
1902
|
+
if (await anyPathExists(io.filesystem, pullSidecars(temp))) {
|
|
1903
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database file operation failed.');
|
|
1904
|
+
}
|
|
1905
|
+
await io.filesystem.chmod(temp, 0o444);
|
|
1906
|
+
await assertNoSymlinkComponents(io.filesystem, target);
|
|
1907
|
+
await io.filesystem.rename(temp, target);
|
|
1908
|
+
}
|
|
1909
|
+
async function isReservedFile(io, temp, expected) {
|
|
1910
|
+
try {
|
|
1911
|
+
const stat = await lstatIfPresent(io.filesystem, temp);
|
|
1912
|
+
return stat !== null
|
|
1913
|
+
&& stat.isFile()
|
|
1914
|
+
&& !stat.isSymbolicLink()
|
|
1915
|
+
&& stat.dev === expected.dev
|
|
1916
|
+
&& stat.ino === expected.ino;
|
|
1917
|
+
}
|
|
1918
|
+
catch {
|
|
1919
|
+
return false;
|
|
1920
|
+
}
|
|
1921
|
+
}
|
|
1922
|
+
/**
|
|
1923
|
+
* The read-only pull through the hidden replica cache (cli#262). `'handled'`: the target is
|
|
1924
|
+
* published (or the error is thrown). `'fallback'`: nothing is published, the run's own reservation
|
|
1925
|
+
* is gone, and the caller runs the uncached path (plan D5).
|
|
1926
|
+
*/
|
|
1927
|
+
async function databaseCachedReadonlyPullWithConfig(name, target, config, io, row) {
|
|
1928
|
+
let temp;
|
|
1929
|
+
let reservationHandle;
|
|
1930
|
+
let reserved;
|
|
1931
|
+
let ownsTemp = false;
|
|
1932
|
+
let ownsSidecars = false;
|
|
1933
|
+
let cache;
|
|
1934
|
+
const discardReservation = async () => {
|
|
1624
1935
|
if (reservationHandle) {
|
|
1625
1936
|
try {
|
|
1626
1937
|
await reservationHandle.close();
|
|
@@ -1628,15 +1939,165 @@ async function databaseWritablePullWithConfig(name, destination, options, config
|
|
|
1628
1939
|
catch {
|
|
1629
1940
|
// Cleanup below remains scoped to the exclusively reserved path.
|
|
1630
1941
|
}
|
|
1942
|
+
reservationHandle = undefined;
|
|
1631
1943
|
}
|
|
1944
|
+
// The temp is ours only while it is still the reserved inode.
|
|
1945
|
+
if (temp && ownsTemp && reserved)
|
|
1946
|
+
ownsTemp = await isReservedFile(io, temp, reserved);
|
|
1632
1947
|
if (temp) {
|
|
1633
1948
|
await removeOwnedFiles(io.filesystem, [
|
|
1634
1949
|
...(ownsTemp ? [temp] : []),
|
|
1635
1950
|
...(ownsSidecars ? pullSidecars(temp) : []),
|
|
1636
1951
|
]);
|
|
1637
1952
|
}
|
|
1953
|
+
ownsTemp = false;
|
|
1954
|
+
ownsSidecars = false;
|
|
1955
|
+
};
|
|
1956
|
+
try {
|
|
1957
|
+
const reservation = await reserveDestinationTemp(io, target);
|
|
1958
|
+
temp = reservation.temp;
|
|
1959
|
+
reservationHandle = reservation.handle;
|
|
1960
|
+
ownsTemp = true;
|
|
1961
|
+
const reservedStat = await reservationHandle.stat();
|
|
1962
|
+
reserved = { dev: reservedStat.dev, ino: reservedStat.ino };
|
|
1963
|
+
if (await anyPathExists(io.filesystem, pullSidecars(temp))) {
|
|
1964
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database replica temporary files already exist.');
|
|
1965
|
+
}
|
|
1966
|
+
ownsSidecars = true;
|
|
1967
|
+
// The first mint comes before the cache: its URL host is part of the cache identity (review finding 1).
|
|
1968
|
+
const first = await (0, database_client_support_1.loadDatabaseClientBeforeMint)(() => (0, database_data_plane_1.requestDatabaseAccess)(config, name, 'read', requestDependencies(io)), { loadClient: io.loadClient });
|
|
1969
|
+
const origin = (0, database_replica_cache_1.syncOriginOf)(first.access.url);
|
|
1970
|
+
const outcome = await (0, database_replica_cache_1.acquireReplicaCache)({
|
|
1971
|
+
root: io.replicaCacheRoot,
|
|
1972
|
+
identity: cacheIdentity(config, name, row, first.access),
|
|
1973
|
+
now: io.now,
|
|
1974
|
+
});
|
|
1975
|
+
if (outcome.kind === 'skipped') {
|
|
1976
|
+
await discardReservation();
|
|
1977
|
+
const note = cacheSkipNote(outcome.reason);
|
|
1978
|
+
if (note !== undefined)
|
|
1979
|
+
io.stderr(note);
|
|
1980
|
+
return 'fallback';
|
|
1981
|
+
}
|
|
1982
|
+
cache = outcome.cache;
|
|
1983
|
+
const replicaUrl = (0, url_1.pathToFileURL)(cache.replicaPath).href;
|
|
1984
|
+
const cacheReplicaStat = async () => {
|
|
1985
|
+
try {
|
|
1986
|
+
return await lstatIfPresent(io.filesystem, cache.replicaPath);
|
|
1987
|
+
}
|
|
1988
|
+
catch {
|
|
1989
|
+
throw new ReplicaCacheFallback('The cache replica cannot be read.');
|
|
1990
|
+
}
|
|
1991
|
+
};
|
|
1992
|
+
let createClient = first.createClient;
|
|
1993
|
+
let access = first.access;
|
|
1994
|
+
let synced = false;
|
|
1995
|
+
let credentialRemintUsed = false;
|
|
1996
|
+
for (let attempt = 0; attempt < PULL_ATTEMPTS; attempt += 1) {
|
|
1997
|
+
if (attempt > 0) {
|
|
1998
|
+
const minted = await (0, database_client_support_1.loadDatabaseClientBeforeMint)(() => (0, database_data_plane_1.requestDatabaseAccess)(config, name, 'read', requestDependencies(io)), { loadClient: io.loadClient });
|
|
1999
|
+
createClient = minted.createClient;
|
|
2000
|
+
access = minted.access;
|
|
2001
|
+
// Another server answered the re-mint: this replica is not its copy (plan D3, I6).
|
|
2002
|
+
if ((0, database_replica_cache_1.syncOriginOf)(access.url) !== origin) {
|
|
2003
|
+
throw new ReplicaCacheFallback('The sync origin changed during the pull.');
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
let client;
|
|
2007
|
+
try {
|
|
2008
|
+
client = createClient({
|
|
2009
|
+
url: replicaUrl,
|
|
2010
|
+
syncUrl: access.url,
|
|
2011
|
+
authToken: access.token,
|
|
2012
|
+
intMode: 'string',
|
|
2013
|
+
});
|
|
2014
|
+
}
|
|
2015
|
+
catch {
|
|
2016
|
+
// A replica the native client cannot open, cold or warm, is a cache fault: the uncached path runs once (D5, I3).
|
|
2017
|
+
throw new ReplicaCacheFallback('The cache replica could not be opened.');
|
|
2018
|
+
}
|
|
2019
|
+
let attemptFailure;
|
|
2020
|
+
let closePromise;
|
|
2021
|
+
const closeClient = () => {
|
|
2022
|
+
closePromise ??= (0, database_data_plane_1.runDatabaseClientOperationWithDeadline)(async () => { await client.close(); }, undefined, io.dataPlaneTimeoutMs);
|
|
2023
|
+
return closePromise;
|
|
2024
|
+
};
|
|
2025
|
+
try {
|
|
2026
|
+
if (typeof client.sync !== 'function')
|
|
2027
|
+
throw new Error('Database sync is unavailable.');
|
|
2028
|
+
await (0, database_data_plane_1.runDatabaseClientOperationWithDeadline)(() => client.sync(), closeClient, io.dataPlaneTimeoutMs);
|
|
2029
|
+
}
|
|
2030
|
+
catch (error) {
|
|
2031
|
+
attemptFailure = error;
|
|
2032
|
+
}
|
|
2033
|
+
finally {
|
|
2034
|
+
try {
|
|
2035
|
+
await closeClient();
|
|
2036
|
+
}
|
|
2037
|
+
catch (error) {
|
|
2038
|
+
if (!attemptFailure)
|
|
2039
|
+
attemptFailure = error;
|
|
2040
|
+
}
|
|
2041
|
+
}
|
|
2042
|
+
// The replica lives in the cache: anything but a regular file there is a cache fault (D5).
|
|
2043
|
+
const replicaStat = await cacheReplicaStat();
|
|
2044
|
+
if (replicaStat && (replicaStat.isSymbolicLink() || !replicaStat.isFile())) {
|
|
2045
|
+
throw new ReplicaCacheFallback('The cache replica is not a regular file.');
|
|
2046
|
+
}
|
|
2047
|
+
if (!attemptFailure) {
|
|
2048
|
+
if (!replicaStat)
|
|
2049
|
+
throw new ReplicaCacheFallback('The cache replica is missing after sync.');
|
|
2050
|
+
synced = true;
|
|
2051
|
+
break;
|
|
2052
|
+
}
|
|
2053
|
+
const failureClass = classifyPullFailure(attemptFailure);
|
|
2054
|
+
// A sync that failed for a reason retrying cannot cure, warm or cold (a replica the server will not
|
|
2055
|
+
// continue, a full disk, an unopenable cache folder): wipe, and the uncached path runs its own attempts (review 14, I3).
|
|
2056
|
+
if (failureClass === 'deterministic') {
|
|
2057
|
+
throw new ReplicaCacheFallback('The cache sync failed.');
|
|
2058
|
+
}
|
|
2059
|
+
const hasAnotherAttempt = attempt + 1 < PULL_ATTEMPTS;
|
|
2060
|
+
if (failureClass === 'credential_expiry' && !credentialRemintUsed && hasAnotherAttempt) {
|
|
2061
|
+
credentialRemintUsed = true;
|
|
2062
|
+
continue;
|
|
2063
|
+
}
|
|
2064
|
+
if (failureClass === 'transient_transport' && hasAnotherAttempt) {
|
|
2065
|
+
await io.sleep(PULL_RETRY_BACKOFF_MS[attempt] ?? PULL_RETRY_BACKOFF_MS.at(-1));
|
|
2066
|
+
continue;
|
|
2067
|
+
}
|
|
2068
|
+
break;
|
|
2069
|
+
}
|
|
2070
|
+
// I2: nothing is published from a replica whose sync did not succeed in this invocation.
|
|
2071
|
+
if (!synced) {
|
|
2072
|
+
throw new database_data_plane_1.DatabaseOperationError('upstream_unavailable', 'Database file operation failed.');
|
|
2073
|
+
}
|
|
2074
|
+
await snapshotCacheReplica(io, createClient, cache, { temp, handle: reservationHandle });
|
|
2075
|
+
reservationHandle = undefined;
|
|
2076
|
+
await publishSnapshot(io, temp, target, reserved);
|
|
2077
|
+
ownsTemp = false;
|
|
2078
|
+
ownsSidecars = false;
|
|
2079
|
+
try {
|
|
2080
|
+
await cache.markSynced();
|
|
2081
|
+
}
|
|
2082
|
+
catch {
|
|
2083
|
+
// The file is published; a cache whose meta.json could not be written is not trusted again (D5).
|
|
2084
|
+
await cache.wipe().catch(() => undefined);
|
|
2085
|
+
}
|
|
2086
|
+
io.stdout(`Database replica saved to ${displayDestination(io, target)}.`);
|
|
2087
|
+
return 'handled';
|
|
2088
|
+
}
|
|
2089
|
+
catch (error) {
|
|
2090
|
+
const fallback = error instanceof ReplicaCacheFallback || error instanceof database_replica_cache_1.ReplicaCacheError;
|
|
2091
|
+
if (fallback)
|
|
2092
|
+
await cache?.wipe().catch(() => undefined);
|
|
2093
|
+
await discardReservation();
|
|
2094
|
+
if (fallback)
|
|
2095
|
+
return 'fallback';
|
|
1638
2096
|
throw freshSafeFileError(error);
|
|
1639
2097
|
}
|
|
2098
|
+
finally {
|
|
2099
|
+
await cache?.release();
|
|
2100
|
+
}
|
|
1640
2101
|
}
|
|
1641
2102
|
async function databasePullWithConfig(name, destination, options, config, dependencies = {}) {
|
|
1642
2103
|
const io = resolveDependencies(dependencies);
|
|
@@ -1651,17 +2112,31 @@ async function databasePullWithConfig(name, destination, options, config, depend
|
|
|
1651
2112
|
throw new database_data_plane_1.DatabaseOperationError('invalid_flag_combination', '--version is only for DuckDB databases; a standard database has no numbered versions.');
|
|
1652
2113
|
}
|
|
1653
2114
|
refuseIfAnalyticalRow(row, name, 'sqlite-only');
|
|
2115
|
+
if ((0, database_pull_guidance_1.isInsideWorkflowRun)(io.env) && options.skipWorkflowWarning !== true) {
|
|
2116
|
+
io.stderr(database_pull_guidance_1.WORKFLOW_RUN_PULL_WARNING);
|
|
2117
|
+
}
|
|
1654
2118
|
if (options.writable) {
|
|
1655
|
-
return databaseWritablePullWithConfig(name, destination, options, config, io);
|
|
2119
|
+
return databaseWritablePullWithConfig(name, destination, options, config, io, row);
|
|
1656
2120
|
}
|
|
1657
2121
|
const target = resolveDatabaseDestination(io, destination, path_1.default.join('.solidactions', 'databases', `${safeDatabaseStem(name)}.db`));
|
|
2122
|
+
// Asked once, before either path (plan D8): a cache fallback never asks again.
|
|
2123
|
+
try {
|
|
2124
|
+
if (!await confirmFileOverwrite(target, options, io))
|
|
2125
|
+
return;
|
|
2126
|
+
}
|
|
2127
|
+
catch (error) {
|
|
2128
|
+
throw freshSafeFileError(error);
|
|
2129
|
+
}
|
|
2130
|
+
const handled = cacheEligible(io, options)
|
|
2131
|
+
? await databaseCachedReadonlyPullWithConfig(name, target, config, io, row)
|
|
2132
|
+
: 'fallback';
|
|
2133
|
+
if (handled === 'handled')
|
|
2134
|
+
return;
|
|
1658
2135
|
let temp;
|
|
1659
2136
|
let reservationHandle;
|
|
1660
2137
|
let ownsTemp = false;
|
|
1661
2138
|
let ownsSidecars = false;
|
|
1662
2139
|
try {
|
|
1663
|
-
if (!await confirmFileOverwrite(target, options, io))
|
|
1664
|
-
return;
|
|
1665
2140
|
const reservation = await reserveDestinationTemp(io, target);
|
|
1666
2141
|
temp = reservation.temp;
|
|
1667
2142
|
reservationHandle = reservation.handle;
|
|
@@ -1988,7 +2463,10 @@ async function databaseDump(name, file, options, dependencies = {}) {
|
|
|
1988
2463
|
await databaseDumpWithConfig(name, file, options, await (0, api_1.requireConfigWithWorkspace)(), dependencies);
|
|
1989
2464
|
}
|
|
1990
2465
|
async function databasePull(name, destination, options, dependencies = {}) {
|
|
1991
|
-
await databasePullWithConfig(name, destination, options, await (0, api_1.requireConfigWithWorkspace)(),
|
|
2466
|
+
await databasePullWithConfig(name, destination, options, await (0, api_1.requireConfigWithWorkspace)(), {
|
|
2467
|
+
...dependencies,
|
|
2468
|
+
replicaCacheRoot: dependencies.replicaCacheRoot ?? (0, database_replica_cache_1.defaultReplicaCacheRoot)(),
|
|
2469
|
+
});
|
|
1992
2470
|
}
|
|
1993
2471
|
async function databaseVersions(name, options, dependencies = {}) {
|
|
1994
2472
|
await databaseVersionsWithConfig(name, options, await (0, api_1.requireConfigWithWorkspace)(), dependencies);
|
package/dist/index.js
CHANGED
|
@@ -102,6 +102,7 @@ const database_1 = require("./commands/database");
|
|
|
102
102
|
const database_push_1 = require("./commands/database-push");
|
|
103
103
|
const database_export_1 = require("./commands/database-export");
|
|
104
104
|
const config_1 = require("./utils/config");
|
|
105
|
+
const database_pull_guidance_1 = require("./utils/database-pull-guidance");
|
|
105
106
|
const host_display_1 = require("./utils/host-display");
|
|
106
107
|
const mutating_commands_1 = require("./utils/mutating-commands");
|
|
107
108
|
const agent_freshness_1 = require("./utils/agent-freshness");
|
|
@@ -395,7 +396,8 @@ database
|
|
|
395
396
|
.option('-y, --yes', 'Overwrite without confirmation')
|
|
396
397
|
.option('--writable', 'Open a foreground writable session')
|
|
397
398
|
.option('--version <n>', 'DuckDB databases: pull this published version instead of the latest', (value) => (0, database_1.parseStrictPositiveInteger)(value))
|
|
398
|
-
.
|
|
399
|
+
.option('--no-cache', 'Do not read or write the local replica cache for this pull')
|
|
400
|
+
.addHelpText('after', `${database_pull_guidance_1.DATABASE_PULL_HELP}
|
|
399
401
|
A DuckDB database (--kind duckdb_file) is saved to .solidactions/databases/<name>.duckdb
|
|
400
402
|
unless you give a path. Pulling it needs Node 22.15 or newer. List its versions with
|
|
401
403
|
\`solidactions database versions <name>\`.`)
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DATABASE_PULL_HELP = exports.WORKFLOW_RUN_PULL_WARNING = exports.WORKFLOW_RUN_ENV_KEYS = void 0;
|
|
4
|
+
exports.isInsideWorkflowRun = isInsideWorkflowRun;
|
|
5
|
+
exports.WORKFLOW_RUN_ENV_KEYS = ['SOLIDACTIONS_RUN_ID', 'STEPS_TRIGGER_ID'];
|
|
6
|
+
function isInsideWorkflowRun(env) {
|
|
7
|
+
return exports.WORKFLOW_RUN_ENV_KEYS.some((key) => typeof env[key] === 'string' && env[key] !== '');
|
|
8
|
+
}
|
|
9
|
+
exports.WORKFLOW_RUN_PULL_WARNING = 'Warning: database pull is running inside a workflow run. A workflow or function can query the database directly (ctx.vars with createDatabaseClient); pulling a copy on every run downloads it again each time. Use pull only for local analysis and keep one copy.';
|
|
10
|
+
exports.DATABASE_PULL_HELP = `
|
|
11
|
+
Pull is for analysing a database on your own machine. Inside a workflow or a function,
|
|
12
|
+
query the database directly instead (ctx.vars with createDatabaseClient); never pull per
|
|
13
|
+
run, per test or per preview. Pull once and reuse that copy.
|
|
14
|
+
|
|
15
|
+
A pull fetches only what changed since your last pull of the same database; the first pull
|
|
16
|
+
downloads everything. A hidden cache for each database lives in
|
|
17
|
+
~/.solidactions/cache/database-replicas (delete the folder to start over) and takes
|
|
18
|
+
roughly the database's size on disk. Your file is always a plain read-only copy.
|
|
19
|
+
Use --no-cache to leave the cache out of one pull.
|
|
20
|
+
`;
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ReplicaCacheError = exports.REPLICA_CACHE_HEARTBEAT_MS = exports.REPLICA_CACHE_STALE_RECLAIM_MS = exports.REPLICA_CACHE_STALE_LOCK_MS = exports.REPLICA_CACHE_SKIP_MS = exports.REPLICA_CACHE_REPLACEMENT_WINDOW_MS = exports.REPLICA_CACHE_REPLACEMENT_LIMIT = void 0;
|
|
7
|
+
exports.defaultReplicaCacheRoot = defaultReplicaCacheRoot;
|
|
8
|
+
exports.replicaCacheKey = replicaCacheKey;
|
|
9
|
+
exports.syncOriginOf = syncOriginOf;
|
|
10
|
+
exports.acquireReplicaCache = acquireReplicaCache;
|
|
11
|
+
/**
|
|
12
|
+
* The hidden per-database replica cache behind `database pull` (cli#262, plan D2-D5).
|
|
13
|
+
* Layout: `<root>/<key>/{lock,lock.reclaim,meta.json,snapshot.tmp,data/}`; everything libSQL
|
|
14
|
+
* creates lives under `data/`, so a wipe removes `data/` whole. A replaced server copy (D3)
|
|
15
|
+
* wipes `data/`; a stale lock (D4) is reclaimed only under `lock.reclaim`. Every cache problem
|
|
16
|
+
* answers `skipped` (D5). Nothing here touches anything outside `<root>/<key>/` (apart from
|
|
17
|
+
* creating `<root>`) or stores a credential (I4).
|
|
18
|
+
*/
|
|
19
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
20
|
+
const fs_1 = __importDefault(require("fs"));
|
|
21
|
+
const os_1 = __importDefault(require("os"));
|
|
22
|
+
const path_1 = __importDefault(require("path"));
|
|
23
|
+
exports.REPLICA_CACHE_REPLACEMENT_LIMIT = 3;
|
|
24
|
+
exports.REPLICA_CACHE_REPLACEMENT_WINDOW_MS = 7 * 24 * 60 * 60 * 1000;
|
|
25
|
+
exports.REPLICA_CACHE_SKIP_MS = 7 * 24 * 60 * 60 * 1000;
|
|
26
|
+
exports.REPLICA_CACHE_STALE_LOCK_MS = 60 * 60 * 1000;
|
|
27
|
+
exports.REPLICA_CACHE_STALE_RECLAIM_MS = 60 * 1000;
|
|
28
|
+
/** How often a held lock's mtime is refreshed, so another host never judges a live lock stale (I5). */
|
|
29
|
+
exports.REPLICA_CACHE_HEARTBEAT_MS = 5 * 60 * 1000;
|
|
30
|
+
class ReplicaCacheError extends Error {
|
|
31
|
+
}
|
|
32
|
+
exports.ReplicaCacheError = ReplicaCacheError;
|
|
33
|
+
const IDENTITY_FIELDS = ['databaseId', 'createdAt', 'syncOrigin'];
|
|
34
|
+
/** `<homedir>/.solidactions/cache/database-replicas`. */
|
|
35
|
+
function defaultReplicaCacheRoot(homedir = os_1.default.homedir()) {
|
|
36
|
+
return path_1.default.join(homedir, '.solidactions', 'cache', 'database-replicas');
|
|
37
|
+
}
|
|
38
|
+
/** First 32 hex characters of sha256(JSON.stringify([host, workspaceId, name])). */
|
|
39
|
+
function replicaCacheKey(identity) {
|
|
40
|
+
return crypto_1.default.createHash('sha256').update(JSON.stringify([identity.host, identity.workspaceId, identity.name])).digest('hex').slice(0, 32);
|
|
41
|
+
}
|
|
42
|
+
/** Lower-cased `host[:port]` of a `libsql://`, `https://` or `http://` URL; null when it cannot be read. */
|
|
43
|
+
function syncOriginOf(syncUrl) {
|
|
44
|
+
if (typeof syncUrl !== 'string' || syncUrl === '')
|
|
45
|
+
return null;
|
|
46
|
+
let parsed;
|
|
47
|
+
try {
|
|
48
|
+
parsed = new URL(syncUrl);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
if (!['libsql:', 'https:', 'http:'].includes(parsed.protocol))
|
|
54
|
+
return null;
|
|
55
|
+
const origin = parsed.host.toLowerCase();
|
|
56
|
+
return origin === '' ? null : origin;
|
|
57
|
+
}
|
|
58
|
+
/** Never throws for a cache problem: it answers `skipped` instead. */
|
|
59
|
+
async function acquireReplicaCache(options) {
|
|
60
|
+
const filesystem = options.filesystem ?? fs_1.default.promises;
|
|
61
|
+
const { now, identity } = options;
|
|
62
|
+
const dir = path_1.default.join(options.root, replicaCacheKey(identity));
|
|
63
|
+
const lockPath = path_1.default.join(dir, 'lock');
|
|
64
|
+
const ours = { pid: options.pid ?? process.pid, host: options.hostname ?? os_1.default.hostname(), nonce: crypto_1.default.randomUUID(), at: now() };
|
|
65
|
+
let cache = null;
|
|
66
|
+
try {
|
|
67
|
+
// Any error from here on (a root that cannot be created included) answers `unavailable` below.
|
|
68
|
+
await filesystem.mkdir(options.root, { recursive: true, mode: 0o700 });
|
|
69
|
+
await filesystem.mkdir(dir, { recursive: true, mode: 0o700 });
|
|
70
|
+
if (!(await filesystem.lstat(dir)).isDirectory())
|
|
71
|
+
return { kind: 'skipped', reason: 'unavailable' };
|
|
72
|
+
const locked = await takeLock(filesystem, dir, ours, now, options.processAlive ?? defaultProcessAlive);
|
|
73
|
+
if (!locked)
|
|
74
|
+
return { kind: 'skipped', reason: 'busy' };
|
|
75
|
+
cache = new CacheHandle(filesystem, dir, lockPath, ours.nonce, now);
|
|
76
|
+
cache.startHeartbeat(options.heartbeatMs ?? exports.REPLICA_CACHE_HEARTBEAT_MS);
|
|
77
|
+
await filesystem.rm(cache.snapshotPath, { force: true });
|
|
78
|
+
const stored = await readMeta(filesystem, path_1.default.join(dir, 'meta.json'));
|
|
79
|
+
const meta = stored === null || stored === 'missing' ? defaultMeta() : stored;
|
|
80
|
+
cache.meta = meta;
|
|
81
|
+
// A corrupt meta, or a missing one beside a non-empty data/, leaves a replica of unknown origin (I6).
|
|
82
|
+
if (stored === null || (stored === 'missing' && await isNonEmptyDirectory(filesystem, path_1.default.join(dir, 'data'))))
|
|
83
|
+
await cache.wipe();
|
|
84
|
+
if (meta.skipUntil !== null && meta.skipUntil > now()) {
|
|
85
|
+
await cache.release();
|
|
86
|
+
return { kind: 'skipped', reason: 'rewritten_often' };
|
|
87
|
+
}
|
|
88
|
+
const replaced = IDENTITY_FIELDS.some((field) => meta[field] !== null && identity[field] !== null && meta[field] !== identity[field]);
|
|
89
|
+
for (const field of IDENTITY_FIELDS) {
|
|
90
|
+
const offered = identity[field];
|
|
91
|
+
if (offered !== null)
|
|
92
|
+
meta[field] = offered;
|
|
93
|
+
}
|
|
94
|
+
if (replaced) {
|
|
95
|
+
await cache.wipe();
|
|
96
|
+
const at = now();
|
|
97
|
+
meta.replacements = [...meta.replacements, at].filter((when) => at - when <= exports.REPLICA_CACHE_REPLACEMENT_WINDOW_MS);
|
|
98
|
+
if (meta.replacements.length >= exports.REPLICA_CACHE_REPLACEMENT_LIMIT) {
|
|
99
|
+
meta.skipUntil = at + exports.REPLICA_CACHE_SKIP_MS;
|
|
100
|
+
await cache.writeMeta();
|
|
101
|
+
await cache.release();
|
|
102
|
+
return { kind: 'skipped', reason: 'rewritten_often' };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
await cache.writeMeta();
|
|
106
|
+
const dataDir = path_1.default.join(dir, 'data');
|
|
107
|
+
if ((await lstatIfPresent(filesystem, dataDir)) === null)
|
|
108
|
+
await filesystem.mkdir(dataDir, { mode: 0o700 });
|
|
109
|
+
if (!(await filesystem.lstat(dataDir)).isDirectory()) {
|
|
110
|
+
await cache.release();
|
|
111
|
+
return { kind: 'skipped', reason: 'unavailable' };
|
|
112
|
+
}
|
|
113
|
+
cache.warm = (await lstatIfPresent(filesystem, cache.replicaPath))?.isFile() === true;
|
|
114
|
+
return { kind: 'ready', cache };
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
if (cache !== null)
|
|
118
|
+
await cache.release();
|
|
119
|
+
return { kind: 'skipped', reason: 'unavailable' };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
class CacheHandle {
|
|
123
|
+
filesystem;
|
|
124
|
+
dir;
|
|
125
|
+
lockPath;
|
|
126
|
+
nonce;
|
|
127
|
+
now;
|
|
128
|
+
replicaPath;
|
|
129
|
+
snapshotPath;
|
|
130
|
+
warm = false;
|
|
131
|
+
meta = defaultMeta();
|
|
132
|
+
released = false;
|
|
133
|
+
heartbeat = null;
|
|
134
|
+
dataDir;
|
|
135
|
+
constructor(filesystem, dir, lockPath, nonce, now) {
|
|
136
|
+
this.filesystem = filesystem;
|
|
137
|
+
this.dir = dir;
|
|
138
|
+
this.lockPath = lockPath;
|
|
139
|
+
this.nonce = nonce;
|
|
140
|
+
this.now = now;
|
|
141
|
+
this.dataDir = path_1.default.join(dir, 'data');
|
|
142
|
+
this.replicaPath = path_1.default.join(this.dataDir, 'replica.db');
|
|
143
|
+
this.snapshotPath = path_1.default.join(dir, 'snapshot.tmp');
|
|
144
|
+
}
|
|
145
|
+
/** Refreshes the lock's mtime every `intervalMs` while it is still ours; the timer never keeps the process alive. */
|
|
146
|
+
startHeartbeat(intervalMs) {
|
|
147
|
+
this.heartbeat = setInterval(() => {
|
|
148
|
+
void this.refreshLock();
|
|
149
|
+
}, intervalMs);
|
|
150
|
+
this.heartbeat.unref();
|
|
151
|
+
}
|
|
152
|
+
async refreshLock() {
|
|
153
|
+
try {
|
|
154
|
+
const stat = await lstatIfPresent(this.filesystem, this.lockPath);
|
|
155
|
+
if (stat === null || !stat.isFile())
|
|
156
|
+
return;
|
|
157
|
+
const record = parseLockRecord(await this.filesystem.readFile(this.lockPath, 'utf8'));
|
|
158
|
+
if (record === null || record.nonce !== this.nonce || this.released)
|
|
159
|
+
return;
|
|
160
|
+
const at = new Date(this.now());
|
|
161
|
+
await this.filesystem.utimes(this.lockPath, at, at);
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
// Ignored: a missed refresh only makes the lock look older; the next tick tries again.
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async wipe() {
|
|
168
|
+
try {
|
|
169
|
+
const existing = await lstatIfPresent(this.filesystem, this.dataDir);
|
|
170
|
+
if (existing !== null) {
|
|
171
|
+
if (!existing.isDirectory())
|
|
172
|
+
throw new ReplicaCacheError(`The cache's data folder is not a plain folder: ${this.dataDir}`);
|
|
173
|
+
await this.filesystem.rm(this.dataDir, { recursive: true, force: true });
|
|
174
|
+
}
|
|
175
|
+
await this.filesystem.mkdir(this.dataDir, { mode: 0o700 });
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
if (error instanceof ReplicaCacheError)
|
|
179
|
+
throw error;
|
|
180
|
+
throw new ReplicaCacheError(`Could not reset the cache's data folder: ${error instanceof Error ? error.message : String(error)}`);
|
|
181
|
+
}
|
|
182
|
+
this.warm = false;
|
|
183
|
+
this.meta.lastSyncedAt = null;
|
|
184
|
+
}
|
|
185
|
+
async discardSnapshot() {
|
|
186
|
+
try {
|
|
187
|
+
await this.filesystem.rm(this.snapshotPath, { force: true });
|
|
188
|
+
}
|
|
189
|
+
catch {
|
|
190
|
+
// Never throws: a snapshot that cannot be removed is removed at the next acquire.
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async markSynced() {
|
|
194
|
+
this.meta.lastSyncedAt = this.now();
|
|
195
|
+
await this.writeMeta();
|
|
196
|
+
}
|
|
197
|
+
async release() {
|
|
198
|
+
if (this.released)
|
|
199
|
+
return;
|
|
200
|
+
this.released = true;
|
|
201
|
+
if (this.heartbeat !== null)
|
|
202
|
+
clearInterval(this.heartbeat);
|
|
203
|
+
this.heartbeat = null;
|
|
204
|
+
await this.discardSnapshot();
|
|
205
|
+
try {
|
|
206
|
+
const stat = await lstatIfPresent(this.filesystem, this.lockPath);
|
|
207
|
+
if (stat === null || !stat.isFile())
|
|
208
|
+
return;
|
|
209
|
+
const record = parseLockRecord(await this.filesystem.readFile(this.lockPath, 'utf8'));
|
|
210
|
+
if (record !== null && record.nonce === this.nonce)
|
|
211
|
+
await this.filesystem.unlink(this.lockPath);
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
// Never throws: a lock left behind is judged stale and reclaimed by the next pull.
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** Writes `meta.json` atomically: `meta.json.<nonce>.tmp`, then `rename`. Only the documented keys. */
|
|
218
|
+
async writeMeta() {
|
|
219
|
+
const { databaseId, createdAt, syncOrigin, lastSyncedAt, replacements, skipUntil } = this.meta;
|
|
220
|
+
const body = { version: 1, databaseId, createdAt, syncOrigin, lastSyncedAt, replacements, skipUntil };
|
|
221
|
+
const temp = path_1.default.join(this.dir, `meta.json.${this.nonce}.tmp`);
|
|
222
|
+
try {
|
|
223
|
+
const handle = await this.filesystem.open(temp, 'wx', 0o600);
|
|
224
|
+
try {
|
|
225
|
+
await handle.writeFile(JSON.stringify(body));
|
|
226
|
+
}
|
|
227
|
+
finally {
|
|
228
|
+
await handle.close();
|
|
229
|
+
}
|
|
230
|
+
await this.filesystem.rename(temp, path_1.default.join(this.dir, 'meta.json'));
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
await this.filesystem.rm(temp, { force: true }).catch(() => undefined);
|
|
234
|
+
throw error;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function defaultMeta() {
|
|
239
|
+
return { version: 1, databaseId: null, createdAt: null, syncOrigin: null, lastSyncedAt: null, replacements: [], skipUntil: null };
|
|
240
|
+
}
|
|
241
|
+
/** The stored meta; `'missing'` when there is no file; null when it is corrupt (not a regular file, unparseable, wrong version or wrong shape). */
|
|
242
|
+
async function readMeta(filesystem, metaPath) {
|
|
243
|
+
const stat = await lstatIfPresent(filesystem, metaPath);
|
|
244
|
+
if (stat === null)
|
|
245
|
+
return 'missing';
|
|
246
|
+
if (!stat.isFile())
|
|
247
|
+
return null;
|
|
248
|
+
let parsed;
|
|
249
|
+
try {
|
|
250
|
+
parsed = JSON.parse(await filesystem.readFile(metaPath, 'utf8'));
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
return null;
|
|
254
|
+
}
|
|
255
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed))
|
|
256
|
+
return null;
|
|
257
|
+
const candidate = parsed;
|
|
258
|
+
const isNullableString = (value) => value === null || typeof value === 'string';
|
|
259
|
+
const isNullableNumber = (value) => value === null || (typeof value === 'number' && Number.isFinite(value));
|
|
260
|
+
if (candidate.version !== 1
|
|
261
|
+
|| !isNullableString(candidate.databaseId)
|
|
262
|
+
|| !isNullableString(candidate.createdAt)
|
|
263
|
+
|| !isNullableString(candidate.syncOrigin)
|
|
264
|
+
|| !isNullableNumber(candidate.lastSyncedAt)
|
|
265
|
+
|| !isNullableNumber(candidate.skipUntil)
|
|
266
|
+
|| !Array.isArray(candidate.replacements)
|
|
267
|
+
|| !candidate.replacements.every((when) => typeof when === 'number' && Number.isFinite(when))) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
// Validated above; extra keys are harmless because writeMeta writes only the documented ones.
|
|
271
|
+
return candidate;
|
|
272
|
+
}
|
|
273
|
+
/** True when this run now holds the lock; false when it is busy. Throws only for an unexpected filesystem error. */
|
|
274
|
+
async function takeLock(filesystem, dir, ours, now, processAlive) {
|
|
275
|
+
const lockPath = path_1.default.join(dir, 'lock');
|
|
276
|
+
if (await createLockFile(filesystem, lockPath, ours))
|
|
277
|
+
return true;
|
|
278
|
+
const judgement = await judgeExistingLock(filesystem, lockPath, ours.host, now, processAlive);
|
|
279
|
+
if (!judgement.stale)
|
|
280
|
+
return false;
|
|
281
|
+
return reclaimStaleLock(filesystem, dir, judgement.lock, ours, now);
|
|
282
|
+
}
|
|
283
|
+
/** Creates `lock` with `wx` and writes our record; false when a lock already exists. */
|
|
284
|
+
async function createLockFile(filesystem, lockPath, record) {
|
|
285
|
+
let handle;
|
|
286
|
+
try {
|
|
287
|
+
handle = await filesystem.open(lockPath, 'wx', 0o600);
|
|
288
|
+
}
|
|
289
|
+
catch (error) {
|
|
290
|
+
if (errorCode(error) === 'EEXIST')
|
|
291
|
+
return false;
|
|
292
|
+
throw error;
|
|
293
|
+
}
|
|
294
|
+
try {
|
|
295
|
+
try {
|
|
296
|
+
await handle.writeFile(JSON.stringify(record));
|
|
297
|
+
}
|
|
298
|
+
finally {
|
|
299
|
+
await handle.close();
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
// A lock we created but could not fill is ours to remove; never leave an empty one behind.
|
|
304
|
+
await filesystem.unlink(lockPath).catch(() => undefined);
|
|
305
|
+
throw error;
|
|
306
|
+
}
|
|
307
|
+
return true;
|
|
308
|
+
}
|
|
309
|
+
async function judgeExistingLock(filesystem, lockPath, ourHost, now, processAlive) {
|
|
310
|
+
const stat = await lstatIfPresent(filesystem, lockPath);
|
|
311
|
+
// Gone between our `wx` and this read: another pull just released it. Busy this time.
|
|
312
|
+
if (stat === null)
|
|
313
|
+
return { stale: false };
|
|
314
|
+
const content = stat.isFile() ? await readIfPresent(filesystem, lockPath) : null;
|
|
315
|
+
const record = content === null ? null : parseLockRecord(content);
|
|
316
|
+
const oldEnough = now() - stat.mtimeMs > exports.REPLICA_CACHE_STALE_LOCK_MS;
|
|
317
|
+
if (record === null) {
|
|
318
|
+
return oldEnough ? { stale: true, lock: { kind: 'unreadable', dev: stat.dev, ino: stat.ino, mtimeMs: stat.mtimeMs, content } } : { stale: false };
|
|
319
|
+
}
|
|
320
|
+
if (record.host !== ourHost) {
|
|
321
|
+
return oldEnough ? { stale: true, lock: { kind: 'parsed', nonce: record.nonce } } : { stale: false };
|
|
322
|
+
}
|
|
323
|
+
return processAlive(record.pid) ? { stale: false } : { stale: true, lock: { kind: 'parsed', nonce: record.nonce } };
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Replaces a stale lock under `lock.reclaim` (`wx`): re-read `lock`, confirm it is still the one
|
|
327
|
+
* judged stale, remove it, create ours with `wx`. Every loser answers busy.
|
|
328
|
+
*/
|
|
329
|
+
async function reclaimStaleLock(filesystem, dir, judged, ours, now) {
|
|
330
|
+
const lockPath = path_1.default.join(dir, 'lock');
|
|
331
|
+
const reclaimPath = path_1.default.join(dir, 'lock.reclaim');
|
|
332
|
+
let reclaimHandle;
|
|
333
|
+
try {
|
|
334
|
+
reclaimHandle = await filesystem.open(reclaimPath, 'wx', 0o600);
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
if (errorCode(error) !== 'EEXIST')
|
|
338
|
+
throw error;
|
|
339
|
+
const reclaimStat = await lstatIfPresent(filesystem, reclaimPath);
|
|
340
|
+
// A reclaim abandoned by a crashed pull: clear it for the next acquirer; busy this time.
|
|
341
|
+
if (reclaimStat !== null && now() - reclaimStat.mtimeMs > exports.REPLICA_CACHE_STALE_RECLAIM_MS) {
|
|
342
|
+
await filesystem.rm(reclaimPath, { force: true });
|
|
343
|
+
}
|
|
344
|
+
return false;
|
|
345
|
+
}
|
|
346
|
+
try {
|
|
347
|
+
await reclaimHandle.close();
|
|
348
|
+
if (!(await isSameStaleLock(filesystem, lockPath, judged)))
|
|
349
|
+
return false;
|
|
350
|
+
await filesystem.unlink(lockPath);
|
|
351
|
+
return await createLockFile(filesystem, lockPath, ours);
|
|
352
|
+
}
|
|
353
|
+
finally {
|
|
354
|
+
await filesystem.rm(reclaimPath, { force: true }).catch(() => undefined);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
async function isSameStaleLock(filesystem, lockPath, judged) {
|
|
358
|
+
const stat = await lstatIfPresent(filesystem, lockPath);
|
|
359
|
+
if (stat === null)
|
|
360
|
+
return false;
|
|
361
|
+
const content = stat.isFile() ? await readIfPresent(filesystem, lockPath) : null;
|
|
362
|
+
const record = content === null ? null : parseLockRecord(content);
|
|
363
|
+
if (judged.kind === 'parsed')
|
|
364
|
+
return record !== null && record.nonce === judged.nonce;
|
|
365
|
+
return record === null
|
|
366
|
+
&& stat.dev === judged.dev
|
|
367
|
+
&& stat.ino === judged.ino
|
|
368
|
+
&& stat.mtimeMs === judged.mtimeMs
|
|
369
|
+
&& content === judged.content;
|
|
370
|
+
}
|
|
371
|
+
function parseLockRecord(content) {
|
|
372
|
+
let parsed;
|
|
373
|
+
try {
|
|
374
|
+
parsed = JSON.parse(content);
|
|
375
|
+
}
|
|
376
|
+
catch {
|
|
377
|
+
return null;
|
|
378
|
+
}
|
|
379
|
+
if (typeof parsed !== 'object' || parsed === null)
|
|
380
|
+
return null;
|
|
381
|
+
const candidate = parsed;
|
|
382
|
+
if (typeof candidate.pid !== 'number' || !Number.isInteger(candidate.pid) || candidate.pid <= 0
|
|
383
|
+
|| typeof candidate.host !== 'string'
|
|
384
|
+
|| typeof candidate.nonce !== 'string' || candidate.nonce === ''
|
|
385
|
+
|| typeof candidate.at !== 'number') {
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
return { pid: candidate.pid, host: candidate.host, nonce: candidate.nonce, at: candidate.at };
|
|
389
|
+
}
|
|
390
|
+
/** `process.kill(pid, 0)`: alive unless it throws `ESRCH` (`EPERM` means alive, owned by someone else). */
|
|
391
|
+
function defaultProcessAlive(pid) {
|
|
392
|
+
try {
|
|
393
|
+
process.kill(pid, 0);
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
catch (error) {
|
|
397
|
+
return errorCode(error) !== 'ESRCH';
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
/** True when `candidate` is a real directory (never followed through a symlink) with anything inside it. */
|
|
401
|
+
async function isNonEmptyDirectory(filesystem, candidate) {
|
|
402
|
+
const stat = await lstatIfPresent(filesystem, candidate);
|
|
403
|
+
if (stat === null || !stat.isDirectory())
|
|
404
|
+
return false;
|
|
405
|
+
return (await filesystem.readdir(candidate)).length > 0;
|
|
406
|
+
}
|
|
407
|
+
async function lstatIfPresent(filesystem, candidate) {
|
|
408
|
+
try {
|
|
409
|
+
return await filesystem.lstat(candidate);
|
|
410
|
+
}
|
|
411
|
+
catch (error) {
|
|
412
|
+
if (isMissingPath(error))
|
|
413
|
+
return null;
|
|
414
|
+
throw error;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
async function readIfPresent(filesystem, candidate) {
|
|
418
|
+
try {
|
|
419
|
+
return await filesystem.readFile(candidate, 'utf8');
|
|
420
|
+
}
|
|
421
|
+
catch (error) {
|
|
422
|
+
if (isMissingPath(error))
|
|
423
|
+
return null;
|
|
424
|
+
throw error;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
function isMissingPath(error) {
|
|
428
|
+
return errorCode(error) === 'ENOENT';
|
|
429
|
+
}
|
|
430
|
+
function errorCode(error) {
|
|
431
|
+
return typeof error === 'object' && error !== null && 'code' in error ? String(error.code) : undefined;
|
|
432
|
+
}
|