@depup/cloudflare__vite-plugin 1.45.1-depup.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 +32 -0
- package/bin/cf-vite +2 -0
- package/changes.json +14 -0
- package/dist/cf-vite.mjs +165 -0
- package/dist/chunk-Q72B4Q5Z-izl5Qmnx.mjs +70 -0
- package/dist/chunk-Q72B4Q5Z-izl5Qmnx.mjs.map +1 -0
- package/dist/experimental-config.d.mts +1551 -0
- package/dist/experimental-config.d.mts.map +1 -0
- package/dist/experimental-config.mjs +223 -0
- package/dist/experimental-config.mjs.map +1 -0
- package/dist/index.d.mts +183 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +76684 -0
- package/dist/index.mjs.map +1 -0
- package/dist/open-PBXRGS4R-B5qmtwhS.mjs +529 -0
- package/dist/open-PBXRGS4R-B5qmtwhS.mjs.map +1 -0
- package/dist/package-VLsxxiiN.mjs +93 -0
- package/dist/package-VLsxxiiN.mjs.map +1 -0
- package/dist/workers/asset-worker/index.js +5827 -0
- package/dist/workers/router-worker/index.js +6414 -0
- package/dist/workers/runner-worker/index.js +450 -0
- package/dist/workers/runner-worker/module-runner-legacy.js +1129 -0
- package/dist/workers/runner-worker/module-runner.js +1231 -0
- package/dist/workers/vite-proxy-worker/index.js +31 -0
- package/package.json +121 -0
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import { DurableObject, WorkerEntrypoint, WorkflowEntrypoint } from "cloudflare:workers";
|
|
2
|
+
import { ModuleRunner, ssrDynamicImportKey, ssrModuleExportsKey } from "vite/module-runner";
|
|
3
|
+
|
|
4
|
+
//#region src/shared.ts
|
|
5
|
+
const UNKNOWN_HOST = "http://localhost";
|
|
6
|
+
const INIT_PATH = "/__vite_plugin_cloudflare_init__";
|
|
7
|
+
const GET_EXPORT_TYPES_PATH = "/__vite_plugin_cloudflare_get_export_types__";
|
|
8
|
+
const WORKER_ENTRY_PATH_HEADER = "__VITE_WORKER_ENTRY_PATH__";
|
|
9
|
+
const IS_ENTRY_WORKER_HEADER = "__VITE_IS_ENTRY_WORKER__";
|
|
10
|
+
const ENVIRONMENT_NAME_HEADER = "__VITE_ENVIRONMENT_NAME__";
|
|
11
|
+
const IS_PARENT_ENVIRONMENT_HEADER = "__VITE_IS_PARENT_ENVIRONMENT__";
|
|
12
|
+
const virtualPrefix = "virtual:cloudflare/";
|
|
13
|
+
const VIRTUAL_WORKER_ENTRY = `${virtualPrefix}worker-entry`;
|
|
14
|
+
const VIRTUAL_EXPORT_TYPES = `${virtualPrefix}export-types`;
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/workers/runner-worker/env.ts
|
|
18
|
+
/**
|
|
19
|
+
* Strips internal properties from the `env` object, returning only the user-defined properties.
|
|
20
|
+
* @param internalEnv - The full wrapper env, including internal properties
|
|
21
|
+
* @returns The user-defined env
|
|
22
|
+
*/
|
|
23
|
+
function stripInternalEnv(internalEnv) {
|
|
24
|
+
const { __VITE_RUNNER_OBJECT__: __VITE_RUNNER_OBJECT__$1, __VITE_INVOKE_MODULE__, __VITE_UNSAFE_EVAL__,...userEnv } = internalEnv;
|
|
25
|
+
return userEnv;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region src/workers/runner-worker/errors.ts
|
|
30
|
+
/**
|
|
31
|
+
* Converts an error to an object that can be be serialized and revived by Miniflare.
|
|
32
|
+
* Copied from `packages/wrangler/templates/middleware/middleware-miniflare3-json-error.ts`
|
|
33
|
+
*/
|
|
34
|
+
function reduceError(e) {
|
|
35
|
+
const errorObj = e ?? {};
|
|
36
|
+
return {
|
|
37
|
+
name: errorObj.name,
|
|
38
|
+
message: errorObj.message ?? String(e),
|
|
39
|
+
stack: errorObj.stack,
|
|
40
|
+
cause: errorObj.cause === void 0 ? void 0 : reduceError(errorObj.cause)
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Captures errors in the `fetch` handler of the default export of the entry Worker.
|
|
45
|
+
* These are returned as a JSON response that is revived by Miniflare.
|
|
46
|
+
* See comment in `/packages/wrangler/src/deployment-bundle/bundle.ts` for more info.
|
|
47
|
+
*/
|
|
48
|
+
async function maybeCaptureError(options, fn) {
|
|
49
|
+
if (!options.isEntryWorker || options.exportName !== "default" || options.key !== "fetch") return fn();
|
|
50
|
+
try {
|
|
51
|
+
return await fn();
|
|
52
|
+
} catch (error) {
|
|
53
|
+
return Response.json(reduceError(error), {
|
|
54
|
+
status: 500,
|
|
55
|
+
headers: { "MF-Experimental-Error-Stack": "true" }
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/workers/runner-worker/keys.ts
|
|
62
|
+
/** Keys that should be ignored during RPC property access */
|
|
63
|
+
const IGNORED_KEYS = ["self"];
|
|
64
|
+
/** Available methods for `WorkerEntrypoint` class */
|
|
65
|
+
const WORKER_ENTRYPOINT_KEYS = [
|
|
66
|
+
"connect",
|
|
67
|
+
"email",
|
|
68
|
+
"fetch",
|
|
69
|
+
"queue",
|
|
70
|
+
"tail",
|
|
71
|
+
"tailStream",
|
|
72
|
+
"test",
|
|
73
|
+
"trace",
|
|
74
|
+
"scheduled"
|
|
75
|
+
];
|
|
76
|
+
/** Available methods for `DurableObject` class */
|
|
77
|
+
const DURABLE_OBJECT_KEYS = [
|
|
78
|
+
"alarm",
|
|
79
|
+
"connect",
|
|
80
|
+
"fetch",
|
|
81
|
+
"webSocketClose",
|
|
82
|
+
"webSocketError",
|
|
83
|
+
"webSocketMessage"
|
|
84
|
+
];
|
|
85
|
+
/** Available methods for `WorkflowEntrypoint` classes */
|
|
86
|
+
const WORKFLOW_ENTRYPOINT_KEYS = ["run"];
|
|
87
|
+
|
|
88
|
+
//#endregion
|
|
89
|
+
//#region src/workers/runner-worker/module-runner.ts
|
|
90
|
+
/** Module runner instances keyed by environment name */
|
|
91
|
+
const moduleRunners = /* @__PURE__ */ new Map();
|
|
92
|
+
/** The parent environment name (set explicitly via IS_PARENT_ENVIRONMENT_HEADER) */
|
|
93
|
+
let parentEnvironmentName;
|
|
94
|
+
let nextCallbackId = 0;
|
|
95
|
+
const pendingCallbacks = /* @__PURE__ */ new Map();
|
|
96
|
+
const callbackResults = /* @__PURE__ */ new Map();
|
|
97
|
+
/**
|
|
98
|
+
* Executes a callback in the runner Durable Object's IoContext via RPC + shared memory.
|
|
99
|
+
* The callback function is stored in a module-scope map (shared with the DO
|
|
100
|
+
* since both run in the same V8 isolate). Only a numeric ID crosses the RPC
|
|
101
|
+
* boundary.
|
|
102
|
+
*/
|
|
103
|
+
async function runInRunnerObject(env, callback) {
|
|
104
|
+
const id = nextCallbackId++;
|
|
105
|
+
pendingCallbacks.set(id, callback);
|
|
106
|
+
try {
|
|
107
|
+
await env.__VITE_RUNNER_OBJECT__.get("singleton").executeCallback(id);
|
|
108
|
+
return callbackResults.get(id);
|
|
109
|
+
} finally {
|
|
110
|
+
pendingCallbacks.delete(id);
|
|
111
|
+
callbackResults.delete(id);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Durable Object that provides an IoContext for module evaluation and handles
|
|
116
|
+
* WebSocket communication with the Vite dev server.
|
|
117
|
+
*
|
|
118
|
+
* In workerd, a Durable Object has a single shared IoContext across all
|
|
119
|
+
* incoming events, so promises are freely shareable within the DO without
|
|
120
|
+
* cross-context issues.
|
|
121
|
+
*/
|
|
122
|
+
var __VITE_RUNNER_OBJECT__ = class extends DurableObject {
|
|
123
|
+
/** Per-environment WebSockets */
|
|
124
|
+
#webSockets = /* @__PURE__ */ new Map();
|
|
125
|
+
/**
|
|
126
|
+
* Creates a WebSocket pair for communication with the Vite dev server and initializes the ModuleRunner.
|
|
127
|
+
*/
|
|
128
|
+
async fetch(request) {
|
|
129
|
+
const { pathname } = new URL(request.url);
|
|
130
|
+
if (pathname !== INIT_PATH) throw new Error(`__VITE_RUNNER_OBJECT__ received invalid pathname: "${pathname}"`);
|
|
131
|
+
const environmentName = request.headers.get(ENVIRONMENT_NAME_HEADER);
|
|
132
|
+
if (!environmentName) throw new Error(`__VITE_RUNNER_OBJECT__ received request without "${ENVIRONMENT_NAME_HEADER}" header`);
|
|
133
|
+
if (moduleRunners.has(environmentName)) throw new Error(`Module runner already initialized for environment: "${environmentName}"`);
|
|
134
|
+
if (request.headers.get(IS_PARENT_ENVIRONMENT_HEADER) === "true") {
|
|
135
|
+
parentEnvironmentName = environmentName;
|
|
136
|
+
globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__ = async (envName, id) => {
|
|
137
|
+
const runner = moduleRunners.get(envName);
|
|
138
|
+
if (!runner) throw new Error(`Module runner not initialized for environment: "${envName}". Do you need to set \`childEnvironments: ["${envName}"]\` in the plugin config?`);
|
|
139
|
+
return runInRunnerObject(this.env, () => runner.import(id));
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
const { 0: client, 1: server } = new WebSocketPair();
|
|
143
|
+
server.accept();
|
|
144
|
+
const moduleRunner = await createModuleRunner(this.env, server, environmentName);
|
|
145
|
+
moduleRunners.set(environmentName, moduleRunner);
|
|
146
|
+
this.#webSockets.set(environmentName, server);
|
|
147
|
+
return new Response(null, {
|
|
148
|
+
status: 101,
|
|
149
|
+
webSocket: client
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Sends data to the Vite dev server via the WebSocket for a specific environment.
|
|
154
|
+
*/
|
|
155
|
+
send(environmentName, data) {
|
|
156
|
+
const webSocket = this.#webSockets.get(environmentName);
|
|
157
|
+
if (!webSocket) throw new Error(`Module runner not initialized for environment: "${environmentName}"`);
|
|
158
|
+
webSocket.send(data);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Executes a callback stored in the module-scope `pendingCallbacks` map.
|
|
162
|
+
* The callback runs in the DO's IoContext, ensuring all promises created
|
|
163
|
+
* during execution belong to the DO's shared context.
|
|
164
|
+
*/
|
|
165
|
+
async executeCallback(id) {
|
|
166
|
+
const callback = pendingCallbacks.get(id);
|
|
167
|
+
if (!callback) throw new Error(`No pending callback with id ${id}`);
|
|
168
|
+
const result = await callback();
|
|
169
|
+
callbackResults.set(id, result);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
/**
|
|
173
|
+
* Creates a new module runner instance with a WebSocket transport.
|
|
174
|
+
* @param env - The wrapper env
|
|
175
|
+
* @param webSocket - WebSocket connection for communication with Vite dev server
|
|
176
|
+
* @param environmentName - The name of the environment this runner is for
|
|
177
|
+
* @returns Configured module runner instance
|
|
178
|
+
*/
|
|
179
|
+
async function createModuleRunner(env, webSocket, environmentName) {
|
|
180
|
+
return new ModuleRunner({
|
|
181
|
+
sourcemapInterceptor: "prepareStackTrace",
|
|
182
|
+
transport: {
|
|
183
|
+
connect({ onMessage }) {
|
|
184
|
+
webSocket.addEventListener("message", async ({ data }) => {
|
|
185
|
+
onMessage(JSON.parse(data.toString()));
|
|
186
|
+
});
|
|
187
|
+
onMessage({
|
|
188
|
+
type: "custom",
|
|
189
|
+
event: "vite:ws:connect",
|
|
190
|
+
data: { webSocket }
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
disconnect() {
|
|
194
|
+
webSocket.close();
|
|
195
|
+
},
|
|
196
|
+
send(data) {
|
|
197
|
+
env.__VITE_RUNNER_OBJECT__.get("singleton").send(environmentName, JSON.stringify(data));
|
|
198
|
+
},
|
|
199
|
+
async invoke(data) {
|
|
200
|
+
return await (await env.__VITE_INVOKE_MODULE__.fetch(new Request(UNKNOWN_HOST, {
|
|
201
|
+
method: "POST",
|
|
202
|
+
headers: { [ENVIRONMENT_NAME_HEADER]: environmentName },
|
|
203
|
+
body: JSON.stringify(data)
|
|
204
|
+
}))).json();
|
|
205
|
+
}
|
|
206
|
+
},
|
|
207
|
+
hmr: true
|
|
208
|
+
}, {
|
|
209
|
+
async runInlinedModule(context, transformed, module) {
|
|
210
|
+
const originalDynamicImport = context[ssrDynamicImportKey];
|
|
211
|
+
context[ssrDynamicImportKey] = (dep) => {
|
|
212
|
+
return runInRunnerObject(env, () => originalDynamicImport(dep));
|
|
213
|
+
};
|
|
214
|
+
const code = `"use strict";async (${Object.keys(context).join(",")})=>{${transformed}\n}`;
|
|
215
|
+
await env.__VITE_UNSAFE_EVAL__.eval(code, module.id)(...Object.values(context));
|
|
216
|
+
Object.seal(context[ssrModuleExportsKey]);
|
|
217
|
+
},
|
|
218
|
+
async runExternalModule(filepath) {
|
|
219
|
+
if (filepath === "cloudflare:workers") {
|
|
220
|
+
const originalCloudflareWorkersModule = await import("cloudflare:workers");
|
|
221
|
+
return Object.seal({
|
|
222
|
+
...originalCloudflareWorkersModule,
|
|
223
|
+
env: stripInternalEnv(originalCloudflareWorkersModule.env)
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return import(filepath);
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Retrieves a specific export from a Worker entry module using the module runner.
|
|
232
|
+
*/
|
|
233
|
+
async function getWorkerEntryExport(workerEntryPath$1, exportName) {
|
|
234
|
+
if (!parentEnvironmentName) throw new Error(`Parent environment not initialized`);
|
|
235
|
+
const module = await globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__(parentEnvironmentName, VIRTUAL_WORKER_ENTRY);
|
|
236
|
+
const exportValue = typeof module === "object" && module !== null && exportName in module && module[exportName];
|
|
237
|
+
if (!exportValue) throw new Error(`"${workerEntryPath$1}" does not define a "${exportName}" export.`);
|
|
238
|
+
return exportValue;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Retrieves the export types of the Worker entry module.
|
|
242
|
+
*/
|
|
243
|
+
async function getWorkerEntryExportTypes() {
|
|
244
|
+
if (!parentEnvironmentName) throw new Error(`Parent environment not initialized`);
|
|
245
|
+
const { getExportTypes } = await globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__(parentEnvironmentName, VIRTUAL_EXPORT_TYPES);
|
|
246
|
+
return getExportTypes(await globalThis.__VITE_ENVIRONMENT_RUNNER_IMPORT__(parentEnvironmentName, VIRTUAL_WORKER_ENTRY));
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/workers/runner-worker/index.ts
|
|
251
|
+
/** The path to the Worker entry file. We store it in the module scope so that it is easily accessible in error messages etc.. */
|
|
252
|
+
let workerEntryPath = "";
|
|
253
|
+
let isEntryWorker = false;
|
|
254
|
+
/**
|
|
255
|
+
* Creates a callable thenable that is used to access the properties of an RPC target.
|
|
256
|
+
* It can be both awaited and invoked as a function.
|
|
257
|
+
* This enables RPC properties to be used both as promises and callable functions.
|
|
258
|
+
* @param key - The property key name used for error messages
|
|
259
|
+
* @param property - The promise that resolves to the property value
|
|
260
|
+
* @returns A callable thenable that implements both Promise and function interfaces
|
|
261
|
+
*/
|
|
262
|
+
function getRpcPropertyCallableThenable(key, property) {
|
|
263
|
+
const fn = async function(...args) {
|
|
264
|
+
const maybeFn = await property;
|
|
265
|
+
if (typeof maybeFn !== "function") throw new TypeError(`"${key}" is not a function.`);
|
|
266
|
+
return maybeFn(...args);
|
|
267
|
+
};
|
|
268
|
+
fn.then = (onFulfilled, onRejected) => property.then(onFulfilled, onRejected);
|
|
269
|
+
fn.catch = (onRejected) => property.catch(onRejected);
|
|
270
|
+
fn.finally = (onFinally) => property.finally(onFinally);
|
|
271
|
+
return fn;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Retrieves an RPC property from a constructor prototype, ensuring it exists on the prototype rather than the instance to maintain RPC compatibility.
|
|
275
|
+
* @param ctor - The constructor function (`WorkerEntrypoint` or `DurableObject`)
|
|
276
|
+
* @param instance - The instance to bind methods to
|
|
277
|
+
* @param key - The property key to retrieve
|
|
278
|
+
* @returns The property value from the prototype
|
|
279
|
+
* @throws TypeError if the property doesn't exist on the prototype
|
|
280
|
+
*/
|
|
281
|
+
function getRpcProperty(ctor, instance, key) {
|
|
282
|
+
if (!Reflect.has(ctor.prototype, key)) {
|
|
283
|
+
if (Reflect.has(instance, key)) throw new TypeError([
|
|
284
|
+
`The RPC receiver's prototype does not implement "${key}", but the receiver instance does.`,
|
|
285
|
+
"Only properties and methods defined on the prototype can be accessed over RPC.",
|
|
286
|
+
`Ensure properties are declared as \`get ${key}() { ... }\` instead of \`${key} = ...\`,`,
|
|
287
|
+
`and methods are declared as \`${key}() { ... }\` instead of \`${key} = () => { ... }\`.`
|
|
288
|
+
].join("\n"));
|
|
289
|
+
throw new TypeError(`The RPC receiver does not implement "${key}".`);
|
|
290
|
+
}
|
|
291
|
+
return Reflect.get(ctor.prototype, key, instance);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Retrieves an RPC property from a `WorkerEntrypoint` export, creating an instance and returning the bound method or property value.
|
|
295
|
+
* @param exportName - The name of the `WorkerEntrypoint` export
|
|
296
|
+
* @param key - The property key to access on the `WorkerEntrypoint` instance
|
|
297
|
+
* @returns The property value, with methods bound to the instance
|
|
298
|
+
* @throws TypeError if the export is not a `WorkerEntrypoint` subclass
|
|
299
|
+
*/
|
|
300
|
+
async function getWorkerEntrypointRpcProperty(exportName, key) {
|
|
301
|
+
const ctor = await getWorkerEntryExport(workerEntryPath, exportName);
|
|
302
|
+
const userEnv = stripInternalEnv(this.env);
|
|
303
|
+
const expectedWorkerEntrypointMessage = `Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`WorkerEntrypoint\` for RPC.`;
|
|
304
|
+
if (typeof ctor !== "function") throw new TypeError(expectedWorkerEntrypointMessage);
|
|
305
|
+
const instance = new ctor(this.ctx, userEnv);
|
|
306
|
+
if (!(instance instanceof WorkerEntrypoint)) throw new TypeError(expectedWorkerEntrypointMessage);
|
|
307
|
+
const value = getRpcProperty(ctor, instance, key);
|
|
308
|
+
if (typeof value === "function") return value.bind(instance);
|
|
309
|
+
return value;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Creates a proxy wrapper for `WorkerEntrypoint` classes that enables RPC functionality.
|
|
313
|
+
* The wrapper intercepts property access and delegates to the user code, handling both direct method calls and RPC property access.
|
|
314
|
+
* @param exportName - The name of the `WorkerEntrypoint` export to wrap
|
|
315
|
+
* @returns A `WorkerEntrypoint` constructor that acts as a proxy to the user code
|
|
316
|
+
*/
|
|
317
|
+
function createWorkerEntrypointWrapper(exportName) {
|
|
318
|
+
class Wrapper extends WorkerEntrypoint {
|
|
319
|
+
constructor(ctx, env) {
|
|
320
|
+
super(ctx, env);
|
|
321
|
+
return new Proxy(this, { get(target, key, receiver) {
|
|
322
|
+
const value = Reflect.get(target, key, receiver);
|
|
323
|
+
if (value !== void 0) return value;
|
|
324
|
+
if (typeof key === "symbol" || IGNORED_KEYS.includes(key) || DURABLE_OBJECT_KEYS.includes(key)) return;
|
|
325
|
+
return getRpcPropertyCallableThenable(key, getWorkerEntrypointRpcProperty.call(receiver, exportName, key));
|
|
326
|
+
} });
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
for (const key of WORKER_ENTRYPOINT_KEYS) Wrapper.prototype[key] = async function(arg) {
|
|
330
|
+
return maybeCaptureError({
|
|
331
|
+
isEntryWorker,
|
|
332
|
+
exportName,
|
|
333
|
+
key
|
|
334
|
+
}, async () => {
|
|
335
|
+
if (key === "fetch") {
|
|
336
|
+
const request = arg;
|
|
337
|
+
const url = new URL(request.url);
|
|
338
|
+
if (url.pathname === INIT_PATH) {
|
|
339
|
+
if (request.headers.get(IS_PARENT_ENVIRONMENT_HEADER) === "true") {
|
|
340
|
+
const workerEntryPathHeader = request.headers.get(WORKER_ENTRY_PATH_HEADER);
|
|
341
|
+
if (!workerEntryPathHeader) throw new Error(`Unexpected error: "${WORKER_ENTRY_PATH_HEADER}" header not set.`);
|
|
342
|
+
const isEntryWorkerHeader = request.headers.get(IS_ENTRY_WORKER_HEADER);
|
|
343
|
+
if (!isEntryWorkerHeader) throw new Error(`Unexpected error: "${IS_ENTRY_WORKER_HEADER}" header not set.`);
|
|
344
|
+
workerEntryPath = decodeURIComponent(workerEntryPathHeader);
|
|
345
|
+
isEntryWorker = isEntryWorkerHeader === "true";
|
|
346
|
+
}
|
|
347
|
+
return this.env.__VITE_RUNNER_OBJECT__.get("singleton").fetch(request);
|
|
348
|
+
}
|
|
349
|
+
if (url.pathname === GET_EXPORT_TYPES_PATH) {
|
|
350
|
+
const workerEntryExportTypes = await getWorkerEntryExportTypes();
|
|
351
|
+
return Response.json(workerEntryExportTypes);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const exportValue = await getWorkerEntryExport(workerEntryPath, exportName);
|
|
355
|
+
const userEnv = stripInternalEnv(this.env);
|
|
356
|
+
if (typeof exportValue === "object" && exportValue !== null) {
|
|
357
|
+
const maybeFn = exportValue[key];
|
|
358
|
+
if (typeof maybeFn !== "function") throw new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to define a \`${key}()\` function.`);
|
|
359
|
+
return maybeFn.call(exportValue, arg, userEnv, this.ctx);
|
|
360
|
+
} else if (typeof exportValue === "function") {
|
|
361
|
+
const instance = new exportValue(this.ctx, userEnv);
|
|
362
|
+
if (!(instance instanceof WorkerEntrypoint)) throw new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`WorkerEntrypoint\`.`);
|
|
363
|
+
const maybeFn = instance[key];
|
|
364
|
+
if (typeof maybeFn !== "function") throw new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to define a \`${key}()\` method.`);
|
|
365
|
+
return maybeFn.call(instance, arg);
|
|
366
|
+
} else return /* @__PURE__ */ new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to be an object or a class.`);
|
|
367
|
+
});
|
|
368
|
+
};
|
|
369
|
+
return Wrapper;
|
|
370
|
+
}
|
|
371
|
+
/** Symbol key for storing the `DurableObject` instance */
|
|
372
|
+
const kInstance = Symbol("kInstance");
|
|
373
|
+
/** Symbol key for the instance initialization method */
|
|
374
|
+
const kEnsureInstance = Symbol("kEnsureInstance");
|
|
375
|
+
/**
|
|
376
|
+
* Retrieves an RPC property from a `DurableObject` export, ensuring an instance is properly initialized and returning the bound method or property value.
|
|
377
|
+
* @param exportName - The name of the `DurableObject` export
|
|
378
|
+
* @param key - The property key to access on the `DurableObject` instance
|
|
379
|
+
* @returns The property value, with methods bound to the instance
|
|
380
|
+
* @throws TypeError if the export is not a `DurableObject` subclass
|
|
381
|
+
*/
|
|
382
|
+
async function getDurableObjectRpcProperty(exportName, key) {
|
|
383
|
+
const { ctor, instance } = await this[kEnsureInstance]();
|
|
384
|
+
if (!(instance instanceof DurableObject)) throw new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`DurableObject\` for RPC.`);
|
|
385
|
+
const value = getRpcProperty(ctor, instance, key);
|
|
386
|
+
if (typeof value === "function") return value.bind(instance);
|
|
387
|
+
return value;
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* Creates a proxy wrapper for `DurableObject` classes that enables RPC functionality.
|
|
391
|
+
* The wrapper manages instance lifecycle and delegates method calls to the user code, handling both direct method calls and RPC property access.
|
|
392
|
+
* @param exportName - The name of the `DurableObject` export to wrap
|
|
393
|
+
* @returns A `DurableObject` constructor that acts as a proxy to the user code
|
|
394
|
+
*/
|
|
395
|
+
function createDurableObjectWrapper(exportName) {
|
|
396
|
+
class Wrapper extends DurableObject {
|
|
397
|
+
[kInstance];
|
|
398
|
+
constructor(ctx, env) {
|
|
399
|
+
super(ctx, env);
|
|
400
|
+
return new Proxy(this, { get(target, key, receiver) {
|
|
401
|
+
const value = Reflect.get(target, key, receiver);
|
|
402
|
+
if (value !== void 0) return value;
|
|
403
|
+
if (typeof key === "symbol" || IGNORED_KEYS.includes(key) || WORKER_ENTRYPOINT_KEYS.includes(key)) return;
|
|
404
|
+
return getRpcPropertyCallableThenable(key, getDurableObjectRpcProperty.call(receiver, exportName, key));
|
|
405
|
+
} });
|
|
406
|
+
}
|
|
407
|
+
async [kEnsureInstance]() {
|
|
408
|
+
const ctor = await getWorkerEntryExport(workerEntryPath, exportName);
|
|
409
|
+
if (typeof ctor !== "function") throw new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`DurableObject\`.`);
|
|
410
|
+
if (!this[kInstance] || this[kInstance].ctor !== ctor) {
|
|
411
|
+
const userEnv = stripInternalEnv(this.env);
|
|
412
|
+
this[kInstance] = {
|
|
413
|
+
ctor,
|
|
414
|
+
instance: new ctor(this.ctx, userEnv)
|
|
415
|
+
};
|
|
416
|
+
await this.ctx.blockConcurrencyWhile(async () => {});
|
|
417
|
+
}
|
|
418
|
+
return this[kInstance];
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
for (const key of DURABLE_OBJECT_KEYS) Wrapper.prototype[key] = async function(...args) {
|
|
422
|
+
const { instance } = await this[kEnsureInstance]();
|
|
423
|
+
const maybeFn = instance[key];
|
|
424
|
+
if (typeof maybeFn !== "function") throw new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to define a \`${key}()\` function.`);
|
|
425
|
+
return maybeFn.apply(instance, args);
|
|
426
|
+
};
|
|
427
|
+
return Wrapper;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Creates a proxy wrapper for `WorkflowEntrypoint` classes.
|
|
431
|
+
* The wrapper delegates method calls to the user code.
|
|
432
|
+
* @param exportName - The name of the `WorkflowEntrypoint` export to wrap
|
|
433
|
+
* @returns A `WorkflowEntrypoint` constructor that acts as a proxy to the user code
|
|
434
|
+
*/
|
|
435
|
+
function createWorkflowEntrypointWrapper(exportName) {
|
|
436
|
+
class Wrapper extends WorkflowEntrypoint {}
|
|
437
|
+
for (const key of WORKFLOW_ENTRYPOINT_KEYS) Wrapper.prototype[key] = async function(...args) {
|
|
438
|
+
const ctor = await getWorkerEntryExport(workerEntryPath, exportName);
|
|
439
|
+
const userEnv = stripInternalEnv(this.env);
|
|
440
|
+
const instance = new ctor(this.ctx, userEnv);
|
|
441
|
+
if (!(instance instanceof WorkflowEntrypoint)) throw new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`WorkflowEntrypoint\`.`);
|
|
442
|
+
const maybeFn = instance[key];
|
|
443
|
+
if (typeof maybeFn !== "function") throw new TypeError(`Expected "${exportName}" export of "${workerEntryPath}" to define a \`${key}()\` function.`);
|
|
444
|
+
return maybeFn.apply(instance, args);
|
|
445
|
+
};
|
|
446
|
+
return Wrapper;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
//#endregion
|
|
450
|
+
export { __VITE_RUNNER_OBJECT__, createDurableObjectWrapper, createWorkerEntrypointWrapper, createWorkflowEntrypointWrapper };
|