Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | 9x 9x 9x 9x 9x 5x 5x 2x 2x 3x 3x 3x 2x 2x 2x 2x 9x 10x 10x 6x 10x 5x 3x 3x 4x 4x 9x 6x 6x 4x 4x 6x 3x 2x 2x 3x 3x 3x 2x 14x 1x 9x 9x 9x 2x 7x 5x 5x 5x 5x 2x 2x 4x 2x 2x 2x 1x 1x 10x 6x 6x 6x 6x 5x 5x 5x 4x 3x 30x 23x 10x 10x 2x 10x 8x 8x 2x 2x 2x 9x 9x 9x 9x 11x 11x | import { randomUUID } from "node:crypto";
import type { Express, Request, Response } from "express";
import {
browserOverrideSelectionSchema,
fullCommitShaSchema,
repositorySlugSchema,
validateExamplesRequestSchema,
type ExamplesRequestSelection,
type FullCommitSha,
type RepositorySlug,
} from "@shared/examples";
import { config } from "../config";
import { getRequestAuthorization, type RequestIdentity, type TrustConfig } from "../security/access-control";
import { asExamplesError, ExamplesError } from "../services/examples/examples-error";
import { ExamplesRepository } from "../services/examples/examples-repository";
import type { RequestContext } from "../services/examples/source-provider";
import { IdentityRateLimiter } from "../services/rate-limiter";
export interface ExamplesRoutesOptions {
trust?: TrustConfig;
disableRateLimit?: boolean;
validateRateLimiter?: IdentityRateLimiter;
overrideRateLimiter?: IdentityRateLimiter;
}
export function registerExamplesRoutes(
app: Express,
repository: Pick<ExamplesRepository, "validate" | "getCatalog" | "getExample">,
options: ExamplesRoutesOptions = {},
): void {
const trust = options.trust ?? config.trust;
const validateLimiter = options.validateRateLimiter ?? new IdentityRateLimiter("Examples validate", {
maxRequests: config.examples.validateRateLimitMaxRequests,
windowMs: 60_000,
blockDurationMs: 60_000,
});
const overrideLimiter = options.overrideRateLimiter ?? new IdentityRateLimiter("Examples override read", {
maxRequests: config.examples.overrideRateLimitMaxRequests,
windowMs: 60_000,
blockDurationMs: 30_000,
});
const disableRateLimit = options.disableRateLimit ?? config.server.disableRateLimit;
app.post("/api/examples/validate", async (req, res) => {
const parsed = validateExamplesRequestSchema.safeParse(req.body);
if (!parsed.success) {
const code = hasInvalidRefOnly(req.body) ? "INVALID_REF" : "INVALID_SELECTION";
return sendError(res, new ExamplesError(code, "Invalid external examples selection"));
}
const identity = authorizeOverride(req, res, trust);
Iif (!identity) return;
if (!disableRateLimit && !checkRateLimit(validateLimiter, identity, res)) return;
res.setHeader("Cache-Control", "private, no-store");
try {
const source = await repository.validate(parsed.data.selection, requestContext(req, identity));
res.json({ schemaVersion: 1, valid: true, source });
} catch (error) {
sendError(res, asExamplesError(error));
}
});
app.get("/api/examples", async (req, res) => {
const selection = parseCatalogSelection(req);
if (selection instanceof ExamplesError) return sendError(res, selection);
const identity = selection.kind === "browser-override"
? authorizeOverride(req, res, trust)
: ({ subject: "default", roles: [] } satisfies RequestIdentity);
if (!identity) return;
if (selection.kind === "browser-override") {
res.setHeader("Cache-Control", "private, no-store");
if (!disableRateLimit && !checkRateLimit(overrideLimiter, identity, res)) return;
}
try {
res.json(await repository.getCatalog(selection, requestContext(req, identity)));
} catch (error) {
sendError(res, asExamplesError(error));
}
});
app.get("/api/examples/:id", async (req, res) => {
const detail = parseDetailSelection(req);
if (detail instanceof ExamplesError) return sendError(res, detail);
const isExternal = detail.repository !== undefined;
const identity = isExternal
? authorizeOverride(req, res, trust)
: ({ subject: "default", roles: [] } satisfies RequestIdentity);
if (!identity) return;
if (isExternal) {
res.setHeader("Cache-Control", "private, no-store");
Iif (!disableRateLimit && !checkRateLimit(overrideLimiter, identity, res)) return;
}
try {
const example = await repository.getExample(
detail.repository,
detail.revision,
req.params.id,
requestContext(req, identity),
);
if (!example) return sendError(res, new ExamplesError("EXAMPLE_NOT_FOUND", "Example not found"));
res.json(example);
} catch (error) {
sendError(res, asExamplesError(error));
}
});
}
function parseCatalogSelection(req: Request): ExamplesRequestSelection | ExamplesError {
if (Object.keys(req.query).some((key) => key !== "repository" && key !== "ref")) {
return new ExamplesError("INVALID_SELECTION", "Unknown external examples query parameter");
}
const repository = singleQueryValue(req.query.repository);
const ref = singleQueryValue(req.query.ref);
if (repository === null || ref === null || (repository === undefined) !== (ref === undefined)) {
return new ExamplesError("INVALID_SELECTION", "Repository and ref must be provided together");
}
if (repository === undefined || ref === undefined) return { kind: "default" };
const parsedRepository = repositorySlugSchema.safeParse(repository);
Iif (!parsedRepository.success) return new ExamplesError("INVALID_SELECTION", "Invalid external examples repository");
const parsed = browserOverrideSelectionSchema.safeParse({ repository, ref });
return parsed.success
? { kind: "browser-override", ...parsed.data }
: new ExamplesError("INVALID_REF", "Invalid external examples ref");
}
function hasInvalidRefOnly(value: unknown): boolean {
Iif (!value || typeof value !== "object" || Array.isArray(value)) return false;
const body = value as Record<string, unknown>;
Iif (body.schemaVersion !== 1 || Object.keys(body).some((key) => key !== "schemaVersion" && key !== "selection")) return false;
Iif (!body.selection || typeof body.selection !== "object" || Array.isArray(body.selection)) return false;
const selection = body.selection as Record<string, unknown>;
if (Object.keys(selection).length !== 2
|| Object.keys(selection).some((key) => key !== "repository" && key !== "ref")
|| typeof selection.ref !== "string") return false;
return repositorySlugSchema.safeParse(selection.repository).success
&& !browserOverrideSelectionSchema.safeParse(selection).success;
}
function parseDetailSelection(req: Request):
| { repository?: RepositorySlug; revision?: FullCommitSha }
| ExamplesError {
Iif (Object.keys(req.query).some((key) => key !== "repository" && key !== "revision")) {
return new ExamplesError("INVALID_SELECTION", "Unknown external examples query parameter");
}
const repository = singleQueryValue(req.query.repository);
const revision = singleQueryValue(req.query.revision);
Iif (repository === null || revision === null || (repository === undefined) !== (revision === undefined)) {
return new ExamplesError("INVALID_REVISION", "Repository and revision must be provided together");
}
if (repository === undefined || revision === undefined) return {};
const parsedRepository = repositorySlugSchema.safeParse(repository);
const parsedRevision = fullCommitShaSchema.safeParse(revision);
if (!parsedRepository.success) return new ExamplesError("INVALID_SELECTION", "Invalid external examples repository");
if (!parsedRevision.success) return new ExamplesError("INVALID_REVISION", "Invalid external examples revision");
return { repository: parsedRepository.data, revision: parsedRevision.data };
}
function singleQueryValue(value: unknown): string | undefined | null {
if (value === undefined) return undefined;
return typeof value === "string" ? value : null;
}
function authorizeOverride(req: Request, res: Response, trust: TrustConfig): RequestIdentity | null {
const authorization = getRequestAuthorization(req, trust);
if (authorization.allowed) return authorization.identity;
res.status(authorization.status).json({ error: authorization.status === 401 ? "Unauthorized" : "Forbidden" });
return null;
}
function checkRateLimit(limiter: IdentityRateLimiter, identity: RequestIdentity, res: Response): boolean {
const result = limiter.checkLimit(identity.subject);
if (result.allowed) return true;
res.setHeader("Retry-After", String(result.retryAfter));
sendError(res, new ExamplesError("RATE_LIMITED", "External examples request rate exceeded", result.retryAfter));
return false;
}
function requestContext(req: Request, identity: RequestIdentity): RequestContext {
const controller = new AbortController();
req.once("aborted", () => controller.abort());
const header = req.header("x-request-id");
return {
identity: identity.subject,
requestId: header && header.length <= 128 ? header : randomUUID(),
signal: controller.signal,
};
}
function sendError(res: Response, error: ExamplesError): Response {
if (error.retryAfterSeconds) res.setHeader("Retry-After", String(error.retryAfterSeconds));
return res.status(error.status).json({
schemaVersion: 1,
error: {
code: error.code,
message: error.message,
...(error.retryAfterSeconds ? { retryAfterSeconds: error.retryAfterSeconds } : {}),
},
});
}
|