devflare 1.0.0-next.5 → 1.0.0-next.7
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 +7 -2
- package/README.md +4 -2
- package/dist/browser.d.ts +50 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +149 -0
- package/dist/{build-mnf6v8gd.js → build-9myaxf07.js} +22 -5
- package/dist/cli/commands/build.d.ts.map +1 -1
- package/dist/cli/commands/deploy.d.ts.map +1 -1
- package/dist/cli/commands/types.d.ts.map +1 -1
- package/dist/{deploy-nhceck39.js → deploy-h1wz5p7m.js} +29 -13
- package/dist/{dev-b9dmrj7b.js → dev-rsdssknb.js} +381 -91
- package/dist/dev-server/server.d.ts.map +1 -1
- package/dist/index-62b3gt2g.js +12 -0
- package/dist/index-9ats0s83.js +70 -0
- package/dist/index-a0fjkq68.js +198 -0
- package/dist/{index-pf5s73n9.js → index-ccrh4w3t.js} +1 -281
- package/dist/{index-ep3445yc.js → index-f8qh2tyh.js} +34 -107
- package/dist/index-k7r18na8.js +0 -0
- package/dist/{index-m2q41jwa.js → index-n3np2d6t.js} +1 -1
- package/dist/index-npc1c8jx.js +44 -0
- package/dist/index-p7g30wd2.js +281 -0
- package/dist/{index-07q6yxyc.js → index-v8vvsn9x.js} +1 -0
- package/dist/index.js +25 -26
- package/dist/runtime/index.d.ts +2 -0
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +73 -0
- package/dist/sveltekit/index.js +5 -3
- package/dist/test/index.js +10 -5
- package/dist/test/simple-context.d.ts +1 -1
- package/dist/test/simple-context.d.ts.map +1 -1
- package/dist/{types-5nyrz1sz.js → types-wdcpnfvy.js} +44 -11
- package/dist/vite/index.js +118 -50
- package/dist/vite/plugin.d.ts.map +1 -1
- package/dist/worker-entry/composed-worker.d.ts +10 -0
- package/dist/worker-entry/composed-worker.d.ts.map +1 -0
- package/package.json +3 -2
- package/dist/{doctor-fmgb3d28.js → doctor-v7jy4s3r.js} +3 -3
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getContextOrNull
|
|
3
|
+
} from "./index-npc1c8jx.js";
|
|
4
|
+
import {
|
|
5
|
+
bridgeEnv
|
|
6
|
+
} from "./index-ccrh4w3t.js";
|
|
7
|
+
|
|
8
|
+
// src/env.ts
|
|
9
|
+
var testContextEnv = null;
|
|
10
|
+
var testContextDispose = null;
|
|
11
|
+
function __setTestContext(envBindings, dispose) {
|
|
12
|
+
testContextEnv = envBindings;
|
|
13
|
+
testContextDispose = dispose;
|
|
14
|
+
}
|
|
15
|
+
function __clearTestContext() {
|
|
16
|
+
testContextEnv = null;
|
|
17
|
+
testContextDispose = null;
|
|
18
|
+
}
|
|
19
|
+
var env = new Proxy({}, {
|
|
20
|
+
get(_target, prop) {
|
|
21
|
+
if (prop === "dispose") {
|
|
22
|
+
return async () => {
|
|
23
|
+
if (testContextDispose) {
|
|
24
|
+
await testContextDispose();
|
|
25
|
+
__clearTestContext();
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
const ctx = getContextOrNull();
|
|
30
|
+
if (ctx?.env) {
|
|
31
|
+
return ctx.env[prop];
|
|
32
|
+
}
|
|
33
|
+
if (testContextEnv) {
|
|
34
|
+
return testContextEnv[prop];
|
|
35
|
+
}
|
|
36
|
+
return bridgeEnv[prop];
|
|
37
|
+
},
|
|
38
|
+
has(_target, prop) {
|
|
39
|
+
if (prop === "dispose")
|
|
40
|
+
return true;
|
|
41
|
+
const ctx = getContextOrNull();
|
|
42
|
+
if (ctx?.env) {
|
|
43
|
+
return prop in ctx.env;
|
|
44
|
+
}
|
|
45
|
+
if (testContextEnv) {
|
|
46
|
+
return prop in testContextEnv;
|
|
47
|
+
}
|
|
48
|
+
return prop in bridgeEnv;
|
|
49
|
+
},
|
|
50
|
+
ownKeys(_target) {
|
|
51
|
+
const ctx = getContextOrNull();
|
|
52
|
+
if (ctx?.env) {
|
|
53
|
+
return Reflect.ownKeys(ctx.env);
|
|
54
|
+
}
|
|
55
|
+
if (testContextEnv) {
|
|
56
|
+
return Reflect.ownKeys(testContextEnv);
|
|
57
|
+
}
|
|
58
|
+
return Reflect.ownKeys(bridgeEnv);
|
|
59
|
+
},
|
|
60
|
+
getOwnPropertyDescriptor(_target, prop) {
|
|
61
|
+
if (prop === "dispose") {
|
|
62
|
+
return { configurable: true, enumerable: false, writable: false };
|
|
63
|
+
}
|
|
64
|
+
const ctx = getContextOrNull();
|
|
65
|
+
const source = ctx?.env ?? testContextEnv ?? bridgeEnv;
|
|
66
|
+
return Reflect.getOwnPropertyDescriptor(source, prop);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export { __setTestContext, __clearTestContext, env };
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__require
|
|
3
|
+
} from "./index-37x76zdn.js";
|
|
4
|
+
|
|
5
|
+
// src/worker-entry/composed-worker.ts
|
|
6
|
+
import { defu } from "defu";
|
|
7
|
+
import { dirname, relative, resolve } from "pathe";
|
|
8
|
+
var DEFAULT_FETCH_ENTRY_FILES = [
|
|
9
|
+
"src/fetch.ts",
|
|
10
|
+
"src/fetch.js",
|
|
11
|
+
"src/fetch.mts",
|
|
12
|
+
"src/fetch.mjs"
|
|
13
|
+
];
|
|
14
|
+
var DEFAULT_QUEUE_ENTRY_FILES = [
|
|
15
|
+
"src/queue.ts",
|
|
16
|
+
"src/queue.js",
|
|
17
|
+
"src/queue.mts",
|
|
18
|
+
"src/queue.mjs"
|
|
19
|
+
];
|
|
20
|
+
var DEFAULT_SCHEDULED_ENTRY_FILES = [
|
|
21
|
+
"src/scheduled.ts",
|
|
22
|
+
"src/scheduled.js",
|
|
23
|
+
"src/scheduled.mts",
|
|
24
|
+
"src/scheduled.mjs"
|
|
25
|
+
];
|
|
26
|
+
var DEFAULT_EMAIL_ENTRY_FILES = [
|
|
27
|
+
"src/email.ts",
|
|
28
|
+
"src/email.js",
|
|
29
|
+
"src/email.mts",
|
|
30
|
+
"src/email.mjs"
|
|
31
|
+
];
|
|
32
|
+
function resolveConfigForEnvironment(config, environment) {
|
|
33
|
+
if (environment && config.env?.[environment]) {
|
|
34
|
+
return defu(config.env[environment], config);
|
|
35
|
+
}
|
|
36
|
+
return config;
|
|
37
|
+
}
|
|
38
|
+
async function resolveWorkerHandlerPath(cwd, configuredPath, defaultEntries) {
|
|
39
|
+
if (configuredPath === false) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
const fs = await import("node:fs/promises");
|
|
43
|
+
const candidates = new Set;
|
|
44
|
+
if (typeof configuredPath === "string" && configuredPath) {
|
|
45
|
+
candidates.add(configuredPath);
|
|
46
|
+
}
|
|
47
|
+
for (const defaultEntry of defaultEntries) {
|
|
48
|
+
candidates.add(defaultEntry);
|
|
49
|
+
}
|
|
50
|
+
for (const candidate of candidates) {
|
|
51
|
+
const absolutePath = resolve(cwd, candidate);
|
|
52
|
+
try {
|
|
53
|
+
await fs.access(absolutePath);
|
|
54
|
+
return absolutePath;
|
|
55
|
+
} catch {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
async function resolveWorkerSurfacePaths(cwd, config) {
|
|
62
|
+
return {
|
|
63
|
+
fetch: await resolveWorkerHandlerPath(cwd, config.files?.fetch, DEFAULT_FETCH_ENTRY_FILES),
|
|
64
|
+
queue: await resolveWorkerHandlerPath(cwd, config.files?.queue, DEFAULT_QUEUE_ENTRY_FILES),
|
|
65
|
+
scheduled: await resolveWorkerHandlerPath(cwd, config.files?.scheduled, DEFAULT_SCHEDULED_ENTRY_FILES),
|
|
66
|
+
email: await resolveWorkerHandlerPath(cwd, config.files?.email, DEFAULT_EMAIL_ENTRY_FILES)
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function toImportSpecifier(fromFilePath, toFilePath) {
|
|
70
|
+
const specifier = relative(dirname(fromFilePath), toFilePath).replace(/\\/g, "/");
|
|
71
|
+
return specifier.startsWith(".") ? specifier : `./${specifier}`;
|
|
72
|
+
}
|
|
73
|
+
function needsComposedWorkerEntrypoint(surfacePaths) {
|
|
74
|
+
return Boolean(surfacePaths.queue || surfacePaths.scheduled || surfacePaths.email);
|
|
75
|
+
}
|
|
76
|
+
function getComposedWorkerEntrypointSource(surfaceImportPaths) {
|
|
77
|
+
const importLines = [`import { runWithContext } from 'devflare/runtime'`];
|
|
78
|
+
const moduleFallbackLines = [];
|
|
79
|
+
const registerSurfaceModule = (identifier, importPath) => {
|
|
80
|
+
if (importPath) {
|
|
81
|
+
importLines.push(`import * as ${identifier} from '${importPath}'`);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
moduleFallbackLines.push(`const ${identifier} = {}`);
|
|
85
|
+
};
|
|
86
|
+
registerSurfaceModule("__devflareFetchModule", surfaceImportPaths.fetch);
|
|
87
|
+
registerSurfaceModule("__devflareQueueModule", surfaceImportPaths.queue);
|
|
88
|
+
registerSurfaceModule("__devflareScheduledModule", surfaceImportPaths.scheduled);
|
|
89
|
+
registerSurfaceModule("__devflareEmailModule", surfaceImportPaths.email);
|
|
90
|
+
return `
|
|
91
|
+
${importLines.join(`
|
|
92
|
+
`)}
|
|
93
|
+
${moduleFallbackLines.join(`
|
|
94
|
+
`)}
|
|
95
|
+
|
|
96
|
+
const __devflareResolveHandler = (module, namedExport) => {
|
|
97
|
+
const defaultExport = module.default
|
|
98
|
+
|
|
99
|
+
if (typeof defaultExport === 'function') {
|
|
100
|
+
return defaultExport
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (defaultExport && typeof defaultExport[namedExport] === 'function') {
|
|
104
|
+
return defaultExport[namedExport].bind(defaultExport)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (typeof module[namedExport] === 'function') {
|
|
108
|
+
return module[namedExport]
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return null
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const __devflareFetchHandler = __devflareResolveHandler(__devflareFetchModule, 'fetch')
|
|
115
|
+
const __devflareQueueHandler = __devflareResolveHandler(__devflareQueueModule, 'queue')
|
|
116
|
+
const __devflareScheduledHandler = __devflareResolveHandler(__devflareScheduledModule, 'scheduled')
|
|
117
|
+
const __devflareEmailHandler = __devflareResolveHandler(__devflareEmailModule, 'email')
|
|
118
|
+
|
|
119
|
+
export default {
|
|
120
|
+
...(__devflareFetchHandler
|
|
121
|
+
? {
|
|
122
|
+
async fetch(request, env, ctx) {
|
|
123
|
+
return runWithContext(
|
|
124
|
+
env,
|
|
125
|
+
ctx,
|
|
126
|
+
request,
|
|
127
|
+
() => __devflareFetchHandler(request, env, ctx),
|
|
128
|
+
'fetch'
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
: {}),
|
|
133
|
+
...(__devflareQueueHandler
|
|
134
|
+
? {
|
|
135
|
+
async queue(batch, env, ctx) {
|
|
136
|
+
return runWithContext(
|
|
137
|
+
env,
|
|
138
|
+
ctx,
|
|
139
|
+
null,
|
|
140
|
+
() => __devflareQueueHandler(batch, env, ctx),
|
|
141
|
+
'queue'
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
: {}),
|
|
146
|
+
...(__devflareScheduledHandler
|
|
147
|
+
? {
|
|
148
|
+
async scheduled(controller, env, ctx) {
|
|
149
|
+
return runWithContext(
|
|
150
|
+
env,
|
|
151
|
+
ctx,
|
|
152
|
+
null,
|
|
153
|
+
() => __devflareScheduledHandler(controller, env, ctx),
|
|
154
|
+
'scheduled'
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
: {}),
|
|
159
|
+
...(__devflareEmailHandler
|
|
160
|
+
? {
|
|
161
|
+
async email(message, env, ctx) {
|
|
162
|
+
return runWithContext(
|
|
163
|
+
env,
|
|
164
|
+
ctx,
|
|
165
|
+
null,
|
|
166
|
+
() => __devflareEmailHandler(message, env, ctx),
|
|
167
|
+
'email'
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
: {})
|
|
172
|
+
}
|
|
173
|
+
`.trimStart();
|
|
174
|
+
}
|
|
175
|
+
async function prepareComposedWorkerEntrypoint(cwd, config, environment) {
|
|
176
|
+
const resolvedConfig = resolveConfigForEnvironment(config, environment);
|
|
177
|
+
if (resolvedConfig.wrangler?.passthrough && Object.prototype.hasOwnProperty.call(resolvedConfig.wrangler.passthrough, "main")) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
const surfacePaths = await resolveWorkerSurfacePaths(cwd, resolvedConfig);
|
|
181
|
+
if (!needsComposedWorkerEntrypoint(surfacePaths)) {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
const fs = await import("node:fs/promises");
|
|
185
|
+
const entryDir = resolve(cwd, ".devflare", "worker-entrypoints");
|
|
186
|
+
const entryPath = resolve(entryDir, "main.ts");
|
|
187
|
+
await fs.mkdir(entryDir, { recursive: true });
|
|
188
|
+
const surfaceImportPaths = {
|
|
189
|
+
fetch: surfacePaths.fetch ? toImportSpecifier(entryPath, surfacePaths.fetch) : null,
|
|
190
|
+
queue: surfacePaths.queue ? toImportSpecifier(entryPath, surfacePaths.queue) : null,
|
|
191
|
+
scheduled: surfacePaths.scheduled ? toImportSpecifier(entryPath, surfacePaths.scheduled) : null,
|
|
192
|
+
email: surfacePaths.email ? toImportSpecifier(entryPath, surfacePaths.email) : null
|
|
193
|
+
};
|
|
194
|
+
await fs.writeFile(entryPath, getComposedWorkerEntrypointSource(surfaceImportPaths));
|
|
195
|
+
return ".devflare/worker-entrypoints/main.ts";
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
export { prepareComposedWorkerEntrypoint };
|
|
@@ -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,
|
|
@@ -1137,277 +1130,4 @@ function setBindingHints(hints) {
|
|
|
1137
1130
|
globalEnvProxy = null;
|
|
1138
1131
|
}
|
|
1139
1132
|
|
|
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 };
|
|
1133
|
+
export { BinaryKind, BinaryFlags, encodeBinaryFrame, decodeBinaryFrame, parseJsonMsg, stringifyJsonMsg, serializeValue, deserializeValue, base64Encode, base64Decode, BridgeClient, getClient, createEnvProxy, bridgeEnv, initEnv, setBindingHints };
|