@reckona/mreact-router 0.0.152 → 0.0.153

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 (96) hide show
  1. package/README.md +1 -1
  2. package/dist/actions.d.ts +10 -0
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actions.js +6 -0
  5. package/dist/actions.js.map +1 -1
  6. package/dist/adapters/aws-lambda.d.ts +18 -0
  7. package/dist/adapters/aws-lambda.d.ts.map +1 -1
  8. package/dist/adapters/aws-lambda.js +18 -0
  9. package/dist/adapters/aws-lambda.js.map +1 -1
  10. package/dist/adapters/cloudflare.d.ts +33 -0
  11. package/dist/adapters/cloudflare.d.ts.map +1 -1
  12. package/dist/adapters/cloudflare.js +33 -0
  13. package/dist/adapters/cloudflare.js.map +1 -1
  14. package/dist/adapters/edge.d.ts +5 -0
  15. package/dist/adapters/edge.d.ts.map +1 -1
  16. package/dist/adapters/edge.js +5 -0
  17. package/dist/adapters/edge.js.map +1 -1
  18. package/dist/adapters/node.d.ts +5 -0
  19. package/dist/adapters/node.d.ts.map +1 -1
  20. package/dist/adapters/node.js +5 -0
  21. package/dist/adapters/node.js.map +1 -1
  22. package/dist/adapters/static.d.ts +5 -0
  23. package/dist/adapters/static.d.ts.map +1 -1
  24. package/dist/adapters/static.js +5 -0
  25. package/dist/adapters/static.js.map +1 -1
  26. package/dist/build.d.ts +15 -0
  27. package/dist/build.d.ts.map +1 -1
  28. package/dist/build.js +19 -0
  29. package/dist/build.js.map +1 -1
  30. package/dist/cache.d.ts +15 -0
  31. package/dist/cache.d.ts.map +1 -1
  32. package/dist/cache.js +15 -0
  33. package/dist/cache.js.map +1 -1
  34. package/dist/cookies.d.ts +16 -0
  35. package/dist/cookies.d.ts.map +1 -1
  36. package/dist/cookies.js +16 -0
  37. package/dist/cookies.js.map +1 -1
  38. package/dist/dev-server.d.ts +5 -0
  39. package/dist/dev-server.d.ts.map +1 -1
  40. package/dist/dev-server.js +5 -0
  41. package/dist/dev-server.js.map +1 -1
  42. package/dist/link.d.ts +10 -0
  43. package/dist/link.d.ts.map +1 -1
  44. package/dist/link.js +5 -0
  45. package/dist/link.js.map +1 -1
  46. package/dist/middleware.d.ts.map +1 -1
  47. package/dist/middleware.js +39 -4
  48. package/dist/middleware.js.map +1 -1
  49. package/dist/navigation.d.ts +53 -0
  50. package/dist/navigation.d.ts.map +1 -1
  51. package/dist/navigation.js +48 -0
  52. package/dist/navigation.js.map +1 -1
  53. package/dist/render.d.ts +28 -0
  54. package/dist/render.d.ts.map +1 -1
  55. package/dist/render.js +127 -27
  56. package/dist/render.js.map +1 -1
  57. package/dist/route-module-loader.d.ts +6 -1
  58. package/dist/route-module-loader.d.ts.map +1 -1
  59. package/dist/route-module-loader.js +8 -1
  60. package/dist/route-module-loader.js.map +1 -1
  61. package/dist/serve.d.ts +5 -0
  62. package/dist/serve.d.ts.map +1 -1
  63. package/dist/serve.js +5 -0
  64. package/dist/serve.js.map +1 -1
  65. package/dist/session.d.ts +19 -0
  66. package/dist/session.d.ts.map +1 -1
  67. package/dist/session.js +19 -0
  68. package/dist/session.js.map +1 -1
  69. package/dist/typed-routes.d.ts +5 -0
  70. package/dist/typed-routes.d.ts.map +1 -1
  71. package/dist/typed-routes.js +5 -0
  72. package/dist/typed-routes.js.map +1 -1
  73. package/dist/types.d.ts +15 -0
  74. package/dist/types.d.ts.map +1 -1
  75. package/dist/types.js +5 -0
  76. package/dist/types.js.map +1 -1
  77. package/package.json +11 -11
  78. package/src/actions.ts +18 -0
  79. package/src/adapters/aws-lambda.ts +18 -0
  80. package/src/adapters/cloudflare.ts +33 -0
  81. package/src/adapters/edge.ts +5 -0
  82. package/src/adapters/node.ts +5 -0
  83. package/src/adapters/static.ts +5 -0
  84. package/src/build.ts +19 -0
  85. package/src/cache.ts +15 -0
  86. package/src/cookies.ts +16 -0
  87. package/src/dev-server.ts +5 -0
  88. package/src/link.ts +10 -0
  89. package/src/middleware.ts +54 -4
  90. package/src/navigation.ts +53 -0
  91. package/src/render.ts +164 -25
  92. package/src/route-module-loader.ts +15 -0
  93. package/src/serve.ts +5 -0
  94. package/src/session.ts +19 -0
  95. package/src/typed-routes.ts +5 -0
  96. package/src/types.ts +15 -0
package/src/cache.ts CHANGED
@@ -56,6 +56,11 @@ const cacheState = ((
56
56
  });
57
57
  cacheState.storage ??= new AsyncLocalStorage<RouteCacheContext>();
58
58
 
59
+ /**
60
+ * Creates an in-memory route response cache for app-router rendering.
61
+ *
62
+ * The cache is process-local, evicts expired entries during reads/writes, and is suitable for development, tests, or single-process deployments that do not need shared invalidation.
63
+ */
59
64
  export function createMemoryRouteCache(options: MemoryRouteCacheOptions = {}): AppRouterCache {
60
65
  const maxEntries = positiveIntegerOrDefault(options.maxEntries, 10_000);
61
66
  const sweepIntervalMs = nonNegativeIntegerOrDefault(options.sweepIntervalMs, 60_000);
@@ -192,6 +197,11 @@ export function routeCachePolicyFromSource(code: string): RouteCachePolicy | und
192
197
  };
193
198
  }
194
199
 
200
+ /**
201
+ * Sets the cache policy for the current app-router render.
202
+ *
203
+ * Call this during a loader or route render to override the response `Cache-Control` policy; it throws outside an active app-router request.
204
+ */
195
205
  export function cacheControl(options: CacheControlOptions): void {
196
206
  const activeContext = activeRouteCacheContext();
197
207
 
@@ -326,6 +336,11 @@ function requestCarriesCredentials(request: Request | undefined): boolean {
326
336
  return request.headers.has("authorization") || request.headers.has("cookie");
327
337
  }
328
338
 
339
+ /**
340
+ * Invalidates cached route responses for a normalized app-router path.
341
+ *
342
+ * During a request the invalidation is scoped to that route cache context; outside a request it is queued for the next cache access.
343
+ */
329
344
  export function revalidatePath(path: string): void {
330
345
  const normalizedPath = normalizeRevalidationPath(path);
331
346
  const activeContext = activeRouteCacheContext();
package/src/cookies.ts CHANGED
@@ -22,6 +22,11 @@ function assertAttributeValue(value: string): void {
22
22
  }
23
23
  }
24
24
 
25
+ /**
26
+ * Parses a raw `Cookie` header into decoded name/value pairs.
27
+ *
28
+ * Malformed percent-encoded values are skipped so one bad cookie does not abort request handling.
29
+ */
25
30
  export function parseCookieHeader(
26
31
  cookieHeader: string | null | undefined,
27
32
  ): Map<string, string> {
@@ -50,6 +55,11 @@ export function parseCookieHeader(
50
55
  return values;
51
56
  }
52
57
 
58
+ /**
59
+ * Serializes a cookie name, value, and attributes for a `Set-Cookie` header.
60
+ *
61
+ * Cookie names and attribute values are validated, and `SameSite=None` requires `Secure`.
62
+ */
53
63
  export function serializeCookie(
54
64
  name: string,
55
65
  value: string,
@@ -91,6 +101,9 @@ export function serializeCookie(
91
101
  return parts.join("; ");
92
102
  }
93
103
 
104
+ /**
105
+ * Appends a serialized cookie to a response's `Set-Cookie` headers and returns the same response.
106
+ */
94
107
  export function setCookie(
95
108
  response: Response,
96
109
  name: string,
@@ -101,6 +114,9 @@ export function setCookie(
101
114
  return response;
102
115
  }
103
116
 
117
+ /**
118
+ * Appends an expiring `Set-Cookie` header that removes a cookie in the browser.
119
+ */
104
120
  export function deleteCookie(
105
121
  response: Response,
106
122
  name: string,
package/src/dev-server.ts CHANGED
@@ -33,6 +33,11 @@ export interface StartDevServerOptions extends AppRouterProjectOptions {
33
33
  onUpgrade?: HttpUpgradeHandler | undefined;
34
34
  }
35
35
 
36
+ /**
37
+ * Starts the local app-router development server with Vite middleware and route compilation.
38
+ *
39
+ * The server trusts only the configured host settings, merges declared application packages into the development import policy, and returns a `close()` method that should be awaited by tests and scripts.
40
+ */
36
41
  export async function startDevServer(
37
42
  options: StartDevServerOptions,
38
43
  ): Promise<{ close(): Promise<void>; server: Server; url: string }> {
package/src/link.ts CHANGED
@@ -39,6 +39,11 @@ export interface LinkProps<Href extends string = LinkHref> extends LinkOptions<H
39
39
  [attribute: string]: unknown;
40
40
  }
41
41
 
42
+ /**
43
+ * Converts router link options into anchor attributes consumed by the client navigation runtime.
44
+ *
45
+ * Use this when rendering a custom anchor component that should still opt into mreact prefetch, scroll, reload, or transition behavior.
46
+ */
42
47
  export function linkProps(options: LinkOptions<string>): Record<string, string> {
43
48
  return {
44
49
  href: options.href,
@@ -61,6 +66,11 @@ export type ConcreteLinkHrefGuard<Href extends string> = [RegisteredAppRoutePath
61
66
  ? { readonly __mreactRoutePatternHrefError__: never }
62
67
  : unknown;
63
68
 
69
+ /**
70
+ * Renders an app-router anchor with typed `href` support and navigation runtime attributes.
71
+ *
72
+ * In JSX it returns an anchor element compatible with the mreact runtime; during server streaming it can also write directly to an `HtmlSink`. Unsafe URL attribute values are dropped during rendering.
73
+ */
64
74
  export function Link<const Href extends LinkHref>(
65
75
  props: LinkProps<Href> & ConcreteLinkHrefGuard<Href>,
66
76
  ): ReactCompatElement;
package/src/middleware.ts CHANGED
@@ -110,28 +110,78 @@ export function middlewareMatches(config: MiddlewareModule["config"], pathname:
110
110
  }
111
111
 
112
112
  export function parseRouteMiddlewareControl(code: string): RouteMiddlewareControl | undefined {
113
- if (!/\bexport\s+const\s+middleware\s*=/.test(code)) {
113
+ const masked = maskCodeLiterals(code);
114
+ const declaration = /\bexport\s+const\s+middleware\s*=\s*\{/.exec(masked);
115
+
116
+ if (declaration === null) {
117
+ return undefined;
118
+ }
119
+
120
+ const objectStart = masked.indexOf("{", declaration.index);
121
+ const objectEnd = findMatchingBrace(masked, objectStart);
122
+
123
+ if (objectStart < 0 || objectEnd < 0) {
114
124
  return undefined;
115
125
  }
116
126
 
117
- if (/\bmiddleware\s*=\s*\{[\s\S]*?\bskip\s*:\s*true\b/.test(code)) {
127
+ const maskedBody = masked.slice(objectStart + 1, objectEnd);
128
+ const body = code.slice(objectStart + 1, objectEnd);
129
+
130
+ if (/\bskip\s*:\s*true\b/.test(maskedBody)) {
118
131
  return { skip: true };
119
132
  }
120
133
 
121
- const skipArray = /\bmiddleware\s*=\s*\{[\s\S]*?\bskip\s*:\s*\[([\s\S]*?)\]/.exec(code);
134
+ const skipArray = /\bskip\s*:\s*\[([\s\S]*?)\]/.exec(maskedBody);
122
135
 
123
136
  if (skipArray === null) {
124
137
  return undefined;
125
138
  }
126
139
 
140
+ const arrayStart = skipArray.index + skipArray[0].indexOf("[") + 1;
141
+ const arrayEnd = skipArray.index + skipArray[0].lastIndexOf("]");
142
+ const arrayBody = body.slice(arrayStart, arrayEnd);
127
143
  const ids = Array.from(
128
- skipArray[1]?.matchAll(/["']([^"']+)["']/g) ?? [],
144
+ arrayBody.matchAll(/["']([^"']+)["']/g),
129
145
  (match) => match[1],
130
146
  ).filter((id) => id !== undefined);
131
147
 
132
148
  return ids.length === 0 ? undefined : { skip: ids };
133
149
  }
134
150
 
151
+ function maskCodeLiterals(code: string): string {
152
+ return code.replace(
153
+ /\/\*[\s\S]*?\*\/|\/\/[^\n\r]*|"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|`(?:\\[\s\S]|[^`\\])*`/g,
154
+ (match) => " ".repeat(match.length),
155
+ );
156
+ }
157
+
158
+ function findMatchingBrace(code: string, openIndex: number): number {
159
+ if (openIndex < 0) {
160
+ return -1;
161
+ }
162
+
163
+ let depth = 0;
164
+
165
+ for (let index = openIndex; index < code.length; index += 1) {
166
+ const char = code[index];
167
+
168
+ if (char === "{") {
169
+ depth += 1;
170
+ continue;
171
+ }
172
+
173
+ if (char === "}") {
174
+ depth -= 1;
175
+
176
+ if (depth === 0) {
177
+ return index;
178
+ }
179
+ }
180
+ }
181
+
182
+ return -1;
183
+ }
184
+
135
185
  export function mergeRouteMiddlewareControls(
136
186
  controls: readonly (RouteMiddlewareControl | undefined)[],
137
187
  ): RouteMiddlewareControl | undefined {
package/src/navigation.ts CHANGED
@@ -61,6 +61,11 @@ function throwUnsafeRewrite(location: string): never {
61
61
  throw new TypeError(`unsafe rewrite target: ${JSON.stringify(location)}`);
62
62
  }
63
63
 
64
+ /**
65
+ * Throws an internal redirect for loaders, middleware, route handlers, or server actions.
66
+ *
67
+ * The target must be same-origin or relative; use `redirectExternal()` for trusted `http` or `https` destinations. The default status is `303`.
68
+ */
64
69
  export function redirect(location: string, options: RedirectOptions = {}): never {
65
70
  if (!isSafeInternalRedirect(location)) {
66
71
  throwUnsafeRedirect(location);
@@ -72,6 +77,11 @@ export function redirect(location: string, options: RedirectOptions = {}): never
72
77
  });
73
78
  }
74
79
 
80
+ /**
81
+ * Throws an external redirect for trusted `http` or `https` destinations.
82
+ *
83
+ * This helper is intentionally separate from `redirect()` so off-site navigation is explicit. The default status is `307`.
84
+ */
75
85
  export function redirectExternal(
76
86
  location: string,
77
87
  options: RedirectOptions = {},
@@ -86,6 +96,11 @@ export function redirectExternal(
86
96
  });
87
97
  }
88
98
 
99
+ /**
100
+ * Creates a same-origin `303 See Other` redirect response.
101
+ *
102
+ * Use this from route handlers when returning a `Response` is more convenient than throwing through the app-router control flow.
103
+ */
89
104
  export function redirect303(location: string, init: ResponseInit = {}): Response {
90
105
  if (!isSafeInternalRedirect(location)) {
91
106
  throwUnsafeRedirect(location);
@@ -101,6 +116,9 @@ export function redirect303(location: string, init: ResponseInit = {}): Response
101
116
  });
102
117
  }
103
118
 
119
+ /**
120
+ * Creates a plain-text error response with a default `text/plain` content type.
121
+ */
104
122
  export function textError(message: string, status = 400, init: ResponseInit = {}): Response {
105
123
  const headers = new Headers(init.headers);
106
124
 
@@ -115,6 +133,11 @@ export function textError(message: string, status = 400, init: ResponseInit = {}
115
133
  });
116
134
  }
117
135
 
136
+ /**
137
+ * Reads `request.formData()` and optionally validates it with a schema-like parser.
138
+ *
139
+ * Pass a schema object with `parse(FormData)` when a route handler or server action should receive typed form data.
140
+ */
118
141
  export async function parseForm(request: Request): Promise<FormData>;
119
142
  export async function parseForm<T>(request: Request, schema: ParseSchema<T>): Promise<T>;
120
143
  export async function parseForm<T>(
@@ -126,6 +149,11 @@ export async function parseForm<T>(
126
149
  return schema === undefined ? form : schema.parse(form);
127
150
  }
128
151
 
152
+ /**
153
+ * Throws a route-level 404 control error.
154
+ *
155
+ * Use this from loaders, metadata, middleware, or route handlers when the matched route is valid but the requested resource is absent.
156
+ */
129
157
  export function notFound(): never {
130
158
  throw Object.assign(new Error("Not Found"), {
131
159
  name: notFoundErrorName,
@@ -133,14 +161,25 @@ export function notFound(): never {
133
161
  });
134
162
  }
135
163
 
164
+ /**
165
+ * Alias for `notFound()` for codebases that prefer an explicit throwing helper name.
166
+ */
136
167
  export function throwNotFound(): never {
137
168
  return notFound();
138
169
  }
139
170
 
171
+ /**
172
+ * Continues middleware processing without changing the request.
173
+ */
140
174
  export function next(): MiddlewareNext {
141
175
  return undefined;
142
176
  }
143
177
 
178
+ /**
179
+ * Creates an internal rewrite response consumed by app-router middleware.
180
+ *
181
+ * The target must be a same-origin route path, query, hash, or relative URL; protocol-relative, external, and control-character targets are rejected.
182
+ */
144
183
  export function rewrite(location: string, init: ResponseInit = {}): Response {
145
184
  if (!isSafeInternalRedirect(location)) {
146
185
  throwUnsafeRewrite(location);
@@ -160,10 +199,16 @@ export function rewrite(location: string, init: ResponseInit = {}): Response {
160
199
  return response;
161
200
  }
162
201
 
202
+ /**
203
+ * Creates a JSON response using the platform `Response.json()` implementation.
204
+ */
163
205
  export function json(value: unknown, init?: ResponseInit): Response {
164
206
  return Response.json(value, init);
165
207
  }
166
208
 
209
+ /**
210
+ * Creates an HTML response and defaults the content type to `text/html; charset=utf-8`.
211
+ */
167
212
  export function html(value: string, init: ResponseInit = {}): Response {
168
213
  if (init.headers === undefined) {
169
214
  return new Response(value, {
@@ -184,6 +229,9 @@ export function html(value: string, init: ResponseInit = {}): Response {
184
229
  });
185
230
  }
186
231
 
232
+ /**
233
+ * Returns the request headers object for route code that follows app-router helper naming.
234
+ */
187
235
  export function headers(request: Request): Headers {
188
236
  return request.headers;
189
237
  }
@@ -194,6 +242,11 @@ export interface RequestCookies {
194
242
  has(name: string): boolean;
195
243
  }
196
244
 
245
+ /**
246
+ * Parses request cookies into a small read-only helper.
247
+ *
248
+ * Values are decoded with `decodeURIComponent`; malformed encoded values are ignored rather than throwing during request handling.
249
+ */
197
250
  export function cookies(request: Request): RequestCookies {
198
251
  let values: ReadonlyMap<string, string> | undefined;
199
252
  const cookieValues = () => {