208 lines
7.2 KiB
TypeScript
208 lines
7.2 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
|
|
import { planTopologyCapacity } from "../../src/platform/cardinality/topology-capacity.ts";
|
|
import {
|
|
selectAppIndexes,
|
|
type ContextDeclaration,
|
|
type FeatureDeclaration,
|
|
type TopologyDeclarationSource,
|
|
} from "../../scripts/generate-product-feature-index.ts";
|
|
import {
|
|
APP_CONTEXT_BUDGET,
|
|
CONTEXT_FEATURE_BUDGET,
|
|
SYNTHETIC_APP_SHAPE,
|
|
} from "./scale-profile.ts";
|
|
|
|
/**
|
|
* Spec §37. A compact model of a global product topology.
|
|
*
|
|
* Only counts are stored. An app's contexts and feature ids are derived on
|
|
* demand from its index, so a 1,000,000-feature topology costs a handful of
|
|
* numbers plus whatever single app a test actually inspects.
|
|
*/
|
|
export type SyntheticContext = Readonly<{
|
|
id: string;
|
|
routePath: string;
|
|
featureIds: readonly string[];
|
|
}>;
|
|
|
|
export type SyntheticApp = Readonly<{
|
|
id: string;
|
|
localFeatureCount: number;
|
|
contexts: readonly SyntheticContext[];
|
|
}>;
|
|
|
|
export type SyntheticTopology = Readonly<{
|
|
globalFeatures: number;
|
|
appCount: number;
|
|
contextCount: number;
|
|
apps: Readonly<{ length: number; [index: number]: SyntheticApp }>;
|
|
app(id: string): SyntheticApp;
|
|
}>;
|
|
|
|
function contextFor(appIndex: number, contextIndex: number, features: number): SyntheticContext {
|
|
const id = `app${appIndex}-ctx${contextIndex}`;
|
|
return Object.freeze({
|
|
id,
|
|
routePath: `ctx${contextIndex}/*`,
|
|
featureIds: Object.freeze(
|
|
Array.from({ length: features }, (_, featureIndex) => `${id}-f${featureIndex}`),
|
|
),
|
|
});
|
|
}
|
|
|
|
export function createSyntheticTopology(
|
|
options: Readonly<{ globalFeatures: number }>,
|
|
): SyntheticTopology {
|
|
const { contextsPerApp, featuresPerContext } = SYNTHETIC_APP_SHAPE;
|
|
const plan = planTopologyCapacity({
|
|
featureCount: options.globalFeatures,
|
|
maxFeaturesPerContext: featuresPerContext,
|
|
maxContextsPerApp: contextsPerApp,
|
|
});
|
|
const featuresPerApp = contextsPerApp * featuresPerContext;
|
|
const cache = new Map<number, SyntheticApp>();
|
|
|
|
function build(appIndex: number): SyntheticApp {
|
|
const cached = cache.get(appIndex);
|
|
if (cached) return cached;
|
|
const firstFeature = appIndex * featuresPerApp;
|
|
const localFeatureCount = Math.max(
|
|
0,
|
|
Math.min(featuresPerApp, options.globalFeatures - firstFeature),
|
|
);
|
|
const contextCount = Math.ceil(localFeatureCount / featuresPerContext);
|
|
const app = Object.freeze({
|
|
id: `app-${appIndex}`,
|
|
localFeatureCount,
|
|
contexts: Object.freeze(
|
|
Array.from({ length: contextCount }, (_, contextIndex) =>
|
|
contextFor(
|
|
appIndex,
|
|
contextIndex,
|
|
Math.min(featuresPerContext, localFeatureCount - contextIndex * featuresPerContext),
|
|
),
|
|
),
|
|
),
|
|
});
|
|
// Inspecting an app is local work; the cache never exceeds what the test
|
|
// itself asked for.
|
|
cache.set(appIndex, app);
|
|
return app;
|
|
}
|
|
|
|
const apps = new Proxy(Object.freeze({ length: plan.appCount }), {
|
|
get(target, property) {
|
|
if (typeof property === "string" && /^\d+$/u.test(property)) {
|
|
const index = Number(property);
|
|
return index < plan.appCount ? build(index) : undefined;
|
|
}
|
|
return Reflect.get(target, property);
|
|
},
|
|
}) as SyntheticTopology["apps"];
|
|
|
|
return Object.freeze({
|
|
globalFeatures: options.globalFeatures,
|
|
appCount: plan.appCount,
|
|
contextCount: plan.contextCount,
|
|
apps,
|
|
app(id: string) {
|
|
const match = /^app-(\d+)$/u.exec(id);
|
|
const index = match ? Number(match[1]) : Number.NaN;
|
|
if (!Number.isSafeInteger(index) || index >= plan.appCount) {
|
|
throw new RangeError(`unknown synthetic app ${id}`);
|
|
}
|
|
return build(index);
|
|
},
|
|
});
|
|
}
|
|
|
|
export type DeclarationReads = { apps: number; contexts: number; features: number };
|
|
|
|
/**
|
|
* The synthetic topology as a production `TopologyDeclarationSource`: the same
|
|
* ownership-indexed interface the filesystem source implements. It counts how
|
|
* many declarations the production selection actually reads, which is the
|
|
* locality evidence at 1,000,000 logical features.
|
|
*/
|
|
export function declarationSource(
|
|
topology: SyntheticTopology,
|
|
): TopologyDeclarationSource & Readonly<{ reads: DeclarationReads }> {
|
|
const reads: DeclarationReads = { apps: 0, contexts: 0, features: 0 };
|
|
const appIndex = (appId: string) => {
|
|
const match = /^app-(\d+)$/u.exec(appId);
|
|
const index = match ? Number(match[1]) : Number.NaN;
|
|
return Number.isSafeInteger(index) && index < topology.appCount ? index : -1;
|
|
};
|
|
const contextOwner = (contextId: string) => {
|
|
const match = /^app(\d+)-ctx(\d+)$/u.exec(contextId);
|
|
return match ? { app: `app-${match[1]}`, context: Number(match[2]) } : null;
|
|
};
|
|
return Object.freeze({
|
|
reads,
|
|
appIds() {
|
|
// Listing app ids is O(apps), never O(features); the generator only
|
|
// needs it when no app is selected explicitly.
|
|
return Array.from({ length: topology.appCount }, (_, index) => `app-${index}`);
|
|
},
|
|
app(appId: string) {
|
|
if (appIndex(appId) < 0) return undefined;
|
|
reads.apps += 1;
|
|
return { id: appId, maxContexts: APP_CONTEXT_BUDGET };
|
|
},
|
|
contextsOf(appId: string): readonly ContextDeclaration[] {
|
|
if (appIndex(appId) < 0) return [];
|
|
const contexts = topology.app(appId).contexts;
|
|
reads.contexts += contexts.length;
|
|
return contexts.map((context) => ({
|
|
id: context.id,
|
|
appId,
|
|
routePath: context.routePath,
|
|
maxFeatures: CONTEXT_FEATURE_BUDGET,
|
|
}));
|
|
},
|
|
featuresOf(contextId: string): readonly FeatureDeclaration[] {
|
|
const owner = contextOwner(contextId);
|
|
if (!owner || appIndex(owner.app) < 0) return [];
|
|
const context = topology.app(owner.app).contexts[owner.context];
|
|
if (!context) return [];
|
|
reads.features += context.featureIds.length;
|
|
return context.featureIds.map((id) => ({ id, contextId, directory: id }));
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Everything one deployable app compiles and boots with, produced by the
|
|
* production generator's own app selection (`selectAppIndexes`).
|
|
*/
|
|
export function selectAppInputs(topology: SyntheticTopology, appId: string) {
|
|
const source = declarationSource(topology);
|
|
const selected = selectAppIndexes(source, appId);
|
|
const contextIds = [...new Set(selected.entries.map((entry) => entry.contextId))];
|
|
const generated = [selected.featureIndex, selected.contextIndex, selected.appIndex];
|
|
// Every module the generated entries hand to the compiler/bundler (static
|
|
// and lazy imports), resolved from `src/.generated/`.
|
|
const compileInputPaths = [
|
|
...new Set(
|
|
generated.flatMap((text) =>
|
|
[...text.matchAll(/(?:from\s+|import\()\s*"(\.\.\/[^"]+)"/gu)].map((match) =>
|
|
`src/${(match[1] as string).slice("../".length)}`,
|
|
),
|
|
),
|
|
),
|
|
].sort();
|
|
return Object.freeze({
|
|
contextIds,
|
|
featureIds: selected.entries.map((entry) => entry.id),
|
|
routeRootIds: [...new Set(selected.entries.map((entry) => entry.contextRoutePath))],
|
|
generatedFeatureIndex: selected.featureIndex,
|
|
generatedContextIndex: selected.contextIndex,
|
|
generatedAppIndex: selected.appIndex,
|
|
generatedHashes: generated.map((text) => createHash("sha256").update(text).digest("hex")),
|
|
compileInputPaths,
|
|
contextBudget: APP_CONTEXT_BUDGET,
|
|
reads: { ...source.reads },
|
|
});
|
|
}
|