devflare 1.0.0-next.1 → 1.0.0-next.10

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 (123) hide show
  1. package/LLM.md +775 -637
  2. package/R2.md +200 -0
  3. package/README.md +285 -514
  4. package/bin/devflare.js +8 -8
  5. package/dist/{account-rvrj687w.js → account-8psavtg6.js} +27 -4
  6. package/dist/bridge/miniflare.d.ts +6 -0
  7. package/dist/bridge/miniflare.d.ts.map +1 -1
  8. package/dist/bridge/proxy.d.ts +5 -6
  9. package/dist/bridge/proxy.d.ts.map +1 -1
  10. package/dist/bridge/server.d.ts.map +1 -1
  11. package/dist/browser.d.ts +50 -0
  12. package/dist/browser.d.ts.map +1 -0
  13. package/dist/{build-mnf6v8gd.js → build-k36xrzvy.js} +26 -7
  14. package/dist/bundler/do-bundler.d.ts +7 -0
  15. package/dist/bundler/do-bundler.d.ts.map +1 -1
  16. package/dist/cli/commands/account.d.ts.map +1 -1
  17. package/dist/cli/commands/build.d.ts.map +1 -1
  18. package/dist/cli/commands/deploy.d.ts.map +1 -1
  19. package/dist/cli/commands/dev.d.ts.map +1 -1
  20. package/dist/cli/commands/doctor.d.ts.map +1 -1
  21. package/dist/cli/commands/init.d.ts.map +1 -1
  22. package/dist/cli/commands/types.d.ts.map +1 -1
  23. package/dist/cli/config-path.d.ts +5 -0
  24. package/dist/cli/config-path.d.ts.map +1 -0
  25. package/dist/cli/index.d.ts.map +1 -1
  26. package/dist/cli/package-metadata.d.ts +16 -0
  27. package/dist/cli/package-metadata.d.ts.map +1 -0
  28. package/dist/config/compiler.d.ts +7 -0
  29. package/dist/config/compiler.d.ts.map +1 -1
  30. package/dist/config/index.d.ts +1 -1
  31. package/dist/config/index.d.ts.map +1 -1
  32. package/dist/config/schema.d.ts +2575 -1221
  33. package/dist/config/schema.d.ts.map +1 -1
  34. package/dist/{deploy-nhceck39.js → deploy-dbvfq8vq.js} +33 -15
  35. package/dist/{dev-qnxet3j9.js → dev-rk8p6pse.js} +900 -234
  36. package/dist/dev-server/miniflare-log.d.ts +12 -0
  37. package/dist/dev-server/miniflare-log.d.ts.map +1 -0
  38. package/dist/dev-server/runtime-stdio.d.ts +8 -0
  39. package/dist/dev-server/runtime-stdio.d.ts.map +1 -0
  40. package/dist/dev-server/server.d.ts +2 -0
  41. package/dist/dev-server/server.d.ts.map +1 -1
  42. package/dist/dev-server/vite-utils.d.ts +37 -0
  43. package/dist/dev-server/vite-utils.d.ts.map +1 -0
  44. package/dist/{doctor-e8fy6fj5.js → doctor-06y8nxd4.js} +73 -50
  45. package/dist/{durable-object-t4kbb0yt.js → durable-object-yt8v1dyn.js} +1 -1
  46. package/dist/index-05fyzwne.js +195 -0
  47. package/dist/index-1p814k7s.js +227 -0
  48. package/dist/{index-hcex3rgh.js → index-1phx14av.js} +84 -7
  49. package/dist/{index-tk6ej9dj.js → index-2q3pmzrx.js} +12 -16
  50. package/dist/{index-pf5s73n9.js → index-59df49vn.js} +11 -281
  51. package/dist/index-5yxg30va.js +304 -0
  52. package/dist/index-62b3gt2g.js +12 -0
  53. package/dist/index-6h8xbs75.js +44 -0
  54. package/dist/{index-67qcae0f.js → index-6v3wjg1r.js} +16 -1
  55. package/dist/index-8gtqgb3q.js +529 -0
  56. package/dist/{index-gz1gndna.js → index-9wt9x09k.js} +42 -62
  57. package/dist/index-fef08w43.js +231 -0
  58. package/dist/{index-ep3445yc.js → index-jht2j546.js} +393 -170
  59. package/dist/index-k7r18na8.js +0 -0
  60. package/dist/{index-m2q41jwa.js → index-n932ytmq.js} +9 -1
  61. package/dist/index-pwgyy2q9.js +39 -0
  62. package/dist/{index-07q6yxyc.js → index-v8vvsn9x.js} +1 -0
  63. package/dist/index-vky23txa.js +70 -0
  64. package/dist/index-vs49yxn4.js +322 -0
  65. package/dist/{index-z14anrqp.js → index-wfbfz02q.js} +14 -15
  66. package/dist/index-ws68xvq2.js +311 -0
  67. package/dist/index-y1d8za14.js +196 -0
  68. package/dist/{init-f9mgmew3.js → init-na2atvz2.js} +42 -55
  69. package/dist/router/types.d.ts +24 -0
  70. package/dist/router/types.d.ts.map +1 -0
  71. package/dist/runtime/context.d.ts +249 -8
  72. package/dist/runtime/context.d.ts.map +1 -1
  73. package/dist/runtime/exports.d.ts +50 -55
  74. package/dist/runtime/exports.d.ts.map +1 -1
  75. package/dist/runtime/index.d.ts +8 -1
  76. package/dist/runtime/index.d.ts.map +1 -1
  77. package/dist/runtime/middleware.d.ts +77 -60
  78. package/dist/runtime/middleware.d.ts.map +1 -1
  79. package/dist/runtime/router.d.ts +7 -0
  80. package/dist/runtime/router.d.ts.map +1 -0
  81. package/dist/runtime/validation.d.ts +1 -1
  82. package/dist/runtime/validation.d.ts.map +1 -1
  83. package/dist/src/browser.js +150 -0
  84. package/dist/src/cli/index.js +10 -0
  85. package/dist/{cloudflare → src/cloudflare}/index.js +3 -3
  86. package/dist/{decorators → src/decorators}/index.js +2 -2
  87. package/dist/src/index.js +132 -0
  88. package/dist/src/runtime/index.js +111 -0
  89. package/dist/{sveltekit → src/sveltekit}/index.js +14 -6
  90. package/dist/{test → src/test}/index.js +22 -13
  91. package/dist/{vite → src/vite}/index.js +128 -59
  92. package/dist/sveltekit/platform.d.ts.map +1 -1
  93. package/dist/test/bridge-context.d.ts +5 -2
  94. package/dist/test/bridge-context.d.ts.map +1 -1
  95. package/dist/test/cf.d.ts +25 -11
  96. package/dist/test/cf.d.ts.map +1 -1
  97. package/dist/test/email.d.ts +16 -7
  98. package/dist/test/email.d.ts.map +1 -1
  99. package/dist/test/queue.d.ts.map +1 -1
  100. package/dist/test/resolve-service-bindings.d.ts.map +1 -1
  101. package/dist/test/scheduled.d.ts.map +1 -1
  102. package/dist/test/simple-context.d.ts +1 -1
  103. package/dist/test/simple-context.d.ts.map +1 -1
  104. package/dist/test/tail.d.ts +2 -1
  105. package/dist/test/tail.d.ts.map +1 -1
  106. package/dist/test/worker.d.ts +6 -0
  107. package/dist/test/worker.d.ts.map +1 -1
  108. package/dist/transform/durable-object.d.ts.map +1 -1
  109. package/dist/transform/worker-entrypoint.d.ts.map +1 -1
  110. package/dist/{types-5nyrz1sz.js → types-x9q7t491.js} +30 -16
  111. package/dist/utils/entrypoint-discovery.d.ts +6 -3
  112. package/dist/utils/entrypoint-discovery.d.ts.map +1 -1
  113. package/dist/utils/send-email.d.ts +15 -0
  114. package/dist/utils/send-email.d.ts.map +1 -0
  115. package/dist/vite/plugin.d.ts.map +1 -1
  116. package/dist/worker-entry/composed-worker.d.ts +13 -0
  117. package/dist/worker-entry/composed-worker.d.ts.map +1 -0
  118. package/dist/worker-entry/routes.d.ts +22 -0
  119. package/dist/worker-entry/routes.d.ts.map +1 -0
  120. package/dist/{worker-entrypoint-m9th0rg0.js → worker-entrypoint-c259fmfs.js} +1 -1
  121. package/package.json +21 -19
  122. package/dist/index.js +0 -298
  123. package/dist/runtime/index.js +0 -111
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  normalizeDOBinding
3
- } from "./index-hcex3rgh.js";
3
+ } from "./index-1phx14av.js";
4
4
  import {
5
5
  __require
6
6
  } from "./index-37x76zdn.js";
@@ -133,6 +133,7 @@ function compileBindings(bindings, result) {
133
133
  result.services = Object.entries(bindings.services).map(([binding, config]) => ({
134
134
  binding,
135
135
  service: config.service,
136
+ ...config.entrypoint && { entrypoint: config.entrypoint },
136
137
  ...config.environment && { environment: config.environment }
137
138
  }));
138
139
  }
@@ -160,6 +161,20 @@ function compileBindings(bindings, result) {
160
161
  dataset: config.dataset
161
162
  }));
162
163
  }
164
+ if (bindings.sendEmail) {
165
+ result.send_email = Object.entries(bindings.sendEmail).map(([name, config]) => ({
166
+ name,
167
+ ...config.destinationAddress && {
168
+ destination_address: config.destinationAddress
169
+ },
170
+ ...config.allowedDestinationAddresses && {
171
+ allowed_destination_addresses: config.allowedDestinationAddresses
172
+ },
173
+ ...config.allowedSenderAddresses && {
174
+ allowed_sender_addresses: config.allowedSenderAddresses
175
+ }
176
+ }));
177
+ }
163
178
  }
164
179
  function stringifyConfig(config) {
165
180
  const header = `// Generated by devflare — Do not edit directly
@@ -0,0 +1,529 @@
1
+ import {
2
+ createFetchEvent,
3
+ getContextOrNull,
4
+ runWithEventContext
5
+ } from "./index-5yxg30va.js";
6
+
7
+ // src/runtime/validation.ts
8
+ class ContextAccessError extends Error {
9
+ contextName;
10
+ propertyName;
11
+ constructor(contextName, propertyName) {
12
+ super(`Cannot access ${contextName}.${propertyName} outside of an active Devflare handler trail.
13
+
14
+ ` + `This typically happens when:
15
+ ` + ` 1. Accessing ${contextName} at module top-level (during import)
16
+ ` + ` 2. Accessing ${contextName} in a callback that runs after the handler ends
17
+ ` + ` 3. Accessing ${contextName} in a setTimeout/setInterval callback
18
+
19
+ ` + `Move the access inside your handler function or middleware.`);
20
+ this.name = "ContextAccessError";
21
+ this.contextName = contextName;
22
+ this.propertyName = propertyName;
23
+ }
24
+ }
25
+ function createContextProxy(getter, name) {
26
+ return new Proxy({}, {
27
+ get(_target, prop) {
28
+ const ctx = getter();
29
+ if (ctx === undefined) {
30
+ throw new ContextAccessError(name, String(prop));
31
+ }
32
+ return ctx[prop];
33
+ },
34
+ set(_target, prop, value) {
35
+ const ctx = getter();
36
+ if (ctx === undefined) {
37
+ throw new ContextAccessError(name, String(prop));
38
+ }
39
+ ctx[prop] = value;
40
+ return true;
41
+ },
42
+ has(_target, prop) {
43
+ const ctx = getter();
44
+ if (ctx === undefined) {
45
+ return false;
46
+ }
47
+ return prop in ctx;
48
+ },
49
+ ownKeys(_target) {
50
+ const ctx = getter();
51
+ if (ctx === undefined) {
52
+ return [];
53
+ }
54
+ return Reflect.ownKeys(ctx);
55
+ },
56
+ getOwnPropertyDescriptor(_target, prop) {
57
+ const ctx = getter();
58
+ if (ctx === undefined) {
59
+ return;
60
+ }
61
+ return Reflect.getOwnPropertyDescriptor(ctx, prop);
62
+ }
63
+ });
64
+ }
65
+
66
+ // src/runtime/exports.ts
67
+ function createReadonlyProxy(getter, name) {
68
+ return new Proxy({}, {
69
+ get(_target, prop) {
70
+ const ctx = getter();
71
+ if (ctx === undefined || ctx === null) {
72
+ throw new ContextAccessError(name, String(prop));
73
+ }
74
+ return ctx[prop];
75
+ },
76
+ set(_target, prop) {
77
+ throw new TypeError(`Cannot assign to '${String(prop)}' on '${name}' because it is read-only.
78
+ ` + `Use 'locals' for mutable request-scoped data.`);
79
+ },
80
+ deleteProperty(_target, prop) {
81
+ throw new TypeError(`Cannot delete property '${String(prop)}' from '${name}' because it is read-only.`);
82
+ },
83
+ has(_target, prop) {
84
+ const ctx = getter();
85
+ if (ctx === undefined || ctx === null) {
86
+ return false;
87
+ }
88
+ return prop in ctx;
89
+ },
90
+ ownKeys(_target) {
91
+ const ctx = getter();
92
+ if (ctx === undefined || ctx === null) {
93
+ return [];
94
+ }
95
+ return Reflect.ownKeys(ctx);
96
+ },
97
+ getOwnPropertyDescriptor(_target, prop) {
98
+ const ctx = getter();
99
+ if (ctx === undefined || ctx === null) {
100
+ return;
101
+ }
102
+ const descriptor = Reflect.getOwnPropertyDescriptor(ctx, prop);
103
+ if (descriptor) {
104
+ return { ...descriptor, writable: false };
105
+ }
106
+ return;
107
+ }
108
+ });
109
+ }
110
+ var env = createReadonlyProxy(() => getContextOrNull()?.env, "env");
111
+ var ctx = createReadonlyProxy(() => getContextOrNull()?.ctx, "ctx");
112
+ var event = createReadonlyProxy(() => getContextOrNull()?.event, "event");
113
+ var locals = createContextProxy(() => getContextOrNull()?.locals, "locals");
114
+ // src/runtime/middleware.ts
115
+ var FETCH_SEQUENCE_SYMBOL = Symbol.for("devflare.fetch-sequence");
116
+ var FETCH_INVOCATION_MODE_SYMBOL = Symbol.for("devflare.fetch-invocation-mode");
117
+ function createNotFoundResponse() {
118
+ return new Response("Not Found", { status: 404 });
119
+ }
120
+ function isFunction(value) {
121
+ return typeof value === "function";
122
+ }
123
+ function markFetchInvocationMode(handler, mode) {
124
+ Object.defineProperty(handler, FETCH_INVOCATION_MODE_SYMBOL, {
125
+ value: mode,
126
+ enumerable: false,
127
+ configurable: true,
128
+ writable: false
129
+ });
130
+ return handler;
131
+ }
132
+ function getFetchInvocationMode(handler) {
133
+ return handler[FETCH_INVOCATION_MODE_SYMBOL] ?? null;
134
+ }
135
+ function splitParameterList(source) {
136
+ const parameters = [];
137
+ let current = "";
138
+ let depth = 0;
139
+ for (const char of source) {
140
+ if (char === "," && depth === 0) {
141
+ if (current.trim()) {
142
+ parameters.push(current.trim());
143
+ }
144
+ current = "";
145
+ continue;
146
+ }
147
+ if (char === "(" || char === "[" || char === "{" || char === "<") {
148
+ depth += 1;
149
+ } else if (char === ")" || char === "]" || char === "}" || char === ">") {
150
+ depth = Math.max(0, depth - 1);
151
+ }
152
+ current += char;
153
+ }
154
+ if (current.trim()) {
155
+ parameters.push(current.trim());
156
+ }
157
+ return parameters;
158
+ }
159
+ function getFunctionParameterNames(handler) {
160
+ const source = handler.toString().trim();
161
+ const parenthesizedMatch = source.match(/^[^(]*\(([^)]*)\)/);
162
+ if (parenthesizedMatch) {
163
+ return splitParameterList(parenthesizedMatch[1]);
164
+ }
165
+ const singleParameterArrowMatch = source.match(/^(?:async\s+)?([^=()\s]+)\s*=>/);
166
+ if (singleParameterArrowMatch) {
167
+ return [singleParameterArrowMatch[1].trim()];
168
+ }
169
+ return [];
170
+ }
171
+ function isResolveStyleFunction(handler) {
172
+ const mode = getFetchInvocationMode(handler);
173
+ if (mode) {
174
+ return mode === "resolve";
175
+ }
176
+ if (handler[FETCH_SEQUENCE_SYMBOL]) {
177
+ return true;
178
+ }
179
+ if (handler.length !== 2) {
180
+ return false;
181
+ }
182
+ const parameterNames = getFunctionParameterNames(handler);
183
+ const secondParameter = parameterNames[1]?.trim().toLowerCase() ?? "";
184
+ return secondParameter === "resolve" || secondParameter.endsWith("resolve");
185
+ }
186
+ function bindMethod(target, key) {
187
+ if (!target || typeof target !== "object") {
188
+ return null;
189
+ }
190
+ const value = target[key];
191
+ if (!isFunction(value)) {
192
+ return null;
193
+ }
194
+ const boundHandler = value.bind(target);
195
+ if (isResolveStyleFunction(value)) {
196
+ return markFetchInvocationMode(boundHandler, "resolve");
197
+ }
198
+ return boundHandler;
199
+ }
200
+ function createLegacySequence(middlewares) {
201
+ return (handler) => {
202
+ if (middlewares.length === 0) {
203
+ return async () => {
204
+ const response = await handler();
205
+ return response ?? createNotFoundResponse();
206
+ };
207
+ }
208
+ return async () => {
209
+ let index = 0;
210
+ const executeMiddleware = async () => {
211
+ if (index < middlewares.length) {
212
+ const middleware = middlewares[index++];
213
+ return middleware(executeMiddleware);
214
+ }
215
+ const response = await handler();
216
+ return response ?? createNotFoundResponse();
217
+ };
218
+ return executeMiddleware();
219
+ };
220
+ };
221
+ }
222
+ function createFetchSequence(middlewares) {
223
+ return async (event2, resolve = async () => createNotFoundResponse()) => {
224
+ const executeMiddleware = async (index, activeEvent) => {
225
+ if (index >= middlewares.length) {
226
+ return resolve(activeEvent);
227
+ }
228
+ const middleware = middlewares[index];
229
+ return middleware(activeEvent, async (nextEvent = activeEvent) => {
230
+ return executeMiddleware(index + 1, nextEvent);
231
+ });
232
+ };
233
+ return executeMiddleware(0, event2);
234
+ };
235
+ }
236
+ function sequence(...middlewares) {
237
+ const legacySequence = createLegacySequence(middlewares);
238
+ const fetchSequence = createFetchSequence(middlewares);
239
+ const composed = (...args) => {
240
+ if (args.length === 1 && isFunction(args[0])) {
241
+ return legacySequence(args[0]);
242
+ }
243
+ return fetchSequence(args[0], args[1] ?? (async () => createNotFoundResponse()));
244
+ };
245
+ Object.defineProperty(composed, FETCH_SEQUENCE_SYMBOL, {
246
+ value: true,
247
+ enumerable: false,
248
+ configurable: false,
249
+ writable: false
250
+ });
251
+ return composed;
252
+ }
253
+ function handle(...handlers) {
254
+ return async () => {
255
+ for (const handler of handlers) {
256
+ const response = await handler();
257
+ if (response !== null) {
258
+ return response;
259
+ }
260
+ }
261
+ return null;
262
+ };
263
+ }
264
+ function resolve(...handlers) {
265
+ return handle(...handlers);
266
+ }
267
+ function pipe(middlewares, handlers) {
268
+ return createLegacySequence(middlewares)(handle(...handlers));
269
+ }
270
+ function getDefaultHandleHandler(module) {
271
+ return bindMethod(module.default, "handle");
272
+ }
273
+ function getDefaultFetchHandler(module) {
274
+ const defaultExport = module.default;
275
+ if (isFunction(defaultExport)) {
276
+ return defaultExport;
277
+ }
278
+ return bindMethod(defaultExport, "fetch");
279
+ }
280
+ function getPrimaryFetchEntryCandidates(module) {
281
+ const candidates = [];
282
+ const namedHandle = isFunction(module.handle) ? module.handle : null;
283
+ if (namedHandle) {
284
+ candidates.push({
285
+ name: "handle",
286
+ handler: namedHandle
287
+ });
288
+ }
289
+ const namedFetch = isFunction(module.fetch) ? module.fetch : null;
290
+ if (namedFetch) {
291
+ candidates.push({
292
+ name: "fetch",
293
+ handler: namedFetch
294
+ });
295
+ }
296
+ const defaultHandle = getDefaultHandleHandler(module);
297
+ if (defaultHandle) {
298
+ candidates.push({
299
+ name: "default.handle",
300
+ handler: defaultHandle
301
+ });
302
+ }
303
+ const defaultFetch = getDefaultFetchHandler(module);
304
+ if (defaultFetch) {
305
+ candidates.push({
306
+ name: isFunction(module.default) ? "default" : "default.fetch",
307
+ handler: defaultFetch
308
+ });
309
+ }
310
+ return candidates;
311
+ }
312
+ function assertSinglePrimaryFetchEntry(candidates) {
313
+ if (candidates.length <= 1) {
314
+ return;
315
+ }
316
+ const foundEntries = candidates.map(({ name }) => `"${name}"`).join(", ");
317
+ throw new Error(`Ambiguous fetch entry module. Export exactly one primary fetch entry per module. ` + `Use either "fetch" or "handle" (or one default equivalent), not both. ` + `Found: ${foundEntries}`);
318
+ }
319
+ function resolveMethodHandler(module, method) {
320
+ const normalizedMethod = method.toUpperCase();
321
+ const directHandler = isFunction(module[normalizedMethod]) ? module[normalizedMethod] : bindMethod(module.default, normalizedMethod);
322
+ if (directHandler) {
323
+ return {
324
+ handler: directHandler,
325
+ stripBody: false
326
+ };
327
+ }
328
+ if (normalizedMethod === "HEAD") {
329
+ const getHandler = isFunction(module.GET) ? module.GET : bindMethod(module.default, "GET");
330
+ if (getHandler) {
331
+ return {
332
+ handler: getHandler,
333
+ stripBody: true
334
+ };
335
+ }
336
+ }
337
+ const allHandler = isFunction(module.ALL) ? module.ALL : bindMethod(module.default, "ALL");
338
+ if (allHandler) {
339
+ return {
340
+ handler: allHandler,
341
+ stripBody: false
342
+ };
343
+ }
344
+ return null;
345
+ }
346
+ async function invokeResolvedFetchHandler(handler, event2) {
347
+ if (isResolveStyleFunction(handler)) {
348
+ return handler(event2, async () => createNotFoundResponse());
349
+ }
350
+ if (handler.length >= 4) {
351
+ return handler(event2, event2.env, event2.ctx, event2.params);
352
+ }
353
+ if (handler.length === 3) {
354
+ return handler(event2, event2.env, event2.ctx);
355
+ }
356
+ if (handler.length === 2) {
357
+ return handler(event2, event2.params);
358
+ }
359
+ if (handler.length === 0) {
360
+ return handler();
361
+ }
362
+ return handler(event2);
363
+ }
364
+ function resolveFetchHandler(module) {
365
+ const candidates = getPrimaryFetchEntryCandidates(module);
366
+ assertSinglePrimaryFetchEntry(candidates);
367
+ return candidates[0]?.handler ?? null;
368
+ }
369
+ async function invokeFetchHandler(handler, event2, resolve2 = async () => createNotFoundResponse()) {
370
+ if (!isFunction(handler)) {
371
+ return resolve2(event2);
372
+ }
373
+ if (isResolveStyleFunction(handler)) {
374
+ const response2 = await handler(event2, resolve2);
375
+ return response2 ?? createNotFoundResponse();
376
+ }
377
+ if (handler.length >= 4) {
378
+ const response2 = await handler(event2, event2.env, event2.ctx, event2.params);
379
+ return response2 ?? createNotFoundResponse();
380
+ }
381
+ if (handler.length === 3) {
382
+ const response2 = await handler(event2, event2.env, event2.ctx);
383
+ return response2 ?? createNotFoundResponse();
384
+ }
385
+ if (handler.length === 2) {
386
+ const response2 = await handler(event2, event2.env);
387
+ return response2 ?? createNotFoundResponse();
388
+ }
389
+ if (handler.length === 0) {
390
+ const response2 = await handler();
391
+ return response2 ?? createNotFoundResponse();
392
+ }
393
+ const response = await handler(event2);
394
+ return response ?? createNotFoundResponse();
395
+ }
396
+ function createResolveFetch(module, _currentEntry, initialEvent, options = {}) {
397
+ return async (nextEvent = initialEvent) => {
398
+ return runWithEventContext(nextEvent, async () => {
399
+ const methodResolution = resolveMethodHandler(module, nextEvent.request.method);
400
+ if (methodResolution) {
401
+ const response = await invokeResolvedFetchHandler(methodResolution.handler, nextEvent);
402
+ const finalResponse = response ?? createNotFoundResponse();
403
+ if (methodResolution.stripBody) {
404
+ return new Response(null, finalResponse);
405
+ }
406
+ return finalResponse;
407
+ }
408
+ if (options.fallbackResolve) {
409
+ return options.fallbackResolve(nextEvent);
410
+ }
411
+ return createNotFoundResponse();
412
+ });
413
+ };
414
+ }
415
+ async function invokeFetchModule(module, event2, fallbackResolve) {
416
+ const handler = resolveFetchHandler(module);
417
+ if (!handler) {
418
+ return createResolveFetch(module, null, event2, { fallbackResolve })(event2);
419
+ }
420
+ return invokeFetchHandler(handler, event2, createResolveFetch(module, handler, event2, { fallbackResolve }));
421
+ }
422
+ // src/runtime/router.ts
423
+ function normalizePathname(pathname) {
424
+ if (!pathname || pathname === "/") {
425
+ return "/";
426
+ }
427
+ const normalized = pathname.startsWith("/") ? pathname : `/${pathname}`;
428
+ const trimmed = normalized.replace(/\/+$|\/+$/g, "");
429
+ return trimmed === "" ? "/" : trimmed;
430
+ }
431
+ function decodePathSegment(segment) {
432
+ try {
433
+ return decodeURIComponent(segment);
434
+ } catch {
435
+ return segment;
436
+ }
437
+ }
438
+ function getPathSegments(pathname) {
439
+ const normalizedPathname = normalizePathname(pathname);
440
+ if (normalizedPathname === "/") {
441
+ return [];
442
+ }
443
+ return normalizedPathname.slice(1).split("/").filter(Boolean).map(decodePathSegment);
444
+ }
445
+ function getMatchPathname(input) {
446
+ if (input instanceof Request) {
447
+ return new URL(input.url).pathname;
448
+ }
449
+ if (input instanceof URL) {
450
+ return input.pathname;
451
+ }
452
+ if (input.includes("://")) {
453
+ return new URL(input).pathname;
454
+ }
455
+ return input;
456
+ }
457
+ function matchRouteSegments(routeSegments, pathnameSegments) {
458
+ if (routeSegments.length === 0) {
459
+ return pathnameSegments.length === 0 ? {} : null;
460
+ }
461
+ const params = {};
462
+ let routeIndex = 0;
463
+ let pathIndex = 0;
464
+ while (routeIndex < routeSegments.length) {
465
+ const routeSegment = routeSegments[routeIndex];
466
+ if (routeSegment.type === "optional-rest") {
467
+ params[routeSegment.name] = pathnameSegments.slice(pathIndex).join("/");
468
+ pathIndex = pathnameSegments.length;
469
+ routeIndex += 1;
470
+ continue;
471
+ }
472
+ if (routeSegment.type === "rest") {
473
+ if (pathIndex >= pathnameSegments.length) {
474
+ return null;
475
+ }
476
+ params[routeSegment.name] = pathnameSegments.slice(pathIndex).join("/");
477
+ pathIndex = pathnameSegments.length;
478
+ routeIndex += 1;
479
+ continue;
480
+ }
481
+ const pathnameSegment = pathnameSegments[pathIndex];
482
+ if (pathnameSegment === undefined) {
483
+ return null;
484
+ }
485
+ if (routeSegment.type === "static") {
486
+ if (pathnameSegment !== routeSegment.value) {
487
+ return null;
488
+ }
489
+ } else {
490
+ params[routeSegment.name] = pathnameSegment;
491
+ }
492
+ pathIndex += 1;
493
+ routeIndex += 1;
494
+ }
495
+ if (pathIndex !== pathnameSegments.length) {
496
+ return null;
497
+ }
498
+ return params;
499
+ }
500
+ function matchFetchRoute(routes, input) {
501
+ const pathnameSegments = getPathSegments(getMatchPathname(input));
502
+ for (const route of routes) {
503
+ const params = matchRouteSegments(route.segments, pathnameSegments);
504
+ if (params) {
505
+ return {
506
+ route,
507
+ params
508
+ };
509
+ }
510
+ }
511
+ return null;
512
+ }
513
+ async function invokeRouteModules(routes, event2) {
514
+ const match = matchFetchRoute(routes, event2.request);
515
+ if (!match) {
516
+ return new Response("Not Found", { status: 404 });
517
+ }
518
+ const routeEvent = createFetchEvent(event2.request, event2.env, event2.ctx, {
519
+ params: match.params,
520
+ locals: event2.locals
521
+ });
522
+ return runWithEventContext(routeEvent, () => invokeFetchModule(match.route.module, routeEvent));
523
+ }
524
+ function createRouteResolve(routes, initialEvent) {
525
+ return async (nextEvent = initialEvent) => {
526
+ return invokeRouteModules(routes, nextEvent);
527
+ };
528
+ }
529
+ export { ContextAccessError, createContextProxy, env, ctx, event, locals, sequence, handle, resolve, pipe, resolveFetchHandler, invokeFetchHandler, createResolveFetch, invokeFetchModule, matchFetchRoute, invokeRouteModules, createRouteResolve };