Wires composePlatformPermissionRegistry into apps/api's production startup: startApi() discovers every installed module, composes the one immutable platform permission registry (Core's own declarations plus every module's), and only then constructs the shared PostgreSQL pool, Better Auth, and the HTTP server. A discovery or composition failure — an invalid manifest, a reserved core.* namespace claim, or a duplicate declaration — propagates before anything exists to close; there is no Core-only registry fallback. apps/api/src/platform-permissions.ts resolves the repository root from its own file location, never process.cwd(), so package-scoped test runs discover the right modules/ directory. createApiApp/createGrantsRoutes now require the composed registry as an injected authorization option, replacing the previous hard-coded CORE_PERMISSION_REGISTRY; requirePermission continues to work identically for Core-owned and module-owned permissions, since it has no concept of the distinction. Extends pnpm doctor with a modules.permissions check (reusing the already-discovered module list, distinguishing reserved-namespace from generic-duplicate failures, never re-discovering modules) and pnpm modules:validate to compose the same registry before a module is considered installable. Adds focused coverage: startup-ordering and failure-isolation tests in apps/api/tests/lifecycle.test.ts, module-owned-declaration enforcement tests in apps/api/tests/authorization-middleware.test.ts, Doctor composition tests in core/tests/doctor.test.ts, and a live PostgreSQL end-to-end scenario proving a real installed module's permission is discoverable, grantable, and deniable once the module is absent again after a restart, without ever executing its server entrypoint.
78 lines
2.9 KiB
TypeScript
78 lines
2.9 KiB
TypeScript
import { readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
import { PermissionRegistryError } from "../authorization/index.js";
|
|
import {
|
|
composePlatformPermissionRegistry,
|
|
discoverModuleSchemas,
|
|
ModuleDiscoveryError,
|
|
ModulePermissionCompositionError,
|
|
ModuleSchemaDiscoveryError,
|
|
} from "../modules/index.js";
|
|
|
|
interface PackageVersion {
|
|
version?: unknown;
|
|
}
|
|
|
|
async function readPackageVersion(packagePath: string): Promise<string> {
|
|
const packageData = JSON.parse(await readFile(packagePath, "utf8")) as PackageVersion;
|
|
if (typeof packageData.version !== "string") {
|
|
throw new Error(`${packagePath} does not declare a version.`);
|
|
}
|
|
return packageData.version;
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const repositoryRoot = process.cwd();
|
|
const [platformVersion, moduleSdkVersion] = await Promise.all([
|
|
readPackageVersion(path.join(repositoryRoot, "package.json")),
|
|
readPackageVersion(path.join(repositoryRoot, "packages/module-sdk/package.json")),
|
|
]);
|
|
const schemaAggregate = await discoverModuleSchemas({
|
|
moduleSdkVersion,
|
|
modulesDirectory: path.join(repositoryRoot, "modules"),
|
|
platformVersion,
|
|
});
|
|
const modules = schemaAggregate.discoveredModules;
|
|
|
|
// Reuses the modules discoverModuleSchemas already discovered — never rediscovers modules
|
|
// solely for permission composition. A manifest that passes structural validation but claims
|
|
// the reserved core.* namespace, or duplicates another declaration, fails validation here
|
|
// before this module is considered installable.
|
|
composePlatformPermissionRegistry(modules);
|
|
|
|
if (modules.length === 0) {
|
|
console.log("Module validation passed (0 modules installed).");
|
|
return;
|
|
}
|
|
|
|
console.log(
|
|
`Module validation passed (${modules.length} modules, ${schemaAggregate.tables.length} tables):`,
|
|
);
|
|
for (const discoveredModule of modules) {
|
|
console.log(`- ${discoveredModule.manifest.id}@${discoveredModule.manifest.version}`);
|
|
}
|
|
}
|
|
|
|
main().catch((error: unknown) => {
|
|
if (error instanceof ModuleDiscoveryError) {
|
|
console.error(error.message);
|
|
for (const issue of error.issues) {
|
|
const issuePath = issue.path ? ` (${issue.path})` : "";
|
|
console.error(`[${issue.code}] ${issue.moduleDirectory}${issuePath}: ${issue.message}`);
|
|
}
|
|
} else if (error instanceof ModuleSchemaDiscoveryError) {
|
|
console.error(error.message);
|
|
for (const issue of error.issues) {
|
|
const issuePath = issue.path ? ` (${issue.path})` : "";
|
|
console.error(`[${issue.code}] ${issue.moduleId}${issuePath}: ${issue.message}`);
|
|
}
|
|
} else if (error instanceof ModulePermissionCompositionError) {
|
|
console.error(`[${error.code}] ${error.message}`);
|
|
} else if (error instanceof PermissionRegistryError) {
|
|
console.error(`[${error.code}] ${error.message}`);
|
|
} else {
|
|
console.error(error instanceof Error ? error.message : String(error));
|
|
}
|
|
process.exitCode = 1;
|
|
});
|