@thednp/rpc 0.3.3 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"server.mjs","names":[],"sources":["../../src/options.ts","../../src/functionsMap.ts","../../src/constants.ts","../../src/server-helpers.ts","../../src/scanForServerFiles.ts","../../src/createFunction.ts","../../src/validate.ts","../../src/getClientModules.ts","../../src/context.ts"],"sourcesContent":["import type {\n MiddlewareOptions,\n RpcPluginOptions,\n ServerFunctionOptions,\n} from \"./types.d.ts\";\n\nexport const defaultServerFnOptions: ServerFunctionOptions = {\n contentType: \"application/json\",\n credentials: \"same-origin\",\n method: \"POST\",\n};\n\nexport const defaultPrefix = \"__rpc\";\n\nexport const defaultRPCOptions: RpcPluginOptions = {\n rpcPrefix: defaultPrefix,\n adapter: \"express\",\n serverFiles: \"exact\",\n scanRoot: undefined,\n};\n\nexport const defaultMiddlewareOptions: MiddlewareOptions = {\n rpcPrefix: undefined,\n path: undefined,\n origin: undefined,\n};\n","import type { ServerFnEntry } from \"./types.d.ts\";\nimport { defaultPrefix } from \"./options.ts\";\n\n/**\n * Global symbol under which the shared `serverFunctionsByPrefix` map is stored\n * on `globalThis`. Keeping it on a `Symbol.for` key makes it instance-stable\n * across the bundled entry copies (`index.mjs`, `server.mjs`, `express.mjs`,\n * ...) and dev-server hot reloads, exactly like the request-context storage in\n * `context.ts`. Without this, `scanForServerFiles` (bundled into the plugin)\n * would populate a map copy the adapter middleware could not read.\n */\nconst functionsMapSymbol = Symbol.for(\"thednp.rpc.functionsMap\");\n\n/**\n * Map of rpcPrefix -> Map of function names -> ServerFnEntry\n * Enables multiple RPC instances with different prefixes to coexist\n * without name collisions.\n */\nexport const serverFunctionsByPrefix: Map<\n string,\n Map<string, ServerFnEntry>\n> = ((globalThis as Record<symbol, Map<string, Map<string, ServerFnEntry>>>)[\n functionsMapSymbol\n] ??= new Map());\n\n/**\n * Gets or creates the function map for a specific prefix.\n * @param prefix - The RPC prefix (e.g., \"__rpc\", \"v1:rpc\", \"admin:rpc\")\n * @returns Map of function names to ServerFnEntry for that prefix\n */\nexport const getFunctionsForPrefix = (\n prefix: string,\n): Map<string, ServerFnEntry> => {\n if (!serverFunctionsByPrefix.has(prefix)) {\n serverFunctionsByPrefix.set(prefix, new Map());\n }\n return serverFunctionsByPrefix.get(prefix)!;\n};\n\n/**\n * Backward compatibility: default map for the default prefix.\n * Legacy code can still use serverFunctionsMap.set(name, entry).\n */\nexport const serverFunctionsMap: Map<string, ServerFnEntry> = {\n get: (key: string) => getFunctionsForPrefix(defaultPrefix).get(key),\n set: (key: string, value: ServerFnEntry) =>\n getFunctionsForPrefix(defaultPrefix).set(key, value),\n has: (key: string) => getFunctionsForPrefix(defaultPrefix).has(key),\n delete: (key: string) => getFunctionsForPrefix(defaultPrefix).delete(key),\n clear: () => getFunctionsForPrefix(defaultPrefix).clear(),\n get size() {\n return getFunctionsForPrefix(defaultPrefix).size;\n },\n entries: () => getFunctionsForPrefix(defaultPrefix).entries(),\n keys: () => getFunctionsForPrefix(defaultPrefix).keys(),\n values: () => getFunctionsForPrefix(defaultPrefix).values(),\n forEach: (\n callback: (\n value: ServerFnEntry,\n key: string,\n map: Map<string, ServerFnEntry>,\n ) => void,\n ) => getFunctionsForPrefix(defaultPrefix).forEach(callback),\n [Symbol.iterator]: () =>\n getFunctionsForPrefix(defaultPrefix)[Symbol.iterator](),\n} as unknown as Map<string, ServerFnEntry>;\n","export const OPERATION_ABORTED = \"Operation aborted\";\n\nexport const REQUEST_CANCELLED = \"Request was cancelled\";\n\nexport const FETCH_ERROR_PREFIX = \"Fetch error: \";\n\nexport const NO_SERVER_FUNCTION_FOUND = \"No server function found.\";\n\nexport const ERROR_LOADING_FILE = \"Error loading file:\";\n\nexport const FUNCTION_NOT_FOUND = \"Function not found\";\n\nexport const METHOD_NOT_ALLOWED = \"Method Not Allowed\";\n\nexport const REQUEST_FORBIDDEN = \"Forbidden\";\n\nexport const UNSUPPORTED_MEDIA_TYPE = \"Unsupported Media Type\";\n\nexport const BAD_REQUEST = \"Bad Request\";\n\nexport const INTERNAL_SERVER_ERROR = \"Internal Server Error\";\n\nexport const CLIENT_DISCONNECTED = \"client disconnected\";\n\n/** Returns a warning when a middleware name is reused, preventing registration conflicts. @param name - The duplicate middleware name */\nexport const MIDDLEWARE_NAME_USED = (name: string) =>\n `The middleware name \"${name}\" is already used.`;\n\n/** Error message when a value fails the safe-identifier validation. @param label - What kind of value was being validated. @param name - The rejected value */\nexport const INVALID_IDENTIFIER = (label: string, name: string) =>\n `Invalid ${label}: \"${name}\" must match /^[A-Za-z_$][A-Za-z0-9_$]*$/`;\n\n/** Error message when a value fails the safe-path-segment validation. @param label - What kind of value was being validated. @param segment - The rejected value */\nexport const INVALID_PATH_SEGMENT = (label: string, segment: string) =>\n `Invalid ${label}: \"${segment}\" must match /^[A-Za-z0-9_$@:][A-Za-z0-9_$@:/-]*$/`;\n\n/** Warning message when a specified RPC config file cannot be resolved on disk. @param configFile - The requested config filename. @param configFilePath - The resolved absolute path */\nexport const CONFIG_FILE_NOT_FOUND = (\n configFile: string,\n configFilePath: string,\n) =>\n ` ⚠︎ The specified RPC config file ${configFile} cannot be found at ${configFilePath}, loading the defaults..`;\n\nexport const NO_CONFIG_FOUND =\n ` ⚡︎ No RPC config found, loading the defaults..`;\n\nexport const FAILED_LOAD_CONFIG = ` ⚠︎ Failed to load RPC config:`;\n\n/** Error template for duplicate server function names across files. @param name - The duplicate registered name */\nexport const DUPLICATE_FUNCTION_NAME = (name: string) =>\n `Duplicate server function \"${name}\" detected. Each server function must have a unique name. Remove or rename the duplicate.`;\n","/** @module Server-side utilities. Exports the `RPCError` class for typed server-side errors, `formatError` for middleware error responses, `isFormContentType` and `hasContentTypeMismatch` for content-type validation, and `walkGlobFiles` for recursively discovering `*.server.*` files. Never import this module in client code — it is server-only. */\nimport type { ContentType, JsonObject, JsonValue } from \"./types.d.ts\";\nimport { readdir } from \"node:fs/promises\";\nimport { join } from \"node:path\";\n\nimport { INTERNAL_SERVER_ERROR } from \"./constants.ts\";\n\nconst GLOB_REGEX = /^.+\\.server\\.(ts|js|mjs|mts)$/;\n\n/**\n * Recursively walks `dir` and collects absolute paths to files whose\n * basename matches the `*.server.{ts,js,mjs,mts}` glob pattern.\n */\nexport const walkGlobFiles = async (dir: string): Promise<string[]> => {\n const results: string[] = [];\n const stack = [dir];\n while (stack.length) {\n const current = stack.pop()!;\n let entries;\n try {\n entries = await readdir(current, { withFileTypes: true });\n } catch (_e) {\n continue;\n }\n for (const entry of entries) {\n const fullPath = join(current, entry.name);\n if (entry.isFile() && GLOB_REGEX.test(entry.name)) {\n results.push(fullPath);\n } else if (entry.isDirectory()) {\n stack.push(fullPath);\n }\n }\n }\n return results;\n};\n\n/**\n * A typed error thrown from server functions.\n * The middleware serializes the `message` and `code` in the response,\n * allowing clients to recognise and handle specific error conditions.\n */\nexport class RPCError extends Error {\n /** Machine-readable error code (e.g. \"VALIDATION_FAILED\", \"UNAUTHORIZED\") */\n code: string;\n /** Optional diagnostic payload */\n data?: JsonValue;\n constructor(message: string, code = \"INTERNAL\", data?: JsonValue) {\n super(message);\n this.name = \"RPCError\";\n this.code = code;\n this.data = data;\n }\n}\n\n/**\n * Formats an error for the RPC middleware response.\n * In development the full `RPCError` payload is included so developers\n * can quickly identify issues. Unexpected exceptions never expose their\n * message — only the generic \"Internal Server Error\" is sent, preventing\n * information disclosure; server-side diagnostics are preserved via the\n * middleware's `console.error` logging.\n */\nexport const formatError = (\n err: unknown,\n isProduction: boolean,\n): JsonObject => {\n if (isProduction) {\n return { error: INTERNAL_SERVER_ERROR };\n }\n if (err instanceof RPCError) {\n const payload: JsonObject = {\n error: err.message || INTERNAL_SERVER_ERROR,\n code: err.code,\n };\n if (err.data !== undefined) payload.data = err.data;\n return payload;\n }\n return { error: INTERNAL_SERVER_ERROR };\n};\n\n/**\n * Checks whether a content type maps to a form encoding\n * (`multipart/form-data` or `application/x-www-form-urlencoded`).\n * Form-declared functions accept either encoding so native browser\n * submissions (urlencoded) keep working without JavaScript.\n */\nexport const isFormContentType = (contentType: string): boolean =>\n contentType === \"multipart/form-data\" ||\n contentType === \"application/x-www-form-urlencoded\";\n\n/**\n * Detects whether an incoming request's `Content-Type` header conflicts\n * with the function's declared content type. JSON and text functions are\n * enforced strictly (exact match wins), while form functions accept both\n * form encodings because the nojs fallback submits urlencoded forms to\n * multipart-declared endpoints. Requests without a `Content-Type` header\n * (curl, GET, legacy clients) are exempt from enforcement.\n * @param declared - The declared `contentType` from the server function options\n * @param rawHeader - The raw `Content-Type` request header, if present\n */\nexport const hasContentTypeMismatch = (\n declared: ContentType,\n rawHeader: string | undefined,\n): boolean => {\n // No Content-Type header → exempt (url bar, GET, curl compatibility)\n if (!rawHeader) return false;\n // Strip parameters (charset, boundary) before comparison\n const incomingType = rawHeader.trim().toLowerCase().split(\";\")[0].trim();\n if (isFormContentType(declared)) {\n // Forms: reject only non-form encodings (lenient between the two)\n return !isFormContentType(incomingType);\n }\n return incomingType !== declared;\n};\n\n/**\n * Escapes special regex metacharacters in a string.\n * Used to safely embed user-configurable values (like rpcPrefix) into regular expressions,\n * preventing ReDoS and regex injection attacks.\n * @param s - The raw string to escape\n * @returns The escaped string safe for use in new RegExp()\n */\nexport function escapeRegExp(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\nconst SAFE_URL_BASE = \"http://localhost\";\n\n/**\n * Parses a raw request URL against a fixed base without ever throwing.\n * Malformed request-targets (e.g. `/\\`, `//`, `/\\/`) make the WHATWG URL\n * parser throw `TypeError: Invalid URL`; the adapters call this while\n * building the per-request URL **before** their dispatch `try` block, so an\n * unhandled rejection there crashes raw `node:http` hosts (and Express 4).\n * On failure we fall back to the base root: the resulting pathname never\n * matches the RPC prefix, so the request is treated as non-RPC and falls\n * through to `next()` / 404 instead of crashing the process.\n * @param rawUrl - Raw request URL (path + optional query string)\n * @param base - Optional base URL, defaults to a fixed localhost origin\n * @returns A URL object; never throws\n */\nexport const safeURL = (rawUrl: string, base = SAFE_URL_BASE): URL => {\n try {\n return new URL(rawUrl, base);\n } catch {\n return new URL(\"/\", base);\n }\n};\n\nconst globalPrefixSymbol = Symbol.for(\"thednp.rpc.globalPrefix\");\n\n/** Global rpcPrefix from the last loaded config / middleware — fallback for functions without explicit prefix. */\nexport const getGlobalPrefix = (): string | undefined =>\n (globalThis as unknown as Record<symbol, string | undefined>)[\n globalPrefixSymbol\n ];\n\nexport const setGlobalPrefix = (prefix: string | undefined): void => {\n if (prefix) {\n (globalThis as unknown as Record<symbol, string | undefined>)[\n globalPrefixSymbol\n ] = prefix;\n } else {\n delete (globalThis as unknown as Record<symbol, string | undefined>)[\n globalPrefixSymbol\n ];\n }\n};\n","import type { ViteDevServer } from \"vite\";\nimport type { ClientFunctionWithOptions, ScanConfig } from \"./types.d.ts\";\nimport { readdir } from \"node:fs/promises\";\nimport { join, resolve } from \"node:path\";\nimport process from \"node:process\";\n\nimport { getFunctionsForPrefix } from \"./functionsMap.ts\";\nimport { defaultPrefix } from \"./options.ts\";\nimport { walkGlobFiles } from \"./server-helpers.ts\";\nimport {\n DUPLICATE_FUNCTION_NAME,\n ERROR_LOADING_FILE,\n NO_SERVER_FUNCTION_FOUND,\n} from \"./constants.ts\";\n\nlet isScanned = false;\n\n/** Absolute ids (normalized) of the scanned server function files. */\nexport const scannedServerFiles: Set<string> = new Set<string>();\n\nconst EXACT_NAMES = [\"server.ts\", \"server.js\", \"server.mjs\", \"server.mts\"];\n\n/**\n * Scans `src/api/` (or an explicit `scanRoot`) for server function files\n * and populates the server functions map (scoped by rpcPrefix) with their exported functions.\n * Uses Vite's SSR module loading to resolve and execute each file.\n *\n * Supports two matching modes via `config.serverFiles`:\n * `\"exact\"` — classic `server.ts|js|mjs|mts` names in the api directory\n * `\"glob\"` — recursively walking `scanRoot` to match `*.server.{ts,js,mjs,mts}`\n * @param initialCfg - Optional Vite config overrides (root, base, server, serverFiles, scanRoot)\n * @param devServer - Optional running Vite dev server instance; when provided, skips creating a new one\n */\nexport const scanForServerFiles = async (\n initialCfg?: ScanConfig,\n devServer?: ViteDevServer,\n): Promise<void> => {\n if (isScanned && !devServer) {\n return;\n }\n // Vite is only needed to spin up the internal dev server that loads the\n // server function files, so it is imported lazily rather than at the top\n // of the module. This keeps consumers of the standalone server entry that\n // register their functions directly (e.g. serverless functions bundling\n // the API module) free of a static Vite dependency — when Vite is\n // externalized by the function bundler, this lazy import is left as a\n // runtime require that is never executed.\n let createServer: typeof import(\"vite\").createServer;\n let normalizePath: typeof import(\"vite\").normalizePath;\n try {\n ({ createServer, normalizePath } = await import(\"vite\"));\n } catch {\n // Vite is not installed in this environment (e.g. a serverless bundle\n // where Vite is externalized or absent). Server functions must have been\n // imported directly into the prefix-scoped map; nothing to scan — exit\n // gracefully instead of crashing the host's cold start with\n // `Cannot find module 'vite'`.\n return;\n }\n const config = (!initialCfg && !devServer) || !initialCfg\n ? {\n root: process.cwd(),\n base: process.env.BASE || \"/\",\n server: { middlewareMode: true },\n }\n : {\n ...initialCfg,\n };\n\n let server = devServer;\n if (!server) {\n server = await createServer({\n server: { ...config.server, ws: false },\n appType: \"custom\",\n base: config.base || \"/\",\n root: config.root || process.cwd(),\n // The internal server is only used to load the server function files:\n // skip the project config so its plugins (including this one) do not\n // re-trigger a nested scan via `configureServer`.\n configFile: false,\n // The internal server never serves a page or HMR, so no dependency\n // optimization or WebSocket server is needed. Without `ws: false`, the\n // middleware-mode server creates a standalone HMR WebSocket on port\n // 24678, and concurrent scans (e.g. the Express middleware's lazy scan\n // racing the plugin scan) fail with EADDRINUSE. Without `noDiscovery`,\n // the default optimizers scan the project entry and pre-bundle the whole\n // `vite` package (imported by the linked @thednp/rpc dist files),\n // hanging startup at 2+ GB RSS.\n optimizeDeps: { noDiscovery: true },\n ssr: { optimizeDeps: { noDiscovery: true } },\n });\n }\n\n const root = config.root || process.cwd();\n const resolvedScanRoot = resolve(\n root,\n config.scanRoot ?? join(root, \"src\", \"api\"),\n );\n const serverFiles: \"exact\" | \"glob\" = config.serverFiles ??\n \"exact\";\n\n // Names registered during this scan run, used for duplicate detection.\n // Keyed by `${prefix}:${registeredName}` so the same name can coexist\n // under different rpcPrefixes (see the registration loop below).\n const seenNames = new Set<string>();\n\n let files: string[];\n try {\n if (serverFiles === \"glob\") {\n files = await walkGlobFiles(resolvedScanRoot);\n } else {\n files = (await readdir(resolvedScanRoot, { withFileTypes: true }))\n .filter((f) => EXACT_NAMES.includes(f.name))\n .map((f) => join(resolvedScanRoot, f.name));\n }\n } catch (_e) {\n files = [];\n }\n\n try {\n for (const file of files) {\n scannedServerFiles.add(normalizePath(file));\n let moduleExports: Record<string, ClientFunctionWithOptions>;\n try {\n moduleExports = (await server.ssrLoadModule(file)) as Record<\n string,\n ClientFunctionWithOptions\n >;\n } catch (error) {\n console.error(ERROR_LOADING_FILE, file, error);\n continue;\n }\n const moduleEntries = Object.entries(moduleExports);\n if (!moduleEntries.length) {\n console.warn(NO_SERVER_FUNCTION_FOUND);\n return;\n }\n\n // Register each export into its prefix-scoped map, recording the\n // original export name so `getClientModules` can emit the matching\n // client stub. `createServerFunction` already auto-registers its name\n // into the appropriate prefix-scoped map at module load, so a function\n // may already exist here — in that case only the export name is added.\n // Track names seen in THIS scan run only, keyed by prefix: a name\n // repeated within one scan under the same prefix (e.g. two files\n // exporting the same function name) is a genuine conflict.\n for (const [exportName, exportValue] of moduleEntries) {\n const registeredName = exportValue.name;\n const prefix = exportValue.options?.rpcPrefix ||\n config.rpcPrefix ||\n defaultPrefix;\n const seenKey = `${prefix}:${registeredName}`;\n if (seenNames.has(seenKey)) {\n if (process.env.NODE_ENV !== \"production\") {\n throw new Error(DUPLICATE_FUNCTION_NAME(registeredName));\n }\n console.warn(DUPLICATE_FUNCTION_NAME(registeredName));\n continue;\n }\n seenNames.add(seenKey);\n const prefixMap = getFunctionsForPrefix(prefix);\n const existing = prefixMap.get(registeredName);\n if (existing) {\n existing.exportName = exportName;\n } else {\n prefixMap.set(registeredName, {\n name: registeredName,\n handler: exportValue,\n options: exportValue.options,\n exportName,\n });\n }\n }\n }\n } finally {\n if (!devServer && server) {\n await server.close();\n }\n isScanned = true;\n }\n};\n","/** @module Server function creation and registration. */\nimport type {\n ClientFunction,\n JsonArray,\n JsonValue,\n ServerFunctionInit,\n ServerFunctionOptions,\n} from \"./types.d.ts\";\nimport { getFunctionsForPrefix } from \"./functionsMap.ts\";\nimport { defaultPrefix, defaultServerFnOptions } from \"./options.ts\";\nimport { getGlobalPrefix } from \"./server-helpers.ts\";\nimport { OPERATION_ABORTED } from \"./constants.ts\";\n\n/**\n * Extended options for createServerFunction, including rpcPrefix for multi-instance support.\n */\nexport interface CreateServerFunctionOptions\n extends Partial<ServerFunctionOptions> {\n /**\n * RPC prefix for this function. Enables multiple RPC instances with different prefixes.\n * When using multi-prefix setup, functions with the same name but different prefixes\n * can coexist without collision.\n * @default \"__rpc\"\n * @example\n * // v1 API\n * export const login = createServerFunction(\n * \"login\",\n * async (signal, email, password) => ({...}),\n * { rpcPrefix: \"v1:rpc\" },\n * );\n *\n * // v2 API - same function name, different prefix\n * export const login = createServerFunction(\n * \"login\",\n * async (signal, credentials) => ({...}),\n * { rpcPrefix: \"v2:rpc\" },\n * );\n */\n rpcPrefix?: string;\n}\n\n/**\n * Creates a server-side RPC function.\n * Registers the function in the server functions map (scoped by rpcPrefix) and returns\n * a client-compatible wrapper that exposes `data` (Promise) and `cancel` (function)\n * for request lifecycle control.\n * @param name - Unique identifier used by the RPC router to dispatch requests\n * @param handler - The actual implementation receiving an AbortSignal followed by JSON-serializable arguments\n * @param fnOptions - Optional contentType, credentials, and rpcPrefix settings\n * @returns A client stub with `data` promise and `cancel` method, auto-registered in the server map\n */\nexport function createServerFunction<\n TArgs extends JsonArray = JsonArray,\n TResult = JsonValue,\n>(\n name: string,\n handler: ServerFunctionInit<TArgs, TResult>,\n fnOptions: CreateServerFunctionOptions = {},\n): ClientFunction<TArgs, TResult> {\n const options = Object.assign({}, defaultServerFnOptions, fnOptions);\n const rpcPrefix = fnOptions.rpcPrefix || getGlobalPrefix() || defaultPrefix;\n\n const wrappedFunction: ClientFunction<TArgs, TResult> = (...args: TArgs) => {\n const controller = new AbortController();\n const cancel = (reason: string) => controller.abort(reason);\n\n const fetcher = async () => {\n if (controller.signal.aborted) {\n throw new Error(OPERATION_ABORTED);\n }\n\n return await handler(controller.signal, ...args);\n };\n\n return {\n data: fetcher(),\n cancel,\n };\n };\n\n Object.defineProperties(wrappedFunction, {\n name: { value: name, enumerable: true, configurable: false },\n options: { value: options, enumerable: true, configurable: false },\n });\n\n // Register to prefix-scoped map\n const prefixMap = getFunctionsForPrefix(rpcPrefix);\n prefixMap.set(name, {\n name,\n handler: wrappedFunction as never,\n options,\n });\n\n return wrappedFunction;\n}\n","import type { Credentials } from \"./types.d.ts\";\nimport { INVALID_IDENTIFIER, INVALID_PATH_SEGMENT } from \"./constants.ts\";\n\nconst SAFE_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\nconst SAFE_PATH_SEGMENT = /^[A-Za-z0-9_$@:][A-Za-z0-9_$@:/-]*$/;\nconst CREDENTIALS_VALUES: readonly Credentials[] = [\n \"same-origin\",\n \"include\",\n \"omit\",\n];\n\n/**\n * Validates that a string is a safe JavaScript identifier.\n * Used to prevent code injection when interpolating export names into generated client code.\n * @param name - The string to validate\n * @param label - Human-readable label for error messages (e.g. \"export name\")\n * @returns The validated name if it passes\n * @throws Error if the name contains characters outside /^[A-Za-z_$][A-Za-z0-9_$]*$/\n */\nexport function validateIdentifier(name: string, label: string): string {\n if (!SAFE_IDENTIFIER.test(name)) {\n throw new Error(INVALID_IDENTIFIER(label, name));\n }\n return name;\n}\n\n/**\n * Validates that a string is a safe path segment for RPC routing.\n * Allows alphanumeric characters, underscores, dollar signs, at signs,\n * colons, hyphens, and forward slashes.\n * @param segment - The string to validate\n * @param label - Human-readable label for error messages (e.g. \"rpcPrefix\")\n * @returns The validated segment if it passes\n * @throws Error if the segment contains disallowed characters\n */\nexport function validatePathSegment(segment: string, label: string): string {\n if (!SAFE_PATH_SEGMENT.test(segment)) {\n throw new Error(INVALID_PATH_SEGMENT(label, segment));\n }\n return segment;\n}\n\n/**\n * Validates and normalizes the credentials option.\n * Accepts \"same-origin\", \"include\", or \"omit\"; defaults to \"same-origin\" when undefined.\n * @param value - Credentials value to validate\n * @returns The validated credentials string\n * @throws Error if the value is not one of the accepted credentials\n */\nexport function validateCredentials(value?: string): Credentials {\n const creds = value || \"same-origin\";\n if (!CREDENTIALS_VALUES.includes(creds as Credentials)) {\n throw new Error(\n `Invalid credentials: \"${value}\" must be one of ${\n CREDENTIALS_VALUES.join(\", \")\n }`,\n );\n }\n return creds as Credentials;\n}\n\n/**\n * Validates and normalizes the HTTP method option for a server function.\n * Accepts \"GET\" or \"POST\" (case-insensitive); defaults to \"POST\" when undefined.\n * @param value - Method value to validate\n * @returns The validated uppercase method string\n * @throws Error if the value is not \"GET\" or \"POST\"\n */\nexport function validateMethod(value?: string): \"GET\" | \"POST\" {\n const method = (value || \"POST\").toUpperCase();\n if (method !== \"GET\" && method !== \"POST\") {\n throw new Error(`Invalid method: \"${value}\" must be one of GET, POST`);\n }\n return method;\n}\n","/**\n * @module Client module generation.\n */\nimport type {\n RpcPluginOptionsInternal,\n ServerFunctionOptions,\n} from \"./types.d.ts\";\nimport { getFunctionsForPrefix } from \"./functionsMap.ts\";\nimport {\n validateCredentials,\n validateIdentifier,\n validateMethod,\n validatePathSegment,\n} from \"./validate.ts\";\n\n/**\n * Generates a JavaScript client module string for a single server function.\n * All interpolated values are validated to prevent code injection.\n * @param fnName - Registered RPC function name (validated as path segment)\n * @param fnEntry - Export name used in the generated module (validated as identifier)\n * @param options - Content type, credentials, and RPC prefix settings\n * @returns A string of JavaScript code exporting the client stub\n */\nconst getModule = (\n fnName: string,\n fnEntry: string,\n options: Partial<ServerFunctionOptions> & {\n contentType: ServerFunctionOptions[\"contentType\"];\n rpcPrefix: string;\n },\n): string => {\n // Validate all interpolated strings to prevent code injection\n const safeFnName = validatePathSegment(fnName, \"function name\");\n const safeFnEntry = validateIdentifier(fnEntry, \"export name\");\n const safePrefix = validatePathSegment(options.rpcPrefix, \"rpcPrefix\");\n const credentials = validateCredentials(options.credentials);\n const method = validateMethod(options.method);\n const contentType =\n (options.contentType ?? \"application/json\") as ServerFunctionOptions[\n \"contentType\"\n ];\n\n const opts: string[] = [];\n if (method !== \"POST\") opts.push(`method: \"${method}\"`);\n if (credentials !== \"same-origin\") opts.push(`credentials: \"${credentials}\"`);\n if (contentType !== \"application/json\") {\n opts.push(`contentType: \"${contentType}\"`);\n }\n const optsStr = opts.length ? `, { ${opts.join(\", \")} }` : \"\";\n\n const output = `\n export const ${safeFnEntry} = getClientStub(\"${safePrefix}\", \"${safeFnName}\"${optsStr});`;\n\n return output.trim();\n};\n\n/**\n * Generates the complete client-side module bundle by iterating all registered server functions\n * for a specific prefix and producing fetch-based stubs for each. The result is transformed by Vite\n * (or Oxc) during the dev server or production build.\n * @param initialOptions - Plugin options containing rpcPrefix and optional adapter\n * @returns A string of JavaScript code with all client RPC modules and their import dependencies\n */\nexport const getClientModules = (\n initialOptions: RpcPluginOptionsInternal,\n): string => {\n // Validate prefix once at the top level\n validatePathSegment(initialOptions.rpcPrefix, \"rpcPrefix\");\n\n // Get functions registered for this specific prefix\n const prefixMap = getFunctionsForPrefix(initialOptions.rpcPrefix);\n const entries = Array.from(prefixMap.entries())\n .filter(([, entry]) => entry.exportName)\n .map(([registeredName, entry]) =>\n getModule(registeredName, entry.exportName!, {\n ...initialOptions,\n ...((entry.options as ServerFunctionOptions) || {}),\n })\n )\n .join(\"\\n\");\n\n const output = `\n// Client-side RPC modules for prefix: ${initialOptions.rpcPrefix}\nimport { getClientStub } from \"@thednp/rpc/helpers\";\n${entries}`;\n\n return output.trim();\n};\n","/** @module Server-side request context. Exports the `RequestEvent` shape, `provideRequestContext` to establish it around a dispatch, `getRequestContext` to read it from anywhere inside the async tree, `redirect` and `sendResponse` for framework-level short-circuits, and `getRequestMeta` for normalized request access. Never import this module in client code — it is server-only. */\n\n// @thednp/rpc/src/context.ts\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport type { JsonValue } from \"./types.d.ts\";\nimport { safeURL } from \"./server-helpers.ts\";\n\n/**\n * Global symbol under which the shared `AsyncLocalStorage` instance is stored\n * on `globalThis`. Keeping it on a `Symbol.for` key makes it instance-stable\n * across module copies and dev-server hot reloads, mirroring\n * `solid-js/web`'s own request-context storage.\n */\nconst requestContextSymbol = Symbol.for(\"thednp.rpc.requestContext\");\n\n/**\n * A per-request context established by the framework adapters around\n * server-function dispatch, mirroring Solid Start's `FetchEvent`. Any code\n * running in the async tree of a dispatch can read the current context through\n * {@link getRequestContext} instead of threading `req`/`res` (or the framework\n * `Context` object) through every nested call. This module is server-only and\n * must never be imported by client code.\n *\n * Each adapter extends this with framework-specific request/response accessors:\n * - Express: `req`/`res` plus `nativeEvent = { req, res }`\n * - Fastify: `request`/`reply` plus `nativeEvent = request`\n * - Koa: `ctx` plus `nativeEvent = ctx`\n * - Hono: `c` (the Hono `Context`) plus `nativeEvent = c`\n * - h3: `event` (the h3 `H3Event`) plus `nativeEvent = event`\n */\nexport interface RequestEvent {\n /** Adapter-specific native event kept for deep framework access */\n nativeEvent?: unknown;\n /** Adapter request object */\n request: unknown;\n /** Adapter response object */\n response: unknown;\n /**\n * Bound adapter-native redirect. Performing a redirect sets `redirected`\n * so the middleware can skip the JSON `{ data }` send.\n * @param location - The URL to redirect to\n * @param status - HTTP status code, defaults to `303 See Other`\n */\n redirect: (location: string, status?: number) => void;\n /**\n * Set by `redirect` once a redirect has been issued. The middleware checks\n * this after `await`ing the server function to avoid double-responding.\n */\n redirected?: { location: string; status: number };\n /**\n * Bound adapter-native response short-circuit. Writes the given status and\n * JSON body (plus optional headers) directly, bypassing the standard\n * `{ data }` response. Setting `sent` makes the middleware skip the JSON\n * `{ data }` send, mirroring `redirect`/`redirected`.\n * @param status - HTTP status code (e.g. 401, 413, 429)\n * @param body - JSON-serializable response body\n * @param headers - Optional response headers (e.g. `{ \"Retry-After\": \"60\" }`)\n */\n send: (\n status: number,\n body: JsonValue,\n headers?: Record<string, string>,\n ) => void;\n /**\n * Set by `send` once a response has been issued. The middleware checks this\n * after `await`ing the server function to avoid double-responding.\n */\n sent?: { status: number; body: JsonValue; headers?: Record<string, string> };\n /**\n * The matched RPC function name for the current request, when available.\n * Useful for per-function rate limiting or auditing inside middleware.\n */\n functionName?: string;\n /** Per-request app data shared across the async tree of the dispatch */\n locals: Record<string, unknown>;\n [prop: string]: unknown;\n}\n\n// Instance-stable across module copies and dev-server hot reloads, exactly like\n// `solid-js/web`'s `provideRequestEvent` (which stores on a globalThis symbol).\nconst requestContextStorage: AsyncLocalStorage<RequestEvent> =\n ((globalThis as Record<symbol, AsyncLocalStorage<RequestEvent>>)[\n requestContextSymbol\n ] ??= new AsyncLocalStorage<RequestEvent>());\n\n/**\n * Runs `cb` with `init` as the current request context. Use inside the\n * adapters around server-function dispatch (the async tree under `cb` can then\n * read the context via {@link getRequestContext}).\n * @param init - The request context for the duration of `cb`\n * @param cb - The work that needs access to the request context\n */\nexport const provideRequestContext = <T>(\n init: RequestEvent,\n cb: () => T,\n): T => requestContextStorage.run(init, cb);\n\n/**\n * Returns the current request context, or throws when called outside of a\n * request (e.g. module scope or a background task).\n * @throws When no request context is established\n */\nexport const getRequestContext = (): RequestEvent => {\n const ctx = requestContextStorage.getStore();\n if (!ctx) {\n throw new Error(\"RequestEvent is not available outside of a request\");\n }\n return ctx;\n};\n\n/**\n * Redirects the current request to `location`. Reads the adapter-bound\n * `redirect` from the current request context — callable from anywhere inside\n * a server-function tree (no `res` threading needed).\n * @param location - The URL to redirect to\n * @param status - HTTP status code, defaults to `303 See Other`\n * @throws When called outside of a request\n */\nexport const redirect = (location: string, status = 303): void => {\n getRequestContext().redirect(location, status);\n};\n\n/**\n * Sends a raw JSON response for the current request, bypassing the standard\n * `{ data }` shape. Reads the adapter-bound `send` from the current request\n * context — callable from anywhere inside a server-function tree. Any code in\n * the async tree of a dispatch can call this (e.g. custom middleware) to\n * short-circuit with a specific status code (401, 413, 429, ...).\n * @param status - HTTP status code\n * @param body - JSON-serializable response body\n * @param headers - Optional response headers\n * @throws When called outside of a request\n */\nexport const sendResponse = (\n status: number,\n body: JsonValue,\n headers?: Record<string, string>,\n): void => {\n getRequestContext().send(status, body, headers);\n};\n\n/**\n * Normalized, adapter-agnostic view of the current request. Reads the request\n * object off the current request context and normalizes it across the five\n * adapter request shapes (Express `req`, Fastify `req`, Koa `ctx.req`,\n * Hono `c.req`, h3 `event.req`) so middleware can be written once.\n */\nexport interface RequestMeta {\n /** HTTP method, upper-cased (e.g. \"GET\", \"POST\") */\n method: string;\n /** URL pathname (e.g. \"/__rpc/greet\") */\n pathname: string;\n /** Raw search string including the leading \"?\", or \"\" when absent */\n search: string;\n /** Parsed search params */\n searchParams: URLSearchParams;\n /** Request headers, lower-cased */\n headers: Record<string, string | string[] | undefined>;\n /** Host header value (e.g. \"localhost:5173\"), when present */\n host?: string;\n /** Client IP when the framework exposes it (e.g. Fastify `req.ip`) */\n ip?: string;\n /** Request protocol (\"http\" or \"https\"), when determinable */\n protocol?: string;\n}\n\nconst pickHeader = (\n headers: Record<string, string | string[] | undefined>,\n name: string,\n): string | undefined => {\n const value = headers[name];\n if (typeof value === \"string\") return value;\n if (Array.isArray(value)) return value[0];\n return undefined;\n};\n\n/** Normalizes any headers shape into a plain lower-cased record. */\nconst toHeaderRecord = (\n headers: unknown,\n): Record<string, string | string[] | undefined> => {\n if (!headers) return {};\n // Headers-like object (h3 Request, Hono c.req.raw.headers, fetch Headers)\n if (typeof (headers as Headers).forEach === \"function\") {\n const record: Record<string, string> = {};\n (headers as Headers).forEach((value, key) => {\n record[key] = value;\n });\n return record;\n }\n // Plain map (Express req.headers, Fastify req.headers, Koa ctx.req.headers)\n return headers as Record<string, string | string[] | undefined>;\n};\n\n/**\n * Reads normalized, adapter-agnostic request metadata from the current request\n * context. Works with Express `req`, Fastify `req`, Koa `ctx.req`,\n * Hono `c.req` and h3 `event.req` by feature-detecting the request shape\n * (`originalUrl`/`url`/`path`, raw `headers` map vs `Headers`-like API).\n * @param event - The request context to read, typically the result of\n * {@link getRequestContext}\n */\nexport const getRequestMeta = (event: RequestEvent): RequestMeta => {\n const req = event.request as {\n method?: string;\n originalUrl?: string;\n url?: string;\n path?: string;\n headers?: unknown;\n header?: (name: string) => string | string[] | undefined;\n ip?: string;\n protocol?: string;\n socket?: { remoteAddress?: string };\n raw?: { headers?: unknown };\n } | undefined;\n\n const method = (req?.method ?? \"GET\").toUpperCase();\n const rawUrl = req?.originalUrl ?? req?.url ?? req?.path ?? \"\";\n const url = safeURL(rawUrl);\n const headers = toHeaderRecord(req?.headers ?? req?.raw?.headers);\n const hostHeader = pickHeader(headers, \"host\");\n\n return {\n method,\n pathname: url.pathname,\n search: url.search,\n searchParams: url.searchParams,\n headers,\n host: hostHeader,\n ip: req?.ip ?? req?.socket?.remoteAddress,\n protocol: req?.protocol ?? url.protocol.replace(\":\", \"\"),\n };\n};\n"],"mappings":";;;;;AAMA,MAAa,yBAAgD;CAC3D,aAAa;CACb,aAAa;CACb,QAAQ;AACV;AAEA,MAAa,gBAAgB;AAE7B,MAAa,oBAAsC;CACjD,WAAW;CACX,SAAS;CACT,aAAa;CACb,UAAU,KAAA;AACZ;AAEA,MAAa,2BAA8C;CACzD,WAAW,KAAA;CACX,MAAM,KAAA;CACN,QAAQ,KAAA;AACV;;;;;;;;;;;ACdA,MAAM,qBAAqB,OAAO,IAAI,yBAAyB;;;;;;AAO/D,MAAa,0BAGR,WACH,wCACI,IAAI,IAAI;;;;;;AAOd,MAAa,yBACX,WAC+B;CAC/B,IAAI,CAAC,wBAAwB,IAAI,MAAM,GACrC,wBAAwB,IAAI,wBAAQ,IAAI,IAAI,CAAC;CAE/C,OAAO,wBAAwB,IAAI,MAAM;AAC3C;;;;;A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../src/server-helpers.ts","../../src/scanForServerFiles.ts","../../src/createFunction.ts","../../src/validate.ts","../../src/getClientModules.ts","../../src/context.ts"],"sourcesContent":["import type {\n MiddlewareOptions,\n RpcPluginOptions,\n ServerFunctionOptions,\n} from \"./types.d.ts\";\n\nexport const defaultServerFnOptions: ServerFunctionOptions = {\n contentType: \"application/json\",\n credentials: \"same-origin\",\n method: \"POST\",\n};\n\nexport const defaultPrefix = \"__rpc\";\n\nexport const defaultRPCOptions: RpcPluginOptions = {\n rpcPrefix: defaultPrefix,\n adapter: \"express\",\n serverFiles: \"exact\",\n scanRoot: undefined,\n};\n\nexport const defaultMiddlewareOptions: MiddlewareOptions = {\n rpcPrefix: undefined,\n path: undefined,\n origin: undefined,\n};\n","import type { ServerFnEntry } from \"./types.d.ts\";\nimport { defaultPrefix } from \"./options.ts\";\n\n/**\n * Global symbol under which the shared `serverFunctionsByPrefix` map is stored\n * on `globalThis`. Keeping it on a `Symbol.for` key makes it instance-stable\n * across the bundled entry copies (`index.mjs`, `server.mjs`, `express.mjs`,\n * ...) and dev-server hot reloads, exactly like the request-context storage in\n * `context.ts`. Without this, `scanForServerFiles` (bundled into the plugin)\n * would populate a map copy the adapter middleware could not read.\n */\nconst functionsMapSymbol = Symbol.for(\"thednp.rpc.functionsMap\");\n\n/**\n * Map of rpcPrefix -> Map of function names -> ServerFnEntry\n * Enables multiple RPC instances with different prefixes to coexist\n * without name collisions.\n */\nexport const serverFunctionsByPrefix: Map<\n string,\n Map<string, ServerFnEntry>\n> = ((globalThis as Record<symbol, Map<string, Map<string, ServerFnEntry>>>)[\n functionsMapSymbol\n] ??= new Map());\n\n/**\n * Gets or creates the function map for a specific prefix.\n * @param prefix - The RPC prefix (e.g., \"__rpc\", \"v1:rpc\", \"admin:rpc\")\n * @returns Map of function names to ServerFnEntry for that prefix\n */\nexport const getFunctionsForPrefix = (\n prefix: string,\n): Map<string, ServerFnEntry> => {\n if (!serverFunctionsByPrefix.has(prefix)) {\n serverFunctionsByPrefix.set(prefix, new Map());\n }\n return serverFunctionsByPrefix.get(prefix)!;\n};\n\n/**\n * Backward compatibility: default map for the default prefix.\n * Legacy code can still use serverFunctionsMap.set(name, entry).\n */\nexport const serverFunctionsMap: Map<string, ServerFnEntry> = {\n get: (key: string) => getFunctionsForPrefix(defaultPrefix).get(key),\n set: (key: string, value: ServerFnEntry) =>\n getFunctionsForPrefix(defaultPrefix).set(key, value),\n has: (key: string) => getFunctionsForPrefix(defaultPrefix).has(key),\n delete: (key: string) => getFunctionsForPrefix(defaultPrefix).delete(key),\n clear: () => getFunctionsForPrefix(defaultPrefix).clear(),\n get size() {\n return getFunctionsForPrefix(defaultPrefix).size;\n },\n entries: () => getFunctionsForPrefix(defaultPrefix).entries(),\n keys: () => getFunctionsForPrefix(defaultPrefix).keys(),\n values: () => getFunctionsForPrefix(defaultPrefix).values(),\n forEach: (\n callback: (\n value: ServerFnEntry,\n key: string,\n map: Map<string, ServerFnEntry>,\n ) => void,\n ) => getFunctionsForPrefix(defaultPrefix).forEach(callback),\n [Symbol.iterator]: () =>\n getFunctionsForPrefix(defaultPrefix)[Symbol.iterator](),\n} as unknown as Map<string, ServerFnEntry>;\n","export const OPERATION_ABORTED = \"Operation aborted\";\n\nexport const REQUEST_CANCELLED = \"Request was cancelled\";\n\nexport const FETCH_ERROR_PREFIX = \"Fetch error: \";\n\nexport const NO_SERVER_FUNCTION_FOUND = \"No server function found.\";\n\nexport const ERROR_LOADING_FILE = \"Error loading file:\";\n\nexport const FUNCTION_NOT_FOUND = \"Function not found\";\n\nexport const METHOD_NOT_ALLOWED = \"Method Not Allowed\";\n\nexport const REQUEST_FORBIDDEN = \"Forbidden\";\n\nexport const UNSUPPORTED_MEDIA_TYPE = \"Unsupported Media Type\";\n\nexport const BAD_REQUEST = \"Bad Request\";\n\nexport const INTERNAL_SERVER_ERROR = \"Internal Server Error\";\n\nexport const CLIENT_DISCONNECTED = \"client disconnected\";\n\n/** Returns a warning when a middleware name is reused, preventing registration conflicts. @param name - The duplicate middleware name */\nexport const MIDDLEWARE_NAME_USED = (name: string) =>\n `The middleware name \"${name}\" is already used.`;\n\n/** Error message when a value fails the safe-identifier validation. @param label - What kind of value was being validated. @param name - The rejected value */\nexport const INVALID_IDENTIFIER = (label: string, name: string) =>\n `Invalid ${label}: \"${name}\" must match /^[A-Za-z_$][A-Za-z0-9_$]*$/`;\n\n/** Error message when a value fails the safe-path-segment validation. @param label - What kind of value was being validated. @param segment - The rejected value */\nexport const INVALID_PATH_SEGMENT = (label: string, segment: string) =>\n `Invalid ${label}: \"${segment}\" must match /^[A-Za-z0-9_$@:][A-Za-z0-9_$@:/-]*$/`;\n\n/** Warning message when a specified RPC config file cannot be resolved on disk. @param configFile - The requested config filename. @param configFilePath - The resolved absolute path */\nexport const CONFIG_FILE_NOT_FOUND = (\n configFile: string,\n configFilePath: string,\n) =>\n ` ⚠︎ The specified RPC config file ${configFile} cannot be found at ${configFilePath}, loading the defaults..`;\n\nexport const NO_CONFIG_FOUND =\n ` ⚡︎ No RPC config found, loading the defaults..`;\n\nexport const FAILED_LOAD_CONFIG = ` ⚠︎ Failed to load RPC config:`;\n\n/** Error template for duplicate server function names across files. @param name - The duplicate registered name */\nexport const DUPLICATE_FUNCTION_NAME = (name: string) =>\n `Duplicate server function \"${name}\" detected. Each server function must have a unique name. Remove or rename the duplicate.`;\n","/** @module Server-side utilities. Exports the `RPCError` class for typed server-side errors, `formatError` for middleware error responses, `isFormContentType` and `hasContentTypeMismatch` for content-type validation, and `walkGlobFiles` for recursively discovering `*.server.*` files. Never import this module in client code — it is server-only. */\nimport type { ContentType, JsonObject, JsonValue } from \"./types.d.ts\";\nimport { readdir } from \"node:fs/promises\";\nimport { join } from \"node:path\";\n\nimport { INTERNAL_SERVER_ERROR } from \"./constants.ts\";\n\nconst GLOB_REGEX = /^.+\\.server\\.(ts|js|mjs|mts)$/;\n\n/**\n * Recursively walks `dir` and collects absolute paths to files whose\n * basename matches the `*.server.{ts,js,mjs,mts}` glob pattern.\n */\nexport const walkGlobFiles = async (dir: string): Promise<string[]> => {\n const results: string[] = [];\n const stack = [dir];\n while (stack.length) {\n const current = stack.pop()!;\n let entries;\n try {\n entries = await readdir(current, { withFileTypes: true });\n } catch (_e) {\n continue;\n }\n for (const entry of entries) {\n const fullPath = join(current, entry.name);\n if (entry.isFile() && GLOB_REGEX.test(entry.name)) {\n results.push(fullPath);\n } else if (entry.isDirectory()) {\n stack.push(fullPath);\n }\n }\n }\n return results;\n};\n\n/**\n * A typed error thrown from server functions.\n * The middleware serializes the `message` and `code` in the response,\n * allowing clients to recognise and handle specific error conditions.\n */\nexport class RPCError extends Error {\n /** Machine-readable error code (e.g. \"VALIDATION_FAILED\", \"UNAUTHORIZED\") */\n code: string;\n /** Optional diagnostic payload */\n data?: JsonValue;\n constructor(message: string, code = \"INTERNAL\", data?: JsonValue) {\n super(message);\n this.name = \"RPCError\";\n this.code = code;\n this.data = data;\n }\n}\n\n/**\n * Formats an error for the RPC middleware response.\n * In development the full `RPCError` payload is included so developers\n * can quickly identify issues. Unexpected exceptions never expose their\n * message — only the generic \"Internal Server Error\" is sent, preventing\n * information disclosure; server-side diagnostics are preserved via the\n * middleware's `console.error` logging.\n */\nexport const formatError = (\n err: unknown,\n isProduction: boolean,\n): JsonObject => {\n if (isProduction) {\n return { error: INTERNAL_SERVER_ERROR };\n }\n if (err instanceof RPCError) {\n const payload: JsonObject = {\n error: err.message || INTERNAL_SERVER_ERROR,\n code: err.code,\n };\n if (err.data !== undefined) payload.data = err.data;\n return payload;\n }\n return { error: INTERNAL_SERVER_ERROR };\n};\n\n/**\n * Checks whether a content type maps to a form encoding\n * (`multipart/form-data` or `application/x-www-form-urlencoded`).\n * Form-declared functions accept either encoding so native browser\n * submissions (urlencoded) keep working without JavaScript.\n */\nexport const isFormContentType = (contentType: string): boolean =>\n contentType === \"multipart/form-data\" ||\n contentType === \"application/x-www-form-urlencoded\";\n\n/**\n * Detects whether an incoming request's `Content-Type` header conflicts\n * with the function's declared content type. JSON and text functions are\n * enforced strictly (exact match wins), while form functions accept both\n * form encodings because the nojs fallback submits urlencoded forms to\n * multipart-declared endpoints. Requests without a `Content-Type` header\n * (curl, GET, legacy clients) are exempt from enforcement.\n * @param declared - The declared `contentType` from the server function options\n * @param rawHeader - The raw `Content-Type` request header, if present\n */\nexport const hasContentTypeMismatch = (\n declared: ContentType,\n rawHeader: string | undefined,\n): boolean => {\n // No Content-Type header → exempt (url bar, GET, curl compatibility)\n if (!rawHeader) return false;\n // Strip parameters (charset, boundary) before comparison\n const incomingType = rawHeader.trim().toLowerCase().split(\";\")[0].trim();\n if (isFormContentType(declared)) {\n // Forms: reject only non-form encodings (lenient between the two)\n return !isFormContentType(incomingType);\n }\n return incomingType !== declared;\n};\n\n/**\n * Escapes special regex metacharacters in a string.\n * Used to safely embed user-configurable values (like rpcPrefix) into regular expressions,\n * preventing ReDoS and regex injection attacks.\n * @param s - The raw string to escape\n * @returns The escaped string safe for use in new RegExp()\n */\nexport function escapeRegExp(s: string): string {\n return s.replace(/[.*+?^${}()|[\\]\\\\]/g, \"\\\\$&\");\n}\n\n/**\n * Decides whether a request's `Origin` header is allowed by the configured\n * allowlist. Shared by all five adapters so the rule lives in exactly one place.\n *\n * - No `allowed` value (option unset) → everything passes: no validation.\n * - No `requestOrigin` header → passes, preserving curl/native-client access.\n * - Otherwise the header must match one of the entries exactly.\n *\n * A single string and a one-element array behave identically, so widening\n * `origin` to `string | string[]` is backward compatible.\n *\n * `Origin: null` (sandboxed iframes, `file://`, extension pages) is rejected\n * whenever an allowlist is set, because it never equals a real origin.\n * @param allowed - The configured `origin` option, if any\n * @param requestOrigin - The raw `Origin` request header, if present\n * @returns `true` when the request may proceed\n */\nexport const isOriginAllowed = (\n allowed: string | string[] | undefined,\n requestOrigin: string | undefined,\n): boolean => {\n if (!allowed || !requestOrigin) return true;\n return Array.isArray(allowed)\n ? allowed.includes(requestOrigin)\n : requestOrigin === allowed;\n};\n\nconst SAFE_URL_BASE = \"http://localhost\";\n\n/**\n * Parses a raw request URL against a fixed base without ever throwing.\n * Malformed request-targets (e.g. `/\\`, `//`, `/\\/`) make the WHATWG URL\n * parser throw `TypeError: Invalid URL`; the adapters call this while\n * building the per-request URL **before** their dispatch `try` block, so an\n * unhandled rejection there crashes raw `node:http` hosts (and Express 4).\n * On failure we fall back to the base root: the resulting pathname never\n * matches the RPC prefix, so the request is treated as non-RPC and falls\n * through to `next()` / 404 instead of crashing the process.\n * @param rawUrl - Raw request URL (path + optional query string)\n * @param base - Optional base URL, defaults to a fixed localhost origin\n * @returns A URL object; never throws\n */\nexport const safeURL = (rawUrl: string, base = SAFE_URL_BASE): URL => {\n try {\n return new URL(rawUrl, base);\n } catch {\n return new URL(\"/\", base);\n }\n};\n\nconst globalPrefixSymbol = Symbol.for(\"thednp.rpc.globalPrefix\");\n\n/** Global rpcPrefix from the last loaded config / middleware — fallback for functions without explicit prefix. */\nexport const getGlobalPrefix = (): string | undefined =>\n (globalThis as unknown as Record<symbol, string | undefined>)[\n globalPrefixSymbol\n ];\n\nexport const setGlobalPrefix = (prefix: string | undefined): void => {\n if (prefix) {\n (globalThis as unknown as Record<symbol, string | undefined>)[\n globalPrefixSymbol\n ] = prefix;\n } else {\n delete (globalThis as unknown as Record<symbol, string | undefined>)[\n globalPrefixSymbol\n ];\n }\n};\n","import type { ViteDevServer } from \"vite\";\nimport type { ClientFunctionWithOptions, ScanConfig } from \"./types.d.ts\";\nimport { readdir } from \"node:fs/promises\";\nimport { join, resolve } from \"node:path\";\nimport process from \"node:process\";\n\nimport { getFunctionsForPrefix } from \"./functionsMap.ts\";\nimport { defaultPrefix } from \"./options.ts\";\nimport { walkGlobFiles } from \"./server-helpers.ts\";\nimport {\n DUPLICATE_FUNCTION_NAME,\n ERROR_LOADING_FILE,\n NO_SERVER_FUNCTION_FOUND,\n} from \"./constants.ts\";\n\nlet isScanned = false;\n\n/** Absolute ids (normalized) of the scanned server function files. */\nexport const scannedServerFiles: Set<string> = new Set<string>();\n\nconst EXACT_NAMES = [\"server.ts\", \"server.js\", \"server.mjs\", \"server.mts\"];\n\n/**\n * Scans `src/api/` (or an explicit `scanRoot`) for server function files\n * and populates the server functions map (scoped by rpcPrefix) with their exported functions.\n * Uses Vite's SSR module loading to resolve and execute each file.\n *\n * Supports two matching modes via `config.serverFiles`:\n * `\"exact\"` — classic `server.ts|js|mjs|mts` names in the api directory\n * `\"glob\"` — recursively walking `scanRoot` to match `*.server.{ts,js,mjs,mts}`\n * @param initialCfg - Optional Vite config overrides (root, base, server, serverFiles, scanRoot)\n * @param devServer - Optional running Vite dev server instance; when provided, skips creating a new one\n */\nexport const scanForServerFiles = async (\n initialCfg?: ScanConfig,\n devServer?: ViteDevServer,\n): Promise<void> => {\n if (isScanned && !devServer) {\n return;\n }\n // Vite is only needed to spin up the internal dev server that loads the\n // server function files, so it is imported lazily rather than at the top\n // of the module. This keeps consumers of the standalone server entry that\n // register their functions directly (e.g. serverless functions bundling\n // the API module) free of a static Vite dependency — when Vite is\n // externalized by the function bundler, this lazy import is left as a\n // runtime require that is never executed.\n let createServer: typeof import(\"vite\").createServer;\n let normalizePath: typeof import(\"vite\").normalizePath;\n try {\n ({ createServer, normalizePath } = await import(\"vite\"));\n } catch {\n // Vite is not installed in this environment (e.g. a serverless bundle\n // where Vite is externalized or absent). Server functions must have been\n // imported directly into the prefix-scoped map; nothing to scan — exit\n // gracefully instead of crashing the host's cold start with\n // `Cannot find module 'vite'`.\n return;\n }\n const config = (!initialCfg && !devServer) || !initialCfg\n ? {\n root: process.cwd(),\n base: process.env.BASE || \"/\",\n server: { middlewareMode: true },\n }\n : {\n ...initialCfg,\n };\n\n let server = devServer;\n if (!server) {\n server = await createServer({\n server: { ...config.server, ws: false },\n appType: \"custom\",\n base: config.base || \"/\",\n root: config.root || process.cwd(),\n // The internal server is only used to load the server function files:\n // skip the project config so its plugins (including this one) do not\n // re-trigger a nested scan via `configureServer`.\n configFile: false,\n // The internal server never serves a page or HMR, so no dependency\n // optimization or WebSocket server is needed. Without `ws: false`, the\n // middleware-mode server creates a standalone HMR WebSocket on port\n // 24678, and concurrent scans (e.g. the Express middleware's lazy scan\n // racing the plugin scan) fail with EADDRINUSE. Without `noDiscovery`,\n // the default optimizers scan the project entry and pre-bundle the whole\n // `vite` package (imported by the linked @thednp/rpc dist files),\n // hanging startup at 2+ GB RSS.\n optimizeDeps: { noDiscovery: true },\n ssr: { optimizeDeps: { noDiscovery: true } },\n });\n }\n\n const root = config.root || process.cwd();\n const resolvedScanRoot = resolve(\n root,\n config.scanRoot ?? join(root, \"src\", \"api\"),\n );\n const serverFiles: \"exact\" | \"glob\" = config.serverFiles ??\n \"exact\";\n\n // Names registered during this scan run, used for duplicate detection.\n // Keyed by `${prefix}:${registeredName}` so the same name can coexist\n // under different rpcPrefixes (see the registration loop below).\n const seenNames = new Set<string>();\n\n let files: string[];\n try {\n if (serverFiles === \"glob\") {\n files = await walkGlobFiles(resolvedScanRoot);\n } else {\n files = (await readdir(resolvedScanRoot, { withFileTypes: true }))\n .filter((f) => EXACT_NAMES.includes(f.name))\n .map((f) => join(resolvedScanRoot, f.name));\n }\n } catch (_e) {\n files = [];\n }\n\n try {\n for (const file of files) {\n scannedServerFiles.add(normalizePath(file));\n let moduleExports: Record<string, ClientFunctionWithOptions>;\n try {\n moduleExports = (await server.ssrLoadModule(file)) as Record<\n string,\n ClientFunctionWithOptions\n >;\n } catch (error) {\n console.error(ERROR_LOADING_FILE, file, error);\n continue;\n }\n const moduleEntries = Object.entries(moduleExports);\n if (!moduleEntries.length) {\n console.warn(NO_SERVER_FUNCTION_FOUND);\n return;\n }\n\n // Register each export into its prefix-scoped map, recording the\n // original export name so `getClientModules` can emit the matching\n // client stub. `createServerFunction` already auto-registers its name\n // into the appropriate prefix-scoped map at module load, so a function\n // may already exist here — in that case only the export name is added.\n // Track names seen in THIS scan run only, keyed by prefix: a name\n // repeated within one scan under the same prefix (e.g. two files\n // exporting the same function name) is a genuine conflict.\n for (const [exportName, exportValue] of moduleEntries) {\n const registeredName = exportValue.name;\n const prefix = exportValue.options?.rpcPrefix ||\n config.rpcPrefix ||\n defaultPrefix;\n const seenKey = `${prefix}:${registeredName}`;\n if (seenNames.has(seenKey)) {\n if (process.env.NODE_ENV !== \"production\") {\n throw new Error(DUPLICATE_FUNCTION_NAME(registeredName));\n }\n console.warn(DUPLICATE_FUNCTION_NAME(registeredName));\n continue;\n }\n seenNames.add(seenKey);\n const prefixMap = getFunctionsForPrefix(prefix);\n const existing = prefixMap.get(registeredName);\n if (existing) {\n existing.exportName = exportName;\n } else {\n prefixMap.set(registeredName, {\n name: registeredName,\n handler: exportValue,\n options: exportValue.options,\n exportName,\n });\n }\n }\n }\n } finally {\n if (!devServer && server) {\n await server.close();\n }\n isScanned = true;\n }\n};\n","/** @module Server function creation and registration. */\nimport type {\n ClientFunction,\n JsonArray,\n JsonValue,\n ServerFunctionInit,\n ServerFunctionOptions,\n} from \"./types.d.ts\";\nimport { getFunctionsForPrefix } from \"./functionsMap.ts\";\nimport { defaultPrefix, defaultServerFnOptions } from \"./options.ts\";\nimport { getGlobalPrefix } from \"./server-helpers.ts\";\nimport { OPERATION_ABORTED } from \"./constants.ts\";\n\n/**\n * Extended options for createServerFunction, including rpcPrefix for multi-instance support.\n */\nexport interface CreateServerFunctionOptions\n extends Partial<ServerFunctionOptions> {\n /**\n * RPC prefix for this function. Enables multiple RPC instances with different prefixes.\n * When using multi-prefix setup, functions with the same name but different prefixes\n * can coexist without collision.\n * @default \"__rpc\"\n * @example\n * // v1 API\n * export const login = createServerFunction(\n * \"login\",\n * async (signal, email, password) => ({...}),\n * { rpcPrefix: \"v1:rpc\" },\n * );\n *\n * // v2 API - same function name, different prefix\n * export const login = createServerFunction(\n * \"login\",\n * async (signal, credentials) => ({...}),\n * { rpcPrefix: \"v2:rpc\" },\n * );\n */\n rpcPrefix?: string;\n}\n\n/**\n * Creates a server-side RPC function.\n * Registers the function in the server functions map (scoped by rpcPrefix) and returns\n * a client-compatible wrapper that exposes `data` (Promise) and `cancel` (function)\n * for request lifecycle control.\n * @param name - Unique identifier used by the RPC router to dispatch requests\n * @param handler - The actual implementation receiving an AbortSignal followed by JSON-serializable arguments\n * @param fnOptions - Optional contentType, credentials, and rpcPrefix settings\n * @returns A client stub with `data` promise and `cancel` method, auto-registered in the server map\n */\nexport function createServerFunction<\n TArgs extends JsonArray = JsonArray,\n TResult = JsonValue,\n>(\n name: string,\n handler: ServerFunctionInit<TArgs, TResult>,\n fnOptions: CreateServerFunctionOptions = {},\n): ClientFunction<TArgs, TResult> {\n const options = Object.assign({}, defaultServerFnOptions, fnOptions);\n const rpcPrefix = fnOptions.rpcPrefix || getGlobalPrefix() || defaultPrefix;\n\n const wrappedFunction: ClientFunction<TArgs, TResult> = (...args: TArgs) => {\n const controller = new AbortController();\n const cancel = (reason: string) => controller.abort(reason);\n\n const fetcher = async () => {\n if (controller.signal.aborted) {\n throw new Error(OPERATION_ABORTED);\n }\n\n return await handler(controller.signal, ...args);\n };\n\n return {\n data: fetcher(),\n cancel,\n };\n };\n\n Object.defineProperties(wrappedFunction, {\n name: { value: name, enumerable: true, configurable: false },\n options: { value: options, enumerable: true, configurable: false },\n });\n\n // Register to prefix-scoped map\n const prefixMap = getFunctionsForPrefix(rpcPrefix);\n prefixMap.set(name, {\n name,\n handler: wrappedFunction as never,\n options,\n });\n\n return wrappedFunction;\n}\n","import type { Credentials } from \"./types.d.ts\";\nimport { INVALID_IDENTIFIER, INVALID_PATH_SEGMENT } from \"./constants.ts\";\n\nconst SAFE_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;\nconst SAFE_PATH_SEGMENT = /^[A-Za-z0-9_$@:][A-Za-z0-9_$@:/-]*$/;\nconst CREDENTIALS_VALUES: readonly Credentials[] = [\n \"same-origin\",\n \"include\",\n \"omit\",\n];\n\n/**\n * Validates that a string is a safe JavaScript identifier.\n * Used to prevent code injection when interpolating export names into generated client code.\n * @param name - The string to validate\n * @param label - Human-readable label for error messages (e.g. \"export name\")\n * @returns The validated name if it passes\n * @throws Error if the name contains characters outside /^[A-Za-z_$][A-Za-z0-9_$]*$/\n */\nexport function validateIdentifier(name: string, label: string): string {\n if (!SAFE_IDENTIFIER.test(name)) {\n throw new Error(INVALID_IDENTIFIER(label, name));\n }\n return name;\n}\n\n/**\n * Validates that a string is a safe path segment for RPC routing.\n * Allows alphanumeric characters, underscores, dollar signs, at signs,\n * colons, hyphens, and forward slashes.\n * @param segment - The string to validate\n * @param label - Human-readable label for error messages (e.g. \"rpcPrefix\")\n * @returns The validated segment if it passes\n * @throws Error if the segment contains disallowed characters\n */\nexport function validatePathSegment(segment: string, label: string): string {\n if (!SAFE_PATH_SEGMENT.test(segment)) {\n throw new Error(INVALID_PATH_SEGMENT(label, segment));\n }\n return segment;\n}\n\n/**\n * Validates and normalizes the credentials option.\n * Accepts \"same-origin\", \"include\", or \"omit\"; defaults to \"same-origin\" when undefined.\n * @param value - Credentials value to validate\n * @returns The validated credentials string\n * @throws Error if the value is not one of the accepted credentials\n */\nexport function validateCredentials(value?: string): Credentials {\n const creds = value || \"same-origin\";\n if (!CREDENTIALS_VALUES.includes(creds as Credentials)) {\n throw new Error(\n `Invalid credentials: \"${value}\" must be one of ${\n CREDENTIALS_VALUES.join(\", \")\n }`,\n );\n }\n return creds as Credentials;\n}\n\n/**\n * Validates and normalizes the HTTP method option for a server function.\n * Accepts \"GET\" or \"POST\" (case-insensitive); defaults to \"POST\" when undefined.\n * @param value - Method value to validate\n * @returns The validated uppercase method string\n * @throws Error if the value is not \"GET\" or \"POST\"\n */\nexport function validateMethod(value?: string): \"GET\" | \"POST\" {\n const method = (value || \"POST\").toUpperCase();\n if (method !== \"GET\" && method !== \"POST\") {\n throw new Error(`Invalid method: \"${value}\" must be one of GET, POST`);\n }\n return method;\n}\n","/**\n * @module Client module generation.\n */\nimport type {\n RpcPluginOptionsInternal,\n ServerFunctionOptions,\n} from \"./types.d.ts\";\nimport { getFunctionsForPrefix } from \"./functionsMap.ts\";\nimport {\n validateCredentials,\n validateIdentifier,\n validateMethod,\n validatePathSegment,\n} from \"./validate.ts\";\n\n/**\n * Generates a JavaScript client module string for a single server function.\n * All interpolated values are validated to prevent code injection.\n * @param fnName - Registered RPC function name (validated as path segment)\n * @param fnEntry - Export name used in the generated module (validated as identifier)\n * @param options - Content type, credentials, and RPC prefix settings\n * @returns A string of JavaScript code exporting the client stub\n */\nconst getModule = (\n fnName: string,\n fnEntry: string,\n options: Partial<ServerFunctionOptions> & {\n contentType: ServerFunctionOptions[\"contentType\"];\n rpcPrefix: string;\n },\n): string => {\n // Validate all interpolated strings to prevent code injection\n const safeFnName = validatePathSegment(fnName, \"function name\");\n const safeFnEntry = validateIdentifier(fnEntry, \"export name\");\n const safePrefix = validatePathSegment(options.rpcPrefix, \"rpcPrefix\");\n const credentials = validateCredentials(options.credentials);\n const method = validateMethod(options.method);\n const contentType =\n (options.contentType ?? \"application/json\") as ServerFunctionOptions[\n \"contentType\"\n ];\n\n const opts: string[] = [];\n if (method !== \"POST\") opts.push(`method: \"${method}\"`);\n if (credentials !== \"same-origin\") opts.push(`credentials: \"${credentials}\"`);\n if (contentType !== \"application/json\") {\n opts.push(`contentType: \"${contentType}\"`);\n }\n const optsStr = opts.length ? `, { ${opts.join(\", \")} }` : \"\";\n\n const output = `\n export const ${safeFnEntry} = getClientStub(\"${safePrefix}\", \"${safeFnName}\"${optsStr});`;\n\n return output.trim();\n};\n\n/**\n * Generates the complete client-side module bundle by iterating all registered server functions\n * for a specific prefix and producing fetch-based stubs for each. The result is transformed by Vite\n * (or Oxc) during the dev server or production build.\n * @param initialOptions - Plugin options containing rpcPrefix and optional adapter\n * @returns A string of JavaScript code with all client RPC modules and their import dependencies\n */\nexport const getClientModules = (\n initialOptions: RpcPluginOptionsInternal,\n): string => {\n // Validate prefix once at the top level\n validatePathSegment(initialOptions.rpcPrefix, \"rpcPrefix\");\n\n // Get functions registered for this specific prefix\n const prefixMap = getFunctionsForPrefix(initialOptions.rpcPrefix);\n const entries = Array.from(prefixMap.entries())\n .filter(([, entry]) => entry.exportName)\n .map(([registeredName, entry]) =>\n getModule(registeredName, entry.exportName!, {\n ...initialOptions,\n ...((entry.options as ServerFunctionOptions) || {}),\n })\n )\n .join(\"\\n\");\n\n const output = `\n// Client-side RPC modules for prefix: ${initialOptions.rpcPrefix}\nimport { getClientStub } from \"@thednp/rpc/helpers\";\n${entries}`;\n\n return output.trim();\n};\n","/** @module Server-side request context. Exports the `RequestEvent` shape, `provideRequestContext` to establish it around a dispatch, `getRequestContext` to read it from anywhere inside the async tree, `redirect` and `sendResponse` for framework-level short-circuits, and `getRequestMeta` for normalized request access. Never import this module in client code — it is server-only. */\n\n// @thednp/rpc/src/context.ts\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport type { JsonValue } from \"./types.d.ts\";\nimport { safeURL } from \"./server-helpers.ts\";\n\n/**\n * Global symbol under which the shared `AsyncLocalStorage` instance is stored\n * on `globalThis`. Keeping it on a `Symbol.for` key makes it instance-stable\n * across module copies and dev-server hot reloads, mirroring\n * `solid-js/web`'s own request-context storage.\n */\nconst requestContextSymbol = Symbol.for(\"thednp.rpc.requestContext\");\n\n/**\n * A per-request context established by the framework adapters around\n * server-function dispatch, mirroring Solid Start's `FetchEvent`. Any code\n * running in the async tree of a dispatch can read the current context through\n * {@link getRequestContext} instead of threading `req`/`res` (or the framework\n * `Context` object) through every nested call. This module is server-only and\n * must never be imported by client code.\n *\n * Each adapter extends this with framework-specific request/response accessors:\n * - Express: `req`/`res` plus `nativeEvent = { req, res }`\n * - Fastify: `request`/`reply` plus `nativeEvent = request`\n * - Koa: `ctx` plus `nativeEvent = ctx`\n * - Hono: `c` (the Hono `Context`) plus `nativeEvent = c`\n * - h3: `event` (the h3 `H3Event`) plus `nativeEvent = event`\n */\nexport interface RequestEvent {\n /** Adapter-specific native event kept for deep framework access */\n nativeEvent?: unknown;\n /** Adapter request object */\n request: unknown;\n /** Adapter response object */\n response: unknown;\n /**\n * Bound adapter-native redirect. Performing a redirect sets `redirected`\n * so the middleware can skip the JSON `{ data }` send.\n * @param location - The URL to redirect to\n * @param status - HTTP status code, defaults to `303 See Other`\n */\n redirect: (location: string, status?: number) => void;\n /**\n * Set by `redirect` once a redirect has been issued. The middleware checks\n * this after `await`ing the server function to avoid double-responding.\n */\n redirected?: { location: string; status: number };\n /**\n * Bound adapter-native response short-circuit. Writes the given status and\n * JSON body (plus optional headers) directly, bypassing the standard\n * `{ data }` response. Setting `sent` makes the middleware skip the JSON\n * `{ data }` send, mirroring `redirect`/`redirected`.\n * @param status - HTTP status code (e.g. 401, 413, 429)\n * @param body - JSON-serializable response body\n * @param headers - Optional response headers (e.g. `{ \"Retry-After\": \"60\" }`)\n */\n send: (\n status: number,\n body: JsonValue,\n headers?: Record<string, string>,\n ) => void;\n /**\n * Set by `send` once a response has been issued. The middleware checks this\n * after `await`ing the server function to avoid double-responding.\n */\n sent?: { status: number; body: JsonValue; headers?: Record<string, string> };\n /**\n * The matched RPC function name for the current request, when available.\n * Useful for per-function rate limiting or auditing inside middleware.\n */\n functionName?: string;\n /** Per-request app data shared across the async tree of the dispatch */\n locals: Record<string, unknown>;\n [prop: string]: unknown;\n}\n\n// Instance-stable across module copies and dev-server hot reloads, exactly like\n// `solid-js/web`'s `provideRequestEvent` (which stores on a globalThis symbol).\nconst requestContextStorage: AsyncLocalStorage<RequestEvent> =\n ((globalThis as Record<symbol, AsyncLocalStorage<RequestEvent>>)[\n requestContextSymbol\n ] ??= new AsyncLocalStorage<RequestEvent>());\n\n/**\n * Runs `cb` with `init` as the current request context. Use inside the\n * adapters around server-function dispatch (the async tree under `cb` can then\n * read the context via {@link getRequestContext}).\n * @param init - The request context for the duration of `cb`\n * @param cb - The work that needs access to the request context\n */\nexport const provideRequestContext = <T>(\n init: RequestEvent,\n cb: () => T,\n): T => requestContextStorage.run(init, cb);\n\n/**\n * Returns the current request context, or throws when called outside of a\n * request (e.g. module scope or a background task).\n * @throws When no request context is established\n */\nexport const getRequestContext = (): RequestEvent => {\n const ctx = requestContextStorage.getStore();\n if (!ctx) {\n throw new Error(\"RequestEvent is not available outside of a request\");\n }\n return ctx;\n};\n\n/**\n * Redirects the current request to `location`. Reads the adapter-bound\n * `redirect` from the current request context — callable from anywhere inside\n * a server-function tree (no `res` threading needed).\n * @param location - The URL to redirect to\n * @param status - HTTP status code, defaults to `303 See Other`\n * @throws When called outside of a request\n */\nexport const redirect = (location: string, status = 303): void => {\n getRequestContext().redirect(location, status);\n};\n\n/**\n * Sends a raw JSON response for the current request, bypassing the standard\n * `{ data }` shape. Reads the adapter-bound `send` from the current request\n * context — callable from anywhere inside a server-function tree. Any code in\n * the async tree of a dispatch can call this (e.g. custom middleware) to\n * short-circuit with a specific status code (401, 413, 429, ...).\n * @param status - HTTP status code\n * @param body - JSON-serializable response body\n * @param headers - Optional response headers\n * @throws When called outside of a request\n */\nexport const sendResponse = (\n status: number,\n body: JsonValue,\n headers?: Record<string, string>,\n): void => {\n getRequestContext().send(status, body, headers);\n};\n\n/**\n * Normalized, adapter-agnostic view of the current request. Reads the request\n * object off the current request context and normalizes it across the five\n * adapter request shapes (Express `req`, Fastify `req`, Koa `ctx.req`,\n * Hono `c.req`, h3 `event.req`) so middleware can be written once.\n */\nexport interface RequestMeta {\n /** HTTP method, upper-cased (e.g. \"GET\", \"POST\") */\n method: string;\n /** URL pathname (e.g. \"/__rpc/greet\") */\n pathname: string;\n /** Raw search string including the leading \"?\", or \"\" when absent */\n search: string;\n /** Parsed search params */\n searchParams: URLSearchParams;\n /** Request headers, lower-cased */\n headers: Record<string, string | string[] | undefined>;\n /** Host header value (e.g. \"localhost:5173\"), when present */\n host?: string;\n /** Client IP when the framework exposes it (e.g. Fastify `req.ip`) */\n ip?: string;\n /** Request protocol (\"http\" or \"https\"), when determinable */\n protocol?: string;\n}\n\nconst pickHeader = (\n headers: Record<string, string | string[] | undefined>,\n name: string,\n): string | undefined => {\n const value = headers[name];\n if (typeof value === \"string\") return value;\n if (Array.isArray(value)) return value[0];\n return undefined;\n};\n\n/** Normalizes any headers shape into a plain lower-cased record. */\nconst toHeaderRecord = (\n headers: unknown,\n): Record<string, string | string[] | undefined> => {\n if (!headers) return {};\n // Headers-like object (h3 Request, Hono c.req.raw.headers, fetch Headers)\n if (typeof (headers as Headers).forEach === \"function\") {\n const record: Record<string, string> = {};\n (headers as Headers).forEach((value, key) => {\n record[key] = value;\n });\n return record;\n }\n // Plain map (Express req.headers, Fastify req.headers, Koa ctx.req.headers)\n return headers as Record<string, string | string[] | undefined>;\n};\n\n/**\n * Reads normalized, adapter-agnostic request metadata from the current request\n * context. Works with Express `req`, Fastify `req`, Koa `ctx.req`,\n * Hono `c.req` and h3 `event.req` by feature-detecting the request shape\n * (`originalUrl`/`url`/`path`, raw `headers` map vs `Headers`-like API).\n * @param event - The request context to read, typically the result of\n * {@link getRequestContext}\n */\nexport const getRequestMeta = (event: RequestEvent): RequestMeta => {\n const req = event.request as {\n method?: string;\n originalUrl?: string;\n url?: string;\n path?: string;\n headers?: unknown;\n header?: (name: string) => string | string[] | undefined;\n ip?: string;\n protocol?: string;\n socket?: { remoteAddress?: string };\n raw?: { headers?: unknown };\n } | undefined;\n\n const method = (req?.method ?? \"GET\").toUpperCase();\n const rawUrl = req?.originalUrl ?? req?.url ?? req?.path ?? \"\";\n const url = safeURL(rawUrl);\n const headers = toHeaderRecord(req?.headers ?? req?.raw?.headers);\n const hostHeader = pickHeader(headers, \"host\");\n\n return {\n method,\n pathname: url.pathname,\n search: url.search,\n searchParams: url.searchParams,\n headers,\n host: hostHeader,\n ip: req?.ip ?? req?.socket?.remoteAddress,\n protocol: req?.protocol ?? url.protocol.replace(\":\", \"\"),\n };\n};\n"],"mappings":";;;;;AAMA,MAAa,yBAAgD;CAC3D,aAAa;CACb,aAAa;CACb,QAAQ;AACV;AAEA,MAAa,gBAAgB;AAE7B,MAAa,oBAAsC;CACjD,WAAW;CACX,SAAS;CACT,aAAa;CACb,UAAU,KAAA;AACZ;AAEA,MAAa,2BAA8C;C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AalB,MAAM,UAAU,KAAK,UAAU,MAAA,CAAO,YAAY;CAClD,MAAM,SAAS,KAAK,eAAe,KAAK,OAAO,KAAK,QAAQ;CAC5D,MAAM,MAAM,QAAQ,MAAM;CAC1B,MAAM,UAAU,eAAe,KAAK,WAAW,KAAK,KAAK,OAAO;CAChE,MAAM,aAAa,WAAW,SAAS,MAAM;CAE7C,OAAO;EACL;EACA,UAAU,IAAI;EACd,QAAQ,IAAI;EACZ,cAAc,IAAI;EAClB;EACA,MAAM;EACN,IAAI,KAAK,MAAM,KAAK,QAAQ;EAC5B,UAAU,KAAK,YAAY,IAAI,SAAS,QAAQ,KAAK,EAAE;CACzD;AACF"}
package/llms.txt CHANGED
@@ -10,7 +10,7 @@ Vite plugin for automatic RPC generation from server functions. One module (`src
10
10
  - **Multi-prefix**: `rpcPrefix` (default `"__rpc"` via the exported `defaultPrefix` constant) registers the function in a prefix-scoped map (`getFunctionsForPrefix(prefix)`). Same names can coexist under different prefixes (versioned/namespaced APIs); middleware dispatches to the prefix-scoped map; the plugin generates client stubs per prefix. See `wiki/multi-prefix-guide.md`
11
11
  - `RPCError(message, code?, data?)` — throwable typed error, exported from `@thednp/rpc/server` (as is `formatError`); throw for server-side failures (not validation — return `{ error }` for expected user-facing problems). Dev 500 body: `{ error, code, data }`; prod: generic `{ error: "Internal Server Error" }` only. Client rejection `message` = the error string
12
12
  - `provideRequestContext(init, cb)` / `getRequestContext()` — per-request `AsyncLocalStorage` context available to all server function code; `RequestEvent` includes `nativeEvent`, `locals`, adapter-bound `redirect`; works across Express/Fastify/Hono/Koa/h3
13
- - `unwrapEnvelope<T>(json)` — client helper from `@thednp/rpc/helpers` to unwrap `{ data }` wire protocol responses; re-throws `RPCError` on `{ error }` bodies. For native HTTP clients (Deno, Bun, curl-equivalent) that don't use auto-generated fetch stubs
13
+ - `unwrapEnvelope<T>(json)` — client helper from `@thednp/rpc/helpers` to unwrap the `{ data }` wire protocol envelope; for native HTTP clients (Deno, Bun, curl-equivalents) that don't use the auto-generated fetch stubs. Same error contract as `handleResponse`: a **top-level** `error` (emitted only for 400/404/405/415/500) throws; `{ data: { error } }` resolves normally (validation-as-data). Status-code agnostic — keep the `res.ok` check. `RPCError` is server-side only (`@thednp/rpc/server`), not a client export, and its `code`/`data` are stripped in production regardless
14
14
  - Registered function names become URL paths: `POST /{prefix}/{name}` with body `JSON.stringify(args)`
15
15
 
16
16
  ## Wire Protocol (client module → server)
@@ -20,7 +20,7 @@ Vite plugin for automatic RPC generation from server functions. One module (`src
20
20
  - **multipart/form-data** POST: client sends the `FormData` passed as the first argument (browser sets the boundary). Server-side, parse before the handler runs — framework multipart middleware BEFORE the RPC middleware (multer, @fastify/multipart, koa-body, hono formData) and the parsed fields object becomes the function argument; without a parser the handler gets `{ raw: "<body>" }`, which you must parse yourself (busboy or formidable — Node has no built-in multipart parser)
21
21
  - **GET:** `/{prefix}/{fnName}?args=encodeURIComponent(JSON.stringify(args))`
22
22
  - **Response:** always `{ data: <result> }` — 200 wraps everything, even `{ error }` responses (they resolve as data, not thrown)
23
- - **Status codes:** 200 (success or validation-as-data), 404/405/415/500 (transport errors → data promise rejects). 415 = request Content-Type doesn't match the function's declared contentType (json/text strict, forms lenient between urlencoded and multipart; headerless requests exempt). 500 body: generic `{ error: "Internal Server Error" }` in production; dev includes the message (and `code`/`data` for `RPCError`)
23
+ - **Status codes:** 200 (success or validation-as-data), 400/404/405/415/500 (transport errors → data promise rejects). 400 = GET `?args=` present but not a JSON array. 415 = request Content-Type doesn't match the function's declared contentType (json/text strict; form functions accept either form encoding, but a JSON-declared function does **not** accept form bodies — leniency is one-directional; headerless requests exempt). 500 body: generic `{ error: "Internal Server Error" }` in production; dev includes the message (and `code`/`data` for `RPCError`)
24
24
  - See `wiki/wire-protocol.md` for full curl examples
25
25
 
26
26
  ## Config
@@ -29,7 +29,7 @@ Vite plugin for automatic RPC generation from server functions. One module (`src
29
29
  - `scanRoot: string` — scan directory (default `<root>/src/api`); point it at a shared package in monorepos
30
30
  - `silent: boolean` — suppress the `NO_CONFIG_FOUND` warning when no config file is found. Useful for wrapper plugins that define server functions directly without a config file
31
31
  - `loadRPCConfig(opts?)` — loads config programmatically; pass `{ silent: true }` to suppress warnings
32
- - `createRPCMiddleware({ origin })` — optional origin check: requests with a mismatching `Origin` header get 403; requests without one pass through
32
+ - `createRPCMiddleware({ origin })` — optional origin check: `origin` accepts a single string or an **allowlist array** (`["https://app.example.com","https://admin.example.com"]`). A request is rejected with 403 only when it carries an `Origin` header matching none of the entries; requests without an `Origin` header pass through; unset means no validation. Matching is exact, so a lookalike host (`https://app.example.com.evil.com`) is rejected. The rule is the shared `isOriginAllowed` helper in `src/server-helpers.ts`, used by all five adapters
33
33
  - Plugin options in `vite.config.ts` (`rpc()`) override `rpc.config.ts` in dev only
34
34
  - Config discovery: `rpc.config.ts` > `.js` > `.mjs` > `.mts` > `.rpcrc.ts` > `.rpcrc.js`
35
35
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@thednp/rpc",
3
- "version": "0.3.3",
4
- "license": "MIT",
3
+ "version": "0.3.5",
4
+ "license": "MIT",
5
5
  "author": "thednp",
6
6
  "description": "⚡ A Vite plugin for creating server functions with automatic Remote Procedure Calls (RPC)",
7
7
  "homepage": "https://github.com/thednp/rpc#readme",
@@ -85,24 +85,24 @@
85
85
  },
86
86
  "dependencies": {
87
87
  "express": "^5.2.1",
88
- "fastify": "^5.12.4",
88
+ "fastify": "^5.12.5",
89
89
  "fastify-plugin": "^6.0.0",
90
- "h3": "2.0.1-rc.31",
91
- "hono": "^4.13.8",
90
+ "h3": "2.0.1-rc.32",
91
+ "hono": "^4.13.9",
92
92
  "koa": "^3.2.1",
93
- "vite": "^8.3.0"
93
+ "vite": "^8.3.1"
94
94
  },
95
95
  "devDependencies": {
96
96
  "@hono/node-server": "^2.1.1",
97
97
  "@types/express": "^5.0.6",
98
98
  "@types/koa": "^3.0.3",
99
- "@types/node": "^26.5.1",
100
- "@vitest/coverage-istanbul": "^5.0.1",
101
- "@vitest/ui": "^5.0.1",
99
+ "@types/node": "^26.6.3",
100
+ "@vitest/coverage-istanbul": "^5.0.2",
101
+ "@vitest/ui": "^5.0.2",
102
102
  "tsdown": "^0.23.0",
103
103
  "typescript": "^7.0.2",
104
104
  "vite-plugin-strip-comments": "^0.0.10",
105
- "vitest": "^5.0.1"
105
+ "vitest": "^5.0.2"
106
106
  },
107
107
  "packageManager": "pnpm@11.22.0"
108
108
  }