Feat(installer): add phase 5 migration guardrails

This commit is contained in:
Tom Boucher
2026-05-11 10:44:57 -04:00
parent c046dbaacc
commit a1f00a8a0c
11 changed files with 167 additions and 38 deletions

View File

@@ -3,6 +3,10 @@
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const {
validateInstallerMigrationActions,
validateInstallerMigrationRecord,
} = require('./installer-migration-authoring.cjs');
const MANIFEST_NAME = 'gsd-file-manifest.json';
const INSTALL_STATE_NAME = 'gsd-install-state.json';
@@ -171,19 +175,6 @@ function migrationMatchesContext(migration, { runtime, scope }) {
return true;
}
function validateMigrationRecord(record, source) {
if (!record || typeof record !== 'object') {
throw new Error(`migration record must export an object: ${source}`);
}
if (typeof record.id !== 'string' || record.id.trim() === '') {
throw new Error(`migration record must include a non-empty id: ${source}`);
}
if (typeof record.plan !== 'function') {
throw new Error(`migration record must include a plan function: ${source}`);
}
return record;
}
function discoverInstallerMigrations({ migrationsDir }) {
if (!migrationsDir || !fs.existsSync(migrationsDir)) return [];
return fs.readdirSync(migrationsDir, { withFileTypes: true })
@@ -196,7 +187,9 @@ function discoverInstallerMigrations({ migrationsDir }) {
delete require.cache[require.resolve(source)];
const exported = require(source);
const records = Array.isArray(exported) ? exported : [exported];
return records.map((record) => validateMigrationRecord({ ...record, checksum: record.checksum || checksum }, source));
return records.map((record) =>
validateInstallerMigrationRecord({ ...record, checksum: record.checksum || checksum }, source)
);
});
}
@@ -302,8 +295,11 @@ function planInstallerMigrations({
const manifest = readInstallManifest(configDir);
const state = readInstallState(configDir);
const scopedMigrations = migrations.filter((migration) =>
migration && migrationMatchesContext(migration, { runtime, scope })
const validatedMigrations = migrations.map((migration) =>
validateInstallerMigrationRecord(migration)
);
const scopedMigrations = validatedMigrations.filter((migration) =>
migrationMatchesContext(migration, { runtime, scope })
);
const applied = appliedMigrationEntries(state);
assertAppliedMigrationChecksums(applied, scopedMigrations);
@@ -320,12 +316,6 @@ function planInstallerMigrations({
};
for (const migration of pending) {
if (typeof migration.id !== 'string' || migration.id.trim() === '') {
throw new Error('migration id must be a non-empty string');
}
if (typeof migration.plan !== 'function') {
throw new Error(`migration ${migration.id} must provide a plan function`);
}
const plannedActions = migration.plan({
configDir,
runtime,
@@ -337,9 +327,7 @@ function planInstallerMigrations({
classifyArtifact: classify,
readJson: (relPath) => readJson(configDir, relPath),
});
if (!Array.isArray(plannedActions)) {
throw new Error(`migration ${migration.id} plan must return an array`);
}
validateInstallerMigrationActions(plannedActions, migration);
const checksum = migrationChecksum(migration);
for (const rawAction of plannedActions) {
const relPath = normalizeRelPath(rawAction.relPath);