Repository: goauthentik/authentik
File: web/src/flow/FlowExecutorStageFactory.ts (TypeScript)

Code before:
 * The metadata needed to load and invoke a stage.
 */
export class StageMapping {
    /**
     * A mapping of server-side stage tokens to client-side custom element tags.
     *
     * This can be used to determine if a given stage component has a corresponding client-side stage.
     */
    public static readonly registry: ReadonlyMap<FlowChallengeComponentName, StageEntry> = new Map(
        StageEntries.map((entry) => [entry[0], entry]),
    );

    public readonly token: FlowChallengeComponentName;
    public readonly variant: PropVariant;
    public readonly tag: string;

    protected constructor({ token, variant, tag }: DeepRequired<StageMappingInit>) {
        this.token = token;
        this.tag = tag;
        this.variant = variant;
    }

    /**
     * Create a `StageMapping`, caching it by token for future use.
     */
    public static async from([token, ...rest]: StageEntry): Promise<StageMapping> {
        const last = rest.at(-1);
        const callback = isImport(last) ? last : null;
        const meta = (callback ? rest.slice(0, -1) : rest) as StageEntryMetadata;

        const init = match<StageEntryMetadata, StageMappingInit>(meta)
            .with([], () => ({ token, variant: STANDARD }))
            .with([PTag, PVariant], ([tag, variant]) => ({ token, variant, tag }))
            .with([PVariant], ([variant]) => ({ token, variant }))
            .with([PTag], ([tag]) => ({ token, variant: STANDARD, tag }))
            .exhaustive();

        const tag = init.tag || (await callback?.().then(resolveStageTag));

        if (!tag) {
            throw new StageMappingError(
                `Invalid stage entry for component ${token}: No tag or import callback provided.`,
            );
        }

        return new StageMapping({ ...init, tag });
    }
}


Review comment (performance):
Your suggestions about removing the cache were spot on and made error handling here a bit easier to lay out.

Code after:
 * The metadata needed to load and invoke a stage.
 */
export class StageMapping {
    /**
     * A mapping of server-side stage tokens to client-side custom element tags.
     *
     * This can be used to determine if a given stage component has a corresponding client-side stage.
     */
    public static readonly registry: ReadonlyMap<FlowChallengeComponentName, StageEntry> = new Map(
        StageEntries.map((entry) => [entry[0], entry]),
    );

    public readonly token: FlowChallengeComponentName;
    public readonly variant: PropVariant;
    public readonly tag: string;

    protected constructor({ token, variant, tag }: DeepRequired<StageMappingInit>) {
        this.token = token;
        this.tag = tag;
        this.variant = variant;
    }

    /**
     * Create a `StageMapping` from a `StageEntry`.
     */
    public static async from([token, ...rest]: StageEntry): Promise<StageMapping> {
        const last = rest.at(-1);
        const callback = isImport(last) ? last : null;
        const meta = (callback ? rest.slice(0, -1) : rest) as StageEntryMetadata;

        const init = match<StageEntryMetadata, StageMappingInit>(meta)
            .with([], () => ({ token, variant: STANDARD }))
            .with([PTag, PVariant], ([tag, variant]) => ({ token, variant, tag }))
            .with([PVariant], ([variant]) => ({ token, variant }))
            .with([PTag], ([tag]) => ({ token, variant: STANDARD, tag }))
            .exhaustive();

        const tag = init.tag || (await callback?.().then(resolveStageTag));

        if (!tag) {
            throw new StageMappingError(
                `Invalid stage entry for component ${token}: No tag or import callback provided.`,
            );
        }

        return new StageMapping({ ...init, tag });
    }
}