@solidactions/cli 3.6.0 → 3.8.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.
@@ -0,0 +1,466 @@
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.prepareDatabaseImportSource = prepareDatabaseImportSource;
7
+ exports.bindPreparedDatabaseImport = bindPreparedDatabaseImport;
8
+ exports.prepareDatabaseImport = prepareDatabaseImport;
9
+ exports.runPreparedDatabaseImport = runPreparedDatabaseImport;
10
+ const crypto_1 = require("crypto");
11
+ const path_1 = __importDefault(require("path"));
12
+ const database_client_support_1 = require("./database-client-support");
13
+ const database_data_plane_1 = require("./database-data-plane");
14
+ const database_sql_import_1 = require("./database-sql-import");
15
+ const CHECKPOINT_VERSION = 1;
16
+ const MAX_BATCH_GROUPS = 100;
17
+ const MAX_BATCH_BYTES = 512 * 1024;
18
+ const RENEWAL_WINDOW_MS = 30_000;
19
+ function importFailure(message = 'Database import failed.') {
20
+ return new database_data_plane_1.DatabaseOperationError('import_failed', message);
21
+ }
22
+ function safeStem(database) {
23
+ return database
24
+ .normalize('NFKD')
25
+ .toLowerCase()
26
+ .replace(/[^a-z0-9]+/g, '-')
27
+ .replace(/^-+|-+$/g, '')
28
+ .slice(0, 80) || 'database';
29
+ }
30
+ function resumeLine(database, file, checkpoint) {
31
+ return `Resume with: solidactions database import ${JSON.stringify(database)} ${JSON.stringify(file)} --resume ${JSON.stringify(checkpoint)} --yes`;
32
+ }
33
+ function checkpointPath(cwd, database, sha256, sizeBytes) {
34
+ return path_1.default.join(cwd, '.solidactions', 'imports', `${safeStem(database)}-${sha256}-${sizeBytes}.json`);
35
+ }
36
+ async function lstatMaybe(filesystem, target) {
37
+ try {
38
+ return await filesystem.lstat(target);
39
+ }
40
+ catch (error) {
41
+ if (error?.code === 'ENOENT')
42
+ return null;
43
+ throw importFailure('The import checkpoint path could not be validated.');
44
+ }
45
+ }
46
+ async function assertSafeAncestors(filesystem, target) {
47
+ const absolute = path_1.default.resolve(target);
48
+ const parent = path_1.default.dirname(absolute);
49
+ const root = path_1.default.parse(parent).root;
50
+ const parts = parent.slice(root.length).split(path_1.default.sep).filter(Boolean);
51
+ let current = root;
52
+ for (const part of parts) {
53
+ current = path_1.default.join(current, part);
54
+ const stat = await lstatMaybe(filesystem, current);
55
+ if (!stat)
56
+ return;
57
+ if (stat.isSymbolicLink() || !stat.isDirectory()) {
58
+ throw importFailure('The import checkpoint path is unsafe.');
59
+ }
60
+ }
61
+ }
62
+ async function assertRegularCheckpoint(filesystem, target) {
63
+ await assertSafeAncestors(filesystem, target);
64
+ const stat = await lstatMaybe(filesystem, target);
65
+ if (!stat || stat.isSymbolicLink() || !stat.isFile()) {
66
+ throw importFailure('The import checkpoint must be a regular non-symlink file.');
67
+ }
68
+ return stat;
69
+ }
70
+ function checkpointBoundary(groups, nextStatement) {
71
+ if (!Number.isInteger(nextStatement) || nextStatement < 0 || nextStatement > groups.length)
72
+ return null;
73
+ return nextStatement === 0 ? 0 : groups[nextStatement - 1].endByte;
74
+ }
75
+ function deterministicBatchCount(groups, nextStatement) {
76
+ if (nextStatement === 0)
77
+ return 0;
78
+ let cursor = 0;
79
+ let count = 0;
80
+ while (cursor < groups.length) {
81
+ const batch = nextBatch(groups, cursor);
82
+ cursor = batch.nextStatement;
83
+ count += 1;
84
+ if (cursor === nextStatement)
85
+ return count;
86
+ if (cursor > nextStatement)
87
+ return null;
88
+ }
89
+ return null;
90
+ }
91
+ function validateCheckpoint(value, database, sha256, sizeBytes, groups) {
92
+ const checkpoint = value;
93
+ const source = checkpoint?.source;
94
+ const nextStatement = checkpoint?.nextStatement;
95
+ const completedSourceBytes = checkpoint?.completedSourceBytes;
96
+ const expectedBoundary = typeof nextStatement === 'number'
97
+ ? checkpointBoundary(groups, nextStatement)
98
+ : null;
99
+ const expectedBatchCount = typeof nextStatement === 'number'
100
+ ? deterministicBatchCount(groups, nextStatement)
101
+ : null;
102
+ if (checkpoint?.version !== CHECKPOINT_VERSION
103
+ || checkpoint.database !== database
104
+ || source?.sha256 !== sha256
105
+ || source.sizeBytes !== sizeBytes
106
+ || typeof checkpoint.lastCompletedBatch !== 'number'
107
+ || !Number.isInteger(checkpoint.lastCompletedBatch)
108
+ || checkpoint.lastCompletedBatch !== expectedBatchCount
109
+ || typeof nextStatement !== 'number'
110
+ || expectedBoundary === null
111
+ || completedSourceBytes !== expectedBoundary) {
112
+ throw importFailure('The import checkpoint does not match this database and SQL source.');
113
+ }
114
+ return {
115
+ version: 1,
116
+ database,
117
+ source: { sha256, sizeBytes },
118
+ lastCompletedBatch: checkpoint.lastCompletedBatch,
119
+ nextStatement,
120
+ completedSourceBytes,
121
+ };
122
+ }
123
+ async function readCheckpoint(filesystem, target, database, sha256, sizeBytes, groups) {
124
+ await assertRegularCheckpoint(filesystem, target);
125
+ let value;
126
+ try {
127
+ value = JSON.parse(await filesystem.readFile(target, 'utf8'));
128
+ }
129
+ catch {
130
+ throw importFailure('The import checkpoint is corrupt.');
131
+ }
132
+ return validateCheckpoint(value, database, sha256, sizeBytes, groups);
133
+ }
134
+ async function prepareDatabaseImportSource(file, cwd, filesystem) {
135
+ const sourcePath = path_1.default.isAbsolute(file) ? file : path_1.default.resolve(cwd, file);
136
+ let source;
137
+ try {
138
+ source = Buffer.from(await filesystem.readFile(sourcePath));
139
+ }
140
+ catch {
141
+ throw importFailure('The SQL import source could not be read.');
142
+ }
143
+ let groups;
144
+ try {
145
+ groups = (0, database_sql_import_1.parseDatabaseImportSql)(source).groups;
146
+ }
147
+ catch (error) {
148
+ if (error instanceof database_data_plane_1.DatabaseOperationError)
149
+ throw importFailure(error.message);
150
+ throw importFailure('The SQL import source is invalid.');
151
+ }
152
+ if (groups.length === 0)
153
+ throw importFailure('The SQL import source contains no statements.');
154
+ const sha256 = (0, crypto_1.createHash)('sha256').update(source).digest('hex');
155
+ const sizeBytes = source.length;
156
+ return { file, source, sha256, sizeBytes, groups };
157
+ }
158
+ async function bindPreparedDatabaseImport(prepared, database, resume, cwd, filesystem) {
159
+ const { file, source, sha256, sizeBytes, groups } = prepared;
160
+ const deterministic = checkpointPath(cwd, database, sha256, sizeBytes);
161
+ await assertSafeAncestors(filesystem, deterministic);
162
+ const deterministicStat = await lstatMaybe(filesystem, deterministic);
163
+ if (deterministicStat && (deterministicStat.isSymbolicLink() || !deterministicStat.isFile())) {
164
+ throw importFailure('The import checkpoint path is unsafe.');
165
+ }
166
+ if (resume) {
167
+ const supplied = path_1.default.isAbsolute(resume) ? resume : path_1.default.resolve(cwd, resume);
168
+ if (deterministicStat && path_1.default.resolve(deterministic) !== path_1.default.resolve(supplied)) {
169
+ throw importFailure('A different import checkpoint already exists for this SQL source.');
170
+ }
171
+ const checkpoint = await readCheckpoint(filesystem, supplied, database, sha256, sizeBytes, groups);
172
+ return {
173
+ database,
174
+ file,
175
+ source,
176
+ sha256,
177
+ sizeBytes,
178
+ groups,
179
+ checkpointPath: supplied,
180
+ checkpoint,
181
+ };
182
+ }
183
+ if (deterministicStat) {
184
+ await readCheckpoint(filesystem, deterministic, database, sha256, sizeBytes, groups);
185
+ throw importFailure(`A matching import checkpoint already exists.\n${resumeLine(database, file, deterministic)}`);
186
+ }
187
+ return {
188
+ database,
189
+ file,
190
+ source,
191
+ sha256,
192
+ sizeBytes,
193
+ groups,
194
+ checkpointPath: deterministic,
195
+ checkpoint: null,
196
+ };
197
+ }
198
+ async function prepareDatabaseImport(database, file, resume, cwd, filesystem) {
199
+ const prepared = await prepareDatabaseImportSource(file, cwd, filesystem);
200
+ return bindPreparedDatabaseImport(prepared, database, resume, cwd, filesystem);
201
+ }
202
+ async function ensureSafeDirectory(filesystem, directory) {
203
+ const absolute = path_1.default.resolve(directory);
204
+ const root = path_1.default.parse(absolute).root;
205
+ const parts = absolute.slice(root.length).split(path_1.default.sep).filter(Boolean);
206
+ let current = root;
207
+ for (const part of parts) {
208
+ current = path_1.default.join(current, part);
209
+ let stat = await lstatMaybe(filesystem, current);
210
+ if (!stat) {
211
+ try {
212
+ await filesystem.mkdir(current, { mode: 0o700 });
213
+ }
214
+ catch (error) {
215
+ if (error?.code !== 'EEXIST') {
216
+ throw importFailure('The import checkpoint directory could not be created.');
217
+ }
218
+ }
219
+ stat = await lstatMaybe(filesystem, current);
220
+ }
221
+ if (!stat || stat.isSymbolicLink() || !stat.isDirectory()) {
222
+ throw importFailure('The import checkpoint path is unsafe.');
223
+ }
224
+ }
225
+ }
226
+ async function publishCheckpoint(filesystem, target, checkpoint, allowExisting) {
227
+ const directory = path_1.default.dirname(target);
228
+ await ensureSafeDirectory(filesystem, directory);
229
+ await assertSafeAncestors(filesystem, target);
230
+ const existing = await lstatMaybe(filesystem, target);
231
+ if ((existing && (existing.isSymbolicLink() || !existing.isFile()))
232
+ || (existing && !allowExisting)) {
233
+ throw importFailure('The import checkpoint path is unsafe.');
234
+ }
235
+ const temp = path_1.default.join(directory, `.${path_1.default.basename(target)}.${(0, crypto_1.randomUUID)()}.tmp`);
236
+ let handle;
237
+ let ownsTemp = false;
238
+ try {
239
+ handle = await filesystem.open(temp, 'wx', 0o600);
240
+ ownsTemp = true;
241
+ await handle.writeFile(`${JSON.stringify(checkpoint)}\n`, 'utf8');
242
+ await handle.sync();
243
+ await handle.close();
244
+ handle = undefined;
245
+ await assertSafeAncestors(filesystem, target);
246
+ const finalExisting = await lstatMaybe(filesystem, target);
247
+ if ((finalExisting && (finalExisting.isSymbolicLink() || !finalExisting.isFile()))
248
+ || (finalExisting && !allowExisting)) {
249
+ throw importFailure('The import checkpoint path is unsafe.');
250
+ }
251
+ const tempStat = await lstatMaybe(filesystem, temp);
252
+ if (!tempStat || tempStat.isSymbolicLink() || !tempStat.isFile()) {
253
+ throw importFailure('The import checkpoint temporary file is unsafe.');
254
+ }
255
+ await filesystem.rename(temp, target);
256
+ ownsTemp = false;
257
+ }
258
+ catch {
259
+ if (handle) {
260
+ try {
261
+ await handle.close();
262
+ }
263
+ catch {
264
+ // The public failure below remains credential- and path-safe.
265
+ }
266
+ }
267
+ if (ownsTemp) {
268
+ try {
269
+ await filesystem.unlink(temp);
270
+ }
271
+ catch {
272
+ // Cleanup is scoped to the exclusively created sibling.
273
+ }
274
+ }
275
+ throw importFailure('The committed batch could not be checkpointed safely.');
276
+ }
277
+ }
278
+ async function removeCheckpoint(filesystem, target) {
279
+ await assertRegularCheckpoint(filesystem, target);
280
+ try {
281
+ await filesystem.unlink(target);
282
+ }
283
+ catch {
284
+ throw importFailure('The completed import checkpoint could not be removed safely.');
285
+ }
286
+ }
287
+ function nextBatch(groups, start) {
288
+ const selected = [];
289
+ let bytes = 0;
290
+ let index = start;
291
+ while (index < groups.length && selected.length < MAX_BATCH_GROUPS) {
292
+ const groupBytes = Buffer.byteLength(groups[index].sql, 'utf8');
293
+ if (selected.length > 0 && bytes + groupBytes > MAX_BATCH_BYTES)
294
+ break;
295
+ selected.push(groups[index]);
296
+ bytes += groupBytes;
297
+ index += 1;
298
+ if (groupBytes > MAX_BATCH_BYTES)
299
+ break;
300
+ }
301
+ return { groups: selected, nextStatement: index };
302
+ }
303
+ function batchSql(groups) {
304
+ return [
305
+ 'PRAGMA foreign_keys=OFF;',
306
+ 'BEGIN IMMEDIATE;',
307
+ ...groups.map((group) => group.sql),
308
+ 'COMMIT;',
309
+ 'PRAGMA foreign_keys=ON;',
310
+ ].join('\n');
311
+ }
312
+ function validateAccess(access, now) {
313
+ const value = access;
314
+ const url = typeof value?.url === 'string' ? value.url.trim() : '';
315
+ const token = typeof value?.token === 'string' ? value.token.trim() : '';
316
+ const expiresAt = typeof value?.expires_at === 'string' ? Date.parse(value.expires_at) : Number.NaN;
317
+ if (value?.mode !== 'write'
318
+ || url === ''
319
+ || token === ''
320
+ || !Number.isFinite(now)
321
+ || !Number.isFinite(expiresAt)
322
+ || expiresAt <= now + RENEWAL_WINDOW_MS) {
323
+ throw importFailure('Database import access was invalid.');
324
+ }
325
+ return { url, token, expiresAt };
326
+ }
327
+ async function acquireClient(prepared, config, dependencies) {
328
+ let loaded;
329
+ try {
330
+ loaded = await (0, database_client_support_1.loadDatabaseClientBeforeMint)(() => (0, database_data_plane_1.requestDatabaseAccess)(config, prepared.database, 'write', {
331
+ post: dependencies.post,
332
+ controlPlaneTimeoutMs: dependencies.controlPlaneTimeoutMs,
333
+ }), { loadClient: dependencies.loadClient });
334
+ }
335
+ catch (error) {
336
+ if (error instanceof database_data_plane_1.DatabaseOperationError)
337
+ throw error;
338
+ throw importFailure('Database import access could not be renewed.');
339
+ }
340
+ const access = validateAccess(loaded.access, dependencies.now());
341
+ let client;
342
+ try {
343
+ client = loaded.createClient({
344
+ url: access.url,
345
+ authToken: access.token,
346
+ intMode: 'string',
347
+ });
348
+ }
349
+ catch {
350
+ throw importFailure('Database import client could not be created.');
351
+ }
352
+ if (typeof client.executeMultiple !== 'function') {
353
+ try {
354
+ await (0, database_data_plane_1.runDatabaseClientOperationWithDeadline)(async () => { await client.close(); }, undefined, dependencies.dataPlaneTimeoutMs ?? database_data_plane_1.DEFAULT_DATABASE_DATA_PLANE_TIMEOUT_MS);
355
+ }
356
+ catch {
357
+ // The unsupported client error below is the only public detail.
358
+ }
359
+ throw importFailure('Database import requires atomic multi-statement execution support.');
360
+ }
361
+ return { client, expiresAt: access.expiresAt };
362
+ }
363
+ async function closeClient(client, timeoutMs = database_data_plane_1.DEFAULT_DATABASE_DATA_PLANE_TIMEOUT_MS) {
364
+ if (!client)
365
+ return;
366
+ try {
367
+ await (0, database_data_plane_1.runDatabaseClientOperationWithDeadline)(async () => { await client.close(); }, undefined, timeoutMs);
368
+ }
369
+ catch {
370
+ throw importFailure('Database import client cleanup failed.');
371
+ }
372
+ }
373
+ async function runPreparedDatabaseImport(prepared, config, dependencies) {
374
+ let nextStatement = prepared.checkpoint?.nextStatement ?? 0;
375
+ let completedBatches = prepared.checkpoint?.lastCompletedBatch ?? 0;
376
+ let completedSourceBytes = prepared.checkpoint?.completedSourceBytes ?? 0;
377
+ let checkpointPublished = prepared.checkpoint !== null;
378
+ let suppressResume = false;
379
+ let client;
380
+ let clientClosePromise;
381
+ let expiresAt = 0;
382
+ let failure;
383
+ const closeCurrentClient = () => {
384
+ clientClosePromise ??= closeClient(client, dependencies.dataPlaneTimeoutMs);
385
+ return clientClosePromise;
386
+ };
387
+ try {
388
+ while (nextStatement < prepared.groups.length) {
389
+ if (!client || dependencies.now() >= expiresAt - RENEWAL_WINDOW_MS) {
390
+ await closeCurrentClient();
391
+ client = undefined;
392
+ clientClosePromise = undefined;
393
+ const acquired = await acquireClient(prepared, config, dependencies);
394
+ client = acquired.client;
395
+ expiresAt = acquired.expiresAt;
396
+ }
397
+ const batch = nextBatch(prepared.groups, nextStatement);
398
+ try {
399
+ await (0, database_data_plane_1.runDatabaseClientOperationWithDeadline)(() => client.executeMultiple(batchSql(batch.groups)), closeCurrentClient, dependencies.dataPlaneTimeoutMs ?? database_data_plane_1.DEFAULT_DATABASE_DATA_PLANE_TIMEOUT_MS);
400
+ }
401
+ catch (error) {
402
+ if (error instanceof database_data_plane_1.DatabaseOperationError)
403
+ throw error;
404
+ throw importFailure('A database import batch failed.');
405
+ }
406
+ const boundary = prepared.groups[batch.nextStatement - 1].endByte;
407
+ const checkpoint = {
408
+ version: 1,
409
+ database: prepared.database,
410
+ source: {
411
+ sha256: prepared.sha256,
412
+ sizeBytes: prepared.sizeBytes,
413
+ },
414
+ lastCompletedBatch: completedBatches + 1,
415
+ nextStatement: batch.nextStatement,
416
+ completedSourceBytes: boundary,
417
+ };
418
+ try {
419
+ await publishCheckpoint(dependencies.filesystem, prepared.checkpointPath, checkpoint, checkpointPublished);
420
+ }
421
+ catch {
422
+ suppressResume = true;
423
+ throw importFailure('A committed database import batch could not be checkpointed safely.');
424
+ }
425
+ checkpointPublished = true;
426
+ completedBatches += 1;
427
+ nextStatement = batch.nextStatement;
428
+ completedSourceBytes = boundary;
429
+ dependencies.stdout(`Imported checkpoint: ${nextStatement} statements, committed source progress: ${boundary} source bytes.`);
430
+ }
431
+ }
432
+ catch (error) {
433
+ failure = error instanceof database_data_plane_1.DatabaseOperationError
434
+ ? error
435
+ : importFailure('Database import failed.');
436
+ }
437
+ try {
438
+ await closeCurrentClient();
439
+ client = undefined;
440
+ clientClosePromise = undefined;
441
+ }
442
+ catch {
443
+ if (!failure)
444
+ failure = importFailure('Database import client cleanup failed.');
445
+ }
446
+ if (!failure) {
447
+ try {
448
+ await removeCheckpoint(dependencies.filesystem, prepared.checkpointPath);
449
+ checkpointPublished = false;
450
+ }
451
+ catch {
452
+ suppressResume = true;
453
+ failure = importFailure('The completed import could not remove its checkpoint safely.');
454
+ }
455
+ }
456
+ if (failure) {
457
+ const policyRefusal = failure.code === 'read_only_mode'
458
+ || failure.code === 'plan_denied'
459
+ || failure.status === 429;
460
+ const guidance = checkpointPublished && !suppressResume && !policyRefusal
461
+ ? `\nLast completed position: batch ${completedBatches}, ${nextStatement} statements, ${completedSourceBytes} source bytes.\n${resumeLine(prepared.database, prepared.file, prepared.checkpointPath)}`
462
+ : '';
463
+ throw new database_data_plane_1.DatabaseOperationError(failure.code, `${failure.message}${guidance}`, failure.status);
464
+ }
465
+ dependencies.stdout(`Imported ${prepared.groups.length} statements (${prepared.sizeBytes} total source bytes) into database "${prepared.database}".`);
466
+ }