|
| 1 | +/** |
| 2 | + * Shared utilities for parsing module fallback service requests. |
| 3 | + * |
| 4 | + * The module fallback service supports two protocols: |
| 5 | + * - V1 (legacy): GET request with query params and X-Resolve-Method header |
| 6 | + * - V2 (new_module_registry): POST request with JSON body |
| 7 | + * |
| 8 | + * The protocol version is determined by the `new_module_registry` compatibility flag. |
| 9 | + */ |
| 10 | +import assert from "node:assert"; |
| 11 | +import type { Request } from "../../http"; |
| 12 | + |
| 13 | +/** V1 protocol request (legacy module registry) */ |
| 14 | +export interface V1ModuleFallbackRequest { |
| 15 | + protocol: "v1"; |
| 16 | + /** Import type: "import" for ES modules, "require" for CommonJS */ |
| 17 | + type: "import" | "require"; |
| 18 | + /** Module specifier as a path (e.g., "/my-module.js") */ |
| 19 | + specifier: string; |
| 20 | + /** Original specifier as written in source code */ |
| 21 | + rawSpecifier?: string; |
| 22 | + /** Referrer module path */ |
| 23 | + referrer?: string; |
| 24 | +} |
| 25 | + |
| 26 | +/** V2 protocol request (new module registry) */ |
| 27 | +export interface V2ModuleFallbackRequest { |
| 28 | + protocol: "v2"; |
| 29 | + /** Import type: includes "internal" for runtime-originated imports */ |
| 30 | + type: "import" | "require" | "internal"; |
| 31 | + /** Module specifier as a URL (e.g., "file:///bundle/my-module.js") */ |
| 32 | + specifier: string; |
| 33 | + /** Original specifier as written in source code */ |
| 34 | + rawSpecifier?: string; |
| 35 | + /** Referrer module URL */ |
| 36 | + referrer?: string; |
| 37 | + /** Import attributes from the import statement */ |
| 38 | + attributes?: Array<{ name: string; value: string }>; |
| 39 | +} |
| 40 | + |
| 41 | +/** Discriminated union of both protocol versions */ |
| 42 | +export type ParsedModuleFallbackRequest = |
| 43 | + | V1ModuleFallbackRequest |
| 44 | + | V2ModuleFallbackRequest; |
| 45 | + |
| 46 | +/** |
| 47 | + * Checks if a request is a module fallback service request. |
| 48 | + * This detects both V1 (GET with X-Resolve-Method header) and V2 (POST) protocols. |
| 49 | + */ |
| 50 | +export function isModuleFallbackRequest(request: Request): boolean { |
| 51 | + // V1: GET request with X-Resolve-Method header |
| 52 | + if (request.method === "GET" && request.headers.has("X-Resolve-Method")) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + |
| 56 | + // V2: POST request (the new module registry always uses POST) |
| 57 | + if (request.method === "POST") { |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | +} |
| 63 | + |
| 64 | +export function assertIsV2ModuleFallbackProtocol( |
| 65 | + body: unknown |
| 66 | +): asserts body is Omit<V2ModuleFallbackRequest, "protocol"> { |
| 67 | + assert(typeof body === "object" && body !== null && "specifier" in body); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Parses a module fallback service request into a protocol-specific format. |
| 72 | + * Automatically detects V1 vs V2 protocol based on HTTP method. |
| 73 | + * |
| 74 | + * @param request - The incoming Request object |
| 75 | + * @returns Parsed request data, or null if the request is malformed |
| 76 | + */ |
| 77 | +export async function parseModuleFallbackRequest( |
| 78 | + request: Request |
| 79 | +): Promise<ParsedModuleFallbackRequest | null> { |
| 80 | + // V1 Protocol: GET with X-Resolve-Method header |
| 81 | + if (request.method === "GET" && request.headers.has("X-Resolve-Method")) { |
| 82 | + const url = new URL(request.url); |
| 83 | + const specifier = url.searchParams.get("specifier"); |
| 84 | + |
| 85 | + if (!specifier) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + const resolveMethod = request.headers.get("X-Resolve-Method"); |
| 90 | + |
| 91 | + return { |
| 92 | + protocol: "v1", |
| 93 | + type: resolveMethod === "require" ? "require" : "import", |
| 94 | + specifier, |
| 95 | + rawSpecifier: url.searchParams.get("rawSpecifier") ?? undefined, |
| 96 | + referrer: url.searchParams.get("referrer") ?? undefined, |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + // V2 Protocol: POST with JSON body |
| 101 | + if (request.method === "POST") { |
| 102 | + try { |
| 103 | + const body = await request.json(); |
| 104 | + assertIsV2ModuleFallbackProtocol(body); |
| 105 | + |
| 106 | + return { |
| 107 | + ...body, |
| 108 | + protocol: "v2", |
| 109 | + }; |
| 110 | + } catch { |
| 111 | + return null; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + return null; |
| 116 | +} |
0 commit comments