@thednp/rpc 0.1.1 → 0.2.1

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.
Files changed (39) hide show
  1. package/AGENTS.md +43 -26
  2. package/CHANGELOG.md +100 -0
  3. package/README.md +39 -31
  4. package/dist/express/express.d.mts +14 -2
  5. package/dist/express/express.d.mts.map +1 -1
  6. package/dist/express/express.mjs +103 -70
  7. package/dist/express/express.mjs.map +1 -1
  8. package/dist/fastify/fastify.d.mts +16 -1
  9. package/dist/fastify/fastify.d.mts.map +1 -1
  10. package/dist/fastify/fastify.mjs +61 -63
  11. package/dist/fastify/fastify.mjs.map +1 -1
  12. package/dist/fastify/plugin/fastify/plugin.d.mts +20 -3
  13. package/dist/fastify/plugin/fastify/plugin.d.mts.map +1 -1
  14. package/dist/fastify/plugin/fastify/plugin.mjs +60 -62
  15. package/dist/fastify/plugin/fastify/plugin.mjs.map +1 -1
  16. package/dist/h3/h3.d.mts +94 -0
  17. package/dist/h3/h3.d.mts.map +1 -0
  18. package/dist/h3/h3.mjs +267 -0
  19. package/dist/h3/h3.mjs.map +1 -0
  20. package/dist/helpers/helpers.d.mts +2 -0
  21. package/dist/helpers/helpers.d.mts.map +1 -1
  22. package/dist/helpers/helpers.mjs.map +1 -1
  23. package/dist/hono/hono.d.mts +14 -40
  24. package/dist/hono/hono.d.mts.map +1 -1
  25. package/dist/hono/hono.mjs +61 -73
  26. package/dist/hono/hono.mjs.map +1 -1
  27. package/dist/index.d.mts +36 -5
  28. package/dist/index.d.mts.map +1 -1
  29. package/dist/index.mjs +4 -0
  30. package/dist/index.mjs.map +1 -1
  31. package/dist/koa/koa.d.mts +12 -1
  32. package/dist/koa/koa.d.mts.map +1 -1
  33. package/dist/koa/koa.mjs +69 -64
  34. package/dist/koa/koa.mjs.map +1 -1
  35. package/dist/server/server.d.mts +180 -3
  36. package/dist/server/server.d.mts.map +1 -1
  37. package/dist/server/server.mjs +157 -1
  38. package/dist/server/server.mjs.map +1 -1
  39. package/package.json +10 -4
package/dist/koa/koa.mjs CHANGED
@@ -1,64 +1,14 @@
1
- import { scanForServerFiles, serverFunctionsMap } from "@thednp/rpc/server";
2
- //#region src/tools.ts
3
- /**
4
- * Escapes special regex metacharacters in a string.
5
- * Used to safely embed user-configurable values (like rpcPrefix) into regular expressions,
6
- * preventing ReDoS and regex injection attacks.
7
- * @param s - The raw string to escape
8
- * @returns The escaped string safe for use in new RegExp()
9
- */
10
- function escapeRegExp(s) {
11
- return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
12
- }
13
- //#endregion
1
+ import { escapeRegExp, formatError, hasContentTypeMismatch, provideRequestContext, safeURL, scanForServerFiles, serverFunctionsMap } from "@thednp/rpc/server";
14
2
  //#region src/constants.ts
15
3
  const FUNCTION_NOT_FOUND = "Function not found";
16
4
  const METHOD_NOT_ALLOWED = "Method Not Allowed";
17
5
  const REQUEST_FORBIDDEN = "Forbidden";
18
- const INTERNAL_SERVER_ERROR = "Internal Server Error";
6
+ const UNSUPPORTED_MEDIA_TYPE = "Unsupported Media Type";
7
+ const BAD_REQUEST = "Bad Request";
19
8
  const CLIENT_DISCONNECTED = "client disconnected";
20
9
  /** Returns a warning when a middleware name is reused, preventing registration conflicts. @param name - The duplicate middleware name */
21
10
  const MIDDLEWARE_NAME_USED = (name) => `The middleware name "${name}" is already used.`;
22
11
  //#endregion
23
- //#region src/server-helpers.ts
24
- /**
25
- * A typed error thrown from server functions.
26
- * The middleware serializes the `message` and `code` in the response,
27
- * allowing clients to recognise and handle specific error conditions.
28
- */
29
- var RPCError = class extends Error {
30
- /** Machine-readable error code (e.g. "VALIDATION_FAILED", "UNAUTHORIZED") */
31
- code;
32
- /** Optional diagnostic payload */
33
- data;
34
- constructor(message, code = "INTERNAL", data) {
35
- super(message);
36
- this.name = "RPCError";
37
- this.code = code;
38
- this.data = data;
39
- }
40
- };
41
- /**
42
- * Formats an error for the RPC middleware response.
43
- * In development the full `RPCError` payload is included so developers
44
- * can quickly identify issues. Unexpected exceptions never expose their
45
- * message — only the generic "Internal Server Error" is sent, preventing
46
- * information disclosure; server-side diagnostics are preserved via the
47
- * middleware's `console.error` logging.
48
- */
49
- const formatError = (err, isProduction) => {
50
- if (isProduction) return { error: INTERNAL_SERVER_ERROR };
51
- if (err instanceof RPCError) {
52
- const payload = {
53
- error: err.message || "Internal Server Error",
54
- code: err.code
55
- };
56
- if (err.data !== void 0) payload.data = err.data;
57
- return payload;
58
- }
59
- return { error: INTERNAL_SERVER_ERROR };
60
- };
61
- //#endregion
62
12
  //#region src/options.ts
63
13
  const defaultRPCOptions = {
64
14
  rpcPrefix: "__rpc",
@@ -121,11 +71,12 @@ const readBody = (ctx) => {
121
71
  return new Promise((resolve, reject) => {
122
72
  const isJSON = contentType.includes("json");
123
73
  const isMultipart = contentType.includes("multipart/form-data");
74
+ const isUrlEncoded = contentType.includes("urlencoded");
124
75
  const reqBody = ctx.request.body;
125
76
  if (reqBody !== void 0) {
126
77
  resolve({
127
- contentType: isMultipart ? "multipart/form-data" : isJSON ? "application/json" : "text/plain",
128
- data: isMultipart ? reqBody : isJSON ? reqBody : String(reqBody)
78
+ contentType: isMultipart ? "multipart/form-data" : isJSON ? "application/json" : isUrlEncoded ? "application/x-www-form-urlencoded" : "text/plain",
79
+ data: isMultipart ? reqBody : isJSON ? reqBody : isUrlEncoded ? reqBody : String(reqBody)
129
80
  });
130
81
  return;
131
82
  }
@@ -143,10 +94,11 @@ const readBody = (ctx) => {
143
94
  toggleListeners();
144
95
  const isJSON = contentType.includes("json");
145
96
  const isMultipart = contentType.includes("multipart/form-data");
97
+ const isUrlEncoded = contentType.includes("urlencoded");
146
98
  try {
147
- const data = isMultipart ? { raw: body } : JSON.parse(body);
99
+ const data = isMultipart ? { raw: body } : isUrlEncoded ? Object.fromEntries(new URLSearchParams(body)) : JSON.parse(body);
148
100
  resolve({
149
- contentType: isMultipart ? "multipart/form-data" : isJSON ? "application/json" : "text/plain",
101
+ contentType: isMultipart ? "multipart/form-data" : isJSON ? "application/json" : isUrlEncoded ? "application/x-www-form-urlencoded" : "text/plain",
150
102
  data: isMultipart ? data : data
151
103
  });
152
104
  } catch (_er) {
@@ -163,6 +115,20 @@ const readBody = (ctx) => {
163
115
  toggleListeners(true);
164
116
  });
165
117
  };
118
+ /**
119
+ * Issues an HTTP redirect on a Koa context. Koa's `ctx.redirect(location)`
120
+ * defaults to `302` and sets the `Location` header; the status code must be
121
+ * overridden *after* the call (setting it before is ignored, see
122
+ * koajs/koa#857). Defaults to `303 See Other` for convention
123
+ * (Post/Redirect/Get).
124
+ * @param ctx - Koa context
125
+ * @param location - The URL to redirect to
126
+ * @param status - HTTP status code, defaults to 303
127
+ */
128
+ const redirect = (ctx, location, status = 303) => {
129
+ ctx.redirect(location);
130
+ ctx.status = status;
131
+ };
166
132
  //#endregion
167
133
  //#region src/koa/createMiddleware.ts
168
134
  let middlewareCount = 0;
@@ -190,7 +156,7 @@ const createMiddleware = (initialOptions = {}) => {
190
156
  const prefixRegex = rpcPrefix ? new RegExp(`^/${escapeRegExp(rpcPrefix)}/`) : null;
191
157
  const pathMatcher = path ? typeof path === "string" ? new RegExp(path) : path : null;
192
158
  const middlewareHandler = async (ctx, next) => {
193
- const url = new URL(ctx.url, "http://localhost").pathname;
159
+ const url = safeURL(ctx.url).pathname;
194
160
  if (serverFunctionsMap.size === 0) await scanForServerFiles();
195
161
  if (!handler) return next();
196
162
  if (pathMatcher && !pathMatcher.test(url)) return next();
@@ -215,7 +181,7 @@ const createRPCMiddleware = (initialOptions = {}) => {
215
181
  return createMiddleware({
216
182
  ...options,
217
183
  handler: async (ctx, _next) => {
218
- const reqUrl = new URL(ctx.url, "http://localhost");
184
+ const reqUrl = safeURL(ctx.url);
219
185
  const url = reqUrl.pathname;
220
186
  if (prefixRegex && !prefixRegex.test(url)) return;
221
187
  const origin = options.origin;
@@ -242,18 +208,57 @@ const createRPCMiddleware = (initialOptions = {}) => {
242
208
  let args = [];
243
209
  if (method === "GET") {
244
210
  const raw = reqUrl.searchParams.get("args");
245
- if (raw) args = JSON.parse(raw);
211
+ if (raw) {
212
+ const parsed = JSON.parse(raw);
213
+ if (!Array.isArray(parsed)) {
214
+ ctx.status = 400;
215
+ ctx.body = { error: BAD_REQUEST };
216
+ return;
217
+ }
218
+ args = parsed;
219
+ }
246
220
  } else {
221
+ if (hasContentTypeMismatch(serverFunction.options?.contentType ?? "application/json", ctx.headers["content-type"])) {
222
+ ctx.status = 415;
223
+ ctx.body = { error: UNSUPPORTED_MEDIA_TYPE };
224
+ return;
225
+ }
247
226
  const body = await readBody(ctx);
248
227
  args = Array.isArray(body.data) ? body.data : [body.data];
249
228
  }
250
- const { data: resultData, cancel } = serverFunction.handler(...args);
229
+ const requestEvent = {
230
+ request: ctx.req,
231
+ response: ctx,
232
+ nativeEvent: ctx,
233
+ locals: ctx.state,
234
+ functionName,
235
+ redirect: (location, status = 303) => {
236
+ requestEvent.redirected = {
237
+ location,
238
+ status
239
+ };
240
+ redirect(ctx, location, status);
241
+ },
242
+ send: (status, body, headers) => {
243
+ requestEvent.sent = {
244
+ status,
245
+ body,
246
+ headers
247
+ };
248
+ if (headers) for (const [name, value] of Object.entries(headers)) ctx.set(name, value);
249
+ ctx.status = status;
250
+ ctx.body = body;
251
+ }
252
+ };
253
+ const { data: resultData, cancel } = provideRequestContext(requestEvent, () => serverFunction.handler(...args));
251
254
  const onClose = () => cancel(CLIENT_DISCONNECTED);
252
255
  ctx.req.on("close", onClose);
253
256
  const result = await resultData;
254
257
  ctx.req.off("close", onClose);
255
- ctx.status = 200;
256
- ctx.body = { data: result };
258
+ if (!requestEvent.redirected && !requestEvent.sent) {
259
+ ctx.status = 200;
260
+ ctx.body = { data: result };
261
+ }
257
262
  } catch (err) {
258
263
  console.error(String(err));
259
264
  const isProduction = process.env.NODE_ENV === "production";
@@ -264,6 +269,6 @@ const createRPCMiddleware = (initialOptions = {}) => {
264
269
  });
265
270
  };
266
271
  //#endregion
267
- export { attachRPC, attachVite, createMiddleware, createRPCMiddleware, readBody };
272
+ export { attachRPC, attachVite, createMiddleware, createRPCMiddleware, readBody, redirect };
268
273
 
269
274
  //# sourceMappingURL=koa.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"koa.mjs","names":[],"sources":["../../src/tools.ts","../../src/constants.ts","../../src/server-helpers.ts","../../src/options.ts","../../src/koa/helpers.ts","../../src/koa/createMiddleware.ts"],"sourcesContent":["/**\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","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 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 = ` ⚡︎ 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, and `walkGlobFiles` for recursively discovering `*.server.*` files. Never import this module in client code — it is server-only. */\nimport type { 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","import type {\n MiddlewareOptions,\n RpcPluginOptions,\n ServerFunctionOptions,\n} from \"./types.d.ts\";\n\nexport const defaultServerFnOptions = {\n contentType: \"application/json\",\n credentials: \"same-origin\",\n method: \"POST\",\n} satisfies ServerFunctionOptions;\n\nexport const defaultRPCOptions: RpcPluginOptions = {\n rpcPrefix: \"__rpc\",\n adapter: \"express\",\n serverFiles: \"exact\",\n scanRoot: undefined,\n};\n\nexport const defaultMiddlewareOptions = {\n rpcPrefix: undefined,\n path: undefined,\n origin: undefined,\n} satisfies MiddlewareOptions;\n","// src/koa/helpers.ts\nimport type { Buffer } from \"node:buffer\";\nimport type { ViteDevServer } from \"vite\";\nimport { createRPCMiddleware } from \"./createMiddleware.ts\";\nimport type { BodyResult } from \"@thednp/rpc\";\nimport type { Koa } from \"./index.ts\";\nimport type { KoaContext } from \"./types.d.ts\";\n\n/**\n * Convenience function to load RPC config and attach the RPC middleware to a Koa app.\n * Dynamically imports loadRPCConfig and registers the middleware.\n * @param app - Koa application instance\n */\nexport async function attachRPC(app: Koa) {\n // The main plugin entry statically imports Vite, so loadRPCConfig is\n // imported lazily: function bundles that never call attachRPC (e.g.\n // serverless functions) keep Vite out of the bundle (or externalized).\n const { loadRPCConfig } = await import(\"@thednp/rpc\");\n\n const config = await loadRPCConfig();\n const { adapter: _adapter, ...options } = config;\n app.use(createRPCMiddleware(options));\n}\n\n/**\n * Attaches Vite's dev server middlewares to a Koa app for development mode.\n * Bridges Koa's context-based middleware to Vite's Connect-compatible middleware stack\n * by forwarding Koa body, wrapping res.end, and delegating back to Koa on 404 or unhandled routes.\n * @param app - Koa application instance\n * @param vite - Running Vite dev server\n */\nexport function attachVite(app: Koa, vite: ViteDevServer): void {\n app.use(async (ctx: KoaContext, next) => {\n const req = ctx.req;\n const res = ctx.res;\n\n // Forward Koa body to req.body for Express/RPC middleware compatibility\n const requestBody = ctx.request?.body;\n if (requestBody !== undefined) {\n Object.assign(req, { body: requestBody });\n }\n\n const originalEnd = res.end.bind(res);\n let viteHandled = false;\n // @ts-ignore - Koa res.end type mismatch with Node's\n res.end = function (...args: unknown[]) {\n viteHandled = true;\n return originalEnd(args[0]);\n };\n\n await new Promise<void>((resolve) => {\n vite.middlewares(req, res, () => resolve(undefined));\n });\n\n // @ts-ignore - Koa res.end type mismatch with Node's\n res.end = originalEnd;\n\n if (!viteHandled || res.statusCode === 404) {\n await next();\n }\n });\n}\n\n/**\n * Reads and parses the HTTP request body from a Koa context.\n * If koa-body or another body parser already consumed the stream,\n * uses the pre-parsed body from `ctx.request.body`.\n * @param ctx - Koa context\n * @returns A promise resolving to the parsed body with its content type\n */\nexport const readBody = (\n ctx: KoaContext,\n): Promise<BodyResult> => {\n const contentType = ctx.request.headers[\"content-type\"]?.toLowerCase() || \"\";\n\n return new Promise((resolve, reject) => {\n // If an koa-body already consumed the stream\n // via app.use(koaBody()), use ctx.request.body directly\n const isJSON = contentType.includes(\"json\");\n const isMultipart = contentType.includes(\"multipart/form-data\");\n const reqBody = ctx.request.body;\n if (reqBody !== undefined) {\n resolve({\n contentType: isMultipart\n ? \"multipart/form-data\"\n : isJSON\n ? \"application/json\"\n : \"text/plain\",\n data: isMultipart\n ? (reqBody as Record<string, unknown>)\n : isJSON\n ? reqBody\n : String(reqBody),\n } as BodyResult);\n return;\n }\n\n // OR read the body normally\n let body = \"\";\n\n const toggleListeners = (add?: boolean) => {\n const method = add ? \"on\" : \"off\";\n ctx.req[method](\"data\", onData);\n ctx.req[method](\"end\", onEnd);\n ctx.req[method](\"error\", onError);\n };\n\n const onData = (chunk: Buffer) => {\n // chunks.push(chunk);\n body += chunk.toString();\n };\n\n const onEnd = () => {\n toggleListeners();\n const isJSON = contentType.includes(\"json\");\n const isMultipart = contentType.includes(\"multipart/form-data\");\n try {\n const data = isMultipart ? { raw: body } : JSON.parse(body);\n resolve({\n contentType: isMultipart\n ? \"multipart/form-data\"\n : isJSON\n ? \"application/json\"\n : \"text/plain\",\n data: isMultipart ? (data as Record<string, unknown>) : data,\n } as BodyResult);\n } catch (_er) {\n resolve({ contentType: \"text/plain\", data: String(body) });\n }\n };\n\n const onError = (err: Error) => {\n toggleListeners();\n reject(err);\n };\n\n toggleListeners(true);\n });\n};\n","// src/koa/createMiddleware.ts\nimport type { Context, Next } from \"koa\";\nimport type { KoaMiddlewareFn, KoaMiddlewareOptions } from \"./types.d.ts\";\nimport type { JsonValue } from \"../types.d.ts\";\nimport { escapeRegExp } from \"../tools.ts\";\nimport { formatError } from \"../server-helpers.ts\";\nimport {\n CLIENT_DISCONNECTED,\n FUNCTION_NOT_FOUND,\n METHOD_NOT_ALLOWED,\n MIDDLEWARE_NAME_USED,\n REQUEST_FORBIDDEN,\n} from \"../constants.ts\";\n\nimport { scanForServerFiles, serverFunctionsMap } from \"@thednp/rpc/server\";\nimport { defaultMiddlewareOptions, defaultRPCOptions } from \"../options.ts\";\nimport { readBody } from \"./helpers.ts\";\n\nlet middlewareCount = 0;\nconst middlewareStack = new Set<string>();\n\n/**\n * Creates a Koa middleware with optional path and rpcPrefix filtering.\n * Middleware names are deduplicated. Prefix and path regexes are compiled once at creation time.\n * Koa URL is normalized via `new URL()` to strip query strings before matching.\n * @param initialOptions - Options for rpcPrefix, path matching, and the handler function\n * @returns A Koa middleware function\n */\nexport const createMiddleware: KoaMiddlewareFn = (initialOptions = {}) => {\n const options = Object.assign(\n {},\n defaultMiddlewareOptions,\n initialOptions,\n ) as KoaMiddlewareOptions;\n\n const middlewareName = options.name;\n const rpcPrefix = options.rpcPrefix;\n const path = options.path;\n const handler = options.handler;\n\n let name = middlewareName;\n if (!name) {\n name = \"viteRPCMiddleware-\" + middlewareCount;\n middlewareCount += 1;\n }\n if (middlewareStack.has(name)) {\n throw new Error(MIDDLEWARE_NAME_USED(name));\n }\n middlewareStack.add(name);\n\n // Hoist regex compilation out of per-request path. Escape the prefix to\n // prevent regex injection via metacharacters in the config string.\n const prefixRegex: RegExp | null = rpcPrefix\n ? new RegExp(`^/${escapeRegExp(rpcPrefix)}/`)\n : null;\n const pathMatcher: RegExp | null = path\n ? (typeof path === \"string\" ? new RegExp(path) : path)\n : null;\n\n const middlewareHandler = async (ctx: Context, next: Next) => {\n const url = new URL(ctx.url, \"http://localhost\").pathname;\n\n if (serverFunctionsMap.size === 0) {\n await scanForServerFiles();\n }\n\n // No need to continue when no handler provided\n if (!handler) {\n return next();\n }\n\n if (pathMatcher && !pathMatcher.test(url)) {\n return next();\n }\n\n if (prefixRegex && !prefixRegex.test(url)) {\n return next();\n }\n\n await handler(ctx, next);\n };\n\n Object.defineProperty(middlewareHandler, \"name\", {\n value: name,\n });\n\n return middlewareHandler;\n};\n\n/**\n * Creates the Koa RPC middleware that routes incoming requests to registered server functions.\n * Wraps the generic createMiddleware with the RPC handler that reads the body, dispatches\n * to the matching function, and sets the JSON-serialized result on ctx.body.\n * @param initialOptions - Options including rpcPrefix for URL routing\n * @returns A Koa middleware function\n */\nexport const createRPCMiddleware: KoaMiddlewareFn = (initialOptions = {}) => {\n const options = Object.assign(\n {},\n defaultMiddlewareOptions,\n { rpcPrefix: defaultRPCOptions.rpcPrefix },\n initialOptions,\n ) as KoaMiddlewareOptions;\n\n // Hoist prefix regex (escaped) and the literal prefix-for-replace out of the\n // per-request handler to avoid regex injection and per-request compilation.\n const rpcPrefix = options.rpcPrefix;\n const prefixRegex = rpcPrefix\n ? new RegExp(`^/${escapeRegExp(rpcPrefix)}/`)\n : /* istanbul ignore next */ null;\n const prefixReplace = `/${rpcPrefix}/`;\n\n return createMiddleware({\n ...options,\n handler: async (ctx: Context, _next: Next) => {\n const reqUrl = new URL(ctx.url, \"http://localhost\");\n const url = reqUrl.pathname;\n // const { rpcPrefix } = options;\n\n // Defense-in-depth: validate prefix match via escaped regex even though\n // the outer createMiddleware gates on the same prefix already.\n // istanbul ignore next\n if (prefixRegex && !prefixRegex.test(url)) {\n return;\n }\n\n // Optional origin check: reject requests whose Origin header does not\n // match the configured origin. Requests without an Origin header\n // (curl, native clients) pass through unchecked.\n const origin = options.origin;\n const requestOrigin = ctx.headers.origin;\n if (origin && requestOrigin && requestOrigin !== origin) {\n ctx.status = 403;\n ctx.body = { error: REQUEST_FORBIDDEN };\n return;\n }\n\n const functionName = url.replace(prefixReplace, \"\");\n const serverFunction = serverFunctionsMap.get(functionName);\n\n if (!serverFunction) {\n ctx.status = 404;\n ctx.body = { error: FUNCTION_NOT_FOUND };\n return;\n }\n\n try {\n const method = serverFunction.options?.method || \"POST\";\n if (ctx.method.toUpperCase() !== method) {\n ctx.status = 405;\n ctx.body = { error: METHOD_NOT_ALLOWED };\n return;\n }\n\n let args: JsonValue[] = [];\n if (method === \"GET\") {\n const raw = reqUrl.searchParams.get(\"args\");\n if (raw) args = JSON.parse(raw);\n } else {\n const body = await readBody(ctx);\n args = Array.isArray(body.data)\n ? body.data as JsonValue[]\n : [body.data as JsonValue];\n }\n const { data: resultData, cancel } = serverFunction.handler(...args);\n const onClose = () => cancel(CLIENT_DISCONNECTED);\n ctx.req.on(\"close\", onClose);\n const result = await resultData;\n ctx.req.off(\"close\", onClose);\n ctx.status = 200;\n ctx.body = { data: result };\n } catch (err) {\n console.error(String(err));\n const isProduction = process.env.NODE_ENV === \"production\";\n ctx.status = 500;\n ctx.body = formatError(err, isProduction);\n }\n },\n });\n};\n"],"mappings":";;;;;;;;;AAOA,SAAgB,aAAa,GAAmB;CAC9C,OAAO,EAAE,QAAQ,uBAAuB,MAAM;AAChD;;;ACCA,MAAa,qBAAqB;AAElC,MAAa,qBAAqB;AAElC,MAAa,oBAAoB;AAEjC,MAAa,wBAAwB;AAErC,MAAa,sBAAsB;;AAGnC,MAAa,wBAAwB,SACnC,wBAAwB,KAAK;;;;;;;;ACmB/B,IAAa,WAAb,cAA8B,MAAM;;CAElC;;CAEA;CACA,YAAY,SAAiB,OAAO,YAAY,MAAkB;EAChE,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,OAAO;CACd;AACF;;;;;;;;;AAUA,MAAa,eACX,KACA,iBACe;CACf,IAAI,cACF,OAAO,EAAE,OAAO,sBAAsB;CAExC,IAAI,eAAe,UAAU;EAC3B,MAAM,UAAsB;GAC1B,OAAO,IAAI,WAAA;GACX,MAAM,IAAI;EACZ;EACA,IAAI,IAAI,SAAS,KAAA,GAAW,QAAQ,OAAO,IAAI;EAC/C,OAAO;CACT;CACA,OAAO,EAAE,OAAO,sBAAsB;AACxC;;;AClEA,MAAa,oBAAsC;CACjD,WAAW;CACX,SAAS;CACT,aAAa;CACb,UAAU,KAAA;AACZ;AAEA,MAAa,2BAA2B;CACtC,WAAW,KAAA;CACX,MAAM,KAAA;CACN,QAAQ,KAAA;AACV;;;;;;;;ACVA,eAAsB,UAAU,KAAU;CAIxC,MAAM,EAAE,kBAAkB,MAAM,OAAO;CAGvC,MAAM,EAAE,SAAS,UAAU,GAAG,YAAY,MADrB,cAAc;CAEnC,IAAI,IAAI,oBAAoB,OAAO,CAAC;AACtC;;;;;;;;AASA,SAAgB,WAAW,KAAU,MAA2B;CAC9D,IAAI,I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KAC1C,IAAI,KAAK,OAAO,KAAK,MAAM,GAAG;IAChC,OAAO;KACL,MAAM,OAAO,MAAM,SAAS,GAAG;KAC/B,OAAO,MAAM,QAAQ,KAAK,IAAI,IAC1B,KAAK,OACL,CAAC,KAAK,IAAiB;IAC7B;IACA,MAAM,EAAE,MAAM,YAAY,WAAW,eAAe,QAAQ,GAAG,IAAI;IACnE,MAAM,gBAAgB,OAAO,mBAAmB;IAChD,IAAI,IAAI,GAAG,SAAS,OAAO;IAC3B,MAAM,SAAS,MAAM;IACrB,IAAI,IAAI,IAAI,SAAS,OAAO;IAC5B,IAAI,SAAS;IACb,IAAI,OAAO,EAAE,MAAM,OAAO;GAC5B,SAAS,KAAK;IACZ,QAAQ,MAAM,OAAO,GAAG,CAAC;IACzB,MAAM,eAAe,QAAQ,IAAI,aAAa;IAC9C,IAAI,SAAS;IACb,IAAI,OAAO,YAAY,KAAK,YAAY;GAC1C;EACF;CACF,CAAC;AACH"}
1
+ {"version":3,"file":"koa.mjs","names":[],"sources":["../../src/constants.ts","../../src/options.ts","../../src/koa/helpers.ts","../../src/koa/createMiddleware.ts"],"sourcesContent":["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 = ` ⚡︎ 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","import type {\n MiddlewareOptions,\n RpcPluginOptions,\n ServerFunctionOptions,\n} from \"./types.d.ts\";\n\nexport const defaultServerFnOptions = {\n contentType: \"application/json\",\n credentials: \"same-origin\",\n method: \"POST\",\n} satisfies ServerFunctionOptions;\n\nexport const defaultRPCOptions: RpcPluginOptions = {\n rpcPrefix: \"__rpc\",\n adapter: \"express\",\n serverFiles: \"exact\",\n scanRoot: undefined,\n};\n\nexport const defaultMiddlewareOptions = {\n rpcPrefix: undefined,\n path: undefined,\n origin: undefined,\n} satisfies MiddlewareOptions;\n","// src/koa/helpers.ts\nimport type { Buffer } from \"node:buffer\";\nimport type { ViteDevServer } from \"vite\";\nimport { createRPCMiddleware } from \"./createMiddleware.ts\";\nimport type { BodyResult } from \"@thednp/rpc\";\nimport type { Koa } from \"./index.ts\";\nimport type { KoaContext } from \"./types.d.ts\";\n\n/**\n * Convenience function to load RPC config and attach the RPC middleware to a Koa app.\n * Dynamically imports loadRPCConfig and registers the middleware.\n * @param app - Koa application instance\n */\nexport async function attachRPC(app: Koa) {\n // The main plugin entry statically imports Vite, so loadRPCConfig is\n // imported lazily: function bundles that never call attachRPC (e.g.\n // serverless functions) keep Vite out of the bundle (or externalized).\n const { loadRPCConfig } = await import(\"@thednp/rpc\");\n\n const config = await loadRPCConfig();\n const { adapter: _adapter, ...options } = config;\n app.use(createRPCMiddleware(options));\n}\n\n/**\n * Attaches Vite's dev server middlewares to a Koa app for development mode.\n * Bridges Koa's context-based middleware to Vite's Connect-compatible middleware stack\n * by forwarding Koa body, wrapping res.end, and delegating back to Koa on 404 or unhandled routes.\n * @param app - Koa application instance\n * @param vite - Running Vite dev server\n */\nexport function attachVite(app: Koa, vite: ViteDevServer): void {\n app.use(async (ctx: KoaContext, next) => {\n const req = ctx.req;\n const res = ctx.res;\n\n // Forward Koa body to req.body for Express/RPC middleware compatibility\n const requestBody = ctx.request?.body;\n if (requestBody !== undefined) {\n Object.assign(req, { body: requestBody });\n }\n\n const originalEnd = res.end.bind(res);\n let viteHandled = false;\n // @ts-ignore - Koa res.end type mismatch with Node's\n res.end = function (...args: unknown[]) {\n viteHandled = true;\n return originalEnd(args[0]);\n };\n\n await new Promise<void>((resolve) => {\n vite.middlewares(req, res, () => resolve(undefined));\n });\n\n // @ts-ignore - Koa res.end type mismatch with Node's\n res.end = originalEnd;\n\n if (!viteHandled || res.statusCode === 404) {\n await next();\n }\n });\n}\n\n/**\n * Reads and parses the HTTP request body from a Koa context.\n * If koa-body or another body parser already consumed the stream,\n * uses the pre-parsed body from `ctx.request.body`.\n * @param ctx - Koa context\n * @returns A promise resolving to the parsed body with its content type\n */\nexport const readBody = (\n ctx: KoaContext,\n): Promise<BodyResult> => {\n const contentType = ctx.request.headers[\"content-type\"]?.toLowerCase() || \"\";\n\n return new Promise((resolve, reject) => {\n // If an koa-body already consumed the stream\n // via app.use(koaBody()), use ctx.request.body directly\n const isJSON = contentType.includes(\"json\");\n const isMultipart = contentType.includes(\"multipart/form-data\");\n const isUrlEncoded = contentType.includes(\"urlencoded\");\n const reqBody = ctx.request.body;\n if (reqBody !== undefined) {\n resolve({\n contentType: isMultipart\n ? \"multipart/form-data\"\n : isJSON\n ? \"application/json\"\n : isUrlEncoded\n ? \"application/x-www-form-urlencoded\"\n : \"text/plain\",\n data: isMultipart\n ? (reqBody as Record<string, unknown>)\n : isJSON\n ? reqBody\n : isUrlEncoded\n ? (reqBody as Record<string, unknown>)\n : String(reqBody),\n } as BodyResult);\n return;\n }\n\n // OR read the body normally\n let body = \"\";\n\n const toggleListeners = (add?: boolean) => {\n const method = add ? \"on\" : \"off\";\n ctx.req[method](\"data\", onData);\n ctx.req[method](\"end\", onEnd);\n ctx.req[method](\"error\", onError);\n };\n\n const onData = (chunk: Buffer) => {\n // chunks.push(chunk);\n body += chunk.toString();\n };\n\n const onEnd = () => {\n toggleListeners();\n const isJSON = contentType.includes(\"json\");\n const isMultipart = contentType.includes(\"multipart/form-data\");\n const isUrlEncoded = contentType.includes(\"urlencoded\");\n try {\n const data = isMultipart\n ? { raw: body }\n : isUrlEncoded\n ? Object.fromEntries(new URLSearchParams(body))\n : JSON.parse(body);\n resolve({\n contentType: isMultipart\n ? \"multipart/form-data\"\n : isJSON\n ? \"application/json\"\n : isUrlEncoded\n ? \"application/x-www-form-urlencoded\"\n : \"text/plain\",\n data: isMultipart ? (data as Record<string, unknown>) : data,\n } as BodyResult);\n } catch (_er) {\n resolve({ contentType: \"text/plain\", data: String(body) });\n }\n };\n\n const onError = (err: Error) => {\n toggleListeners();\n reject(err);\n };\n\n toggleListeners(true);\n });\n};\n\n/**\n * Issues an HTTP redirect on a Koa context. Koa's `ctx.redirect(location)`\n * defaults to `302` and sets the `Location` header; the status code must be\n * overridden *after* the call (setting it before is ignored, see\n * koajs/koa#857). Defaults to `303 See Other` for convention\n * (Post/Redirect/Get).\n * @param ctx - Koa context\n * @param location - The URL to redirect to\n * @param status - HTTP status code, defaults to 303\n */\nexport const redirect = (\n ctx: KoaContext,\n location: string,\n status = 303,\n): void => {\n ctx.redirect(location);\n ctx.status = status;\n};\n","// src/koa/createMiddleware.ts\nimport type { Context, Next } from \"koa\";\nimport type { KoaMiddlewareFn, KoaMiddlewareOptions } from \"./types.d.ts\";\nimport type { JsonValue } from \"@thednp/rpc\";\nimport type { RequestEvent } from \"@thednp/rpc/server\";\nimport {\n escapeRegExp,\n formatError,\n hasContentTypeMismatch,\n provideRequestContext,\n safeURL,\n scanForServerFiles,\n serverFunctionsMap,\n} from \"@thednp/rpc/server\";\nimport {\n BAD_REQUEST,\n CLIENT_DISCONNECTED,\n FUNCTION_NOT_FOUND,\n METHOD_NOT_ALLOWED,\n MIDDLEWARE_NAME_USED,\n REQUEST_FORBIDDEN,\n UNSUPPORTED_MEDIA_TYPE,\n} from \"../constants.ts\";\n\nimport { defaultMiddlewareOptions, defaultRPCOptions } from \"../options.ts\";\nimport { readBody, redirect as koaRedirect } from \"./helpers.ts\";\n\nlet middlewareCount = 0;\nconst middlewareStack = new Set<string>();\n\n/**\n * Creates a Koa middleware with optional path and rpcPrefix filtering.\n * Middleware names are deduplicated. Prefix and path regexes are compiled once at creation time.\n * Koa URL is normalized via `new URL()` to strip query strings before matching.\n * @param initialOptions - Options for rpcPrefix, path matching, and the handler function\n * @returns A Koa middleware function\n */\nexport const createMiddleware: KoaMiddlewareFn = (initialOptions = {}) => {\n const options = Object.assign(\n {},\n defaultMiddlewareOptions,\n initialOptions,\n ) as KoaMiddlewareOptions;\n\n const middlewareName = options.name;\n const rpcPrefix = options.rpcPrefix;\n const path = options.path;\n const handler = options.handler;\n\n let name = middlewareName;\n if (!name) {\n name = \"viteRPCMiddleware-\" + middlewareCount;\n middlewareCount += 1;\n }\n if (middlewareStack.has(name)) {\n throw new Error(MIDDLEWARE_NAME_USED(name));\n }\n middlewareStack.add(name);\n\n // Hoist regex compilation out of per-request path. Escape the prefix to\n // prevent regex injection via metacharacters in the config string.\n const prefixRegex: RegExp | null = rpcPrefix\n ? new RegExp(`^/${escapeRegExp(rpcPrefix)}/`)\n : null;\n const pathMatcher: RegExp | null = path\n ? (typeof path === \"string\" ? new RegExp(path) : path)\n : null;\n\n const middlewareHandler = async (ctx: Context, next: Next) => {\n const url = safeURL(ctx.url).pathname;\n\n if (serverFunctionsMap.size === 0) {\n await scanForServerFiles();\n }\n\n // No need to continue when no handler provided\n if (!handler) {\n return next();\n }\n\n if (pathMatcher && !pathMatcher.test(url)) {\n return next();\n }\n\n if (prefixRegex && !prefixRegex.test(url)) {\n return next();\n }\n\n await handler(ctx, next);\n };\n\n Object.defineProperty(middlewareHandler, \"name\", {\n value: name,\n });\n\n return middlewareHandler;\n};\n\n/**\n * Creates the Koa RPC middleware that routes incoming requests to registered server functions.\n * Wraps the generic createMiddleware with the RPC handler that reads the body, dispatches\n * to the matching function, and sets the JSON-serialized result on ctx.body.\n * @param initialOptions - Options including rpcPrefix for URL routing\n * @returns A Koa middleware function\n */\nexport const createRPCMiddleware: KoaMiddlewareFn = (initialOptions = {}) => {\n const options = Object.assign(\n {},\n defaultMiddlewareOptions,\n { rpcPrefix: defaultRPCOptions.rpcPrefix },\n initialOptions,\n ) as KoaMiddlewareOptions;\n\n // Hoist prefix regex (escaped) and the literal prefix-for-replace out of the\n // per-request handler to avoid regex injection and per-request compilation.\n const rpcPrefix = options.rpcPrefix;\n const prefixRegex = rpcPrefix\n ? new RegExp(`^/${escapeRegExp(rpcPrefix)}/`)\n : /* istanbul ignore next */ null;\n const prefixReplace = `/${rpcPrefix}/`;\n\n return createMiddleware({\n ...options,\n handler: async (ctx: Context, _next: Next) => {\n const reqUrl = safeURL(ctx.url);\n const url = reqUrl.pathname;\n // const { rpcPrefix } = options;\n\n // Defense-in-depth: validate prefix match via escaped regex even though\n // the outer createMiddleware gates on the same prefix already.\n // istanbul ignore next\n if (prefixRegex && !prefixRegex.test(url)) {\n return;\n }\n\n // Optional origin check: reject requests whose Origin header does not\n // match the configured origin. Requests without an Origin header\n // (curl, native clients) pass through unchecked.\n const origin = options.origin;\n const requestOrigin = ctx.headers.origin;\n if (origin && requestOrigin && requestOrigin !== origin) {\n ctx.status = 403;\n ctx.body = { error: REQUEST_FORBIDDEN };\n return;\n }\n\n const functionName = url.replace(prefixReplace, \"\");\n const serverFunction = serverFunctionsMap.get(functionName);\n\n if (!serverFunction) {\n ctx.status = 404;\n ctx.body = { error: FUNCTION_NOT_FOUND };\n return;\n }\n\n try {\n const method = serverFunction.options?.method || \"POST\";\n if (ctx.method.toUpperCase() !== method) {\n ctx.status = 405;\n ctx.body = { error: METHOD_NOT_ALLOWED };\n return;\n }\n\n let args: JsonValue[] = [];\n if (method === \"GET\") {\n const raw = reqUrl.searchParams.get(\"args\");\n if (raw) {\n const parsed: unknown = JSON.parse(raw);\n if (!Array.isArray(parsed)) {\n ctx.status = 400;\n ctx.body = { error: BAD_REQUEST };\n return;\n }\n args = parsed as JsonValue[];\n }\n } else {\n // Content-type enforcement: strict for json/text, lenient between forms.\n // Requests without a Content-Type header are exempt (curl/GET compat).\n // Checked BEFORE readBody so mismatched bodies are never buffered.\n if (\n hasContentTypeMismatch(\n serverFunction.options?.contentType ?? \"application/json\",\n ctx.headers[\"content-type\"],\n )\n ) {\n ctx.status = 415;\n ctx.body = { error: UNSUPPORTED_MEDIA_TYPE };\n return;\n }\n const body = await readBody(ctx);\n args = Array.isArray(body.data)\n ? body.data as JsonValue[]\n : [body.data as JsonValue];\n }\n const requestEvent: RequestEvent = {\n request: ctx.req,\n response: ctx,\n nativeEvent: ctx,\n locals: ctx.state,\n functionName,\n redirect: (location, status = 303) => {\n requestEvent.redirected = { location, status };\n koaRedirect(ctx, location, status);\n },\n send: (status, body, headers) => {\n requestEvent.sent = { status, body, headers };\n if (headers) {\n for (const [name, value] of Object.entries(headers)) {\n ctx.set(name, value);\n }\n }\n ctx.status = status;\n ctx.body = body;\n },\n };\n const { data: resultData, cancel } = provideRequestContext(\n requestEvent,\n () => serverFunction.handler(...args),\n );\n const onClose = () => cancel(CLIENT_DISCONNECTED);\n ctx.req.on(\"close\", onClose);\n const result = await resultData;\n ctx.req.off(\"close\", onClose);\n\n // Skip the JSON send when the server function issued a redirect or\n // short-circuited with `send`; the bound Koa adapter already set\n // ctx.status/ctx.body.\n // istanbul ignore else\n if (!requestEvent.redirected && !requestEvent.sent) {\n ctx.status = 200;\n ctx.body = { data: result };\n }\n } catch (err) {\n console.error(String(err));\n const isProduction = process.env.NODE_ENV === \"production\";\n ctx.status = 500;\n ctx.body = formatError(err, isProduction);\n }\n },\n });\n};\n"],"mappings":";;AAUA,MAAa,qBAAqB;AAElC,MAAa,qBAAqB;AAElC,MAAa,oBAAoB;AAEjC,MAAa,yBAAyB;AAEtC,MAAa,cAAc;AAI3B,MAAa,sBAAsB;;AAGnC,MAAa,wBAAwB,SACnC,wBAAwB,KAAK;;;ACd/B,MAAa,oBAAsC;CACjD,WAAW;CACX,SAAS;CACT,aAAa;CACb,UAAU,KAAA;AACZ;AAEA,MAAa,2BAA2B;CACtC,WAAW,KAAA;CACX,MAAM,KAAA;CACN,QAAQ,KAAA;AACV;;;;;;;;ACVA,eAAsB,UAAU,KAAU;CAIxC,MAAM,EAAE,kBAAkB,MAAM,OAAO;CAGvC,MAAM,EAAE,SAAS,UAAU,GAAG,YAAY,MADrB,cAAc;CAEnC,IAAI,IAAI,oBAAoB,OAAO,CAAC;AACtC;;;;;;;;AASA,SAAgB,WAAW,KAAU,MAA2B;CAC9D,IAAI,IAAI,OAAO,KAAiB,SAAS;EACvC,MAAM,MAAM,IAAI;EAChB,MAAM,MAAM,IAAI;EAGhB,MAAM,cAAc,IAAI,SAAS;EACjC,IAAI,gBAAgB,KAAA,GAClB,OAAO,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;EAG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,MAAM,OAAO,GAAG,CAAC;IACzB,MAAM,eAAe,QAAQ,IAAI,aAAa;IAC9C,IAAI,SAAS;IACb,IAAI,OAAO,YAAY,KAAK,YAAY;GAC1C;EACF;CACF,CAAC;AACH"}
@@ -3,15 +3,17 @@ import "@thednp/rpc";
3
3
  import "express";
4
4
  import "hono";
5
5
  import "@hono/node-server";
6
+ import "hono/utils/http-status";
6
7
  import "hono/factory";
7
8
  import "fastify";
8
9
  import "fastify-plugin";
9
10
  import "koa";
11
+ import "h3";
10
12
  //#region src/types.d.ts
11
13
  /**
12
14
  * Content types the RPC client modules send with each request.
13
15
  */
14
- type ContentType = "application/json" | "text/plain" | "multipart/form-data";
16
+ type ContentType = "application/json" | "text/plain" | "application/x-www-form-urlencoded" | "multipart/form-data";
15
17
  /**
16
18
  * Fetch `credentials` policy used by the generated client modules.
17
19
  */
@@ -140,7 +142,7 @@ interface RpcPluginOptions {
140
142
  * also compatible with the vite's Connect development server.
141
143
  * @default express
142
144
  */
143
- adapter: "express" | "hono" | "fastify" | "koa";
145
+ adapter: "express" | "hono" | "h3" | "fastify" | "koa";
144
146
  /**
145
147
  * Root directory from which the plugin scans for server files.
146
148
  * Defaults to `<root>/src/api`. Use this in monorepos where server files
@@ -225,6 +227,181 @@ declare class RPCError extends Error {
225
227
  * middleware's `console.error` logging.
226
228
  */
227
229
  declare const formatError: (err: unknown, isProduction: boolean) => JsonObject;
230
+ /**
231
+ * Checks whether a content type maps to a form encoding
232
+ * (`multipart/form-data` or `application/x-www-form-urlencoded`).
233
+ * Form-declared functions accept either encoding so native browser
234
+ * submissions (urlencoded) keep working without JavaScript.
235
+ */
236
+ declare const isFormContentType: (contentType: string) => boolean;
237
+ /**
238
+ * Detects whether an incoming request's `Content-Type` header conflicts
239
+ * with the function's declared content type. JSON and text functions are
240
+ * enforced strictly (exact match wins), while form functions accept both
241
+ * form encodings because the nojs fallback submits urlencoded forms to
242
+ * multipart-declared endpoints. Requests without a `Content-Type` header
243
+ * (curl, GET, legacy clients) are exempt from enforcement.
244
+ * @param declared - The declared `contentType` from the server function options
245
+ * @param rawHeader - The raw `Content-Type` request header, if present
246
+ */
247
+ declare const hasContentTypeMismatch: (declared: ContentType, rawHeader: string | undefined) => boolean;
248
+ /**
249
+ * Escapes special regex metacharacters in a string.
250
+ * Used to safely embed user-configurable values (like rpcPrefix) into regular expressions,
251
+ * preventing ReDoS and regex injection attacks.
252
+ * @param s - The raw string to escape
253
+ * @returns The escaped string safe for use in new RegExp()
254
+ */
255
+ declare function escapeRegExp(s: string): string;
256
+ /**
257
+ * Parses a raw request URL against a fixed base without ever throwing.
258
+ * Malformed request-targets (e.g. `/\`, `//`, `/\/`) make the WHATWG URL
259
+ * parser throw `TypeError: Invalid URL`; the adapters call this while
260
+ * building the per-request URL **before** their dispatch `try` block, so an
261
+ * unhandled rejection there crashes raw `node:http` hosts (and Express 4).
262
+ * On failure we fall back to the base root: the resulting pathname never
263
+ * matches the RPC prefix, so the request is treated as non-RPC and falls
264
+ * through to `next()` / 404 instead of crashing the process.
265
+ * @param rawUrl - Raw request URL (path + optional query string)
266
+ * @param base - Optional base URL, defaults to a fixed localhost origin
267
+ * @returns A URL object; never throws
268
+ */
269
+ declare const safeURL: (rawUrl: string, base?: string) => URL;
270
+ //#endregion
271
+ //#region src/context.d.ts
272
+ /**
273
+ * A per-request context established by the framework adapters around
274
+ * server-function dispatch, mirroring Solid Start's `FetchEvent`. Any code
275
+ * running in the async tree of a dispatch can read the current context through
276
+ * {@link getRequestContext} instead of threading `req`/`res` (or the framework
277
+ * `Context` object) through every nested call. This module is server-only and
278
+ * must never be imported by client code.
279
+ *
280
+ * Each adapter extends this with framework-specific request/response accessors:
281
+ * - Express: `req`/`res` plus `nativeEvent = { req, res }`
282
+ * - Fastify: `request`/`reply` plus `nativeEvent = request`
283
+ * - Koa: `ctx` plus `nativeEvent = ctx`
284
+ * - Hono: `c` (the Hono `Context`) plus `nativeEvent = c`
285
+ * - h3: `event` (the h3 `H3Event`) plus `nativeEvent = event`
286
+ */
287
+ interface RequestEvent {
288
+ /** Adapter-specific native event kept for deep framework access */
289
+ nativeEvent?: unknown;
290
+ /** Adapter request object */
291
+ request: unknown;
292
+ /** Adapter response object */
293
+ response: unknown;
294
+ /**
295
+ * Bound adapter-native redirect. Performing a redirect sets `redirected`
296
+ * so the middleware can skip the JSON `{ data }` send.
297
+ * @param location - The URL to redirect to
298
+ * @param status - HTTP status code, defaults to `303 See Other`
299
+ */
300
+ redirect: (location: string, status?: number) => void;
301
+ /**
302
+ * Set by `redirect` once a redirect has been issued. The middleware checks
303
+ * this after `await`ing the server function to avoid double-responding.
304
+ */
305
+ redirected?: {
306
+ location: string;
307
+ status: number;
308
+ };
309
+ /**
310
+ * Bound adapter-native response short-circuit. Writes the given status and
311
+ * JSON body (plus optional headers) directly, bypassing the standard
312
+ * `{ data }` response. Setting `sent` makes the middleware skip the JSON
313
+ * `{ data }` send, mirroring `redirect`/`redirected`.
314
+ * @param status - HTTP status code (e.g. 401, 413, 429)
315
+ * @param body - JSON-serializable response body
316
+ * @param headers - Optional response headers (e.g. `{ "Retry-After": "60" }`)
317
+ */
318
+ send: (status: number, body: JsonValue, headers?: Record<string, string>) => void;
319
+ /**
320
+ * Set by `send` once a response has been issued. The middleware checks this
321
+ * after `await`ing the server function to avoid double-responding.
322
+ */
323
+ sent?: {
324
+ status: number;
325
+ body: JsonValue;
326
+ headers?: Record<string, string>;
327
+ };
328
+ /**
329
+ * The matched RPC function name for the current request, when available.
330
+ * Useful for per-function rate limiting or auditing inside middleware.
331
+ */
332
+ functionName?: string;
333
+ /** Per-request app data shared across the async tree of the dispatch */
334
+ locals: Record<string, unknown>;
335
+ [prop: string]: unknown;
336
+ }
337
+ /**
338
+ * Runs `cb` with `init` as the current request context. Use inside the
339
+ * adapters around server-function dispatch (the async tree under `cb` can then
340
+ * read the context via {@link getRequestContext}).
341
+ * @param init - The request context for the duration of `cb`
342
+ * @param cb - The work that needs access to the request context
343
+ */
344
+ declare const provideRequestContext: <T>(init: RequestEvent, cb: () => T) => T;
345
+ /**
346
+ * Returns the current request context, or throws when called outside of a
347
+ * request (e.g. module scope or a background task).
348
+ * @throws When no request context is established
349
+ */
350
+ declare const getRequestContext: () => RequestEvent;
351
+ /**
352
+ * Redirects the current request to `location`. Reads the adapter-bound
353
+ * `redirect` from the current request context — callable from anywhere inside
354
+ * a server-function tree (no `res` threading needed).
355
+ * @param location - The URL to redirect to
356
+ * @param status - HTTP status code, defaults to `303 See Other`
357
+ * @throws When called outside of a request
358
+ */
359
+ declare const redirect: (location: string, status?: number) => void;
360
+ /**
361
+ * Sends a raw JSON response for the current request, bypassing the standard
362
+ * `{ data }` shape. Reads the adapter-bound `send` from the current request
363
+ * context — callable from anywhere inside a server-function tree. Any code in
364
+ * the async tree of a dispatch can call this (e.g. custom middleware) to
365
+ * short-circuit with a specific status code (401, 413, 429, ...).
366
+ * @param status - HTTP status code
367
+ * @param body - JSON-serializable response body
368
+ * @param headers - Optional response headers
369
+ * @throws When called outside of a request
370
+ */
371
+ declare const sendResponse: (status: number, body: JsonValue, headers?: Record<string, string>) => void;
372
+ /**
373
+ * Normalized, adapter-agnostic view of the current request. Reads the request
374
+ * object off the current request context and normalizes it across the five
375
+ * adapter request shapes (Express `req`, Fastify `req`, Koa `ctx.req`,
376
+ * Hono `c.req`, h3 `event.req`) so middleware can be written once.
377
+ */
378
+ interface RequestMeta {
379
+ /** HTTP method, upper-cased (e.g. "GET", "POST") */
380
+ method: string;
381
+ /** URL pathname (e.g. "/__rpc/greet") */
382
+ pathname: string;
383
+ /** Raw search string including the leading "?", or "" when absent */
384
+ search: string;
385
+ /** Parsed search params */
386
+ searchParams: URLSearchParams;
387
+ /** Request headers, lower-cased */
388
+ headers: Record<string, string | string[] | undefined>;
389
+ /** Host header value (e.g. "localhost:5173"), when present */
390
+ host?: string;
391
+ /** Client IP when the framework exposes it (e.g. Fastify `req.ip`) */
392
+ ip?: string;
393
+ /** Request protocol ("http" or "https"), when determinable */
394
+ protocol?: string;
395
+ }
396
+ /**
397
+ * Reads normalized, adapter-agnostic request metadata from the current request
398
+ * context. Works with Express `req`, Fastify `req`, Koa `ctx.req`,
399
+ * Hono `c.req` and h3 `event.req` by feature-detecting the request shape
400
+ * (`originalUrl`/`url`/`path`, raw `headers` map vs `Headers`-like API).
401
+ * @param event - The request context to read, typically the result of
402
+ * {@link getRequestContext}
403
+ */
404
+ declare const getRequestMeta: (event: RequestEvent) => RequestMeta;
228
405
  //#endregion
229
406
  //#region src/options.d.ts
230
407
  declare const defaultServerFnOptions: {
@@ -239,5 +416,5 @@ declare const defaultMiddlewareOptions: {
239
416
  origin: undefined;
240
417
  };
241
418
  //#endregion
242
- export { RPCError, createServerFunction, defaultMiddlewareOptions, defaultRPCOptions, defaultServerFnOptions, formatError, getClientModules, scanForServerFiles, scannedServerFiles, serverFunctionsMap, walkGlobFiles };
419
+ export { RPCError, RequestEvent, RequestMeta, createServerFunction, defaultMiddlewareOptions, defaultRPCOptions, defaultServerFnOptions, escapeRegExp, formatError, getClientModules, getRequestContext, getRequestMeta, hasContentTypeMismatch, isFormContentType, provideRequestContext, redirect, safeURL, scanForServerFiles, scannedServerFiles, sendResponse, serverFunctionsMap, walkGlobFiles };
243
420
  //# sourceMappingURL=server.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"server.d.mts","names":[],"sources":["../../src/types.d.ts","../../src/functionsMap.ts","../../src/scanForServerFiles.ts","../../src/createFunction.ts","../../src/getClientModules.ts","../../src/server-helpers.ts","../../src/options.ts"],"mappings":";;;;;;;;;;;;;KA2DY;;;;KAQA;;;;;UAcK;;;;;EAKf,aAAa;;;;;EAKb,cAAc;;;;;;;EAOd;;;;;;KAOU;;;;KAIA;GAAgB,cAAc,YAAY;;;;;KAI1C,aAAa,WAAW;;;;KAIxB,YAAY,gBAAgB,YAAY;;;;;KAsCxC,mBACV,cAAc,WAAW,YAAY,WACrC,gBAAgB,YAAY,cACzB,QAAQ,gBAAgB,MAAM,UAAU,QAAQ;;;;;;KAOzC,eACV,cAAc,YAAY,WAC1B,gBAAgB,YAAY,iBACtB,MAAM;;EAEZ,MAAM,QAAQ;;EAEd,SAAS;;;;;;KAOC,4BAA4B;;EAEtC;;EAEA,UAAU;;;;;UAMK;;EAEf;;EAEA;;;;;KAMU,aAAa,KAAK;EAC5B;EACA,SAAS,QAAQ;EACjB;EACA;;;;;;UAOe;;EAEf;;EAEA,SAAS;;EAET,UAAU;;EAEV;;;;;;;;UASe;;;;;;;;;;;EAWf;;;;;;;EAQA;;;;;;;EAQA;;;;;;;EAQA;;;;cCpQW,oBAAoB,YAAY;;;;cCehC,oBAAoB;;;;;;;;;;;;cAepB,qBAAkB,aAChB,YAAU,YACX,kBACX;;;;;;;;;;;;iBCda,qBACd,cAAc,YAAY,WAC1B,gBAAgB,YAAY,WAE5B,cACA,SAAS,mBAAmB,OAAO,UACnC,YAAW,QAAQ,yBAClB,eAAe,OAAO;;;;;;;;;;cC0DZ,mBAAgB,gBACX;;;;;;;cC1EL,gBAAa,gBAAwB;;;;;;cA4BrC,iBAAiB;;EAE5B;;EAEA,OAAO;EACP,YAAY,iBAAiB,eAAmB,OAAO;;;;;;;;;;cAgB5C,cAAW,cACV,0BAEX;;;cC3DU;;;;;cAMA,mBAAmB;cAOnB"}
1
+ {"version":3,"file":"server.d.mts","names":[],"sources":["../../src/types.d.ts","../../src/functionsMap.ts","../../src/scanForServerFiles.ts","../../src/createFunction.ts","../../src/getClientModules.ts","../../src/server-helpers.ts","../../src/context.ts","../../src/options.ts"],"mappings":";;;;;;;;;;;;;;;KAkEY;;;;KASA;;;;;UAkBK;;;;;EAKf,aAAa;;;;;EAKb,cAAc;;;;;;;EAOd;;;;;;KAOU;;;;KAIA;GAAgB,cAAc,YAAY;;;;;KAI1C,aAAa,WAAW;;;;KAIxB,YAAY,gBAAgB,YAAY;;;;;KAsCxC,mBACV,cAAc,WAAW,YAAY,WACrC,gBAAgB,YAAY,cACzB,QAAQ,gBAAgB,MAAM,UAAU,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