devflare 1.0.0-next.1 → 1.0.0-next.11
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 +1424 -610
- package/R2.md +200 -0
- package/README.md +302 -505
- package/bin/devflare.js +8 -8
- package/dist/{account-rvrj687w.js → account-8psavtg6.js} +27 -4
- package/dist/bridge/miniflare.d.ts +6 -0
- package/dist/bridge/miniflare.d.ts.map +1 -1
- package/dist/bridge/proxy.d.ts +5 -6
- package/dist/bridge/proxy.d.ts.map +1 -1
- package/dist/bridge/server.d.ts.map +1 -1
- package/dist/browser.d.ts +50 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/{build-mnf6v8gd.js → build-ezksv2dd.js} +26 -7
- package/dist/bundler/do-bundler.d.ts +7 -0
- package/dist/bundler/do-bundler.d.ts.map +1 -1
- package/dist/cli/commands/account.d.ts.map +1 -1
- package/dist/cli/commands/build.d.ts.map +1 -1
- package/dist/cli/commands/deploy.d.ts.map +1 -1
- package/dist/cli/commands/dev.d.ts.map +1 -1
- package/dist/cli/commands/doctor.d.ts.map +1 -1
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/types.d.ts.map +1 -1
- package/dist/cli/config-path.d.ts +5 -0
- package/dist/cli/config-path.d.ts.map +1 -0
- package/dist/cli/index.d.ts.map +1 -1
- package/dist/cli/package-metadata.d.ts +16 -0
- package/dist/cli/package-metadata.d.ts.map +1 -0
- package/dist/config/compiler.d.ts +7 -0
- package/dist/config/compiler.d.ts.map +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/schema.d.ts +2594 -1234
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/{deploy-nhceck39.js → deploy-jdpy21t6.js} +33 -15
- package/dist/{dev-qnxet3j9.js → dev-9mq7zhww.js} +900 -234
- package/dist/dev-server/miniflare-log.d.ts +12 -0
- package/dist/dev-server/miniflare-log.d.ts.map +1 -0
- package/dist/dev-server/runtime-stdio.d.ts +8 -0
- package/dist/dev-server/runtime-stdio.d.ts.map +1 -0
- package/dist/dev-server/server.d.ts +2 -0
- package/dist/dev-server/server.d.ts.map +1 -1
- package/dist/dev-server/vite-utils.d.ts +37 -0
- package/dist/dev-server/vite-utils.d.ts.map +1 -0
- package/dist/{doctor-e8fy6fj5.js → doctor-z4ffybce.js} +73 -50
- package/dist/{durable-object-t4kbb0yt.js → durable-object-yt8v1dyn.js} +1 -1
- package/dist/index-1p814k7s.js +227 -0
- package/dist/{index-tk6ej9dj.js → index-2q3pmzrx.js} +12 -16
- package/dist/{index-67qcae0f.js → index-51s1hkw4.js} +16 -1
- package/dist/{index-ep3445yc.js → index-53xcakh8.js} +414 -171
- package/dist/{index-pf5s73n9.js → index-59df49vn.js} +11 -281
- package/dist/index-5yxg30va.js +304 -0
- package/dist/index-62b3gt2g.js +12 -0
- package/dist/index-6h8xbs75.js +44 -0
- package/dist/index-8gtqgb3q.js +529 -0
- package/dist/{index-gz1gndna.js → index-9wt9x09k.js} +42 -62
- package/dist/index-dr6sbp8d.js +39 -0
- package/dist/index-fef08w43.js +231 -0
- package/dist/index-k7r18na8.js +0 -0
- package/dist/{index-m2q41jwa.js → index-n932ytmq.js} +9 -1
- package/dist/{index-07q6yxyc.js → index-v8vvsn9x.js} +1 -0
- package/dist/index-vky23txa.js +70 -0
- package/dist/{index-z14anrqp.js → index-wfbfz02q.js} +14 -15
- package/dist/index-ws68xvq2.js +311 -0
- package/dist/{index-hcex3rgh.js → index-wyf3s77s.js} +85 -8
- package/dist/index-xqfbd9fx.js +195 -0
- package/dist/index-xxwbb2nt.js +322 -0
- package/dist/index-y1d8za14.js +196 -0
- package/dist/{init-f9mgmew3.js → init-na2atvz2.js} +42 -55
- package/dist/router/types.d.ts +24 -0
- package/dist/router/types.d.ts.map +1 -0
- package/dist/runtime/context.d.ts +249 -8
- package/dist/runtime/context.d.ts.map +1 -1
- package/dist/runtime/exports.d.ts +50 -55
- package/dist/runtime/exports.d.ts.map +1 -1
- package/dist/runtime/index.d.ts +8 -1
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/middleware.d.ts +77 -60
- package/dist/runtime/middleware.d.ts.map +1 -1
- package/dist/runtime/router.d.ts +7 -0
- package/dist/runtime/router.d.ts.map +1 -0
- package/dist/runtime/validation.d.ts +1 -1
- package/dist/runtime/validation.d.ts.map +1 -1
- package/dist/src/browser.js +150 -0
- package/dist/src/cli/index.js +10 -0
- package/dist/{cloudflare → src/cloudflare}/index.js +3 -3
- package/dist/{decorators → src/decorators}/index.js +2 -2
- package/dist/src/index.js +132 -0
- package/dist/src/runtime/index.js +111 -0
- package/dist/{sveltekit → src/sveltekit}/index.js +14 -6
- package/dist/{test → src/test}/index.js +22 -13
- package/dist/{vite → src/vite}/index.js +128 -59
- package/dist/sveltekit/platform.d.ts.map +1 -1
- package/dist/test/bridge-context.d.ts +5 -2
- package/dist/test/bridge-context.d.ts.map +1 -1
- package/dist/test/cf.d.ts +25 -11
- package/dist/test/cf.d.ts.map +1 -1
- package/dist/test/email.d.ts +16 -7
- package/dist/test/email.d.ts.map +1 -1
- package/dist/test/queue.d.ts.map +1 -1
- package/dist/test/resolve-service-bindings.d.ts.map +1 -1
- package/dist/test/scheduled.d.ts.map +1 -1
- package/dist/test/simple-context.d.ts +1 -1
- package/dist/test/simple-context.d.ts.map +1 -1
- package/dist/test/tail.d.ts +2 -1
- package/dist/test/tail.d.ts.map +1 -1
- package/dist/test/worker.d.ts +6 -0
- package/dist/test/worker.d.ts.map +1 -1
- package/dist/transform/durable-object.d.ts.map +1 -1
- package/dist/transform/worker-entrypoint.d.ts.map +1 -1
- package/dist/{types-5nyrz1sz.js → types-nq5acrwh.js} +30 -16
- package/dist/utils/entrypoint-discovery.d.ts +6 -3
- package/dist/utils/entrypoint-discovery.d.ts.map +1 -1
- package/dist/utils/send-email.d.ts +15 -0
- package/dist/utils/send-email.d.ts.map +1 -0
- package/dist/vite/plugin.d.ts.map +1 -1
- package/dist/worker-entry/composed-worker.d.ts +13 -0
- package/dist/worker-entry/composed-worker.d.ts.map +1 -0
- package/dist/worker-entry/routes.d.ts +22 -0
- package/dist/worker-entry/routes.d.ts.map +1 -0
- package/dist/{worker-entrypoint-m9th0rg0.js → worker-entrypoint-c259fmfs.js} +1 -1
- package/package.json +21 -19
- package/dist/index.js +0 -298
- package/dist/runtime/index.js +0 -111
|
@@ -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 };
|
|
@@ -104,30 +104,26 @@ function generateWrapper(className, options = {}) {
|
|
|
104
104
|
const includeWebsockets = options.websockets ?? false;
|
|
105
105
|
let wrapper = `
|
|
106
106
|
// ============ Devflare DO Wrapper for ${className} ============
|
|
107
|
-
import {
|
|
107
|
+
import { createDurableObjectAlarmEvent, createDurableObjectFetchEvent, createDurableObjectWebSocketCloseEvent, createDurableObjectWebSocketErrorEvent, createDurableObjectWebSocketMessageEvent, runWithEventContext } from 'devflare/runtime'
|
|
108
108
|
|
|
109
109
|
const __Original${className} = ${className}
|
|
110
110
|
|
|
111
111
|
class ${className}Wrapper extends __Original${className} {
|
|
112
112
|
async fetch(request: Request): Promise<Response> {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
() => super.fetch(request),
|
|
118
|
-
'fetch'
|
|
113
|
+
const __devflareEvent = createDurableObjectFetchEvent(request, this.env, this.ctx)
|
|
114
|
+
return runWithEventContext(
|
|
115
|
+
__devflareEvent,
|
|
116
|
+
() => super.fetch(__devflareEvent)
|
|
119
117
|
)
|
|
120
118
|
}
|
|
121
119
|
`;
|
|
122
120
|
if (includeAlarms) {
|
|
123
121
|
wrapper += `
|
|
124
122
|
async alarm(): Promise<void> {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
() => super.alarm?.() ?? Promise.resolve(),
|
|
130
|
-
'scheduled'
|
|
123
|
+
const __devflareEvent = createDurableObjectAlarmEvent(this.env, this.ctx)
|
|
124
|
+
return runWithEventContext(
|
|
125
|
+
__devflareEvent,
|
|
126
|
+
() => super.alarm?.(__devflareEvent) ?? Promise.resolve()
|
|
131
127
|
)
|
|
132
128
|
}
|
|
133
129
|
`;
|
|
@@ -135,32 +131,26 @@ class ${className}Wrapper extends __Original${className} {
|
|
|
135
131
|
if (includeWebsockets) {
|
|
136
132
|
wrapper += `
|
|
137
133
|
async webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): Promise<void> {
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
() => super.webSocketMessage?.(ws, message) ?? Promise.resolve(),
|
|
143
|
-
'fetch'
|
|
134
|
+
const __devflareEvent = createDurableObjectWebSocketMessageEvent(ws, message, this.env, this.ctx)
|
|
135
|
+
return runWithEventContext(
|
|
136
|
+
__devflareEvent,
|
|
137
|
+
() => super.webSocketMessage?.(__devflareEvent, message) ?? Promise.resolve()
|
|
144
138
|
)
|
|
145
139
|
}
|
|
146
140
|
|
|
147
141
|
async webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise<void> {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
() => super.webSocketClose?.(ws, code, reason, wasClean) ?? Promise.resolve(),
|
|
153
|
-
'fetch'
|
|
142
|
+
const __devflareEvent = createDurableObjectWebSocketCloseEvent(ws, code, reason, wasClean, this.env, this.ctx)
|
|
143
|
+
return runWithEventContext(
|
|
144
|
+
__devflareEvent,
|
|
145
|
+
() => super.webSocketClose?.(__devflareEvent, code, reason, wasClean) ?? Promise.resolve()
|
|
154
146
|
)
|
|
155
147
|
}
|
|
156
148
|
|
|
157
149
|
async webSocketError(ws: WebSocket, error: unknown): Promise<void> {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
() => super.webSocketError?.(ws, error) ?? Promise.resolve(),
|
|
163
|
-
'fetch'
|
|
150
|
+
const __devflareEvent = createDurableObjectWebSocketErrorEvent(ws, error, this.env, this.ctx)
|
|
151
|
+
return runWithEventContext(
|
|
152
|
+
__devflareEvent,
|
|
153
|
+
() => super.webSocketError?.(__devflareEvent, error) ?? Promise.resolve()
|
|
164
154
|
)
|
|
165
155
|
}
|
|
166
156
|
`;
|
|
@@ -212,7 +202,7 @@ async function transformDurableObject(code, id) {
|
|
|
212
202
|
}
|
|
213
203
|
}
|
|
214
204
|
const imports = `
|
|
215
|
-
import {
|
|
205
|
+
import { createDurableObjectAlarmEvent, createDurableObjectFetchEvent, createDurableObjectWebSocketCloseEvent, createDurableObjectWebSocketErrorEvent, createDurableObjectWebSocketMessageEvent, runWithEventContext } from 'devflare/runtime'
|
|
216
206
|
`;
|
|
217
207
|
s.prepend(imports);
|
|
218
208
|
for (const classInfo of doClasses) {
|
|
@@ -241,24 +231,20 @@ function generateWrapperCodeInternal(className, options) {
|
|
|
241
231
|
// ============ Devflare DO Wrapper for ${className} ============
|
|
242
232
|
class ${className}Wrapper extends __Original${className} {
|
|
243
233
|
async fetch(request: Request): Promise<Response> {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
() => super.fetch(request),
|
|
249
|
-
'fetch'
|
|
234
|
+
const __devflareEvent = createDurableObjectFetchEvent(request, this.env, this.ctx)
|
|
235
|
+
return runWithEventContext(
|
|
236
|
+
__devflareEvent,
|
|
237
|
+
() => super.fetch(__devflareEvent)
|
|
250
238
|
)
|
|
251
239
|
}
|
|
252
240
|
`;
|
|
253
241
|
if (options.alarms) {
|
|
254
242
|
wrapper += `
|
|
255
243
|
async alarm(): Promise<void> {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
() => super.alarm?.() ?? Promise.resolve(),
|
|
261
|
-
'scheduled'
|
|
244
|
+
const __devflareEvent = createDurableObjectAlarmEvent(this.env, this.ctx)
|
|
245
|
+
return runWithEventContext(
|
|
246
|
+
__devflareEvent,
|
|
247
|
+
() => super.alarm?.(__devflareEvent) ?? Promise.resolve()
|
|
262
248
|
)
|
|
263
249
|
}
|
|
264
250
|
`;
|
|
@@ -266,32 +252,26 @@ class ${className}Wrapper extends __Original${className} {
|
|
|
266
252
|
if (options.websockets) {
|
|
267
253
|
wrapper += `
|
|
268
254
|
async webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): Promise<void> {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
() => super.webSocketMessage?.(ws, message) ?? Promise.resolve(),
|
|
274
|
-
'fetch'
|
|
255
|
+
const __devflareEvent = createDurableObjectWebSocketMessageEvent(ws, message, this.env, this.ctx)
|
|
256
|
+
return runWithEventContext(
|
|
257
|
+
__devflareEvent,
|
|
258
|
+
() => super.webSocketMessage?.(__devflareEvent, message) ?? Promise.resolve()
|
|
275
259
|
)
|
|
276
260
|
}
|
|
277
261
|
|
|
278
262
|
async webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise<void> {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
() => super.webSocketClose?.(ws, code, reason, wasClean) ?? Promise.resolve(),
|
|
284
|
-
'fetch'
|
|
263
|
+
const __devflareEvent = createDurableObjectWebSocketCloseEvent(ws, code, reason, wasClean, this.env, this.ctx)
|
|
264
|
+
return runWithEventContext(
|
|
265
|
+
__devflareEvent,
|
|
266
|
+
() => super.webSocketClose?.(__devflareEvent, code, reason, wasClean) ?? Promise.resolve()
|
|
285
267
|
)
|
|
286
268
|
}
|
|
287
269
|
|
|
288
270
|
async webSocketError(ws: WebSocket, error: unknown): Promise<void> {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
() => super.webSocketError?.(ws, error) ?? Promise.resolve(),
|
|
294
|
-
'fetch'
|
|
271
|
+
const __devflareEvent = createDurableObjectWebSocketErrorEvent(ws, error, this.env, this.ctx)
|
|
272
|
+
return runWithEventContext(
|
|
273
|
+
__devflareEvent,
|
|
274
|
+
() => super.webSocketError?.(__devflareEvent, error) ?? Promise.resolve()
|
|
295
275
|
)
|
|
296
276
|
}
|
|
297
277
|
`;
|