@rehearsal-db/core 0.1.0-beta.4 → 0.1.0-beta.6
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 +55 -1
- package/COMPATIBILITY.md +5 -0
- package/README.md +105 -368
- package/SECURITY.md +2 -1
- package/SUPPORT.md +5 -0
- package/docs/adapters.md +5 -0
- package/docs/baselines.md +81 -22
- package/docs/commands.md +76 -64
- package/docs/configuration.md +47 -4
- package/docs/getting-started.md +87 -218
- package/docs/glossary.md +7 -8
- package/docs/sanitization.md +8 -5
- package/docs/security-model.md +5 -1
- package/docs/troubleshooting.md +46 -1
- package/docs/tutorial.md +70 -59
- package/package.json +5 -3
- package/scripts/lib/rehearsal/baseline_preparation.mjs +138 -23
- package/scripts/lib/rehearsal/configuration.d.mts +18 -3
- package/scripts/lib/rehearsal/configuration.mjs +227 -56
- package/scripts/lib/rehearsal/diagnostics.mjs +4 -2
- package/scripts/lib/rehearsal/input_discovery.mjs +155 -0
- package/scripts/lib/rehearsal/plan.mjs +85 -10
- package/scripts/lib/rehearsal/policy_review.mjs +45 -5
- package/scripts/lib/rehearsal/runtime_restore.mjs +6 -2
- package/scripts/lib/rehearsal/setup.mjs +60 -28
- package/scripts/lib/rehearsal/support_report.mjs +102 -0
- package/scripts/lib/runtime/postgresql_runtime.mjs +772 -0
- package/scripts/lib/runtime/runtime_target.mjs +41 -0
- package/scripts/lib/runtime/supabase_runtime.mjs +790 -0
- package/scripts/operations/database/manage_rehearsal_database.mjs +6 -785
- package/scripts/operations/rehearsal/rehearsal_cli.mjs +522 -149
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { access, readFile, stat } from "node:fs/promises";
|
|
8
8
|
import { basename, isAbsolute, join, relative, resolve } from "node:path";
|
|
9
9
|
import { pathToFileURL } from "node:url";
|
|
10
|
+
import { resolveRuntimeTarget } from "../runtime/runtime_target.mjs";
|
|
10
11
|
|
|
11
12
|
export {
|
|
12
13
|
EXCLUDED_VALUE,
|
|
@@ -29,6 +30,11 @@ export const REHEARSAL_DEFAULTS = Object.freeze({
|
|
|
29
30
|
safety: Object.freeze({
|
|
30
31
|
allowedHosts: Object.freeze(["127.0.0.1", "::1", "localhost"]),
|
|
31
32
|
blockedEnvironmentVariables: Object.freeze([
|
|
33
|
+
"DATABASE_URL",
|
|
34
|
+
"PGHOST",
|
|
35
|
+
"PGPASSWORD",
|
|
36
|
+
"PGPORT",
|
|
37
|
+
"PGUSER",
|
|
32
38
|
"SUPABASE_ACCESS_TOKEN",
|
|
33
39
|
"SUPABASE_DB_PASSWORD",
|
|
34
40
|
"SUPABASE_PROJECT_ID",
|
|
@@ -48,6 +54,8 @@ const CONFIG_FILENAMES = Object.freeze([
|
|
|
48
54
|
|
|
49
55
|
const LOOPBACK_HOSTS = new Set(REHEARSAL_DEFAULTS.safety.allowedHosts);
|
|
50
56
|
const IDENTIFIER_PATTERN = /^[a-z0-9][a-z0-9-]{1,62}$/u;
|
|
57
|
+
const DATABASE_IDENTIFIER_PATTERN = /^[a-z][a-z0-9_]{0,62}$/u;
|
|
58
|
+
const POSTGRES_IMAGE_PATTERN = /^postgres:\d+(?:\.\d+)?-alpine$/u;
|
|
51
59
|
const SAFE_COMMAND_PATTERN = /^[^\n\r\0]+$/u;
|
|
52
60
|
|
|
53
61
|
const isPlainObject = (value) =>
|
|
@@ -141,6 +149,7 @@ const normalizeConfig = (input) => {
|
|
|
141
149
|
"schemaVersion",
|
|
142
150
|
"project",
|
|
143
151
|
"supabase",
|
|
152
|
+
"postgresql",
|
|
144
153
|
"baseline",
|
|
145
154
|
"application",
|
|
146
155
|
"runtime",
|
|
@@ -164,19 +173,46 @@ const normalizeConfig = (input) => {
|
|
|
164
173
|
);
|
|
165
174
|
}
|
|
166
175
|
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
supabase
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
"
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
176
|
+
const runtimeTarget = resolveRuntimeTarget(root.runtime?.target).id;
|
|
177
|
+
const supabase =
|
|
178
|
+
runtimeTarget === "supabase"
|
|
179
|
+
? assertPlainObject(root.supabase, "config.supabase")
|
|
180
|
+
: null;
|
|
181
|
+
const postgresql =
|
|
182
|
+
runtimeTarget === "postgresql"
|
|
183
|
+
? assertPlainObject(root.postgresql, "config.postgresql")
|
|
184
|
+
: null;
|
|
185
|
+
if (runtimeTarget === "supabase" && root.postgresql !== undefined) {
|
|
186
|
+
throw new Error(
|
|
187
|
+
"config.postgresql is not allowed when config.runtime.target is supabase.",
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
if (runtimeTarget === "postgresql" && root.supabase !== undefined) {
|
|
191
|
+
throw new Error(
|
|
192
|
+
"config.supabase is not allowed when config.runtime.target is postgresql.",
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
if (supabase) {
|
|
196
|
+
assertKnownKeys(
|
|
197
|
+
supabase,
|
|
198
|
+
[
|
|
199
|
+
"workdir",
|
|
200
|
+
"migrationDirectory",
|
|
201
|
+
"rehearsalConfig",
|
|
202
|
+
"runtimeWorkdir",
|
|
203
|
+
"serviceEnvironmentFile",
|
|
204
|
+
"serviceEnvironmentVariables",
|
|
205
|
+
],
|
|
206
|
+
"config.supabase",
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
if (postgresql) {
|
|
210
|
+
assertKnownKeys(
|
|
211
|
+
postgresql,
|
|
212
|
+
["migrationDirectory", "runtimeWorkdir", "image", "database", "user"],
|
|
213
|
+
"config.postgresql",
|
|
214
|
+
);
|
|
215
|
+
}
|
|
180
216
|
const baseline = assertPlainObject(root.baseline, "config.baseline");
|
|
181
217
|
assertKnownKeys(
|
|
182
218
|
baseline,
|
|
@@ -192,7 +228,14 @@ const normalizeConfig = (input) => {
|
|
|
192
228
|
const runtime = assertPlainObject(root.runtime ?? {}, "config.runtime");
|
|
193
229
|
assertKnownKeys(
|
|
194
230
|
runtime,
|
|
195
|
-
[
|
|
231
|
+
[
|
|
232
|
+
"target",
|
|
233
|
+
"applicationUrl",
|
|
234
|
+
"projectId",
|
|
235
|
+
"apiPort",
|
|
236
|
+
"databasePort",
|
|
237
|
+
"studioPort",
|
|
238
|
+
],
|
|
196
239
|
"config.runtime",
|
|
197
240
|
);
|
|
198
241
|
const safety = assertPlainObject(root.safety ?? {}, "config.safety");
|
|
@@ -234,9 +277,13 @@ const normalizeConfig = (input) => {
|
|
|
234
277
|
}
|
|
235
278
|
|
|
236
279
|
const ports = {
|
|
237
|
-
|
|
280
|
+
...(runtimeTarget === "supabase"
|
|
281
|
+
? { api: assertPort(runtime.apiPort, "config.runtime.apiPort") }
|
|
282
|
+
: {}),
|
|
238
283
|
database: assertPort(runtime.databasePort, "config.runtime.databasePort"),
|
|
239
|
-
|
|
284
|
+
...(runtimeTarget === "supabase"
|
|
285
|
+
? { studio: assertPort(runtime.studioPort, "config.runtime.studioPort") }
|
|
286
|
+
: {}),
|
|
240
287
|
};
|
|
241
288
|
if (new Set(Object.values(ports)).size !== Object.values(ports).length) {
|
|
242
289
|
throw new Error("Rehearsal runtime ports must be unique.");
|
|
@@ -247,7 +294,7 @@ const normalizeConfig = (input) => {
|
|
|
247
294
|
throw new Error("config.verification.commands must be an array.");
|
|
248
295
|
}
|
|
249
296
|
const serviceEnvironmentVariables =
|
|
250
|
-
supabase
|
|
297
|
+
supabase?.serviceEnvironmentVariables ?? [];
|
|
251
298
|
if (!Array.isArray(serviceEnvironmentVariables)) {
|
|
252
299
|
throw new Error(
|
|
253
300
|
"config.supabase.serviceEnvironmentVariables must be an array.",
|
|
@@ -269,41 +316,105 @@ const normalizeConfig = (input) => {
|
|
|
269
316
|
);
|
|
270
317
|
}
|
|
271
318
|
if (
|
|
272
|
-
Boolean(supabase
|
|
319
|
+
Boolean(supabase?.serviceEnvironmentFile) !==
|
|
273
320
|
Boolean(normalizedServiceEnvironmentVariables.length)
|
|
274
321
|
) {
|
|
275
322
|
throw new Error(
|
|
276
323
|
"config.supabase.serviceEnvironmentFile and serviceEnvironmentVariables must be configured together.",
|
|
277
324
|
);
|
|
278
325
|
}
|
|
326
|
+
const authenticationProviders = safety.authenticationProviders
|
|
327
|
+
? assertStringArray(
|
|
328
|
+
safety.authenticationProviders,
|
|
329
|
+
"config.safety.authenticationProviders",
|
|
330
|
+
)
|
|
331
|
+
: [];
|
|
332
|
+
if (runtimeTarget === "postgresql" && authenticationProviders.length > 0) {
|
|
333
|
+
throw new Error(
|
|
334
|
+
"config.safety.authenticationProviders is available only for the Supabase target.",
|
|
335
|
+
);
|
|
336
|
+
}
|
|
279
337
|
|
|
280
338
|
return Object.freeze({
|
|
281
339
|
schemaVersion: REHEARSAL_CONFIG_VERSION,
|
|
282
340
|
project: Object.freeze({ name: projectName }),
|
|
283
|
-
supabase:
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
supabase.
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
341
|
+
supabase: supabase
|
|
342
|
+
? Object.freeze({
|
|
343
|
+
workdir: assertRelativePath(
|
|
344
|
+
supabase.workdir,
|
|
345
|
+
"config.supabase.workdir",
|
|
346
|
+
),
|
|
347
|
+
migrationDirectory: assertRelativePath(
|
|
348
|
+
supabase.migrationDirectory,
|
|
349
|
+
"config.supabase.migrationDirectory",
|
|
350
|
+
),
|
|
351
|
+
rehearsalConfig: assertRelativePath(
|
|
352
|
+
supabase.rehearsalConfig,
|
|
353
|
+
"config.supabase.rehearsalConfig",
|
|
354
|
+
),
|
|
355
|
+
runtimeWorkdir: assertRelativePath(
|
|
356
|
+
supabase.runtimeWorkdir,
|
|
357
|
+
"config.supabase.runtimeWorkdir",
|
|
358
|
+
),
|
|
359
|
+
serviceEnvironmentFile: supabase.serviceEnvironmentFile
|
|
360
|
+
? assertRelativePath(
|
|
361
|
+
supabase.serviceEnvironmentFile,
|
|
362
|
+
"config.supabase.serviceEnvironmentFile",
|
|
363
|
+
)
|
|
364
|
+
: null,
|
|
365
|
+
serviceEnvironmentVariables: Object.freeze(
|
|
366
|
+
normalizedServiceEnvironmentVariables,
|
|
367
|
+
),
|
|
368
|
+
})
|
|
369
|
+
: null,
|
|
370
|
+
postgresql: postgresql
|
|
371
|
+
? Object.freeze({
|
|
372
|
+
migrationDirectory: assertRelativePath(
|
|
373
|
+
postgresql.migrationDirectory,
|
|
374
|
+
"config.postgresql.migrationDirectory",
|
|
375
|
+
),
|
|
376
|
+
runtimeWorkdir: assertRelativePath(
|
|
377
|
+
postgresql.runtimeWorkdir ?? ".rehearsal/runtime",
|
|
378
|
+
"config.postgresql.runtimeWorkdir",
|
|
379
|
+
),
|
|
380
|
+
image: (() => {
|
|
381
|
+
const image = assertNonEmptyString(
|
|
382
|
+
postgresql.image ?? "postgres:17-alpine",
|
|
383
|
+
"config.postgresql.image",
|
|
384
|
+
);
|
|
385
|
+
if (!POSTGRES_IMAGE_PATTERN.test(image)) {
|
|
386
|
+
throw new Error(
|
|
387
|
+
"config.postgresql.image must name an official versioned Alpine PostgreSQL image, such as postgres:17-alpine.",
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
return image;
|
|
391
|
+
})(),
|
|
392
|
+
database: (() => {
|
|
393
|
+
const database = assertNonEmptyString(
|
|
394
|
+
postgresql.database ?? "postgres",
|
|
395
|
+
"config.postgresql.database",
|
|
396
|
+
);
|
|
397
|
+
if (!DATABASE_IDENTIFIER_PATTERN.test(database)) {
|
|
398
|
+
throw new Error(
|
|
399
|
+
"config.postgresql.database must be a safe lowercase PostgreSQL identifier.",
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
return database;
|
|
403
|
+
})(),
|
|
404
|
+
user: (() => {
|
|
405
|
+
const user = assertNonEmptyString(
|
|
406
|
+
postgresql.user ?? "postgres",
|
|
407
|
+
"config.postgresql.user",
|
|
408
|
+
);
|
|
409
|
+
if (!DATABASE_IDENTIFIER_PATTERN.test(user)) {
|
|
410
|
+
throw new Error(
|
|
411
|
+
"config.postgresql.user must be a safe lowercase PostgreSQL identifier.",
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
return user;
|
|
415
|
+
})(),
|
|
416
|
+
})
|
|
417
|
+
: null,
|
|
307
418
|
baseline: Object.freeze({
|
|
308
419
|
artifactDirectory: assertRelativePath(
|
|
309
420
|
baseline.artifactDirectory ??
|
|
@@ -337,6 +448,7 @@ const normalizeConfig = (input) => {
|
|
|
337
448
|
),
|
|
338
449
|
}),
|
|
339
450
|
runtime: Object.freeze({
|
|
451
|
+
target: runtimeTarget,
|
|
340
452
|
applicationUrl: assertLoopbackUrl(
|
|
341
453
|
runtime.applicationUrl ?? REHEARSAL_DEFAULTS.runtime.applicationUrl,
|
|
342
454
|
"config.runtime.applicationUrl",
|
|
@@ -357,14 +469,7 @@ const normalizeConfig = (input) => {
|
|
|
357
469
|
"config.safety.blockedEnvironmentVariables",
|
|
358
470
|
),
|
|
359
471
|
),
|
|
360
|
-
authenticationProviders: Object.freeze(
|
|
361
|
-
safety.authenticationProviders
|
|
362
|
-
? assertStringArray(
|
|
363
|
-
safety.authenticationProviders,
|
|
364
|
-
"config.safety.authenticationProviders",
|
|
365
|
-
)
|
|
366
|
-
: [],
|
|
367
|
-
),
|
|
472
|
+
authenticationProviders: Object.freeze(authenticationProviders),
|
|
368
473
|
hostedAccess: "disabled",
|
|
369
474
|
outboundNetwork: "deny",
|
|
370
475
|
}),
|
|
@@ -428,14 +533,22 @@ export const loadRehearsalConfig = async ({
|
|
|
428
533
|
applicationEnvironment: resolveOwnedPath(
|
|
429
534
|
config.application.environmentFile,
|
|
430
535
|
),
|
|
431
|
-
migrationDirectory: resolveOwnedPath(
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
536
|
+
migrationDirectory: resolveOwnedPath(
|
|
537
|
+
(config.supabase ?? config.postgresql).migrationDirectory,
|
|
538
|
+
),
|
|
539
|
+
rehearsalConfig: config.supabase
|
|
540
|
+
? resolveOwnedPath(config.supabase.rehearsalConfig)
|
|
541
|
+
: null,
|
|
542
|
+
runtimeWorkdir: resolveOwnedPath(
|
|
543
|
+
(config.supabase ?? config.postgresql).runtimeWorkdir,
|
|
544
|
+
),
|
|
545
|
+
serviceEnvironment: config.supabase?.serviceEnvironmentFile
|
|
435
546
|
? resolveOwnedPath(config.supabase.serviceEnvironmentFile)
|
|
436
547
|
: null,
|
|
437
548
|
sanitizationPolicy: resolveOwnedPath(config.baseline.sanitizationPolicy),
|
|
438
|
-
supabaseWorkdir:
|
|
549
|
+
supabaseWorkdir: config.supabase
|
|
550
|
+
? resolveOwnedPath(config.supabase.workdir)
|
|
551
|
+
: null,
|
|
439
552
|
runtimeAdapter: config.application.runtimeAdapter
|
|
440
553
|
? resolveOwnedPath(config.application.runtimeAdapter)
|
|
441
554
|
: null,
|
|
@@ -448,7 +561,7 @@ export const loadRehearsalConfig = async ({
|
|
|
448
561
|
const requiredRuntimeWorkdir = join(paths.artifactDirectory, "runtime");
|
|
449
562
|
if (paths.runtimeWorkdir !== requiredRuntimeWorkdir) {
|
|
450
563
|
throw new Error(
|
|
451
|
-
|
|
564
|
+
`config.${config.runtime.target}.runtimeWorkdir must resolve to the runtime directory inside config.baseline.artifactDirectory.`,
|
|
452
565
|
);
|
|
453
566
|
}
|
|
454
567
|
const environmentRelativePath = relative(
|
|
@@ -496,6 +609,13 @@ export const inspectDetectedProject = async ({
|
|
|
496
609
|
? "yarn"
|
|
497
610
|
: "npm";
|
|
498
611
|
const scripts = packageJson.scripts ?? {};
|
|
612
|
+
const postgresqlMigrationDirectory = (
|
|
613
|
+
await Promise.all(
|
|
614
|
+
["database/migrations", "db/migrations", "migrations"].map(
|
|
615
|
+
async (path) => ({ path, exists: await hasPath(path) }),
|
|
616
|
+
),
|
|
617
|
+
)
|
|
618
|
+
).find(({ exists }) => exists)?.path;
|
|
499
619
|
const normalizedProjectName = String(packageJson.name ?? basename(root))
|
|
500
620
|
.toLowerCase()
|
|
501
621
|
.replace(/[^a-z0-9-]+/gu, "-")
|
|
@@ -508,6 +628,9 @@ export const inspectDetectedProject = async ({
|
|
|
508
628
|
packageManager,
|
|
509
629
|
hasSupabaseConfig: await hasPath("supabase/config.toml"),
|
|
510
630
|
hasMigrations: await hasPath("supabase/migrations"),
|
|
631
|
+
hasPostgresqlMigrations: Boolean(postgresqlMigrationDirectory),
|
|
632
|
+
postgresqlMigrationDirectory:
|
|
633
|
+
postgresqlMigrationDirectory ?? "database/migrations",
|
|
511
634
|
applicationCommand:
|
|
512
635
|
["dev:rehearsal", "dev", "start"]
|
|
513
636
|
.find((name) => typeof scripts[name] === "string")
|
|
@@ -548,6 +671,7 @@ export default defineRehearsalConfig({
|
|
|
548
671
|
environmentFile: ".rehearsal/runtime.env",
|
|
549
672
|
},
|
|
550
673
|
runtime: {
|
|
674
|
+
target: "supabase",
|
|
551
675
|
applicationUrl: ${JSON.stringify(applicationUrl)},
|
|
552
676
|
projectId: "${detected.projectName}-rehearsal",
|
|
553
677
|
apiPort: ${ports.api},
|
|
@@ -567,3 +691,50 @@ export default defineRehearsalConfig({
|
|
|
567
691
|
verification: { commands: [${JSON.stringify(detected.verificationCommand)}] },
|
|
568
692
|
});
|
|
569
693
|
`;
|
|
694
|
+
|
|
695
|
+
export const renderDetectedPostgresqlConfig = (
|
|
696
|
+
detected,
|
|
697
|
+
{ applicationUrl = "http://localhost:5175", databasePort = 58322 } = {},
|
|
698
|
+
) => `// @ts-check
|
|
699
|
+
import { defineRehearsalConfig } from "@rehearsal-db/core";
|
|
700
|
+
|
|
701
|
+
export default defineRehearsalConfig({
|
|
702
|
+
schemaVersion: 1,
|
|
703
|
+
project: { name: ${JSON.stringify(detected.projectName)} },
|
|
704
|
+
postgresql: {
|
|
705
|
+
migrationDirectory: ${JSON.stringify(detected.postgresqlMigrationDirectory)},
|
|
706
|
+
runtimeWorkdir: ".rehearsal/runtime",
|
|
707
|
+
image: "postgres:17-alpine",
|
|
708
|
+
database: "postgres",
|
|
709
|
+
user: "postgres",
|
|
710
|
+
},
|
|
711
|
+
baseline: {
|
|
712
|
+
artifactDirectory: ".rehearsal",
|
|
713
|
+
sanitizationPolicy: "infrastructure/rehearsal/sanitization-policy.json",
|
|
714
|
+
},
|
|
715
|
+
application: {
|
|
716
|
+
startCommand: ${JSON.stringify(detected.applicationCommand)},
|
|
717
|
+
proofCommand: ${JSON.stringify(detected.verificationCommand)},
|
|
718
|
+
environmentFile: ".rehearsal/runtime.env",
|
|
719
|
+
},
|
|
720
|
+
runtime: {
|
|
721
|
+
target: "postgresql",
|
|
722
|
+
applicationUrl: ${JSON.stringify(applicationUrl)},
|
|
723
|
+
projectId: "${detected.projectName}-rehearsal",
|
|
724
|
+
databasePort: ${databasePort},
|
|
725
|
+
},
|
|
726
|
+
safety: {
|
|
727
|
+
allowedHosts: ["127.0.0.1", "::1", "localhost"],
|
|
728
|
+
blockedEnvironmentVariables: [
|
|
729
|
+
"DATABASE_URL",
|
|
730
|
+
"PGHOST",
|
|
731
|
+
"PGPASSWORD",
|
|
732
|
+
"PGPORT",
|
|
733
|
+
"PGUSER",
|
|
734
|
+
],
|
|
735
|
+
hostedAccess: "disabled",
|
|
736
|
+
outboundNetwork: "deny",
|
|
737
|
+
},
|
|
738
|
+
verification: { commands: [${JSON.stringify(detected.verificationCommand)}] },
|
|
739
|
+
});
|
|
740
|
+
`;
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import { createHash } from "node:crypto";
|
|
8
8
|
|
|
9
9
|
export const REHEARSAL_RESULT_VERSION = 1;
|
|
10
|
-
export const REHEARSAL_VERSION = "0.1.0-beta.
|
|
10
|
+
export const REHEARSAL_VERSION = "0.1.0-beta.6";
|
|
11
11
|
|
|
12
12
|
export const REHEARSAL_EXIT_CODES = Object.freeze({
|
|
13
13
|
success: 0,
|
|
@@ -109,7 +109,9 @@ const inferCategory = (error) => {
|
|
|
109
109
|
return "unsafe_environment";
|
|
110
110
|
}
|
|
111
111
|
if (
|
|
112
|
-
/docker|supabase|runtime|command|enoent|node\.js|ports?\b/iu.test(
|
|
112
|
+
/docker|postgres(?:ql)?|supabase|runtime|command|enoent|node\.js|ports?\b/iu.test(
|
|
113
|
+
message,
|
|
114
|
+
)
|
|
113
115
|
) {
|
|
114
116
|
return "runtime_dependency_failure";
|
|
115
117
|
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Discover likely project-owned baseline inputs without exposing their values.
|
|
3
|
+
* Discovery is bounded, ignores generated/private directories, and only sniffs
|
|
4
|
+
* small JSON or NDJSON files before returning project-relative paths.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { open, readFile, readdir, stat } from "node:fs/promises";
|
|
8
|
+
import { extname, join, relative, sep } from "node:path";
|
|
9
|
+
|
|
10
|
+
const ignoredDirectories = new Set([
|
|
11
|
+
".git",
|
|
12
|
+
".next",
|
|
13
|
+
".rehearsal",
|
|
14
|
+
".turbo",
|
|
15
|
+
"build",
|
|
16
|
+
"coverage",
|
|
17
|
+
"dist",
|
|
18
|
+
"node_modules",
|
|
19
|
+
]);
|
|
20
|
+
const maximumDepth = 4;
|
|
21
|
+
const maximumFiles = 2_000;
|
|
22
|
+
const maximumJsonBytes = 2 * 1024 * 1024;
|
|
23
|
+
const maximumNdjsonProbeBytes = 64 * 1024;
|
|
24
|
+
|
|
25
|
+
const projectRelativePath = (projectRoot, path) =>
|
|
26
|
+
relative(projectRoot, path).split(sep).join("/");
|
|
27
|
+
|
|
28
|
+
const preference = {
|
|
29
|
+
records: [
|
|
30
|
+
"rehearsal/sanitized-data.ndjson",
|
|
31
|
+
"rehearsal/synthetic-data.ndjson",
|
|
32
|
+
],
|
|
33
|
+
ledgers: ["rehearsal/migration-ledger.json"],
|
|
34
|
+
assetManifests: ["rehearsal/assets.json"],
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const sortCandidates = (kind, candidates) => {
|
|
38
|
+
const preferred = new Map(
|
|
39
|
+
preference[kind].map((path, index) => [path, index]),
|
|
40
|
+
);
|
|
41
|
+
return candidates.sort((left, right) => {
|
|
42
|
+
const leftRank = preferred.get(left) ?? Number.POSITIVE_INFINITY;
|
|
43
|
+
const rightRank = preferred.get(right) ?? Number.POSITIVE_INFINITY;
|
|
44
|
+
return leftRank - rightRank || left.localeCompare(right);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const readFirstRecord = async (path) => {
|
|
49
|
+
const handle = await open(path, "r");
|
|
50
|
+
try {
|
|
51
|
+
const buffer = Buffer.alloc(maximumNdjsonProbeBytes);
|
|
52
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
53
|
+
const probe = buffer.subarray(0, bytesRead).toString("utf8");
|
|
54
|
+
const lines = probe.split(/\r?\n/u);
|
|
55
|
+
for (const line of lines) {
|
|
56
|
+
if (!line.trim()) continue;
|
|
57
|
+
return JSON.parse(line);
|
|
58
|
+
}
|
|
59
|
+
} finally {
|
|
60
|
+
await handle.close();
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const isRecord = (value) =>
|
|
66
|
+
Boolean(
|
|
67
|
+
value &&
|
|
68
|
+
typeof value === "object" &&
|
|
69
|
+
!Array.isArray(value) &&
|
|
70
|
+
typeof value.table === "string" &&
|
|
71
|
+
value.row &&
|
|
72
|
+
typeof value.row === "object" &&
|
|
73
|
+
!Array.isArray(value.row),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
const isMigrationLedger = (value) =>
|
|
77
|
+
Array.isArray(value) &&
|
|
78
|
+
value.length > 0 &&
|
|
79
|
+
value.every(
|
|
80
|
+
(entry) =>
|
|
81
|
+
entry &&
|
|
82
|
+
typeof entry === "object" &&
|
|
83
|
+
typeof entry.version === "string" &&
|
|
84
|
+
typeof entry.name === "string" &&
|
|
85
|
+
Array.isArray(entry.statements),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
const isAssetManifest = (value) =>
|
|
89
|
+
Array.isArray(value) &&
|
|
90
|
+
value.length > 0 &&
|
|
91
|
+
value.every(
|
|
92
|
+
(entry) =>
|
|
93
|
+
entry &&
|
|
94
|
+
typeof entry === "object" &&
|
|
95
|
+
typeof entry.bucket === "string" &&
|
|
96
|
+
typeof entry.objectPath === "string" &&
|
|
97
|
+
typeof entry.file === "string",
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const collectCandidateFiles = async (projectRoot) => {
|
|
101
|
+
const files = [];
|
|
102
|
+
const visit = async (directory, depth) => {
|
|
103
|
+
if (depth > maximumDepth || files.length >= maximumFiles) return;
|
|
104
|
+
const entries = await readdir(directory, { withFileTypes: true }).catch(
|
|
105
|
+
() => [],
|
|
106
|
+
);
|
|
107
|
+
entries.sort((left, right) => left.name.localeCompare(right.name));
|
|
108
|
+
for (const entry of entries) {
|
|
109
|
+
if (files.length >= maximumFiles) return;
|
|
110
|
+
if (entry.isSymbolicLink()) continue;
|
|
111
|
+
const path = join(directory, entry.name);
|
|
112
|
+
if (entry.isDirectory()) {
|
|
113
|
+
if (!ignoredDirectories.has(entry.name)) await visit(path, depth + 1);
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (!entry.isFile() || ![".json", ".ndjson"].includes(extname(path))) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
files.push(path);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
await visit(projectRoot, 0);
|
|
123
|
+
return files;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export const discoverBaselineInputFiles = async ({
|
|
127
|
+
projectRoot = process.cwd(),
|
|
128
|
+
} = {}) => {
|
|
129
|
+
const result = { records: [], ledgers: [], assetManifests: [] };
|
|
130
|
+
for (const path of await collectCandidateFiles(projectRoot)) {
|
|
131
|
+
const pathStats = await stat(path).catch(() => undefined);
|
|
132
|
+
if (!pathStats?.isFile()) continue;
|
|
133
|
+
const relativePath = projectRelativePath(projectRoot, path);
|
|
134
|
+
try {
|
|
135
|
+
if (extname(path) === ".ndjson") {
|
|
136
|
+
if (isRecord(await readFirstRecord(path))) {
|
|
137
|
+
result.records.push(relativePath);
|
|
138
|
+
}
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
if (pathStats.size > maximumJsonBytes) continue;
|
|
142
|
+
const value = JSON.parse(await readFile(path, "utf8"));
|
|
143
|
+
if (isMigrationLedger(value)) result.ledgers.push(relativePath);
|
|
144
|
+
else if (isAssetManifest(value)) result.assetManifests.push(relativePath);
|
|
145
|
+
} catch {
|
|
146
|
+
// Invalid or unreadable files are not candidates; explicit validation will
|
|
147
|
+
// provide actionable errors if the user enters one manually.
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
records: sortCandidates("records", result.records),
|
|
152
|
+
ledgers: sortCandidates("ledgers", result.ledgers),
|
|
153
|
+
assetManifests: sortCandidates("assetManifests", result.assetManifests),
|
|
154
|
+
};
|
|
155
|
+
};
|