devflare 1.0.0-next.21 → 1.0.0-next.23
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.
- package/LLM.md +144 -5
- package/dist/account-j8nfggg4.js +475 -0
- package/dist/account-qhe8vtds.js +475 -0
- package/dist/bridge/gateway-runtime.d.ts +1 -1
- package/dist/bridge/gateway-runtime.d.ts.map +1 -1
- package/dist/bridge/miniflare.d.ts +1 -1
- package/dist/bridge/miniflare.d.ts.map +1 -1
- package/dist/bridge/proxy.d.ts +2 -0
- package/dist/bridge/proxy.d.ts.map +1 -1
- package/dist/bridge/server.d.ts.map +1 -1
- package/dist/browser.d.ts +13 -13
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +5 -3
- package/dist/build-qsgnme4z.js +54 -0
- package/dist/build-vy95gy3f.js +54 -0
- package/dist/build-yzx0gsaj.js +54 -0
- package/dist/cli/commands/build-artifacts.d.ts.map +1 -1
- package/dist/cli/commands/config.d.ts.map +1 -1
- package/dist/cli/commands/type-generation/generator.d.ts +4 -2
- package/dist/cli/commands/type-generation/generator.d.ts.map +1 -1
- package/dist/cli/commands/types.d.ts.map +1 -1
- package/dist/cli/index.js +1 -1
- package/dist/config/compiler/types.d.ts +1 -1
- package/dist/config/compiler/types.d.ts.map +1 -1
- package/dist/config/define.d.ts +7 -4
- package/dist/config/define.d.ts.map +1 -1
- package/dist/config/env-vars.d.ts +309 -0
- package/dist/config/env-vars.d.ts.map +1 -0
- package/dist/config/index.d.ts +2 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/loader.d.ts.map +1 -1
- package/dist/config/local-dev-vars.d.ts +2 -2
- package/dist/config/local-dev-vars.d.ts.map +1 -1
- package/dist/config/schema-env.d.ts +6 -6
- package/dist/config/schema-types-bindings-platform.d.ts +378 -0
- package/dist/config/schema-types-bindings-platform.d.ts.map +1 -0
- package/dist/config/schema-types-bindings-resources.d.ts +551 -0
- package/dist/config/schema-types-bindings-resources.d.ts.map +1 -0
- package/dist/config/schema-types-bindings.d.ts +254 -0
- package/dist/config/schema-types-bindings.d.ts.map +1 -0
- package/dist/config/schema-types-build.d.ts +86 -0
- package/dist/config/schema-types-build.d.ts.map +1 -0
- package/dist/config/schema-types-runtime.d.ts +882 -0
- package/dist/config/schema-types-runtime.d.ts.map +1 -0
- package/dist/config/schema-types.d.ts +377 -0
- package/dist/config/schema-types.d.ts.map +1 -0
- package/dist/config/schema.d.ts +14 -15
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/config-entry.d.ts +2 -0
- package/dist/config-entry.d.ts.map +1 -1
- package/dist/config-entry.js +3 -1
- package/dist/config-gq5jh4cx.js +105 -0
- package/dist/config-vec13050.js +105 -0
- package/dist/deploy-01j0ep5n.js +1055 -0
- package/dist/deploy-nh5tbv45.js +1055 -0
- package/dist/deploy-tjypkhg7.js +1055 -0
- package/dist/dev-bh581ew3.js +2597 -0
- package/dist/dev-cme5de75.js +2551 -0
- package/dist/dev-gn5y93z9.js +2597 -0
- package/dist/dev-server/server.d.ts.map +1 -1
- package/dist/doctor-h5q28qt1.js +259 -0
- package/dist/doctor-khk550tw.js +259 -0
- package/dist/env.d.ts +10 -0
- package/dist/env.d.ts.map +1 -1
- package/dist/index-0bv2qjs1.js +1555 -0
- package/dist/index-35bmgpfw.js +573 -0
- package/dist/index-3tkzn06q.js +413 -0
- package/dist/index-4se6krdj.js +574 -0
- package/dist/index-8fyz6gcm.js +699 -0
- package/dist/index-97z629zr.js +109 -0
- package/dist/index-b28c4yr4.js +1205 -0
- package/dist/index-c1cj9085.js +2250 -0
- package/dist/index-c8p4njqy.js +479 -0
- package/dist/index-cr06zrgw.js +1033 -0
- package/dist/index-cwjjdtgn.js +74 -0
- package/dist/index-dref9ecb.js +476 -0
- package/dist/index-e151t4ge.js +895 -0
- package/dist/index-e7kakw0j.js +1033 -0
- package/dist/index-f1g5jdm8.js +1426 -0
- package/dist/index-f46984zs.js +1554 -0
- package/dist/index-grk8pzhr.js +185 -0
- package/dist/index-hbxkmb1q.js +1426 -0
- package/dist/index-hzmpecq9.js +52 -0
- package/dist/index-j1csb7gb.js +581 -0
- package/dist/index-j7x7f72h.js +185 -0
- package/dist/index-jkqbjwt2.js +476 -0
- package/dist/index-jwd3fanx.js +412 -0
- package/dist/index-mh5renra.js +895 -0
- package/dist/index-p9xq83p7.js +147 -0
- package/dist/index-q15nj71j.js +52 -0
- package/dist/index-qqp65pyv.js +699 -0
- package/dist/index-s0fmwxbk.js +74 -0
- package/dist/index-s9q605sq.js +1033 -0
- package/dist/index-stzx8nc4.js +111 -0
- package/dist/index-th4vrnbk.js +1205 -0
- package/dist/index-vtcmsgaf.js +581 -0
- package/dist/index-w36q6819.js +895 -0
- package/dist/index-x2k3awjs.js +147 -0
- package/dist/index-x8x547tz.js +1426 -0
- package/dist/index-xp0qkkxf.js +68 -0
- package/dist/index-xxxd0mvw.js +109 -0
- package/dist/index-zawn5tte.js +109 -0
- package/dist/index-zpy9caxn.js +1193 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -7
- package/dist/login-280p2cm9.js +77 -0
- package/dist/login-4n266whq.js +77 -0
- package/dist/previews-3m3ffpaw.js +1337 -0
- package/dist/previews-tr8sm03d.js +1337 -0
- package/dist/productions-62y489ff.js +505 -0
- package/dist/productions-cgn3fz7d.js +505 -0
- package/dist/runtime/exports.d.ts +23 -0
- package/dist/runtime/exports.d.ts.map +1 -1
- package/dist/runtime/index.d.ts +1 -1
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +6 -4
- package/dist/secrets-4050kqf5.js +91 -0
- package/dist/secrets-p112cajt.js +91 -0
- package/dist/sveltekit/index.js +8 -7
- package/dist/sveltekit/local-bindings.d.ts.map +1 -1
- package/dist/test/index.js +24 -12
- package/dist/test/resolve-service-bindings.d.ts +1 -1
- package/dist/test/resolve-service-bindings.d.ts.map +1 -1
- package/dist/test/simple-context-lifecycle.d.ts.map +1 -1
- package/dist/types-apmt10yj.js +705 -0
- package/dist/types-ttrrgdfj.js +705 -0
- package/dist/vite/index.js +5 -5
- package/dist/vite/plugin-context.d.ts.map +1 -1
- package/dist/vite/plugin-programmatic.d.ts.map +1 -1
- package/dist/worker-2k1jyr6p.js +513 -0
- package/dist/worker-jqgn6jyj.js +513 -0
- package/package.json +1 -1
|
@@ -0,0 +1,413 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContextProxy,
|
|
3
|
+
createFetchEvent,
|
|
4
|
+
getContextOrNull,
|
|
5
|
+
runWithEventContext
|
|
6
|
+
} from "./index-c1cj9085.js";
|
|
7
|
+
|
|
8
|
+
// src/runtime/exports.ts
|
|
9
|
+
function createReadonlyProxy(getter, name) {
|
|
10
|
+
return createContextProxy(getter, name, { mutable: false });
|
|
11
|
+
}
|
|
12
|
+
var env = createReadonlyProxy(() => getContextOrNull()?.env, "env");
|
|
13
|
+
var vars = createReadonlyProxy(() => getContextOrNull()?.env, "vars");
|
|
14
|
+
var ctx = createReadonlyProxy(() => getContextOrNull()?.ctx, "ctx");
|
|
15
|
+
var event = createReadonlyProxy(() => getContextOrNull()?.event, "event");
|
|
16
|
+
var locals = createContextProxy(() => getContextOrNull()?.locals, "locals");
|
|
17
|
+
// src/runtime/middleware.ts
|
|
18
|
+
var FETCH_SEQUENCE_SYMBOL = Symbol.for("devflare.fetch-sequence");
|
|
19
|
+
var FETCH_RESOLVE_STYLE_SYMBOL = Symbol.for("devflare.fetch-resolve-style");
|
|
20
|
+
var FETCH_WORKER_STYLE_SYMBOL = Symbol.for("devflare.fetch-worker-style");
|
|
21
|
+
var QUEUE_WORKER_STYLE_SYMBOL = Symbol.for("devflare.queue-worker-style");
|
|
22
|
+
var SCHEDULED_WORKER_STYLE_SYMBOL = Symbol.for("devflare.scheduled-worker-style");
|
|
23
|
+
function createNotFoundResponse() {
|
|
24
|
+
return new Response("Not Found", { status: 404 });
|
|
25
|
+
}
|
|
26
|
+
function isFunction(value) {
|
|
27
|
+
return typeof value === "function";
|
|
28
|
+
}
|
|
29
|
+
function markResolveStyle(handler) {
|
|
30
|
+
Object.defineProperty(handler, FETCH_RESOLVE_STYLE_SYMBOL, {
|
|
31
|
+
value: true,
|
|
32
|
+
enumerable: false,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: false
|
|
35
|
+
});
|
|
36
|
+
return handler;
|
|
37
|
+
}
|
|
38
|
+
function markWorkerStyle(handler) {
|
|
39
|
+
Object.defineProperty(handler, FETCH_WORKER_STYLE_SYMBOL, {
|
|
40
|
+
value: true,
|
|
41
|
+
enumerable: false,
|
|
42
|
+
configurable: true,
|
|
43
|
+
writable: false
|
|
44
|
+
});
|
|
45
|
+
return handler;
|
|
46
|
+
}
|
|
47
|
+
function defineFetchHandler(handler, options) {
|
|
48
|
+
if (options?.style === "resolve") {
|
|
49
|
+
return markResolveStyle(handler);
|
|
50
|
+
}
|
|
51
|
+
if (options?.style === "worker") {
|
|
52
|
+
return markWorkerStyle(handler);
|
|
53
|
+
}
|
|
54
|
+
return handler;
|
|
55
|
+
}
|
|
56
|
+
function hasResolveStyleMarker(handler) {
|
|
57
|
+
const record = handler;
|
|
58
|
+
return Boolean(record[FETCH_RESOLVE_STYLE_SYMBOL] || record[FETCH_SEQUENCE_SYMBOL]);
|
|
59
|
+
}
|
|
60
|
+
function hasWorkerStyleMarker(handler) {
|
|
61
|
+
const record = handler;
|
|
62
|
+
return Boolean(record[FETCH_WORKER_STYLE_SYMBOL]);
|
|
63
|
+
}
|
|
64
|
+
function isResolveStyleFunction(handler) {
|
|
65
|
+
return hasResolveStyleMarker(handler);
|
|
66
|
+
}
|
|
67
|
+
function assertExplicit2ArgStyle(handler) {
|
|
68
|
+
if (handler.length !== 2) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
if (hasResolveStyleMarker(handler) || hasWorkerStyleMarker(handler)) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
throw new Error("[devflare] Ambiguous 2-argument fetch handler. The calling convention must be declared explicitly via " + "`defineFetchHandler(fn, { style: 'resolve' })` (for `(event, resolve) => Response`) or " + "`defineFetchHandler(fn, { style: 'worker' })` (for `(request, env) => Response`). " + "Single-arg `(event) => Response` and 3-arg worker-style `(request, env, ctx) => Response` " + "handlers do not require wrapping.");
|
|
75
|
+
}
|
|
76
|
+
function defineQueueHandler(handler) {
|
|
77
|
+
Object.defineProperty(handler, QUEUE_WORKER_STYLE_SYMBOL, {
|
|
78
|
+
value: true,
|
|
79
|
+
enumerable: false,
|
|
80
|
+
configurable: true,
|
|
81
|
+
writable: false
|
|
82
|
+
});
|
|
83
|
+
return handler;
|
|
84
|
+
}
|
|
85
|
+
function defineScheduledHandler(handler) {
|
|
86
|
+
Object.defineProperty(handler, SCHEDULED_WORKER_STYLE_SYMBOL, {
|
|
87
|
+
value: true,
|
|
88
|
+
enumerable: false,
|
|
89
|
+
configurable: true,
|
|
90
|
+
writable: false
|
|
91
|
+
});
|
|
92
|
+
return handler;
|
|
93
|
+
}
|
|
94
|
+
function hasQueueWorkerStyleMarker(handler) {
|
|
95
|
+
const record = handler;
|
|
96
|
+
return Boolean(record[QUEUE_WORKER_STYLE_SYMBOL]);
|
|
97
|
+
}
|
|
98
|
+
function hasScheduledWorkerStyleMarker(handler) {
|
|
99
|
+
const record = handler;
|
|
100
|
+
return Boolean(record[SCHEDULED_WORKER_STYLE_SYMBOL]);
|
|
101
|
+
}
|
|
102
|
+
function assertExplicitQueueHandlerStyle(handler) {
|
|
103
|
+
if (handler.length !== 2) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (hasQueueWorkerStyleMarker(handler)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
throw new Error("[devflare] Ambiguous 2-argument queue handler. The calling convention must be declared explicitly via " + "`defineQueueHandler(fn)` for `(batch, env) => void` worker-style handlers. " + "Single-arg `(event) => void` and 3-arg `(batch, env, ctx) => void` handlers do not require wrapping.");
|
|
110
|
+
}
|
|
111
|
+
function assertExplicitScheduledHandlerStyle(handler) {
|
|
112
|
+
if (handler.length !== 2) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (hasScheduledWorkerStyleMarker(handler)) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
throw new Error("[devflare] Ambiguous 2-argument scheduled handler. The calling convention must be declared explicitly via " + "`defineScheduledHandler(fn)` for `(controller, env) => void` worker-style handlers. " + "Single-arg `(event) => void` and 3-arg `(controller, env, ctx) => void` handlers do not require wrapping.");
|
|
119
|
+
}
|
|
120
|
+
function isWorkerStyleFetchFunction(handler) {
|
|
121
|
+
if (isResolveStyleFunction(handler)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (handler.length >= 3) {
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
return hasWorkerStyleMarker(handler);
|
|
128
|
+
}
|
|
129
|
+
function invokeWorkerStyleFetchFunction(handler, event2) {
|
|
130
|
+
return handler(event2.request, event2.env, event2.ctx);
|
|
131
|
+
}
|
|
132
|
+
function bindMethod(target, key) {
|
|
133
|
+
if (!target || typeof target !== "object") {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
const value = target[key];
|
|
137
|
+
if (!isFunction(value)) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
const boundHandler = value.bind(target);
|
|
141
|
+
if (isResolveStyleFunction(value)) {
|
|
142
|
+
markResolveStyle(boundHandler);
|
|
143
|
+
}
|
|
144
|
+
if (hasWorkerStyleMarker(value)) {
|
|
145
|
+
markWorkerStyle(boundHandler);
|
|
146
|
+
}
|
|
147
|
+
return boundHandler;
|
|
148
|
+
}
|
|
149
|
+
function createFetchSequence(middlewares) {
|
|
150
|
+
return async (event2, resolve = async () => createNotFoundResponse()) => {
|
|
151
|
+
const executeMiddleware = async (index, activeEvent) => {
|
|
152
|
+
if (index >= middlewares.length) {
|
|
153
|
+
return resolve(activeEvent);
|
|
154
|
+
}
|
|
155
|
+
const middleware = middlewares[index];
|
|
156
|
+
return middleware(activeEvent, async (nextEvent = activeEvent) => {
|
|
157
|
+
return executeMiddleware(index + 1, nextEvent);
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
return executeMiddleware(0, event2);
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function sequence(...middlewares) {
|
|
164
|
+
const composed = createFetchSequence(middlewares);
|
|
165
|
+
Object.defineProperty(composed, FETCH_SEQUENCE_SYMBOL, {
|
|
166
|
+
value: true,
|
|
167
|
+
enumerable: false,
|
|
168
|
+
configurable: false,
|
|
169
|
+
writable: false
|
|
170
|
+
});
|
|
171
|
+
return markResolveStyle(composed);
|
|
172
|
+
}
|
|
173
|
+
function getDefaultHandleHandler(module) {
|
|
174
|
+
return bindMethod(module.default, "handle");
|
|
175
|
+
}
|
|
176
|
+
function getDefaultFetchHandler(module) {
|
|
177
|
+
const defaultExport = module.default;
|
|
178
|
+
if (isFunction(defaultExport)) {
|
|
179
|
+
return defaultExport;
|
|
180
|
+
}
|
|
181
|
+
return bindMethod(defaultExport, "fetch");
|
|
182
|
+
}
|
|
183
|
+
function getPrimaryFetchEntryCandidates(module) {
|
|
184
|
+
const candidates = [];
|
|
185
|
+
const namedHandle = isFunction(module.handle) ? module.handle : null;
|
|
186
|
+
if (namedHandle) {
|
|
187
|
+
candidates.push({
|
|
188
|
+
name: "handle",
|
|
189
|
+
handler: namedHandle
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
const namedFetch = isFunction(module.fetch) ? module.fetch : null;
|
|
193
|
+
if (namedFetch) {
|
|
194
|
+
candidates.push({
|
|
195
|
+
name: "fetch",
|
|
196
|
+
handler: namedFetch
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
const defaultHandle = getDefaultHandleHandler(module);
|
|
200
|
+
if (defaultHandle) {
|
|
201
|
+
candidates.push({
|
|
202
|
+
name: "default.handle",
|
|
203
|
+
handler: defaultHandle
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const defaultFetch = getDefaultFetchHandler(module);
|
|
207
|
+
if (defaultFetch) {
|
|
208
|
+
candidates.push({
|
|
209
|
+
name: isFunction(module.default) ? "default" : "default.fetch",
|
|
210
|
+
handler: defaultFetch
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return candidates;
|
|
214
|
+
}
|
|
215
|
+
function assertSinglePrimaryFetchEntry(candidates) {
|
|
216
|
+
if (candidates.length <= 1) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const foundEntries = candidates.map(({ name }) => `"${name}"`).join(", ");
|
|
220
|
+
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}`);
|
|
221
|
+
}
|
|
222
|
+
function resolveMethodHandler(module, method) {
|
|
223
|
+
const normalizedMethod = method.toUpperCase();
|
|
224
|
+
const directHandler = isFunction(module[normalizedMethod]) ? module[normalizedMethod] : bindMethod(module.default, normalizedMethod);
|
|
225
|
+
if (directHandler) {
|
|
226
|
+
return {
|
|
227
|
+
handler: directHandler,
|
|
228
|
+
stripBody: false
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
if (normalizedMethod === "HEAD") {
|
|
232
|
+
const getHandler = isFunction(module.GET) ? module.GET : bindMethod(module.default, "GET");
|
|
233
|
+
if (getHandler) {
|
|
234
|
+
return {
|
|
235
|
+
handler: getHandler,
|
|
236
|
+
stripBody: true
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const allHandler = isFunction(module.ALL) ? module.ALL : bindMethod(module.default, "ALL");
|
|
241
|
+
if (allHandler) {
|
|
242
|
+
return {
|
|
243
|
+
handler: allHandler,
|
|
244
|
+
stripBody: false
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
async function invokeResolvedFetchHandler(handler, event2) {
|
|
250
|
+
if (isResolveStyleFunction(handler)) {
|
|
251
|
+
return handler(event2, async () => createNotFoundResponse());
|
|
252
|
+
}
|
|
253
|
+
if (isWorkerStyleFetchFunction(handler)) {
|
|
254
|
+
return invokeWorkerStyleFetchFunction(handler, event2);
|
|
255
|
+
}
|
|
256
|
+
assertExplicit2ArgStyle(handler);
|
|
257
|
+
return handler(event2);
|
|
258
|
+
}
|
|
259
|
+
function resolveFetchHandler(module) {
|
|
260
|
+
const candidates = getPrimaryFetchEntryCandidates(module);
|
|
261
|
+
assertSinglePrimaryFetchEntry(candidates);
|
|
262
|
+
return candidates[0]?.handler ?? null;
|
|
263
|
+
}
|
|
264
|
+
async function invokeFetchHandler(handler, event2, resolve = async () => createNotFoundResponse()) {
|
|
265
|
+
if (!isFunction(handler)) {
|
|
266
|
+
return resolve(event2);
|
|
267
|
+
}
|
|
268
|
+
if (isResolveStyleFunction(handler)) {
|
|
269
|
+
const response2 = await handler(event2, resolve);
|
|
270
|
+
return response2 ?? createNotFoundResponse();
|
|
271
|
+
}
|
|
272
|
+
if (isWorkerStyleFetchFunction(handler)) {
|
|
273
|
+
const response2 = await invokeWorkerStyleFetchFunction(handler, event2);
|
|
274
|
+
return response2 ?? createNotFoundResponse();
|
|
275
|
+
}
|
|
276
|
+
assertExplicit2ArgStyle(handler);
|
|
277
|
+
const response = await handler(event2);
|
|
278
|
+
return response ?? createNotFoundResponse();
|
|
279
|
+
}
|
|
280
|
+
function createResolveFetch(module, _currentEntry, initialEvent, options = {}) {
|
|
281
|
+
return async (nextEvent = initialEvent) => {
|
|
282
|
+
return runWithEventContext(nextEvent, async () => {
|
|
283
|
+
const methodResolution = resolveMethodHandler(module, nextEvent.request.method);
|
|
284
|
+
if (methodResolution) {
|
|
285
|
+
const response = await invokeResolvedFetchHandler(methodResolution.handler, nextEvent);
|
|
286
|
+
const finalResponse = response ?? createNotFoundResponse();
|
|
287
|
+
if (methodResolution.stripBody) {
|
|
288
|
+
return new Response(null, finalResponse);
|
|
289
|
+
}
|
|
290
|
+
return finalResponse;
|
|
291
|
+
}
|
|
292
|
+
if (options.fallbackResolve) {
|
|
293
|
+
return options.fallbackResolve(nextEvent);
|
|
294
|
+
}
|
|
295
|
+
return createNotFoundResponse();
|
|
296
|
+
});
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
async function invokeFetchModule(module, event2, fallbackResolve) {
|
|
300
|
+
const handler = resolveFetchHandler(module);
|
|
301
|
+
if (!handler) {
|
|
302
|
+
return createResolveFetch(module, null, event2, { fallbackResolve })(event2);
|
|
303
|
+
}
|
|
304
|
+
return invokeFetchHandler(handler, event2, createResolveFetch(module, handler, event2, { fallbackResolve }));
|
|
305
|
+
}
|
|
306
|
+
// src/runtime/router/index.ts
|
|
307
|
+
function normalizePathname(pathname) {
|
|
308
|
+
if (!pathname || pathname === "/") {
|
|
309
|
+
return "/";
|
|
310
|
+
}
|
|
311
|
+
const normalized = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
312
|
+
const trimmed = normalized.replace(/\/+$|\/+$/g, "");
|
|
313
|
+
return trimmed === "" ? "/" : trimmed;
|
|
314
|
+
}
|
|
315
|
+
function decodePathSegment(segment) {
|
|
316
|
+
try {
|
|
317
|
+
return decodeURIComponent(segment);
|
|
318
|
+
} catch {
|
|
319
|
+
return segment;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
function getPathSegments(pathname) {
|
|
323
|
+
const normalizedPathname = normalizePathname(pathname);
|
|
324
|
+
if (normalizedPathname === "/") {
|
|
325
|
+
return [];
|
|
326
|
+
}
|
|
327
|
+
return normalizedPathname.slice(1).split("/").filter(Boolean).map(decodePathSegment);
|
|
328
|
+
}
|
|
329
|
+
function getMatchPathname(input) {
|
|
330
|
+
if (input instanceof Request) {
|
|
331
|
+
return new URL(input.url).pathname;
|
|
332
|
+
}
|
|
333
|
+
if (input instanceof URL) {
|
|
334
|
+
return input.pathname;
|
|
335
|
+
}
|
|
336
|
+
if (input.includes("://")) {
|
|
337
|
+
return new URL(input).pathname;
|
|
338
|
+
}
|
|
339
|
+
return input;
|
|
340
|
+
}
|
|
341
|
+
function matchRouteSegments(routeSegments, pathnameSegments) {
|
|
342
|
+
if (routeSegments.length === 0) {
|
|
343
|
+
return pathnameSegments.length === 0 ? {} : null;
|
|
344
|
+
}
|
|
345
|
+
const params = {};
|
|
346
|
+
let routeIndex = 0;
|
|
347
|
+
let pathIndex = 0;
|
|
348
|
+
while (routeIndex < routeSegments.length) {
|
|
349
|
+
const routeSegment = routeSegments[routeIndex];
|
|
350
|
+
if (routeSegment.type === "optional-rest") {
|
|
351
|
+
params[routeSegment.name] = pathnameSegments.slice(pathIndex).join("/");
|
|
352
|
+
pathIndex = pathnameSegments.length;
|
|
353
|
+
routeIndex += 1;
|
|
354
|
+
continue;
|
|
355
|
+
}
|
|
356
|
+
if (routeSegment.type === "rest") {
|
|
357
|
+
if (pathIndex >= pathnameSegments.length) {
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
params[routeSegment.name] = pathnameSegments.slice(pathIndex).join("/");
|
|
361
|
+
pathIndex = pathnameSegments.length;
|
|
362
|
+
routeIndex += 1;
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
const pathnameSegment = pathnameSegments[pathIndex];
|
|
366
|
+
if (pathnameSegment === undefined) {
|
|
367
|
+
return null;
|
|
368
|
+
}
|
|
369
|
+
if (routeSegment.type === "static") {
|
|
370
|
+
if (pathnameSegment !== routeSegment.value) {
|
|
371
|
+
return null;
|
|
372
|
+
}
|
|
373
|
+
} else {
|
|
374
|
+
params[routeSegment.name] = pathnameSegment;
|
|
375
|
+
}
|
|
376
|
+
pathIndex += 1;
|
|
377
|
+
routeIndex += 1;
|
|
378
|
+
}
|
|
379
|
+
if (pathIndex !== pathnameSegments.length) {
|
|
380
|
+
return null;
|
|
381
|
+
}
|
|
382
|
+
return params;
|
|
383
|
+
}
|
|
384
|
+
function matchFetchRoute(routes, input) {
|
|
385
|
+
const pathnameSegments = getPathSegments(getMatchPathname(input));
|
|
386
|
+
for (const route of routes) {
|
|
387
|
+
const params = matchRouteSegments(route.segments, pathnameSegments);
|
|
388
|
+
if (params) {
|
|
389
|
+
return {
|
|
390
|
+
route,
|
|
391
|
+
params
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return null;
|
|
396
|
+
}
|
|
397
|
+
async function invokeRouteModules(routes, event2) {
|
|
398
|
+
const match = matchFetchRoute(routes, event2.request);
|
|
399
|
+
if (!match) {
|
|
400
|
+
return new Response("Not Found", { status: 404 });
|
|
401
|
+
}
|
|
402
|
+
const routeEvent = createFetchEvent(event2.request, event2.env, event2.ctx, {
|
|
403
|
+
params: match.params,
|
|
404
|
+
locals: event2.locals
|
|
405
|
+
});
|
|
406
|
+
return runWithEventContext(routeEvent, () => invokeFetchModule(match.route.module, routeEvent));
|
|
407
|
+
}
|
|
408
|
+
function createRouteResolve(routes, initialEvent) {
|
|
409
|
+
return async (nextEvent = initialEvent) => {
|
|
410
|
+
return invokeRouteModules(routes, nextEvent);
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
export { env, vars, ctx, event, locals, markResolveStyle, markWorkerStyle, defineFetchHandler, defineQueueHandler, defineScheduledHandler, assertExplicitQueueHandlerStyle, assertExplicitScheduledHandlerStyle, sequence, resolveFetchHandler, invokeFetchHandler, createResolveFetch, invokeFetchModule, matchFetchRoute, invokeRouteModules, createRouteResolve };
|