devflare 0.0.0 → 1.0.0-next.0
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/README.md +737 -1
- package/bin/devflare.js +14 -0
- package/dist/account-rvrj687w.js +397 -0
- package/dist/ai-dx4fr9jh.js +107 -0
- package/dist/bridge/client.d.ts +82 -0
- package/dist/bridge/client.d.ts.map +1 -0
- package/dist/bridge/index.d.ts +7 -0
- package/dist/bridge/index.d.ts.map +1 -0
- package/dist/bridge/miniflare.d.ts +70 -0
- package/dist/bridge/miniflare.d.ts.map +1 -0
- package/dist/bridge/protocol.d.ts +146 -0
- package/dist/bridge/protocol.d.ts.map +1 -0
- package/dist/bridge/proxy.d.ts +49 -0
- package/dist/bridge/proxy.d.ts.map +1 -0
- package/dist/bridge/serialization.d.ts +83 -0
- package/dist/bridge/serialization.d.ts.map +1 -0
- package/dist/bridge/server.d.ts +8 -0
- package/dist/bridge/server.d.ts.map +1 -0
- package/dist/browser-shim/binding-worker.d.ts +7 -0
- package/dist/browser-shim/binding-worker.d.ts.map +1 -0
- package/dist/browser-shim/handler.d.ts +21 -0
- package/dist/browser-shim/handler.d.ts.map +1 -0
- package/dist/browser-shim/index.d.ts +3 -0
- package/dist/browser-shim/index.d.ts.map +1 -0
- package/dist/browser-shim/server.d.ts +25 -0
- package/dist/browser-shim/server.d.ts.map +1 -0
- package/dist/browser-shim/worker.d.ts +14 -0
- package/dist/browser-shim/worker.d.ts.map +1 -0
- package/dist/build-mnf6v8gd.js +53 -0
- package/dist/bundler/do-bundler.d.ts +42 -0
- package/dist/bundler/do-bundler.d.ts.map +1 -0
- package/dist/bundler/index.d.ts +2 -0
- package/dist/bundler/index.d.ts.map +1 -0
- package/dist/cli/bin.d.ts +3 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/colors.d.ts +11 -0
- package/dist/cli/colors.d.ts.map +1 -0
- package/dist/cli/commands/account.d.ts +4 -0
- package/dist/cli/commands/account.d.ts.map +1 -0
- package/dist/cli/commands/ai.d.ts +3 -0
- package/dist/cli/commands/ai.d.ts.map +1 -0
- package/dist/cli/commands/build.d.ts +4 -0
- package/dist/cli/commands/build.d.ts.map +1 -0
- package/dist/cli/commands/deploy.d.ts +4 -0
- package/dist/cli/commands/deploy.d.ts.map +1 -0
- package/dist/cli/commands/dev.d.ts +4 -0
- package/dist/cli/commands/dev.d.ts.map +1 -0
- package/dist/cli/commands/doctor.d.ts +4 -0
- package/dist/cli/commands/doctor.d.ts.map +1 -0
- package/dist/cli/commands/init.d.ts +4 -0
- package/dist/cli/commands/init.d.ts.map +1 -0
- package/dist/cli/commands/remote.d.ts +4 -0
- package/dist/cli/commands/remote.d.ts.map +1 -0
- package/dist/cli/commands/types.d.ts +4 -0
- package/dist/cli/commands/types.d.ts.map +1 -0
- package/dist/cli/dependencies.d.ts +90 -0
- package/dist/cli/dependencies.d.ts.map +1 -0
- package/dist/cli/index.d.ts +23 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/wrangler-auth.d.ts +36 -0
- package/dist/cli/wrangler-auth.d.ts.map +1 -0
- package/dist/cloudflare/account.d.ts +65 -0
- package/dist/cloudflare/account.d.ts.map +1 -0
- package/dist/cloudflare/api.d.ts +51 -0
- package/dist/cloudflare/api.d.ts.map +1 -0
- package/dist/cloudflare/auth.d.ts +35 -0
- package/dist/cloudflare/auth.d.ts.map +1 -0
- package/dist/cloudflare/index.d.ts +107 -0
- package/dist/cloudflare/index.d.ts.map +1 -0
- package/dist/cloudflare/index.js +13 -0
- package/dist/cloudflare/preferences.d.ts +46 -0
- package/dist/cloudflare/preferences.d.ts.map +1 -0
- package/dist/cloudflare/pricing.d.ts +15 -0
- package/dist/cloudflare/pricing.d.ts.map +1 -0
- package/dist/cloudflare/remote-config.d.ts +37 -0
- package/dist/cloudflare/remote-config.d.ts.map +1 -0
- package/dist/cloudflare/types.d.ts +161 -0
- package/dist/cloudflare/types.d.ts.map +1 -0
- package/dist/cloudflare/usage.d.ts +77 -0
- package/dist/cloudflare/usage.d.ts.map +1 -0
- package/dist/config/compiler.d.ts +146 -0
- package/dist/config/compiler.d.ts.map +1 -0
- package/dist/config/define.d.ts +44 -0
- package/dist/config/define.d.ts.map +1 -0
- package/dist/config/index.d.ts +6 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/loader.d.ts +52 -0
- package/dist/config/loader.d.ts.map +1 -0
- package/dist/config/ref.d.ts +160 -0
- package/dist/config/ref.d.ts.map +1 -0
- package/dist/config/schema.d.ts +3318 -0
- package/dist/config/schema.d.ts.map +1 -0
- package/dist/decorators/durable-object.d.ts +59 -0
- package/dist/decorators/durable-object.d.ts.map +1 -0
- package/dist/decorators/index.d.ts +3 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/index.js +9 -0
- package/dist/deploy-nhceck39.js +70 -0
- package/dist/dev-qnxet3j9.js +2096 -0
- package/dist/dev-server/index.d.ts +2 -0
- package/dist/dev-server/index.d.ts.map +1 -0
- package/dist/dev-server/server.d.ts +30 -0
- package/dist/dev-server/server.d.ts.map +1 -0
- package/dist/doctor-e8fy6fj5.js +186 -0
- package/dist/durable-object-t4kbb0yt.js +13 -0
- package/dist/env.d.ts +48 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/index-07q6yxyc.js +168 -0
- package/dist/index-1xpj0m4r.js +57 -0
- package/dist/index-37x76zdn.js +4 -0
- package/dist/index-3t6rypgc.js +13 -0
- package/dist/index-67qcae0f.js +183 -0
- package/dist/index-a855bdsx.js +18 -0
- package/dist/index-d8bdkx2h.js +109 -0
- package/dist/index-ep3445yc.js +2225 -0
- package/dist/index-gz1gndna.js +307 -0
- package/dist/index-hcex3rgh.js +266 -0
- package/dist/index-m2q41jwa.js +462 -0
- package/dist/index-n7rs26ft.js +77 -0
- package/dist/index-pf5s73n9.js +1413 -0
- package/dist/index-rbht7m9r.js +36 -0
- package/dist/index-tfyxa77h.js +850 -0
- package/dist/index-tk6ej9dj.js +94 -0
- package/dist/index-z14anrqp.js +226 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +298 -0
- package/dist/init-f9mgmew3.js +186 -0
- package/dist/remote-q59qk463.js +97 -0
- package/dist/runtime/context.d.ts +46 -0
- package/dist/runtime/context.d.ts.map +1 -0
- package/dist/runtime/exports.d.ts +118 -0
- package/dist/runtime/exports.d.ts.map +1 -0
- package/dist/runtime/index.d.ts +4 -0
- package/dist/runtime/index.d.ts.map +1 -0
- package/dist/runtime/index.js +111 -0
- package/dist/runtime/middleware.d.ts +82 -0
- package/dist/runtime/middleware.d.ts.map +1 -0
- package/dist/runtime/validation.d.ts +37 -0
- package/dist/runtime/validation.d.ts.map +1 -0
- package/dist/sveltekit/index.d.ts +2 -0
- package/dist/sveltekit/index.d.ts.map +1 -0
- package/dist/sveltekit/index.js +182 -0
- package/dist/sveltekit/platform.d.ts +141 -0
- package/dist/sveltekit/platform.d.ts.map +1 -0
- package/dist/test/bridge-context.d.ts +73 -0
- package/dist/test/bridge-context.d.ts.map +1 -0
- package/dist/test/cf.d.ts +130 -0
- package/dist/test/cf.d.ts.map +1 -0
- package/dist/test/email.d.ts +75 -0
- package/dist/test/email.d.ts.map +1 -0
- package/dist/test/index.d.ts +22 -0
- package/dist/test/index.d.ts.map +1 -0
- package/dist/test/index.js +71 -0
- package/dist/test/multi-worker-context.d.ts +114 -0
- package/dist/test/multi-worker-context.d.ts.map +1 -0
- package/dist/test/queue.d.ts +74 -0
- package/dist/test/queue.d.ts.map +1 -0
- package/dist/test/remote-ai.d.ts +6 -0
- package/dist/test/remote-ai.d.ts.map +1 -0
- package/dist/test/remote-vectorize.d.ts +6 -0
- package/dist/test/remote-vectorize.d.ts.map +1 -0
- package/dist/test/resolve-service-bindings.d.ts +68 -0
- package/dist/test/resolve-service-bindings.d.ts.map +1 -0
- package/dist/test/scheduled.d.ts +58 -0
- package/dist/test/scheduled.d.ts.map +1 -0
- package/dist/test/should-skip.d.ts +50 -0
- package/dist/test/should-skip.d.ts.map +1 -0
- package/dist/test/simple-context.d.ts +43 -0
- package/dist/test/simple-context.d.ts.map +1 -0
- package/dist/test/tail.d.ts +86 -0
- package/dist/test/tail.d.ts.map +1 -0
- package/dist/test/utilities.d.ts +99 -0
- package/dist/test/utilities.d.ts.map +1 -0
- package/dist/test/worker.d.ts +76 -0
- package/dist/test/worker.d.ts.map +1 -0
- package/dist/transform/durable-object.d.ts +46 -0
- package/dist/transform/durable-object.d.ts.map +1 -0
- package/dist/transform/index.d.ts +3 -0
- package/dist/transform/index.d.ts.map +1 -0
- package/dist/transform/worker-entrypoint.d.ts +66 -0
- package/dist/transform/worker-entrypoint.d.ts.map +1 -0
- package/dist/types-5nyrz1sz.js +454 -0
- package/dist/utils/entrypoint-discovery.d.ts +29 -0
- package/dist/utils/entrypoint-discovery.d.ts.map +1 -0
- package/dist/utils/glob.d.ts +33 -0
- package/dist/utils/glob.d.ts.map +1 -0
- package/dist/utils/resolve-package.d.ts +10 -0
- package/dist/utils/resolve-package.d.ts.map +1 -0
- package/dist/vite/index.d.ts +3 -0
- package/dist/vite/index.d.ts.map +1 -0
- package/dist/vite/index.js +339 -0
- package/dist/vite/plugin.d.ts +138 -0
- package/dist/vite/plugin.d.ts.map +1 -0
- package/dist/worker-entrypoint-m9th0rg0.js +13 -0
- package/dist/workerName.d.ts +17 -0
- package/dist/workerName.d.ts.map +1 -0
- package/package.json +111 -1
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
// src/transform/durable-object.ts
|
|
2
|
+
import MagicString from "magic-string";
|
|
3
|
+
var DO_CLASS_REGEX = /export\s+class\s+(\w+)\s+extends\s+DurableObject(?:<[^>]+>)?(?:\s+implements\s+[\w,\s]+)?\s*\{/g;
|
|
4
|
+
var DECORATOR_CLASS_REGEX = /@durableObject\s*\([^)]*\)\s*\n?\s*export\s+class\s+(\w+)/g;
|
|
5
|
+
function findDurableObjectClasses(code) {
|
|
6
|
+
const classes = new Set;
|
|
7
|
+
DO_CLASS_REGEX.lastIndex = 0;
|
|
8
|
+
DECORATOR_CLASS_REGEX.lastIndex = 0;
|
|
9
|
+
let match;
|
|
10
|
+
while ((match = DO_CLASS_REGEX.exec(code)) !== null) {
|
|
11
|
+
classes.add(match[1]);
|
|
12
|
+
}
|
|
13
|
+
while ((match = DECORATOR_CLASS_REGEX.exec(code)) !== null) {
|
|
14
|
+
classes.add(match[1]);
|
|
15
|
+
}
|
|
16
|
+
return Array.from(classes);
|
|
17
|
+
}
|
|
18
|
+
function findDurableObjectClassesDetailed(code) {
|
|
19
|
+
const classMap = new Map;
|
|
20
|
+
DO_CLASS_REGEX.lastIndex = 0;
|
|
21
|
+
DECORATOR_CLASS_REGEX.lastIndex = 0;
|
|
22
|
+
let match;
|
|
23
|
+
while ((match = DO_CLASS_REGEX.exec(code)) !== null) {
|
|
24
|
+
const name = match[1];
|
|
25
|
+
classMap.set(name, {
|
|
26
|
+
name,
|
|
27
|
+
extendsBase: true,
|
|
28
|
+
hasDecorator: false
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const decoratorWithOptionsRegex = /@durableObject\s*\((\{[^}]*\})?\)\s*\n?\s*export\s+class\s+(\w+)/g;
|
|
32
|
+
while ((match = decoratorWithOptionsRegex.exec(code)) !== null) {
|
|
33
|
+
const optionsStr = match[1];
|
|
34
|
+
const name = match[2];
|
|
35
|
+
const existing = classMap.get(name);
|
|
36
|
+
if (existing) {
|
|
37
|
+
existing.hasDecorator = true;
|
|
38
|
+
if (optionsStr) {
|
|
39
|
+
try {
|
|
40
|
+
existing.decoratorOptions = parseSimpleObject(optionsStr);
|
|
41
|
+
} catch {}
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
const info = {
|
|
45
|
+
name,
|
|
46
|
+
extendsBase: false,
|
|
47
|
+
hasDecorator: true
|
|
48
|
+
};
|
|
49
|
+
if (optionsStr) {
|
|
50
|
+
try {
|
|
51
|
+
info.decoratorOptions = parseSimpleObject(optionsStr);
|
|
52
|
+
} catch {}
|
|
53
|
+
}
|
|
54
|
+
classMap.set(name, info);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return Array.from(classMap.values());
|
|
58
|
+
}
|
|
59
|
+
function parseSimpleObject(str) {
|
|
60
|
+
const result = {};
|
|
61
|
+
const inner = str.trim().slice(1, -1);
|
|
62
|
+
let depth = 0;
|
|
63
|
+
let current = "";
|
|
64
|
+
const pairs = [];
|
|
65
|
+
for (const char of inner) {
|
|
66
|
+
if (char === "[")
|
|
67
|
+
depth++;
|
|
68
|
+
else if (char === "]")
|
|
69
|
+
depth--;
|
|
70
|
+
else if (char === "," && depth === 0) {
|
|
71
|
+
pairs.push(current.trim());
|
|
72
|
+
current = "";
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
current += char;
|
|
76
|
+
}
|
|
77
|
+
if (current.trim())
|
|
78
|
+
pairs.push(current.trim());
|
|
79
|
+
for (const pair of pairs) {
|
|
80
|
+
const colonIndex = pair.indexOf(":");
|
|
81
|
+
if (colonIndex === -1)
|
|
82
|
+
continue;
|
|
83
|
+
const key = pair.slice(0, colonIndex).trim();
|
|
84
|
+
const valueStr = pair.slice(colonIndex + 1).trim();
|
|
85
|
+
if (valueStr === "true") {
|
|
86
|
+
result[key] = true;
|
|
87
|
+
} else if (valueStr === "false") {
|
|
88
|
+
result[key] = false;
|
|
89
|
+
} else if (valueStr.startsWith("[")) {
|
|
90
|
+
const arrayContent = valueStr.slice(1, -1);
|
|
91
|
+
result[key] = arrayContent.split(",").map((s) => s.trim().replace(/^['"]|['"]$/g, ""));
|
|
92
|
+
} else if (valueStr.startsWith("'") || valueStr.startsWith('"')) {
|
|
93
|
+
result[key] = valueStr.slice(1, -1);
|
|
94
|
+
} else if (!isNaN(Number(valueStr))) {
|
|
95
|
+
result[key] = Number(valueStr);
|
|
96
|
+
} else {
|
|
97
|
+
result[key] = valueStr;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
function generateWrapper(className, options = {}) {
|
|
103
|
+
const includeAlarms = options.alarms ?? false;
|
|
104
|
+
const includeWebsockets = options.websockets ?? false;
|
|
105
|
+
let wrapper = `
|
|
106
|
+
// ============ Devflare DO Wrapper for ${className} ============
|
|
107
|
+
import { runWithContext } from 'devflare/runtime'
|
|
108
|
+
|
|
109
|
+
const __Original${className} = ${className}
|
|
110
|
+
|
|
111
|
+
class ${className}Wrapper extends __Original${className} {
|
|
112
|
+
async fetch(request: Request): Promise<Response> {
|
|
113
|
+
return runWithContext(
|
|
114
|
+
this.env,
|
|
115
|
+
this.ctx,
|
|
116
|
+
request,
|
|
117
|
+
() => super.fetch(request),
|
|
118
|
+
'fetch'
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
`;
|
|
122
|
+
if (includeAlarms) {
|
|
123
|
+
wrapper += `
|
|
124
|
+
async alarm(): Promise<void> {
|
|
125
|
+
return runWithContext(
|
|
126
|
+
this.env,
|
|
127
|
+
this.ctx,
|
|
128
|
+
null,
|
|
129
|
+
() => super.alarm?.() ?? Promise.resolve(),
|
|
130
|
+
'scheduled'
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
`;
|
|
134
|
+
}
|
|
135
|
+
if (includeWebsockets) {
|
|
136
|
+
wrapper += `
|
|
137
|
+
async webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): Promise<void> {
|
|
138
|
+
return runWithContext(
|
|
139
|
+
this.env,
|
|
140
|
+
this.ctx,
|
|
141
|
+
null,
|
|
142
|
+
() => super.webSocketMessage?.(ws, message) ?? Promise.resolve(),
|
|
143
|
+
'fetch'
|
|
144
|
+
)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise<void> {
|
|
148
|
+
return runWithContext(
|
|
149
|
+
this.env,
|
|
150
|
+
this.ctx,
|
|
151
|
+
null,
|
|
152
|
+
() => super.webSocketClose?.(ws, code, reason, wasClean) ?? Promise.resolve(),
|
|
153
|
+
'fetch'
|
|
154
|
+
)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async webSocketError(ws: WebSocket, error: unknown): Promise<void> {
|
|
158
|
+
return runWithContext(
|
|
159
|
+
this.env,
|
|
160
|
+
this.ctx,
|
|
161
|
+
null,
|
|
162
|
+
() => super.webSocketError?.(ws, error) ?? Promise.resolve(),
|
|
163
|
+
'fetch'
|
|
164
|
+
)
|
|
165
|
+
}
|
|
166
|
+
`;
|
|
167
|
+
}
|
|
168
|
+
wrapper += `}
|
|
169
|
+
|
|
170
|
+
export { ${className}Wrapper as ${className} }
|
|
171
|
+
// ============ End Devflare DO Wrapper ============
|
|
172
|
+
`;
|
|
173
|
+
return wrapper;
|
|
174
|
+
}
|
|
175
|
+
async function transformDurableObject(code, id) {
|
|
176
|
+
const doClasses = findDurableObjectClassesDetailed(code);
|
|
177
|
+
if (doClasses.length === 0) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const s = new MagicString(code);
|
|
181
|
+
for (const classInfo of doClasses) {
|
|
182
|
+
const className = classInfo.name;
|
|
183
|
+
if (classInfo.extendsBase) {
|
|
184
|
+
const exportPattern = new RegExp(`export\\s+class\\s+${className}\\s+extends\\s+DurableObject`, "g");
|
|
185
|
+
let match;
|
|
186
|
+
while ((match = exportPattern.exec(code)) !== null) {
|
|
187
|
+
const start = match.index;
|
|
188
|
+
const exportKeywordEnd = start + "export ".length;
|
|
189
|
+
s.overwrite(start, exportKeywordEnd, "");
|
|
190
|
+
const classNameStart = start + match[0].indexOf(className);
|
|
191
|
+
const classNameEnd = classNameStart + className.length;
|
|
192
|
+
s.overwrite(classNameStart, classNameEnd, `__Original${className}`);
|
|
193
|
+
}
|
|
194
|
+
} else if (classInfo.hasDecorator) {
|
|
195
|
+
const decoratorPattern = new RegExp(`@durableObject\\s*\\([^)]*\\)\\s*\\n?\\s*export\\s+class\\s+${className}(?:\\s+extends\\s+(\\w+))?`, "g");
|
|
196
|
+
let match;
|
|
197
|
+
while ((match = decoratorPattern.exec(code)) !== null) {
|
|
198
|
+
const start = match.index;
|
|
199
|
+
const existingBaseClass = match[1];
|
|
200
|
+
const decoratorEnd = code.indexOf(")", start) + 1;
|
|
201
|
+
const exportStart = code.indexOf("export", decoratorEnd);
|
|
202
|
+
const classDefEnd = start + match[0].length;
|
|
203
|
+
s.overwrite(start, exportStart, "");
|
|
204
|
+
const classNameStart = code.indexOf(className, exportStart);
|
|
205
|
+
const classNameEnd = classNameStart + className.length;
|
|
206
|
+
if (existingBaseClass) {
|
|
207
|
+
s.overwrite(classNameStart, classNameEnd, `__Original${className}`);
|
|
208
|
+
} else {
|
|
209
|
+
s.overwrite(classNameStart, classDefEnd, `__Original${className} extends DurableObject`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const imports = `
|
|
215
|
+
import { runWithContext } from 'devflare/runtime'
|
|
216
|
+
`;
|
|
217
|
+
s.prepend(imports);
|
|
218
|
+
for (const classInfo of doClasses) {
|
|
219
|
+
const className = classInfo.name;
|
|
220
|
+
const options = classInfo.decoratorOptions || {};
|
|
221
|
+
const includeAlarms = options.alarms === true;
|
|
222
|
+
const includeWebsockets = options.websockets === true;
|
|
223
|
+
const wrapper = generateWrapperCodeInternal(className, {
|
|
224
|
+
alarms: includeAlarms,
|
|
225
|
+
websockets: includeWebsockets
|
|
226
|
+
});
|
|
227
|
+
s.append(wrapper);
|
|
228
|
+
}
|
|
229
|
+
return {
|
|
230
|
+
code: s.toString(),
|
|
231
|
+
map: s.generateMap({
|
|
232
|
+
source: id,
|
|
233
|
+
file: id + ".map",
|
|
234
|
+
includeContent: true
|
|
235
|
+
})
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
function generateWrapperCodeInternal(className, options) {
|
|
239
|
+
let wrapper = `
|
|
240
|
+
|
|
241
|
+
// ============ Devflare DO Wrapper for ${className} ============
|
|
242
|
+
class ${className}Wrapper extends __Original${className} {
|
|
243
|
+
async fetch(request: Request): Promise<Response> {
|
|
244
|
+
return runWithContext(
|
|
245
|
+
this.env,
|
|
246
|
+
this.ctx,
|
|
247
|
+
request,
|
|
248
|
+
() => super.fetch(request),
|
|
249
|
+
'fetch'
|
|
250
|
+
)
|
|
251
|
+
}
|
|
252
|
+
`;
|
|
253
|
+
if (options.alarms) {
|
|
254
|
+
wrapper += `
|
|
255
|
+
async alarm(): Promise<void> {
|
|
256
|
+
return runWithContext(
|
|
257
|
+
this.env,
|
|
258
|
+
this.ctx,
|
|
259
|
+
null,
|
|
260
|
+
() => super.alarm?.() ?? Promise.resolve(),
|
|
261
|
+
'scheduled'
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
`;
|
|
265
|
+
}
|
|
266
|
+
if (options.websockets) {
|
|
267
|
+
wrapper += `
|
|
268
|
+
async webSocketMessage(ws: WebSocket, message: string | ArrayBuffer): Promise<void> {
|
|
269
|
+
return runWithContext(
|
|
270
|
+
this.env,
|
|
271
|
+
this.ctx,
|
|
272
|
+
null,
|
|
273
|
+
() => super.webSocketMessage?.(ws, message) ?? Promise.resolve(),
|
|
274
|
+
'fetch'
|
|
275
|
+
)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
async webSocketClose(ws: WebSocket, code: number, reason: string, wasClean: boolean): Promise<void> {
|
|
279
|
+
return runWithContext(
|
|
280
|
+
this.env,
|
|
281
|
+
this.ctx,
|
|
282
|
+
null,
|
|
283
|
+
() => super.webSocketClose?.(ws, code, reason, wasClean) ?? Promise.resolve(),
|
|
284
|
+
'fetch'
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async webSocketError(ws: WebSocket, error: unknown): Promise<void> {
|
|
289
|
+
return runWithContext(
|
|
290
|
+
this.env,
|
|
291
|
+
this.ctx,
|
|
292
|
+
null,
|
|
293
|
+
() => super.webSocketError?.(ws, error) ?? Promise.resolve(),
|
|
294
|
+
'fetch'
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
`;
|
|
298
|
+
}
|
|
299
|
+
wrapper += `}
|
|
300
|
+
|
|
301
|
+
export { ${className}Wrapper as ${className} }
|
|
302
|
+
// ============ End Devflare DO Wrapper ============
|
|
303
|
+
`;
|
|
304
|
+
return wrapper;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export { findDurableObjectClasses, findDurableObjectClassesDetailed, generateWrapper, transformDurableObject };
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// src/config/schema.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var dateRegex = /^\d{4}-\d{2}-\d{2}$/;
|
|
4
|
+
var compatibilityDateSchema = z.string().regex(dateRegex, {
|
|
5
|
+
message: "Compatibility date must be in YYYY-MM-DD format"
|
|
6
|
+
});
|
|
7
|
+
var routesConfigSchema = z.object({
|
|
8
|
+
dir: z.string(),
|
|
9
|
+
prefix: z.string().optional()
|
|
10
|
+
});
|
|
11
|
+
var filesSchema = z.object({
|
|
12
|
+
fetch: z.union([z.string(), z.literal(false)]).optional(),
|
|
13
|
+
queue: z.union([z.string(), z.literal(false)]).optional(),
|
|
14
|
+
scheduled: z.union([z.string(), z.literal(false)]).optional(),
|
|
15
|
+
email: z.union([z.string(), z.literal(false)]).optional(),
|
|
16
|
+
durableObjects: z.union([z.string(), z.literal(false)]).optional(),
|
|
17
|
+
entrypoints: z.union([z.string(), z.literal(false)]).optional(),
|
|
18
|
+
workflows: z.union([z.string(), z.literal(false)]).optional(),
|
|
19
|
+
routes: z.union([routesConfigSchema, z.literal(false)]).optional(),
|
|
20
|
+
transport: z.string().optional()
|
|
21
|
+
}).optional();
|
|
22
|
+
var durableObjectBindingSchema = z.custom((val) => {
|
|
23
|
+
if (typeof val === "string")
|
|
24
|
+
return true;
|
|
25
|
+
if (val && typeof val === "object" && "className" in val) {
|
|
26
|
+
const obj = val;
|
|
27
|
+
return typeof obj.className === "string";
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}, {
|
|
31
|
+
message: "Expected string or { className: string, scriptName?: string }"
|
|
32
|
+
});
|
|
33
|
+
var queueConsumerSchema = z.object({
|
|
34
|
+
queue: z.string(),
|
|
35
|
+
maxBatchSize: z.number().optional(),
|
|
36
|
+
maxBatchTimeout: z.number().optional(),
|
|
37
|
+
maxRetries: z.number().optional(),
|
|
38
|
+
deadLetterQueue: z.string().optional(),
|
|
39
|
+
maxConcurrency: z.number().optional(),
|
|
40
|
+
retryDelay: z.number().optional()
|
|
41
|
+
});
|
|
42
|
+
var queuesConfigSchema = z.object({
|
|
43
|
+
producers: z.record(z.string(), z.string()).optional(),
|
|
44
|
+
consumers: z.array(queueConsumerSchema).optional()
|
|
45
|
+
});
|
|
46
|
+
var serviceBindingSchema = z.custom((val) => {
|
|
47
|
+
if (typeof val !== "object" && typeof val !== "function")
|
|
48
|
+
return false;
|
|
49
|
+
const obj = val;
|
|
50
|
+
const service = obj.service;
|
|
51
|
+
if (typeof service !== "string")
|
|
52
|
+
return false;
|
|
53
|
+
return true;
|
|
54
|
+
}, {
|
|
55
|
+
message: "Expected service binding object with { service: string } or ref().worker"
|
|
56
|
+
});
|
|
57
|
+
var aiBindingSchema = z.object({
|
|
58
|
+
binding: z.string()
|
|
59
|
+
});
|
|
60
|
+
var vectorizeBindingSchema = z.object({
|
|
61
|
+
indexName: z.string()
|
|
62
|
+
});
|
|
63
|
+
var hyperdriveBindingSchema = z.object({
|
|
64
|
+
id: z.string()
|
|
65
|
+
});
|
|
66
|
+
var browserBindingSchema = z.object({
|
|
67
|
+
binding: z.string()
|
|
68
|
+
});
|
|
69
|
+
var analyticsBindingSchema = z.object({
|
|
70
|
+
dataset: z.string()
|
|
71
|
+
});
|
|
72
|
+
var sendEmailBindingSchema = z.object({
|
|
73
|
+
destinationAddress: z.string().optional()
|
|
74
|
+
});
|
|
75
|
+
var bindingsSchema = z.object({
|
|
76
|
+
kv: z.record(z.string(), z.string()).optional(),
|
|
77
|
+
d1: z.record(z.string(), z.string()).optional(),
|
|
78
|
+
r2: z.record(z.string(), z.string()).optional(),
|
|
79
|
+
durableObjects: z.record(z.string(), durableObjectBindingSchema).optional(),
|
|
80
|
+
queues: queuesConfigSchema.optional(),
|
|
81
|
+
services: z.record(z.string(), serviceBindingSchema).optional(),
|
|
82
|
+
ai: aiBindingSchema.optional(),
|
|
83
|
+
vectorize: z.record(z.string(), vectorizeBindingSchema).optional(),
|
|
84
|
+
hyperdrive: z.record(z.string(), hyperdriveBindingSchema).optional(),
|
|
85
|
+
browser: browserBindingSchema.optional(),
|
|
86
|
+
analyticsEngine: z.record(z.string(), analyticsBindingSchema).optional(),
|
|
87
|
+
sendEmail: z.record(z.string(), sendEmailBindingSchema).optional()
|
|
88
|
+
}).optional();
|
|
89
|
+
var triggersSchema = z.object({
|
|
90
|
+
crons: z.array(z.string()).optional()
|
|
91
|
+
}).optional();
|
|
92
|
+
var secretConfigSchema = z.object({
|
|
93
|
+
required: z.boolean().optional().default(true)
|
|
94
|
+
});
|
|
95
|
+
var routeConfigSchema = z.object({
|
|
96
|
+
pattern: z.string(),
|
|
97
|
+
zone_name: z.string().optional(),
|
|
98
|
+
zone_id: z.string().optional(),
|
|
99
|
+
custom_domain: z.boolean().optional()
|
|
100
|
+
});
|
|
101
|
+
var wsRouteConfigSchema = z.object({
|
|
102
|
+
pattern: z.string(),
|
|
103
|
+
doNamespace: z.string(),
|
|
104
|
+
idParam: z.string().default("id"),
|
|
105
|
+
forwardPath: z.string().default("/websocket")
|
|
106
|
+
});
|
|
107
|
+
var assetsConfigSchema = z.object({
|
|
108
|
+
directory: z.string(),
|
|
109
|
+
binding: z.string().optional()
|
|
110
|
+
}).optional();
|
|
111
|
+
var observabilitySchema = z.object({
|
|
112
|
+
enabled: z.boolean().optional(),
|
|
113
|
+
head_sampling_rate: z.number().min(0).max(1).optional()
|
|
114
|
+
}).optional();
|
|
115
|
+
var limitsSchema = z.object({
|
|
116
|
+
cpu_ms: z.number().optional()
|
|
117
|
+
}).optional();
|
|
118
|
+
var buildConfigSchema = z.object({
|
|
119
|
+
target: z.string().optional(),
|
|
120
|
+
minify: z.boolean().optional(),
|
|
121
|
+
sourcemap: z.boolean().optional(),
|
|
122
|
+
rolldownOptions: z.record(z.string(), z.unknown()).optional()
|
|
123
|
+
}).optional();
|
|
124
|
+
var migrationSchema = z.object({
|
|
125
|
+
tag: z.string(),
|
|
126
|
+
new_classes: z.array(z.string()).optional(),
|
|
127
|
+
renamed_classes: z.array(z.object({
|
|
128
|
+
from: z.string(),
|
|
129
|
+
to: z.string()
|
|
130
|
+
})).optional(),
|
|
131
|
+
deleted_classes: z.array(z.string()).optional(),
|
|
132
|
+
new_sqlite_classes: z.array(z.string()).optional()
|
|
133
|
+
});
|
|
134
|
+
var wranglerConfigSchema = z.object({
|
|
135
|
+
passthrough: z.record(z.string(), z.unknown()).optional()
|
|
136
|
+
}).optional();
|
|
137
|
+
var envConfigSchemaInner = z.object({
|
|
138
|
+
name: z.string().optional(),
|
|
139
|
+
compatibilityDate: compatibilityDateSchema.optional(),
|
|
140
|
+
compatibilityFlags: z.array(z.string()).optional(),
|
|
141
|
+
files: filesSchema,
|
|
142
|
+
bindings: bindingsSchema,
|
|
143
|
+
triggers: triggersSchema,
|
|
144
|
+
vars: z.record(z.string(), z.string()).optional(),
|
|
145
|
+
secrets: z.record(z.string(), secretConfigSchema).optional(),
|
|
146
|
+
routes: z.array(routeConfigSchema).optional(),
|
|
147
|
+
assets: assetsConfigSchema,
|
|
148
|
+
limits: limitsSchema,
|
|
149
|
+
observability: observabilitySchema,
|
|
150
|
+
migrations: z.array(migrationSchema).optional(),
|
|
151
|
+
build: buildConfigSchema,
|
|
152
|
+
wrangler: wranglerConfigSchema
|
|
153
|
+
}).partial();
|
|
154
|
+
function getCurrentDate() {
|
|
155
|
+
const now = new Date;
|
|
156
|
+
return now.toISOString().split("T")[0];
|
|
157
|
+
}
|
|
158
|
+
var FORCED_COMPATIBILITY_FLAGS = ["nodejs_compat", "nodejs_als"];
|
|
159
|
+
var configSchema = z.object({
|
|
160
|
+
name: z.string({
|
|
161
|
+
required_error: "Worker name is required"
|
|
162
|
+
}),
|
|
163
|
+
accountId: z.string().optional(),
|
|
164
|
+
compatibilityDate: compatibilityDateSchema.optional().default(getCurrentDate),
|
|
165
|
+
compatibilityFlags: z.array(z.string()).optional().transform((flags = []) => {
|
|
166
|
+
const merged = new Set([...FORCED_COMPATIBILITY_FLAGS, ...flags]);
|
|
167
|
+
return [...merged];
|
|
168
|
+
}),
|
|
169
|
+
files: filesSchema,
|
|
170
|
+
bindings: bindingsSchema,
|
|
171
|
+
triggers: triggersSchema,
|
|
172
|
+
vars: z.record(z.string(), z.string()).optional(),
|
|
173
|
+
secrets: z.record(z.string(), secretConfigSchema).optional(),
|
|
174
|
+
routes: z.array(routeConfigSchema).optional(),
|
|
175
|
+
wsRoutes: z.array(wsRouteConfigSchema).optional(),
|
|
176
|
+
assets: assetsConfigSchema,
|
|
177
|
+
limits: limitsSchema,
|
|
178
|
+
observability: observabilitySchema,
|
|
179
|
+
migrations: z.array(migrationSchema).optional(),
|
|
180
|
+
build: buildConfigSchema,
|
|
181
|
+
plugins: z.array(z.unknown()).optional(),
|
|
182
|
+
env: z.record(z.string(), envConfigSchemaInner).optional(),
|
|
183
|
+
wrangler: wranglerConfigSchema
|
|
184
|
+
});
|
|
185
|
+
function normalizeDOBinding(config) {
|
|
186
|
+
if (typeof config === "string") {
|
|
187
|
+
return { className: config };
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
className: config.className,
|
|
191
|
+
scriptName: config.scriptName,
|
|
192
|
+
__ref: config.__ref
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// src/config/loader.ts
|
|
197
|
+
import { loadConfig as c12LoadConfig } from "c12";
|
|
198
|
+
import { existsSync } from "node:fs";
|
|
199
|
+
import { join } from "pathe";
|
|
200
|
+
var CONFIG_FILES = [
|
|
201
|
+
"devflare.config.ts",
|
|
202
|
+
"devflare.config.mts",
|
|
203
|
+
"devflare.config.js",
|
|
204
|
+
"devflare.config.mjs"
|
|
205
|
+
];
|
|
206
|
+
async function resolveConfigPath(cwd) {
|
|
207
|
+
for (const file of CONFIG_FILES) {
|
|
208
|
+
const path = join(cwd, file);
|
|
209
|
+
if (existsSync(path)) {
|
|
210
|
+
return path;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
async function loadConfig(options = {}) {
|
|
216
|
+
const cwd = options.cwd ?? process.cwd();
|
|
217
|
+
const configFile = options.configFile ?? "devflare.config";
|
|
218
|
+
const { config, configFile: loadedFile } = await c12LoadConfig({
|
|
219
|
+
name: "devflare",
|
|
220
|
+
cwd,
|
|
221
|
+
configFile,
|
|
222
|
+
defaultConfig: undefined,
|
|
223
|
+
rcFile: false,
|
|
224
|
+
globalRc: false,
|
|
225
|
+
dotenv: false
|
|
226
|
+
});
|
|
227
|
+
if (!config || !loadedFile) {
|
|
228
|
+
throw new ConfigNotFoundError(cwd, configFile);
|
|
229
|
+
}
|
|
230
|
+
const result = configSchema.safeParse(config);
|
|
231
|
+
if (!result.success) {
|
|
232
|
+
throw new ConfigValidationError(result.error.issues, loadedFile);
|
|
233
|
+
}
|
|
234
|
+
return result.data;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
class ConfigNotFoundError extends Error {
|
|
238
|
+
cwd;
|
|
239
|
+
configFile;
|
|
240
|
+
code = "CONFIG_NOT_FOUND";
|
|
241
|
+
constructor(cwd, configFile) {
|
|
242
|
+
super(`Config file not found in ${cwd}.
|
|
243
|
+
` + `Expected one of: ${CONFIG_FILES.join(", ")}
|
|
244
|
+
` + `Run 'devflare init' to create a new config.`);
|
|
245
|
+
this.cwd = cwd;
|
|
246
|
+
this.configFile = configFile;
|
|
247
|
+
this.name = "ConfigNotFoundError";
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
class ConfigValidationError extends Error {
|
|
252
|
+
issues;
|
|
253
|
+
configFile;
|
|
254
|
+
code = "CONFIG_VALIDATION_ERROR";
|
|
255
|
+
constructor(issues, configFile) {
|
|
256
|
+
const issueMessages = issues.map((i) => ` - ${i.path.join(".")}: ${i.message}`).join(`
|
|
257
|
+
`);
|
|
258
|
+
super(`Invalid config in ${configFile}:
|
|
259
|
+
${issueMessages}`);
|
|
260
|
+
this.issues = issues;
|
|
261
|
+
this.configFile = configFile;
|
|
262
|
+
this.name = "ConfigValidationError";
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export { configSchema, normalizeDOBinding, resolveConfigPath, loadConfig, ConfigNotFoundError, ConfigValidationError };
|