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.
- package/LLM.md +775 -637
- package/R2.md +200 -0
- package/README.md +285 -514
- 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-k36xrzvy.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 +2575 -1221
- package/dist/config/schema.d.ts.map +1 -1
- package/dist/{deploy-nhceck39.js → deploy-dbvfq8vq.js} +33 -15
- package/dist/{dev-qnxet3j9.js → dev-rk8p6pse.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-06y8nxd4.js} +73 -50
- package/dist/{durable-object-t4kbb0yt.js → durable-object-yt8v1dyn.js} +1 -1
- package/dist/index-05fyzwne.js +195 -0
- package/dist/index-1p814k7s.js +227 -0
- package/dist/{index-hcex3rgh.js → index-1phx14av.js} +84 -7
- package/dist/{index-tk6ej9dj.js → index-2q3pmzrx.js} +12 -16
- 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-67qcae0f.js → index-6v3wjg1r.js} +16 -1
- package/dist/index-8gtqgb3q.js +529 -0
- package/dist/{index-gz1gndna.js → index-9wt9x09k.js} +42 -62
- package/dist/index-fef08w43.js +231 -0
- package/dist/{index-ep3445yc.js → index-jht2j546.js} +393 -170
- package/dist/index-k7r18na8.js +0 -0
- package/dist/{index-m2q41jwa.js → index-n932ytmq.js} +9 -1
- package/dist/index-pwgyy2q9.js +39 -0
- package/dist/{index-07q6yxyc.js → index-v8vvsn9x.js} +1 -0
- package/dist/index-vky23txa.js +70 -0
- package/dist/index-vs49yxn4.js +322 -0
- package/dist/{index-z14anrqp.js → index-wfbfz02q.js} +14 -15
- package/dist/index-ws68xvq2.js +311 -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-x9q7t491.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
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
normalizeDOBinding
|
|
3
|
-
} from "./index-hcex3rgh.js";
|
|
4
|
-
import {
|
|
5
|
-
__require
|
|
6
|
-
} from "./index-37x76zdn.js";
|
|
7
|
-
|
|
8
1
|
// src/bridge/protocol.ts
|
|
9
2
|
var BinaryKind = {
|
|
10
3
|
StreamChunk: 1,
|
|
@@ -1016,6 +1009,13 @@ function createAIProxy(client, bindingName) {
|
|
|
1016
1009
|
}
|
|
1017
1010
|
};
|
|
1018
1011
|
}
|
|
1012
|
+
function createSendEmailProxy(client, bindingName) {
|
|
1013
|
+
return {
|
|
1014
|
+
async send(message) {
|
|
1015
|
+
return client.call(`${bindingName}.email.send`, [message]);
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
1019
|
var globalBindingHints = {};
|
|
1020
1020
|
function createEnvProxy(options = {}) {
|
|
1021
1021
|
const client = options.client ?? getClient();
|
|
@@ -1050,6 +1050,9 @@ function createEnvProxy(options = {}) {
|
|
|
1050
1050
|
case "ai":
|
|
1051
1051
|
proxy = createAIProxy(client, prop);
|
|
1052
1052
|
break;
|
|
1053
|
+
case "sendEmail":
|
|
1054
|
+
proxy = createSendEmailProxy(client, prop);
|
|
1055
|
+
break;
|
|
1053
1056
|
case "secret":
|
|
1054
1057
|
case "var":
|
|
1055
1058
|
proxy = createSimpleBindingProxy(client, prop);
|
|
@@ -1137,277 +1140,4 @@ function setBindingHints(hints) {
|
|
|
1137
1140
|
globalEnvProxy = null;
|
|
1138
1141
|
}
|
|
1139
1142
|
|
|
1140
|
-
|
|
1141
|
-
function generateGatewayScript() {
|
|
1142
|
-
return `
|
|
1143
|
-
// Gateway Worker — Provides RPC access to all bindings
|
|
1144
|
-
export default {
|
|
1145
|
-
async fetch(request, env, ctx) {
|
|
1146
|
-
const url = new URL(request.url)
|
|
1147
|
-
|
|
1148
|
-
// Health check
|
|
1149
|
-
if (url.pathname === '/_devflare/health') {
|
|
1150
|
-
return new Response(JSON.stringify({ status: 'ok', bindings: Object.keys(env) }), {
|
|
1151
|
-
headers: { 'Content-Type': 'application/json' }
|
|
1152
|
-
})
|
|
1153
|
-
}
|
|
1154
|
-
|
|
1155
|
-
// RPC endpoint
|
|
1156
|
-
if (url.pathname === '/_devflare/rpc' && request.method === 'POST') {
|
|
1157
|
-
try {
|
|
1158
|
-
const { method, params } = await request.json()
|
|
1159
|
-
const result = await executeRpc(env, method, params)
|
|
1160
|
-
return new Response(JSON.stringify({ ok: true, result }), {
|
|
1161
|
-
headers: { 'Content-Type': 'application/json' }
|
|
1162
|
-
})
|
|
1163
|
-
} catch (error) {
|
|
1164
|
-
return new Response(JSON.stringify({
|
|
1165
|
-
ok: false,
|
|
1166
|
-
error: { code: 'RPC_ERROR', message: error.message }
|
|
1167
|
-
}), {
|
|
1168
|
-
status: 500,
|
|
1169
|
-
headers: { 'Content-Type': 'application/json' }
|
|
1170
|
-
})
|
|
1171
|
-
}
|
|
1172
|
-
}
|
|
1173
|
-
|
|
1174
|
-
return new Response('Devflare Gateway', { status: 200 })
|
|
1175
|
-
}
|
|
1176
|
-
}
|
|
1177
|
-
|
|
1178
|
-
async function executeRpc(env, method, params) {
|
|
1179
|
-
const [bindingName, ...methodPath] = method.split('.')
|
|
1180
|
-
const binding = env[bindingName]
|
|
1181
|
-
|
|
1182
|
-
if (!binding) {
|
|
1183
|
-
throw new Error(\`Binding "\${bindingName}" not found\`)
|
|
1184
|
-
}
|
|
1185
|
-
|
|
1186
|
-
const methodName = methodPath.join('.')
|
|
1187
|
-
|
|
1188
|
-
// KV operations
|
|
1189
|
-
if (methodName === 'get') return binding.get(params[0], params[1])
|
|
1190
|
-
if (methodName === 'put') return binding.put(params[0], params[1], params[2])
|
|
1191
|
-
if (methodName === 'delete') return binding.delete(params[0])
|
|
1192
|
-
if (methodName === 'list') return binding.list(params[0])
|
|
1193
|
-
if (methodName === 'getWithMetadata') return binding.getWithMetadata(params[0], params[1])
|
|
1194
|
-
|
|
1195
|
-
// R2 operations
|
|
1196
|
-
if (methodName === 'head') return binding.head(params[0])
|
|
1197
|
-
if (methodName === 'r2.get') return serializeR2Object(await binding.get(params[0], params[1]))
|
|
1198
|
-
if (methodName === 'r2.put') return serializeR2Object(await binding.put(params[0], params[1], params[2]))
|
|
1199
|
-
if (methodName === 'r2.delete') return binding.delete(params[0])
|
|
1200
|
-
if (methodName === 'r2.list') return serializeR2Objects(await binding.list(params[0]))
|
|
1201
|
-
|
|
1202
|
-
// D1 operations
|
|
1203
|
-
if (methodName === 'exec') return binding.exec(params[0])
|
|
1204
|
-
if (methodName === 'batch') {
|
|
1205
|
-
const statements = params[0].map(s => binding.prepare(s.sql).bind(...(s.bindings || [])))
|
|
1206
|
-
return binding.batch(statements)
|
|
1207
|
-
}
|
|
1208
|
-
if (methodName.startsWith('stmt.')) {
|
|
1209
|
-
const [, stmtMethod] = methodName.split('.')
|
|
1210
|
-
const [sql, ...bindings] = params
|
|
1211
|
-
const stmt = binding.prepare(sql).bind(...bindings.slice(0, -1))
|
|
1212
|
-
|
|
1213
|
-
if (stmtMethod === 'first') return stmt.first(bindings[bindings.length - 1])
|
|
1214
|
-
if (stmtMethod === 'all') return stmt.all()
|
|
1215
|
-
if (stmtMethod === 'run') return stmt.run()
|
|
1216
|
-
if (stmtMethod === 'raw') return stmt.raw(bindings[bindings.length - 1])
|
|
1217
|
-
}
|
|
1218
|
-
|
|
1219
|
-
// DO operations
|
|
1220
|
-
if (methodName === 'idFromName') {
|
|
1221
|
-
const id = binding.idFromName(params[0])
|
|
1222
|
-
return { __type: 'DOId', hex: id.toString() }
|
|
1223
|
-
}
|
|
1224
|
-
if (methodName === 'idFromString') {
|
|
1225
|
-
const id = binding.idFromString(params[0])
|
|
1226
|
-
return { __type: 'DOId', hex: id.toString() }
|
|
1227
|
-
}
|
|
1228
|
-
if (methodName === 'newUniqueId') {
|
|
1229
|
-
const id = binding.newUniqueId(params[0])
|
|
1230
|
-
return { __type: 'DOId', hex: id.toString() }
|
|
1231
|
-
}
|
|
1232
|
-
if (methodName === 'stub.fetch') {
|
|
1233
|
-
const [, doId, serializedReq] = params
|
|
1234
|
-
const id = binding.idFromString(doId.hex)
|
|
1235
|
-
const stub = binding.get(id)
|
|
1236
|
-
const response = await stub.fetch(new Request(serializedReq.url, {
|
|
1237
|
-
method: serializedReq.method,
|
|
1238
|
-
headers: serializedReq.headers,
|
|
1239
|
-
body: serializedReq.body?.type === 'bytes' ? atob(serializedReq.body.data) : undefined
|
|
1240
|
-
}))
|
|
1241
|
-
return serializeResponse(response)
|
|
1242
|
-
}
|
|
1243
|
-
if (methodName === 'stub.rpc') {
|
|
1244
|
-
// DO RPC: Call a method on the DO instance
|
|
1245
|
-
const [, doId, rpcMethod, rpcParams] = params
|
|
1246
|
-
const id = binding.idFromString(doId.hex)
|
|
1247
|
-
const stub = binding.get(id)
|
|
1248
|
-
|
|
1249
|
-
// Use fetch to call the RPC endpoint
|
|
1250
|
-
const response = await stub.fetch(new Request('http://do/_rpc', {
|
|
1251
|
-
method: 'POST',
|
|
1252
|
-
headers: { 'Content-Type': 'application/json' },
|
|
1253
|
-
body: JSON.stringify({ method: rpcMethod, params: rpcParams })
|
|
1254
|
-
}))
|
|
1255
|
-
|
|
1256
|
-
const result = await response.json()
|
|
1257
|
-
if (!result.ok) throw new Error(result.error?.message || 'RPC failed')
|
|
1258
|
-
return result.result
|
|
1259
|
-
}
|
|
1260
|
-
|
|
1261
|
-
// Queue operations
|
|
1262
|
-
if (methodName === 'send') return binding.send(params[0], params[1])
|
|
1263
|
-
if (methodName === 'sendBatch') return binding.sendBatch(params[0], params[1])
|
|
1264
|
-
|
|
1265
|
-
// Generic fallback
|
|
1266
|
-
if (typeof binding[methodName] === 'function') {
|
|
1267
|
-
return binding[methodName](...params)
|
|
1268
|
-
}
|
|
1269
|
-
|
|
1270
|
-
throw new Error(\`Unknown method: \${method}\`)
|
|
1271
|
-
}
|
|
1272
|
-
|
|
1273
|
-
function serializeResponse(response) {
|
|
1274
|
-
return {
|
|
1275
|
-
status: response.status,
|
|
1276
|
-
statusText: response.statusText,
|
|
1277
|
-
headers: [...response.headers.entries()],
|
|
1278
|
-
body: null // Will be streamed separately for large bodies
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
|
|
1282
|
-
function serializeR2Object(obj) {
|
|
1283
|
-
if (!obj) return null
|
|
1284
|
-
return {
|
|
1285
|
-
key: obj.key,
|
|
1286
|
-
version: obj.version,
|
|
1287
|
-
size: obj.size,
|
|
1288
|
-
etag: obj.etag,
|
|
1289
|
-
httpEtag: obj.httpEtag,
|
|
1290
|
-
uploaded: obj.uploaded?.toISOString(),
|
|
1291
|
-
httpMetadata: obj.httpMetadata,
|
|
1292
|
-
customMetadata: obj.customMetadata
|
|
1293
|
-
}
|
|
1294
|
-
}
|
|
1295
|
-
|
|
1296
|
-
function serializeR2Objects(result) {
|
|
1297
|
-
if (!result) return null
|
|
1298
|
-
return {
|
|
1299
|
-
objects: result.objects.map(serializeR2Object),
|
|
1300
|
-
truncated: result.truncated,
|
|
1301
|
-
cursor: result.cursor,
|
|
1302
|
-
delimitedPrefixes: result.delimitedPrefixes
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
`;
|
|
1306
|
-
}
|
|
1307
|
-
async function startMiniflare(options = {}) {
|
|
1308
|
-
const { Miniflare, Log, LogLevel } = await import("miniflare");
|
|
1309
|
-
const port = options.port ?? 8787;
|
|
1310
|
-
const persistPath = options.persistPath ?? ".devflare/data";
|
|
1311
|
-
const mfConfig = {
|
|
1312
|
-
modules: true,
|
|
1313
|
-
script: generateGatewayScript(),
|
|
1314
|
-
port,
|
|
1315
|
-
host: "127.0.0.1",
|
|
1316
|
-
log: options.verbose ? new Log(LogLevel.DEBUG) : new Log(LogLevel.WARN),
|
|
1317
|
-
compatibilityDate: options.compatibilityDate ?? "2024-01-01",
|
|
1318
|
-
compatibilityFlags: options.compatibilityFlags ?? []
|
|
1319
|
-
};
|
|
1320
|
-
const hasBindings = (val) => {
|
|
1321
|
-
if (!val)
|
|
1322
|
-
return false;
|
|
1323
|
-
if (Array.isArray(val))
|
|
1324
|
-
return val.length > 0;
|
|
1325
|
-
return Object.keys(val).length > 0;
|
|
1326
|
-
};
|
|
1327
|
-
if (hasBindings(options.kvNamespaces)) {
|
|
1328
|
-
mfConfig.kvNamespaces = options.kvNamespaces;
|
|
1329
|
-
if (options.persist) {
|
|
1330
|
-
mfConfig.kvPersist = `${persistPath}/kv`;
|
|
1331
|
-
}
|
|
1332
|
-
}
|
|
1333
|
-
if (hasBindings(options.r2Buckets)) {
|
|
1334
|
-
mfConfig.r2Buckets = options.r2Buckets;
|
|
1335
|
-
if (options.persist) {
|
|
1336
|
-
mfConfig.r2Persist = `${persistPath}/r2`;
|
|
1337
|
-
}
|
|
1338
|
-
}
|
|
1339
|
-
if (hasBindings(options.d1Databases)) {
|
|
1340
|
-
mfConfig.d1Databases = options.d1Databases;
|
|
1341
|
-
if (options.persist) {
|
|
1342
|
-
mfConfig.d1Persist = `${persistPath}/d1`;
|
|
1343
|
-
}
|
|
1344
|
-
}
|
|
1345
|
-
if (options.durableObjects) {
|
|
1346
|
-
mfConfig.durableObjects = options.durableObjects;
|
|
1347
|
-
if (options.persist) {
|
|
1348
|
-
mfConfig.durableObjectsPersist = `${persistPath}/do`;
|
|
1349
|
-
}
|
|
1350
|
-
}
|
|
1351
|
-
if (options.bindings) {
|
|
1352
|
-
mfConfig.bindings = options.bindings;
|
|
1353
|
-
}
|
|
1354
|
-
if (options.queues?.length) {
|
|
1355
|
-
mfConfig.queueProducers = Object.fromEntries(options.queues.map((q) => [q, { queueName: q }]));
|
|
1356
|
-
}
|
|
1357
|
-
const mf = new Miniflare(mfConfig);
|
|
1358
|
-
await mf.ready;
|
|
1359
|
-
return {
|
|
1360
|
-
ready: Promise.resolve(),
|
|
1361
|
-
async dispose() {
|
|
1362
|
-
await mf.dispose();
|
|
1363
|
-
},
|
|
1364
|
-
async getBindings() {
|
|
1365
|
-
return mf.getBindings();
|
|
1366
|
-
},
|
|
1367
|
-
getKVNamespace: mf.getKVNamespace.bind(mf),
|
|
1368
|
-
getR2Bucket: mf.getR2Bucket.bind(mf),
|
|
1369
|
-
getD1Database: mf.getD1Database.bind(mf),
|
|
1370
|
-
getDurableObjectNamespace: mf.getDurableObjectNamespace.bind(mf),
|
|
1371
|
-
dispatchFetch: mf.dispatchFetch.bind(mf),
|
|
1372
|
-
_mf: mf
|
|
1373
|
-
};
|
|
1374
|
-
}
|
|
1375
|
-
async function startMiniflareFromConfig(config, options = {}) {
|
|
1376
|
-
const bindings = config.bindings ?? {};
|
|
1377
|
-
const mfOptions = {
|
|
1378
|
-
...options,
|
|
1379
|
-
compatibilityDate: config.compatibilityDate,
|
|
1380
|
-
compatibilityFlags: config.compatibilityFlags,
|
|
1381
|
-
kvNamespaces: bindings.kv ? bindings.kv : undefined,
|
|
1382
|
-
r2Buckets: bindings.r2 ? bindings.r2 : undefined,
|
|
1383
|
-
d1Databases: bindings.d1 ? bindings.d1 : undefined,
|
|
1384
|
-
queues: bindings.queues?.consumers?.map((c) => c.queue),
|
|
1385
|
-
bindings: config.vars,
|
|
1386
|
-
durableObjects: bindings.durableObjects ? Object.fromEntries(Object.entries(bindings.durableObjects).map(([bindingName, doConfig]) => {
|
|
1387
|
-
const normalized = normalizeDOBinding(doConfig);
|
|
1388
|
-
return [
|
|
1389
|
-
bindingName,
|
|
1390
|
-
{
|
|
1391
|
-
className: normalized.className,
|
|
1392
|
-
scriptPath: normalized.scriptName
|
|
1393
|
-
}
|
|
1394
|
-
];
|
|
1395
|
-
})) : undefined
|
|
1396
|
-
};
|
|
1397
|
-
return startMiniflare(mfOptions);
|
|
1398
|
-
}
|
|
1399
|
-
var globalMiniflare = null;
|
|
1400
|
-
async function getMiniflare(options) {
|
|
1401
|
-
if (!globalMiniflare) {
|
|
1402
|
-
globalMiniflare = await startMiniflare(options);
|
|
1403
|
-
}
|
|
1404
|
-
return globalMiniflare;
|
|
1405
|
-
}
|
|
1406
|
-
async function stopMiniflare() {
|
|
1407
|
-
if (globalMiniflare) {
|
|
1408
|
-
await globalMiniflare.dispose();
|
|
1409
|
-
globalMiniflare = null;
|
|
1410
|
-
}
|
|
1411
|
-
}
|
|
1412
|
-
|
|
1413
|
-
export { BinaryKind, BinaryFlags, encodeBinaryFrame, decodeBinaryFrame, parseJsonMsg, stringifyJsonMsg, serializeValue, deserializeValue, base64Encode, base64Decode, BridgeClient, getClient, createEnvProxy, bridgeEnv, initEnv, setBindingHints, startMiniflare, startMiniflareFromConfig, getMiniflare, stopMiniflare };
|
|
1143
|
+
export { BinaryKind, BinaryFlags, encodeBinaryFrame, decodeBinaryFrame, parseJsonMsg, stringifyJsonMsg, serializeValue, deserializeValue, base64Encode, base64Decode, BridgeClient, getClient, createEnvProxy, bridgeEnv, initEnv, setBindingHints };
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import {
|
|
2
|
+
wrapEnvSendEmailBindings
|
|
3
|
+
} from "./index-fef08w43.js";
|
|
4
|
+
|
|
5
|
+
// src/runtime/context.ts
|
|
6
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
7
|
+
var storage = new AsyncLocalStorage;
|
|
8
|
+
function createLocals(locals) {
|
|
9
|
+
return locals ?? {};
|
|
10
|
+
}
|
|
11
|
+
function createAugmentedTarget(target, extra) {
|
|
12
|
+
return new Proxy(target, {
|
|
13
|
+
get(target2, prop) {
|
|
14
|
+
if (prop in extra) {
|
|
15
|
+
return extra[prop];
|
|
16
|
+
}
|
|
17
|
+
const value = Reflect.get(target2, prop, target2);
|
|
18
|
+
return typeof value === "function" ? value.bind(target2) : value;
|
|
19
|
+
},
|
|
20
|
+
has(target2, prop) {
|
|
21
|
+
return prop in extra || prop in target2;
|
|
22
|
+
},
|
|
23
|
+
ownKeys(target2) {
|
|
24
|
+
return Array.from(new Set([
|
|
25
|
+
...Reflect.ownKeys(target2),
|
|
26
|
+
...Reflect.ownKeys(extra)
|
|
27
|
+
]));
|
|
28
|
+
},
|
|
29
|
+
getOwnPropertyDescriptor(target2, prop) {
|
|
30
|
+
if (prop in extra) {
|
|
31
|
+
return {
|
|
32
|
+
configurable: true,
|
|
33
|
+
enumerable: true,
|
|
34
|
+
writable: false,
|
|
35
|
+
value: extra[prop]
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return Reflect.getOwnPropertyDescriptor(target2, prop);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
function createBaseEvent(type, env, ctx, options = {}) {
|
|
43
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
44
|
+
const locals = createLocals(options.locals);
|
|
45
|
+
return {
|
|
46
|
+
type,
|
|
47
|
+
env: runtimeEnv,
|
|
48
|
+
ctx,
|
|
49
|
+
locals,
|
|
50
|
+
request: options.request ?? null,
|
|
51
|
+
params: options.params
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
function createFetchEvent(request, env, ctx, options = {}) {
|
|
55
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
56
|
+
const locals = createLocals(options.locals);
|
|
57
|
+
return createAugmentedTarget(request, {
|
|
58
|
+
type: "fetch",
|
|
59
|
+
env: runtimeEnv,
|
|
60
|
+
ctx,
|
|
61
|
+
locals,
|
|
62
|
+
request,
|
|
63
|
+
params: options.params ?? {}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
function createQueueEvent(batch, env, ctx, options = {}) {
|
|
67
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
68
|
+
const locals = createLocals(options.locals);
|
|
69
|
+
return createAugmentedTarget(batch, {
|
|
70
|
+
type: "queue",
|
|
71
|
+
env: runtimeEnv,
|
|
72
|
+
ctx,
|
|
73
|
+
locals,
|
|
74
|
+
batch
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function createScheduledEvent(controller, env, ctx, options = {}) {
|
|
78
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
79
|
+
const locals = createLocals(options.locals);
|
|
80
|
+
return createAugmentedTarget(controller, {
|
|
81
|
+
type: "scheduled",
|
|
82
|
+
env: runtimeEnv,
|
|
83
|
+
ctx,
|
|
84
|
+
locals,
|
|
85
|
+
controller
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function createEmailEvent(message, env, ctx, options = {}) {
|
|
89
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
90
|
+
const locals = createLocals(options.locals);
|
|
91
|
+
return createAugmentedTarget(message, {
|
|
92
|
+
type: "email",
|
|
93
|
+
env: runtimeEnv,
|
|
94
|
+
ctx,
|
|
95
|
+
locals,
|
|
96
|
+
message
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function createTailEvent(events, env, ctx, options = {}) {
|
|
100
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
101
|
+
const locals = createLocals(options.locals);
|
|
102
|
+
return createAugmentedTarget(events, {
|
|
103
|
+
type: "tail",
|
|
104
|
+
env: runtimeEnv,
|
|
105
|
+
ctx,
|
|
106
|
+
locals,
|
|
107
|
+
events
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function createDurableObjectFetchEvent(request, env, state, options = {}) {
|
|
111
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
112
|
+
const locals = createLocals(options.locals);
|
|
113
|
+
return createAugmentedTarget(request, {
|
|
114
|
+
type: "durable-object-fetch",
|
|
115
|
+
env: runtimeEnv,
|
|
116
|
+
ctx: state,
|
|
117
|
+
state,
|
|
118
|
+
locals,
|
|
119
|
+
request
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function createDurableObjectAlarmEvent(env, state, options = {}) {
|
|
123
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
124
|
+
const locals = createLocals(options.locals);
|
|
125
|
+
return {
|
|
126
|
+
type: "durable-object-alarm",
|
|
127
|
+
env: runtimeEnv,
|
|
128
|
+
ctx: state,
|
|
129
|
+
state,
|
|
130
|
+
locals
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function createDurableObjectWebSocketMessageEvent(ws, message, env, state, options = {}) {
|
|
134
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
135
|
+
const locals = createLocals(options.locals);
|
|
136
|
+
return createAugmentedTarget(ws, {
|
|
137
|
+
type: "durable-object-websocket-message",
|
|
138
|
+
env: runtimeEnv,
|
|
139
|
+
ctx: state,
|
|
140
|
+
state,
|
|
141
|
+
locals,
|
|
142
|
+
ws,
|
|
143
|
+
message
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
function createDurableObjectWebSocketCloseEvent(ws, code, reason, wasClean, env, state, options = {}) {
|
|
147
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
148
|
+
const locals = createLocals(options.locals);
|
|
149
|
+
return createAugmentedTarget(ws, {
|
|
150
|
+
type: "durable-object-websocket-close",
|
|
151
|
+
env: runtimeEnv,
|
|
152
|
+
ctx: state,
|
|
153
|
+
state,
|
|
154
|
+
locals,
|
|
155
|
+
ws,
|
|
156
|
+
code,
|
|
157
|
+
reason,
|
|
158
|
+
wasClean
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
function createDurableObjectWebSocketErrorEvent(ws, error, env, state, options = {}) {
|
|
162
|
+
const runtimeEnv = wrapEnvSendEmailBindings(env);
|
|
163
|
+
const locals = createLocals(options.locals);
|
|
164
|
+
return createAugmentedTarget(ws, {
|
|
165
|
+
type: "durable-object-websocket-error",
|
|
166
|
+
env: runtimeEnv,
|
|
167
|
+
ctx: state,
|
|
168
|
+
state,
|
|
169
|
+
locals,
|
|
170
|
+
ws,
|
|
171
|
+
error
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function createDefaultEvent(env, ctx, request, type, locals) {
|
|
175
|
+
if (type === "fetch" && request && ctx) {
|
|
176
|
+
return createFetchEvent(request, env, ctx, { locals });
|
|
177
|
+
}
|
|
178
|
+
if (type === "durable-object-fetch" && request && ctx) {
|
|
179
|
+
return createDurableObjectFetchEvent(request, env, ctx, { locals });
|
|
180
|
+
}
|
|
181
|
+
return createBaseEvent(type, env, ctx, {
|
|
182
|
+
locals,
|
|
183
|
+
request
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
function runWithContext(env, ctx, request, fn, type = "fetch") {
|
|
187
|
+
const locals = createLocals();
|
|
188
|
+
const event = createDefaultEvent(env, ctx, request, type, locals);
|
|
189
|
+
return runWithEventContext(event, fn);
|
|
190
|
+
}
|
|
191
|
+
function runWithEventContext(event, fn) {
|
|
192
|
+
const context = {
|
|
193
|
+
env: event.env,
|
|
194
|
+
ctx: event.ctx,
|
|
195
|
+
request: event.request ?? null,
|
|
196
|
+
locals: event.locals,
|
|
197
|
+
type: event.type,
|
|
198
|
+
event
|
|
199
|
+
};
|
|
200
|
+
return storage.run(context, fn);
|
|
201
|
+
}
|
|
202
|
+
function getContext() {
|
|
203
|
+
const context = storage.getStore();
|
|
204
|
+
if (!context) {
|
|
205
|
+
throw new ContextUnavailableError;
|
|
206
|
+
}
|
|
207
|
+
return context;
|
|
208
|
+
}
|
|
209
|
+
function getContextOrNull() {
|
|
210
|
+
const context = storage.getStore();
|
|
211
|
+
return context ?? null;
|
|
212
|
+
}
|
|
213
|
+
function getEventContext() {
|
|
214
|
+
return getContext().event;
|
|
215
|
+
}
|
|
216
|
+
function getEventContextOrNull() {
|
|
217
|
+
return getContextOrNull()?.event ?? null;
|
|
218
|
+
}
|
|
219
|
+
function hasContext() {
|
|
220
|
+
return storage.getStore() !== undefined;
|
|
221
|
+
}
|
|
222
|
+
function createEventAccessor(name, matcher) {
|
|
223
|
+
const accessor = () => {
|
|
224
|
+
const currentEvent = getEventContextOrNull();
|
|
225
|
+
if (!currentEvent) {
|
|
226
|
+
throw new ContextUnavailableError;
|
|
227
|
+
}
|
|
228
|
+
if (!matcher(currentEvent)) {
|
|
229
|
+
throw new ContextUnavailableError(`${name} is not available in the current '${currentEvent.type}' context.
|
|
230
|
+
|
|
231
|
+
Devflare stores event objects in AsyncLocalStorage so helpers called within a handler can reach the active event.
|
|
232
|
+
Use ${name}.safe() to return null instead of throwing, or call the getter that matches the active surface.`);
|
|
233
|
+
}
|
|
234
|
+
return currentEvent;
|
|
235
|
+
};
|
|
236
|
+
accessor.safe = () => {
|
|
237
|
+
const currentEvent = getEventContextOrNull();
|
|
238
|
+
return currentEvent && matcher(currentEvent) ? currentEvent : null;
|
|
239
|
+
};
|
|
240
|
+
return accessor;
|
|
241
|
+
}
|
|
242
|
+
function isFetchEvent(event) {
|
|
243
|
+
return event.type === "fetch" && event.request instanceof Request;
|
|
244
|
+
}
|
|
245
|
+
function isQueueEvent(event) {
|
|
246
|
+
return event.type === "queue" && "batch" in event;
|
|
247
|
+
}
|
|
248
|
+
function isScheduledEvent(event) {
|
|
249
|
+
return event.type === "scheduled" && "controller" in event;
|
|
250
|
+
}
|
|
251
|
+
function isEmailEvent(event) {
|
|
252
|
+
return event.type === "email" && "message" in event;
|
|
253
|
+
}
|
|
254
|
+
function isTailEvent(event) {
|
|
255
|
+
return event.type === "tail" && Array.isArray(event) && "events" in event;
|
|
256
|
+
}
|
|
257
|
+
function isDurableObjectEvent(event) {
|
|
258
|
+
return event.type.startsWith("durable-object-") && "state" in event;
|
|
259
|
+
}
|
|
260
|
+
function isDurableObjectFetchEvent(event) {
|
|
261
|
+
return event.type === "durable-object-fetch" && event.request instanceof Request;
|
|
262
|
+
}
|
|
263
|
+
function isDurableObjectAlarmEvent(event) {
|
|
264
|
+
return event.type === "durable-object-alarm";
|
|
265
|
+
}
|
|
266
|
+
function isDurableObjectWebSocketMessageEvent(event) {
|
|
267
|
+
return event.type === "durable-object-websocket-message" && "ws" in event && "message" in event;
|
|
268
|
+
}
|
|
269
|
+
function isDurableObjectWebSocketCloseEvent(event) {
|
|
270
|
+
return event.type === "durable-object-websocket-close" && "ws" in event && "code" in event;
|
|
271
|
+
}
|
|
272
|
+
function isDurableObjectWebSocketErrorEvent(event) {
|
|
273
|
+
return event.type === "durable-object-websocket-error" && "ws" in event && "error" in event;
|
|
274
|
+
}
|
|
275
|
+
var getFetchEvent = createEventAccessor("getFetchEvent()", isFetchEvent);
|
|
276
|
+
var getQueueEvent = createEventAccessor("getQueueEvent()", isQueueEvent);
|
|
277
|
+
var getScheduledEvent = createEventAccessor("getScheduledEvent()", isScheduledEvent);
|
|
278
|
+
var getEmailEvent = createEventAccessor("getEmailEvent()", isEmailEvent);
|
|
279
|
+
var getTailEvent = createEventAccessor("getTailEvent()", isTailEvent);
|
|
280
|
+
var getDurableObjectEvent = createEventAccessor("getDurableObjectEvent()", isDurableObjectEvent);
|
|
281
|
+
var getDurableObjectFetchEvent = createEventAccessor("getDurableObjectFetchEvent()", isDurableObjectFetchEvent);
|
|
282
|
+
var getDurableObjectAlarmEvent = createEventAccessor("getDurableObjectAlarmEvent()", isDurableObjectAlarmEvent);
|
|
283
|
+
var getDurableObjectWebSocketMessageEvent = createEventAccessor("getDurableObjectWebSocketMessageEvent()", isDurableObjectWebSocketMessageEvent);
|
|
284
|
+
var getDurableObjectWebSocketCloseEvent = createEventAccessor("getDurableObjectWebSocketCloseEvent()", isDurableObjectWebSocketCloseEvent);
|
|
285
|
+
var getDurableObjectWebSocketErrorEvent = createEventAccessor("getDurableObjectWebSocketErrorEvent()", isDurableObjectWebSocketErrorEvent);
|
|
286
|
+
|
|
287
|
+
class ContextUnavailableError extends Error {
|
|
288
|
+
code = "CONTEXT_UNAVAILABLE";
|
|
289
|
+
constructor(message) {
|
|
290
|
+
super(message ?? `Context not available. Devflare uses AsyncLocalStorage to carry the active event through fetch, queue, scheduled, email, tail, and Durable Object handler call chains.
|
|
291
|
+
|
|
292
|
+
` + `This usually means one of:
|
|
293
|
+
|
|
294
|
+
` + `1. Accessing context at module top-level (runs at cold start, not per-request)
|
|
295
|
+
` + `2. Accessing context in setTimeout/setInterval callbacks
|
|
296
|
+
` + `3. Missing 'nodejs_compat' compatibility flag in your worker config
|
|
297
|
+
|
|
298
|
+
` + `Fix: Move the access inside your handler, middleware, or a helper called from that handler trail.
|
|
299
|
+
` + `Learn more: https://devflare.dev/docs/context-errors`);
|
|
300
|
+
this.name = "ContextUnavailableError";
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export { createFetchEvent, createQueueEvent, createScheduledEvent, createEmailEvent, createTailEvent, createDurableObjectFetchEvent, createDurableObjectAlarmEvent, createDurableObjectWebSocketMessageEvent, createDurableObjectWebSocketCloseEvent, createDurableObjectWebSocketErrorEvent, runWithContext, runWithEventContext, getContext, getContextOrNull, getEventContext, getEventContextOrNull, hasContext, getFetchEvent, getQueueEvent, getScheduledEvent, getEmailEvent, getTailEvent, getDurableObjectEvent, getDurableObjectFetchEvent, getDurableObjectAlarmEvent, getDurableObjectWebSocketMessageEvent, getDurableObjectWebSocketCloseEvent, getDurableObjectWebSocketErrorEvent, ContextUnavailableError };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/workerName.ts
|
|
2
|
+
var workerName = (() => {
|
|
3
|
+
if (typeof __DEVFLARE_WORKER_NAME__ !== "undefined") {
|
|
4
|
+
return __DEVFLARE_WORKER_NAME__;
|
|
5
|
+
}
|
|
6
|
+
if (typeof process !== "undefined" && process.env?.DEVFLARE_WORKER_NAME) {
|
|
7
|
+
return process.env.DEVFLARE_WORKER_NAME;
|
|
8
|
+
}
|
|
9
|
+
return "unknown";
|
|
10
|
+
})();
|
|
11
|
+
|
|
12
|
+
export { workerName };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/cli/package-metadata.ts
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { dirname, resolve } from "pathe";
|
|
5
|
+
var packageMetadataPromise = null;
|
|
6
|
+
async function loadPackageMetadata() {
|
|
7
|
+
let currentDir = dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
while (true) {
|
|
9
|
+
const packageJsonPath = resolve(currentDir, "package.json");
|
|
10
|
+
try {
|
|
11
|
+
const packageJson = await readFile(packageJsonPath, "utf8");
|
|
12
|
+
const metadata = JSON.parse(packageJson);
|
|
13
|
+
if (metadata.name === "devflare") {
|
|
14
|
+
return metadata;
|
|
15
|
+
}
|
|
16
|
+
} catch {}
|
|
17
|
+
const parentDir = dirname(currentDir);
|
|
18
|
+
if (parentDir === currentDir) {
|
|
19
|
+
throw new Error("Could not resolve the devflare package.json file");
|
|
20
|
+
}
|
|
21
|
+
currentDir = parentDir;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async function getPackageMetadata() {
|
|
25
|
+
if (!packageMetadataPromise) {
|
|
26
|
+
packageMetadataPromise = loadPackageMetadata();
|
|
27
|
+
}
|
|
28
|
+
return packageMetadataPromise;
|
|
29
|
+
}
|
|
30
|
+
async function getPackageVersion() {
|
|
31
|
+
return (await getPackageMetadata()).version ?? "0.0.0";
|
|
32
|
+
}
|
|
33
|
+
async function getInitDependencyVersions() {
|
|
34
|
+
const metadata = await getPackageMetadata();
|
|
35
|
+
const devDependencies = metadata.devDependencies ?? {};
|
|
36
|
+
return {
|
|
37
|
+
devflare: `^${metadata.version ?? "0.0.0"}`,
|
|
38
|
+
typescript: devDependencies.typescript ?? "^5.7.0",
|
|
39
|
+
wrangler: devDependencies.wrangler ?? "^3.99.0",
|
|
40
|
+
workersTypes: devDependencies["@cloudflare/workers-types"] ?? "^4.20250109.0"
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { getPackageVersion, getInitDependencyVersions };
|