@cloudflare/vite-plugin 1.11.0 → 1.11.2
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/dist/asset-workers/asset-worker.js +12 -0
- package/dist/index.js +88 -86
- package/dist/runner-worker/index.js +194 -170
- package/dist/vite-proxy-worker/index.js +39 -0
- package/package.json +7 -7
|
@@ -6012,6 +6012,18 @@ var CustomAssetWorker = class extends worker_default {
|
|
|
6012
6012
|
const modifiedResponse = new Response(response.body, response);
|
|
6013
6013
|
modifiedResponse.headers.delete("ETag");
|
|
6014
6014
|
modifiedResponse.headers.delete("Cache-Control");
|
|
6015
|
+
const viteHeaders = JSON.parse(
|
|
6016
|
+
this.env.__VITE_HEADERS__
|
|
6017
|
+
);
|
|
6018
|
+
for (const [key, value] of Object.entries(viteHeaders)) {
|
|
6019
|
+
if (Array.isArray(value)) {
|
|
6020
|
+
for (const item of value) {
|
|
6021
|
+
modifiedResponse.headers.append(key, item);
|
|
6022
|
+
}
|
|
6023
|
+
} else if (value !== void 0) {
|
|
6024
|
+
modifiedResponse.headers.set(key, String(value));
|
|
6025
|
+
}
|
|
6026
|
+
}
|
|
6015
6027
|
return modifiedResponse;
|
|
6016
6028
|
}
|
|
6017
6029
|
async unstable_getByETag(eTag) {
|
package/dist/index.js
CHANGED
|
@@ -486,10 +486,9 @@ var require_mime = __commonJS({
|
|
|
486
486
|
|
|
487
487
|
// src/index.ts
|
|
488
488
|
import assert12 from "node:assert";
|
|
489
|
-
import { randomUUID as randomUUID2 } from "node:crypto";
|
|
490
489
|
import * as fsp2 from "node:fs/promises";
|
|
491
490
|
import * as path13 from "node:path";
|
|
492
|
-
import util3 from "node:util";
|
|
491
|
+
import * as util3 from "node:util";
|
|
493
492
|
|
|
494
493
|
// ../containers-shared/src/utils.ts
|
|
495
494
|
import { execFileSync, spawn as spawn2 } from "child_process";
|
|
@@ -625,10 +624,22 @@ var isDockerfile = (image, configPath) => {
|
|
|
625
624
|
}
|
|
626
625
|
return false;
|
|
627
626
|
};
|
|
628
|
-
|
|
627
|
+
var cleanupContainers = (dockerPath, imageTags) => {
|
|
628
|
+
try {
|
|
629
|
+
const containerIds = getContainerIdsByImageTags(dockerPath, imageTags);
|
|
630
|
+
if (containerIds.length === 0) {
|
|
631
|
+
return true;
|
|
632
|
+
}
|
|
633
|
+
runDockerCmdWithOutput(dockerPath, ["rm", "--force", ...containerIds]);
|
|
634
|
+
return true;
|
|
635
|
+
} catch {
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
function getContainerIdsByImageTags(dockerPath, imageTags) {
|
|
629
640
|
const ids = /* @__PURE__ */ new Set();
|
|
630
641
|
for (const imageTag of imageTags) {
|
|
631
|
-
const containerIdsFromImage =
|
|
642
|
+
const containerIdsFromImage = getContainerIdsFromImage(
|
|
632
643
|
dockerPath,
|
|
633
644
|
imageTag
|
|
634
645
|
);
|
|
@@ -636,7 +647,7 @@ async function getContainerIdsByImageTags(dockerPath, imageTags) {
|
|
|
636
647
|
}
|
|
637
648
|
return Array.from(ids);
|
|
638
649
|
}
|
|
639
|
-
var getContainerIdsFromImage =
|
|
650
|
+
var getContainerIdsFromImage = (dockerPath, ancestorImage) => {
|
|
640
651
|
const output = runDockerCmdWithOutput(dockerPath, [
|
|
641
652
|
"ps",
|
|
642
653
|
"-a",
|
|
@@ -1794,6 +1805,7 @@ import * as vite6 from "vite";
|
|
|
1794
1805
|
// src/constants.ts
|
|
1795
1806
|
var ROUTER_WORKER_NAME = "__router-worker__";
|
|
1796
1807
|
var ASSET_WORKER_NAME = "__asset-worker__";
|
|
1808
|
+
var VITE_PROXY_WORKER_NAME = "__vite_proxy_worker__";
|
|
1797
1809
|
var ASSET_WORKERS_COMPATIBILITY_DATE = "2024-10-04";
|
|
1798
1810
|
var ADDITIONAL_MODULE_TYPES = [
|
|
1799
1811
|
"CompiledWasm",
|
|
@@ -6288,7 +6300,7 @@ function getImportedAssetPaths(viteManifest) {
|
|
|
6288
6300
|
|
|
6289
6301
|
// src/cloudflare-environment.ts
|
|
6290
6302
|
import assert4 from "node:assert";
|
|
6291
|
-
import util2 from "node:util";
|
|
6303
|
+
import * as util2 from "node:util";
|
|
6292
6304
|
import * as vite2 from "vite";
|
|
6293
6305
|
|
|
6294
6306
|
// src/node-js-compat.ts
|
|
@@ -13404,7 +13416,7 @@ var additionalModuleGlobalRE = new RegExp(
|
|
|
13404
13416
|
ADDITIONAL_MODULE_PATTERN,
|
|
13405
13417
|
"g"
|
|
13406
13418
|
);
|
|
13407
|
-
var
|
|
13419
|
+
var WORKER_ENTRY_PATH_HEADER = "__VITE_WORKER_ENTRY_PATH__";
|
|
13408
13420
|
|
|
13409
13421
|
// src/utils.ts
|
|
13410
13422
|
import * as path6 from "node:path";
|
|
@@ -13458,7 +13470,7 @@ function toMiniflareRequest(request2) {
|
|
|
13458
13470
|
|
|
13459
13471
|
// src/cloudflare-environment.ts
|
|
13460
13472
|
var webSocketUndefinedError = "The WebSocket is undefined";
|
|
13461
|
-
var
|
|
13473
|
+
var debuglog2 = util2.debuglog("@cloudflare:vite-plugin");
|
|
13462
13474
|
function createHotChannel(webSocketContainer) {
|
|
13463
13475
|
const listenersMap = /* @__PURE__ */ new Map();
|
|
13464
13476
|
const client = {
|
|
@@ -13512,16 +13524,13 @@ var CloudflareDevEnvironment = class extends vite2.DevEnvironment {
|
|
|
13512
13524
|
});
|
|
13513
13525
|
this.#webSocketContainer = webSocketContainer;
|
|
13514
13526
|
}
|
|
13515
|
-
async initRunner(worker, workerConfig
|
|
13527
|
+
async initRunner(worker, workerConfig) {
|
|
13516
13528
|
this.#worker = worker;
|
|
13517
13529
|
const response = await this.#worker.fetch(
|
|
13518
13530
|
new URL(INIT_PATH, UNKNOWN_HOST),
|
|
13519
13531
|
{
|
|
13520
13532
|
headers: {
|
|
13521
|
-
[
|
|
13522
|
-
entryPath: workerConfig.main,
|
|
13523
|
-
configId
|
|
13524
|
-
}),
|
|
13533
|
+
[WORKER_ENTRY_PATH_HEADER]: workerConfig.main,
|
|
13525
13534
|
upgrade: "websocket"
|
|
13526
13535
|
}
|
|
13527
13536
|
}
|
|
@@ -13604,13 +13613,13 @@ function createCloudflareEnvironmentOptions(workerConfig, userConfig, environmen
|
|
|
13604
13613
|
keepProcessEnv: isNodeCompat(workerConfig)
|
|
13605
13614
|
};
|
|
13606
13615
|
}
|
|
13607
|
-
function initRunners(resolvedPluginConfig, viteDevServer, miniflare2
|
|
13616
|
+
function initRunners(resolvedPluginConfig, viteDevServer, miniflare2) {
|
|
13608
13617
|
return Promise.all(
|
|
13609
13618
|
Object.entries(resolvedPluginConfig.workers).map(
|
|
13610
13619
|
async ([environmentName, workerConfig]) => {
|
|
13611
|
-
|
|
13620
|
+
debuglog2("Initializing worker:", workerConfig.name);
|
|
13612
13621
|
const worker = await miniflare2.getWorker(workerConfig.name);
|
|
13613
|
-
return viteDevServer.environments[environmentName].initRunner(worker, workerConfig
|
|
13622
|
+
return viteDevServer.environments[environmentName].initRunner(worker, workerConfig);
|
|
13614
13623
|
}
|
|
13615
13624
|
)
|
|
13616
13625
|
);
|
|
@@ -14587,20 +14596,6 @@ function getDockerPath() {
|
|
|
14587
14596
|
const dockerPathEnvVar = "WRANGLER_DOCKER_BIN";
|
|
14588
14597
|
return process.env[dockerPathEnvVar] || defaultDockerPath;
|
|
14589
14598
|
}
|
|
14590
|
-
async function removeContainersByIds(dockerPath, containerIds) {
|
|
14591
|
-
try {
|
|
14592
|
-
if (containerIds.length === 0) {
|
|
14593
|
-
return;
|
|
14594
|
-
}
|
|
14595
|
-
await runDockerCmd(
|
|
14596
|
-
dockerPath,
|
|
14597
|
-
["rm", "--force", ...containerIds],
|
|
14598
|
-
["inherit", "pipe", "pipe"]
|
|
14599
|
-
);
|
|
14600
|
-
} catch (error) {
|
|
14601
|
-
return;
|
|
14602
|
-
}
|
|
14603
|
-
}
|
|
14604
14599
|
async function getContainerOptions(options) {
|
|
14605
14600
|
const {
|
|
14606
14601
|
containersConfig,
|
|
@@ -14878,6 +14873,7 @@ import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
|
14878
14873
|
import {
|
|
14879
14874
|
getDefaultDevRegistryPath,
|
|
14880
14875
|
kCurrentWorker,
|
|
14876
|
+
kUnsafeEphemeralUniqueKey,
|
|
14881
14877
|
Log,
|
|
14882
14878
|
LogLevel,
|
|
14883
14879
|
Response as MiniflareResponse2
|
|
@@ -14979,6 +14975,7 @@ var ROUTER_WORKER_PATH = "./asset-workers/router-worker.js";
|
|
|
14979
14975
|
var ASSET_WORKER_PATH = "./asset-workers/asset-worker.js";
|
|
14980
14976
|
var WRAPPER_PATH = "__VITE_WORKER_ENTRY__";
|
|
14981
14977
|
var RUNNER_PATH = "./runner-worker/index.js";
|
|
14978
|
+
var VITE_PROXY_WORKER_PATH = "./vite-proxy-worker/index.js";
|
|
14982
14979
|
function getEntryWorkerConfig(resolvedPluginConfig) {
|
|
14983
14980
|
if (resolvedPluginConfig.type === "assets-only") {
|
|
14984
14981
|
return;
|
|
@@ -15060,7 +15057,8 @@ async function getDevMiniflareOptions(config) {
|
|
|
15060
15057
|
}
|
|
15061
15058
|
],
|
|
15062
15059
|
bindings: {
|
|
15063
|
-
CONFIG: assetsConfig
|
|
15060
|
+
CONFIG: assetsConfig,
|
|
15061
|
+
__VITE_HEADERS__: JSON.stringify(viteDevServer.config.server.headers)
|
|
15064
15062
|
},
|
|
15065
15063
|
serviceBindings: {
|
|
15066
15064
|
__VITE_HTML_EXISTS__: async (request2) => {
|
|
@@ -15108,6 +15106,26 @@ async function getDevMiniflareOptions(config) {
|
|
|
15108
15106
|
}
|
|
15109
15107
|
}
|
|
15110
15108
|
}
|
|
15109
|
+
},
|
|
15110
|
+
{
|
|
15111
|
+
name: VITE_PROXY_WORKER_NAME,
|
|
15112
|
+
compatibilityDate: ASSET_WORKERS_COMPATIBILITY_DATE,
|
|
15113
|
+
modulesRoot: miniflareModulesRoot,
|
|
15114
|
+
modules: [
|
|
15115
|
+
{
|
|
15116
|
+
type: "ESModule",
|
|
15117
|
+
path: path10.join(miniflareModulesRoot, VITE_PROXY_WORKER_PATH),
|
|
15118
|
+
contents: fs4.readFileSync(
|
|
15119
|
+
fileURLToPath2(new URL(VITE_PROXY_WORKER_PATH, import.meta.url))
|
|
15120
|
+
)
|
|
15121
|
+
}
|
|
15122
|
+
],
|
|
15123
|
+
serviceBindings: {
|
|
15124
|
+
...entryWorkerConfig ? { ENTRY_USER_WORKER: entryWorkerConfig.name } : {},
|
|
15125
|
+
__VITE_MIDDLEWARE__: {
|
|
15126
|
+
node: (req, res) => viteDevServer.middlewares(req, res)
|
|
15127
|
+
}
|
|
15128
|
+
}
|
|
15111
15129
|
}
|
|
15112
15130
|
];
|
|
15113
15131
|
const workersFromConfig = resolvedPluginConfig.type === "workers" ? await Promise.all(
|
|
@@ -15150,10 +15168,15 @@ async function getDevMiniflareOptions(config) {
|
|
|
15150
15168
|
...workerOptions,
|
|
15151
15169
|
name: workerOptions.name ?? workerConfig.name,
|
|
15152
15170
|
unsafeInspectorProxy: inspectorPort !== false,
|
|
15153
|
-
unsafeDirectSockets: environmentName === resolvedPluginConfig.entryWorkerEnvironmentName ?
|
|
15154
|
-
|
|
15155
|
-
|
|
15156
|
-
|
|
15171
|
+
unsafeDirectSockets: environmentName === resolvedPluginConfig.entryWorkerEnvironmentName ? [
|
|
15172
|
+
{
|
|
15173
|
+
// This exposes the default entrypoint of the asset proxy worker
|
|
15174
|
+
// on the dev registry with the name of the entry worker
|
|
15175
|
+
serviceName: VITE_PROXY_WORKER_NAME,
|
|
15176
|
+
entrypoint: void 0,
|
|
15177
|
+
proxy: true
|
|
15178
|
+
}
|
|
15179
|
+
] : [],
|
|
15157
15180
|
modulesRoot: miniflareModulesRoot,
|
|
15158
15181
|
unsafeEvalBinding: "__VITE_UNSAFE_EVAL__",
|
|
15159
15182
|
serviceBindings: {
|
|
@@ -15222,6 +15245,7 @@ async function getDevMiniflareOptions(config) {
|
|
|
15222
15245
|
...userWorkers.map((workerOptions) => {
|
|
15223
15246
|
const wrappers = [
|
|
15224
15247
|
`import { createWorkerEntrypointWrapper, createDurableObjectWrapper, createWorkflowEntrypointWrapper } from '${RUNNER_PATH}';`,
|
|
15248
|
+
`export { __VITE_RUNNER_OBJECT__ } from '${RUNNER_PATH}';`,
|
|
15225
15249
|
`export default createWorkerEntrypointWrapper('default');`
|
|
15226
15250
|
];
|
|
15227
15251
|
const workerEntrypointNames = workerToWorkerEntrypointNamesMap.get(
|
|
@@ -15265,6 +15289,14 @@ async function getDevMiniflareOptions(config) {
|
|
|
15265
15289
|
);
|
|
15266
15290
|
return {
|
|
15267
15291
|
...workerOptions,
|
|
15292
|
+
durableObjects: {
|
|
15293
|
+
...workerOptions.durableObjects,
|
|
15294
|
+
__VITE_RUNNER_OBJECT__: {
|
|
15295
|
+
className: "__VITE_RUNNER_OBJECT__",
|
|
15296
|
+
unsafeUniqueKey: kUnsafeEphemeralUniqueKey,
|
|
15297
|
+
unsafePreventEviction: true
|
|
15298
|
+
}
|
|
15299
|
+
},
|
|
15268
15300
|
modules: [
|
|
15269
15301
|
{
|
|
15270
15302
|
type: "ESModule",
|
|
@@ -15984,7 +16016,7 @@ function handleWebSocket(httpServer, getFetcher) {
|
|
|
15984
16016
|
}
|
|
15985
16017
|
|
|
15986
16018
|
// src/index.ts
|
|
15987
|
-
var
|
|
16019
|
+
var debuglog4 = util3.debuglog("@cloudflare:vite-plugin");
|
|
15988
16020
|
var workersConfigsWarningShown = false;
|
|
15989
16021
|
var miniflare;
|
|
15990
16022
|
function cloudflare(pluginConfig = {}) {
|
|
@@ -15992,8 +16024,7 @@ function cloudflare(pluginConfig = {}) {
|
|
|
15992
16024
|
let resolvedViteConfig;
|
|
15993
16025
|
const additionalModulePaths = /* @__PURE__ */ new Set();
|
|
15994
16026
|
const nodeJsCompatWarningsMap = /* @__PURE__ */ new Map();
|
|
15995
|
-
let containerImageTagsSeen;
|
|
15996
|
-
let runningContainerIds;
|
|
16027
|
+
let containerImageTagsSeen = /* @__PURE__ */ new Set();
|
|
15997
16028
|
let restartingServer = false;
|
|
15998
16029
|
return [
|
|
15999
16030
|
{
|
|
@@ -16184,15 +16215,14 @@ if (import.meta.hot) {
|
|
|
16184
16215
|
viteDevServer.restart = async () => {
|
|
16185
16216
|
try {
|
|
16186
16217
|
restartingServer = true;
|
|
16187
|
-
|
|
16218
|
+
debuglog4("From server.restart(): Restarting server...");
|
|
16188
16219
|
await restartServer();
|
|
16189
|
-
|
|
16220
|
+
debuglog4("From server.restart(): Restarted server...");
|
|
16190
16221
|
} finally {
|
|
16191
16222
|
restartingServer = false;
|
|
16192
16223
|
}
|
|
16193
16224
|
};
|
|
16194
16225
|
assertIsNotPreview(resolvedPluginConfig);
|
|
16195
|
-
const configId = randomUUID2();
|
|
16196
16226
|
const inputInspectorPort = await getInputInspectorPortOption(
|
|
16197
16227
|
resolvedPluginConfig,
|
|
16198
16228
|
viteDevServer,
|
|
@@ -16205,12 +16235,9 @@ if (import.meta.hot) {
|
|
|
16205
16235
|
resolvedViteConfig,
|
|
16206
16236
|
changedFilePath
|
|
16207
16237
|
)) {
|
|
16208
|
-
|
|
16238
|
+
debuglog4("Config changed: " + changedFilePath);
|
|
16209
16239
|
viteDevServer.watcher.off("change", configChangedHandler);
|
|
16210
|
-
|
|
16211
|
-
configId,
|
|
16212
|
-
"Restarting dev server and aborting previous setup"
|
|
16213
|
-
);
|
|
16240
|
+
debuglog4("Restarting dev server and aborting previous setup");
|
|
16214
16241
|
await viteDevServer.restart();
|
|
16215
16242
|
}
|
|
16216
16243
|
};
|
|
@@ -16230,23 +16257,18 @@ if (import.meta.hot) {
|
|
|
16230
16257
|
containerBuildId
|
|
16231
16258
|
});
|
|
16232
16259
|
if (!miniflare) {
|
|
16233
|
-
|
|
16260
|
+
debuglog4("Creating new Miniflare instance");
|
|
16234
16261
|
miniflare = new Miniflare(miniflareDevOptions);
|
|
16235
16262
|
} else {
|
|
16236
|
-
|
|
16263
|
+
debuglog4("Updating the existing Miniflare instance");
|
|
16237
16264
|
await miniflare.setOptions(miniflareDevOptions);
|
|
16238
|
-
|
|
16265
|
+
debuglog4("Miniflare is ready");
|
|
16239
16266
|
}
|
|
16240
16267
|
let preMiddleware;
|
|
16241
16268
|
if (resolvedPluginConfig.type === "workers") {
|
|
16242
16269
|
assert12(entryWorkerConfig, `No entry Worker config`);
|
|
16243
|
-
|
|
16244
|
-
await initRunners(
|
|
16245
|
-
resolvedPluginConfig,
|
|
16246
|
-
viteDevServer,
|
|
16247
|
-
miniflare,
|
|
16248
|
-
configId
|
|
16249
|
-
);
|
|
16270
|
+
debuglog4("Initializing the Vite module runners");
|
|
16271
|
+
await initRunners(resolvedPluginConfig, viteDevServer, miniflare);
|
|
16250
16272
|
const entryWorkerName = entryWorkerConfig.name;
|
|
16251
16273
|
if (viteDevServer.httpServer) {
|
|
16252
16274
|
handleWebSocket(viteDevServer.httpServer, async () => {
|
|
@@ -16305,17 +16327,10 @@ if (import.meta.hot) {
|
|
|
16305
16327
|
)
|
|
16306
16328
|
)
|
|
16307
16329
|
);
|
|
16308
|
-
|
|
16309
|
-
if (containerImageTagsSeen
|
|
16310
|
-
|
|
16311
|
-
dockerPath,
|
|
16312
|
-
containerImageTagsSeen
|
|
16313
|
-
);
|
|
16330
|
+
process.on("exit", async () => {
|
|
16331
|
+
if (containerImageTagsSeen.size) {
|
|
16332
|
+
cleanupContainers(dockerPath, containerImageTagsSeen);
|
|
16314
16333
|
}
|
|
16315
|
-
}, 2e3);
|
|
16316
|
-
process.on("exit", () => {
|
|
16317
|
-
clearInterval(dockerPollIntervalId);
|
|
16318
|
-
removeContainersByIds(dockerPath, runningContainerIds);
|
|
16319
16334
|
});
|
|
16320
16335
|
}
|
|
16321
16336
|
}
|
|
@@ -16391,17 +16406,10 @@ if (import.meta.hot) {
|
|
|
16391
16406
|
vitePreviewServer.config.logger.info(
|
|
16392
16407
|
colors4.dim(colors4.yellow("\n\u26A1\uFE0F Containers successfully built.\n"))
|
|
16393
16408
|
);
|
|
16394
|
-
const dockerPollIntervalId = setInterval(async () => {
|
|
16395
|
-
if (containerImageTagsSeen?.size) {
|
|
16396
|
-
runningContainerIds = await getContainerIdsByImageTags(
|
|
16397
|
-
dockerPath,
|
|
16398
|
-
containerImageTagsSeen
|
|
16399
|
-
);
|
|
16400
|
-
}
|
|
16401
|
-
}, 2e3);
|
|
16402
16409
|
process.on("exit", () => {
|
|
16403
|
-
|
|
16404
|
-
|
|
16410
|
+
if (containerImageTagsSeen.size) {
|
|
16411
|
+
cleanupContainers(dockerPath, containerImageTagsSeen);
|
|
16412
|
+
}
|
|
16405
16413
|
});
|
|
16406
16414
|
}
|
|
16407
16415
|
handleWebSocket(vitePreviewServer.httpServer, () => {
|
|
@@ -16418,19 +16426,13 @@ if (import.meta.hot) {
|
|
|
16418
16426
|
async buildEnd() {
|
|
16419
16427
|
if (resolvedViteConfig.command === "serve" && containerImageTagsSeen?.size) {
|
|
16420
16428
|
const dockerPath = getDockerPath();
|
|
16421
|
-
|
|
16422
|
-
dockerPath,
|
|
16423
|
-
containerImageTagsSeen
|
|
16424
|
-
);
|
|
16425
|
-
await removeContainersByIds(dockerPath, runningContainerIds);
|
|
16426
|
-
containerImageTagsSeen.clear();
|
|
16427
|
-
runningContainerIds = [];
|
|
16429
|
+
cleanupContainers(dockerPath, containerImageTagsSeen);
|
|
16428
16430
|
}
|
|
16429
|
-
|
|
16431
|
+
debuglog4("buildEnd:", restartingServer ? "restarted" : "disposing");
|
|
16430
16432
|
if (!restartingServer) {
|
|
16431
|
-
|
|
16433
|
+
debuglog4("buildEnd: disposing Miniflare instance");
|
|
16432
16434
|
await miniflare?.dispose().catch((error) => {
|
|
16433
|
-
|
|
16435
|
+
debuglog4("buildEnd: failed to dispose Miniflare instance:", error);
|
|
16434
16436
|
});
|
|
16435
16437
|
miniflare = void 0;
|
|
16436
16438
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// src/runner-worker/index.ts
|
|
2
2
|
import {
|
|
3
|
-
DurableObject,
|
|
3
|
+
DurableObject as DurableObject2,
|
|
4
4
|
WorkerEntrypoint,
|
|
5
5
|
WorkflowEntrypoint
|
|
6
6
|
} from "cloudflare:workers";
|
|
@@ -22,14 +22,22 @@ var additionalModuleGlobalRE = new RegExp(
|
|
|
22
22
|
ADDITIONAL_MODULE_PATTERN,
|
|
23
23
|
"g"
|
|
24
24
|
);
|
|
25
|
-
var
|
|
25
|
+
var WORKER_ENTRY_PATH_HEADER = "__VITE_WORKER_ENTRY_PATH__";
|
|
26
26
|
|
|
27
27
|
// src/runner-worker/env.ts
|
|
28
28
|
function stripInternalEnv(internalEnv) {
|
|
29
|
-
const {
|
|
29
|
+
const {
|
|
30
|
+
__VITE_RUNNER_OBJECT__: __VITE_RUNNER_OBJECT__2,
|
|
31
|
+
__VITE_INVOKE_MODULE__,
|
|
32
|
+
__VITE_UNSAFE_EVAL__,
|
|
33
|
+
...userEnv
|
|
34
|
+
} = internalEnv;
|
|
30
35
|
return userEnv;
|
|
31
36
|
}
|
|
32
37
|
|
|
38
|
+
// src/runner-worker/module-runner.ts
|
|
39
|
+
import { DurableObject } from "cloudflare:workers";
|
|
40
|
+
|
|
33
41
|
// ../../node_modules/.pnpm/vite@7.0.0_@types+node@20.19.9_jiti@2.4.2_lightningcss@1.29.2/node_modules/vite/dist/node/module-runner.js
|
|
34
42
|
var VALID_ID_PREFIX = "/@id/";
|
|
35
43
|
var NULL_BYTE_PLACEHOLDER = "__x00__";
|
|
@@ -686,45 +694,6 @@ var normalizeModuleRunnerTransport = (transport) => {
|
|
|
686
694
|
}
|
|
687
695
|
};
|
|
688
696
|
};
|
|
689
|
-
var createWebSocketModuleRunnerTransport = (options) => {
|
|
690
|
-
let pingInterval = options.pingInterval ?? 3e4, ws, pingIntervalId;
|
|
691
|
-
return {
|
|
692
|
-
async connect({ onMessage, onDisconnection }) {
|
|
693
|
-
let socket = options.createConnection();
|
|
694
|
-
socket.addEventListener("message", async ({ data }) => {
|
|
695
|
-
onMessage(JSON.parse(data));
|
|
696
|
-
});
|
|
697
|
-
let isOpened = socket.readyState === socket.OPEN;
|
|
698
|
-
isOpened || await new Promise((resolve$1, reject) => {
|
|
699
|
-
socket.addEventListener("open", () => {
|
|
700
|
-
isOpened = true, resolve$1();
|
|
701
|
-
}, { once: true }), socket.addEventListener("close", async () => {
|
|
702
|
-
if (!isOpened) {
|
|
703
|
-
reject(/* @__PURE__ */ Error("WebSocket closed without opened."));
|
|
704
|
-
return;
|
|
705
|
-
}
|
|
706
|
-
onMessage({
|
|
707
|
-
type: "custom",
|
|
708
|
-
event: "vite:ws:disconnect",
|
|
709
|
-
data: { webSocket: socket }
|
|
710
|
-
}), onDisconnection();
|
|
711
|
-
});
|
|
712
|
-
}), onMessage({
|
|
713
|
-
type: "custom",
|
|
714
|
-
event: "vite:ws:connect",
|
|
715
|
-
data: { webSocket: socket }
|
|
716
|
-
}), ws = socket, pingIntervalId = setInterval(() => {
|
|
717
|
-
socket.readyState === socket.OPEN && socket.send(JSON.stringify({ type: "ping" }));
|
|
718
|
-
}, pingInterval);
|
|
719
|
-
},
|
|
720
|
-
disconnect() {
|
|
721
|
-
clearInterval(pingIntervalId), ws?.close();
|
|
722
|
-
},
|
|
723
|
-
send(data) {
|
|
724
|
-
ws.send(JSON.stringify(data));
|
|
725
|
-
}
|
|
726
|
-
};
|
|
727
|
-
};
|
|
728
697
|
var ssrModuleExportsKey = "__vite_ssr_exports__";
|
|
729
698
|
var ssrImportKey = "__vite_ssr_import__";
|
|
730
699
|
var ssrDynamicImportKey = "__vite_ssr_dynamic_import__";
|
|
@@ -1223,26 +1192,108 @@ function exportAll(exports, sourceModule) {
|
|
|
1223
1192
|
}
|
|
1224
1193
|
|
|
1225
1194
|
// src/runner-worker/module-runner.ts
|
|
1195
|
+
var CustomModuleRunner = class extends ModuleRunner {
|
|
1196
|
+
#env;
|
|
1197
|
+
constructor(options, evaluator, env) {
|
|
1198
|
+
super(options, evaluator);
|
|
1199
|
+
this.#env = env;
|
|
1200
|
+
}
|
|
1201
|
+
async cachedModule(url, importer) {
|
|
1202
|
+
const stub = this.#env.__VITE_RUNNER_OBJECT__.get("singleton");
|
|
1203
|
+
const moduleId = await stub.getFetchedModuleId(url, importer);
|
|
1204
|
+
const module = this.evaluatedModules.getModuleById(moduleId);
|
|
1205
|
+
if (!module) {
|
|
1206
|
+
throw new Error(`Module "${moduleId}" is undefined`);
|
|
1207
|
+
}
|
|
1208
|
+
return module;
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1226
1211
|
var moduleRunner;
|
|
1227
|
-
var
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1212
|
+
var __VITE_RUNNER_OBJECT__ = class extends DurableObject {
|
|
1213
|
+
/** WebSocket connection to the Vite dev server */
|
|
1214
|
+
#webSocket;
|
|
1215
|
+
#concurrentModuleNodePromises = /* @__PURE__ */ new Map();
|
|
1216
|
+
/**
|
|
1217
|
+
* Handles fetch requests to initialize the module runner.
|
|
1218
|
+
* Creates a WebSocket pair for communication with the Vite dev server and initializes the ModuleRunner.
|
|
1219
|
+
* @param request - The incoming fetch request
|
|
1220
|
+
* @returns Response with WebSocket
|
|
1221
|
+
* @throws Error if the path is invalid or the module runner is already initialized
|
|
1222
|
+
*/
|
|
1223
|
+
async fetch(request) {
|
|
1224
|
+
const { pathname } = new URL(request.url);
|
|
1225
|
+
if (pathname !== INIT_PATH) {
|
|
1226
|
+
throw new Error(
|
|
1227
|
+
`__VITE_RUNNER_OBJECT__ received invalid pathname: ${pathname}`
|
|
1228
|
+
);
|
|
1229
|
+
}
|
|
1230
|
+
if (moduleRunner) {
|
|
1231
|
+
throw new Error(`Module runner already initialized`);
|
|
1232
|
+
}
|
|
1233
|
+
const { 0: client, 1: server } = new WebSocketPair();
|
|
1234
|
+
server.accept();
|
|
1235
|
+
this.#webSocket = server;
|
|
1236
|
+
moduleRunner = await createModuleRunner(this.env, this.#webSocket);
|
|
1237
|
+
return new Response(null, { status: 101, webSocket: client });
|
|
1233
1238
|
}
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1239
|
+
/**
|
|
1240
|
+
* Sends data to the Vite dev server via the WebSocket.
|
|
1241
|
+
* @param data - The data to send as a string
|
|
1242
|
+
* @throws Error if the WebSocket is not initialized
|
|
1243
|
+
*/
|
|
1244
|
+
send(data) {
|
|
1245
|
+
if (!this.#webSocket) {
|
|
1246
|
+
throw new Error(`Module runner WebSocket not initialized`);
|
|
1238
1247
|
}
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1248
|
+
this.#webSocket.send(data);
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Based on the implementation of `cachedModule` from Vite's `ModuleRunner`.
|
|
1252
|
+
* Running this in the DO enables us to share promises across invocations.
|
|
1253
|
+
* @param url - The module URL
|
|
1254
|
+
* @param importer - The module's importer
|
|
1255
|
+
* @returns The ID of the fetched module
|
|
1256
|
+
*/
|
|
1257
|
+
async getFetchedModuleId(url, importer) {
|
|
1258
|
+
if (!moduleRunner) {
|
|
1259
|
+
throw new Error(`Module runner not initialized`);
|
|
1260
|
+
}
|
|
1261
|
+
let cached = this.#concurrentModuleNodePromises.get(url);
|
|
1262
|
+
if (!cached) {
|
|
1263
|
+
const cachedModule = moduleRunner.evaluatedModules.getModuleByUrl(url);
|
|
1264
|
+
cached = moduleRunner.getModuleInformation(url, importer, cachedModule).finally(() => {
|
|
1265
|
+
this.#concurrentModuleNodePromises.delete(url);
|
|
1266
|
+
});
|
|
1267
|
+
this.#concurrentModuleNodePromises.set(url, cached);
|
|
1268
|
+
} else {
|
|
1269
|
+
moduleRunner.debug?.("[module runner] using cached module info for", url);
|
|
1270
|
+
}
|
|
1271
|
+
const module = await cached;
|
|
1272
|
+
return module.id;
|
|
1273
|
+
}
|
|
1274
|
+
};
|
|
1275
|
+
async function createModuleRunner(env, webSocket) {
|
|
1276
|
+
return new CustomModuleRunner(
|
|
1242
1277
|
{
|
|
1243
1278
|
sourcemapInterceptor: "prepareStackTrace",
|
|
1244
1279
|
transport: {
|
|
1245
|
-
|
|
1280
|
+
connect({ onMessage }) {
|
|
1281
|
+
webSocket.addEventListener("message", async ({ data }) => {
|
|
1282
|
+
onMessage(JSON.parse(data.toString()));
|
|
1283
|
+
});
|
|
1284
|
+
onMessage({
|
|
1285
|
+
type: "custom",
|
|
1286
|
+
event: "vite:ws:connect",
|
|
1287
|
+
data: { webSocket }
|
|
1288
|
+
});
|
|
1289
|
+
},
|
|
1290
|
+
disconnect() {
|
|
1291
|
+
webSocket.close();
|
|
1292
|
+
},
|
|
1293
|
+
send(data) {
|
|
1294
|
+
const stub = env.__VITE_RUNNER_OBJECT__.get("singleton");
|
|
1295
|
+
stub.send(JSON.stringify(data));
|
|
1296
|
+
},
|
|
1246
1297
|
async invoke(data) {
|
|
1247
1298
|
const response = await env.__VITE_INVOKE_MODULE__.fetch(
|
|
1248
1299
|
new Request(UNKNOWN_HOST, {
|
|
@@ -1250,9 +1301,6 @@ async function createModuleRunner(env, webSocket, configId) {
|
|
|
1250
1301
|
body: JSON.stringify(data)
|
|
1251
1302
|
})
|
|
1252
1303
|
);
|
|
1253
|
-
if (!response.ok) {
|
|
1254
|
-
throw new Error(await response.text());
|
|
1255
|
-
}
|
|
1256
1304
|
const result = await response.json();
|
|
1257
1305
|
return result;
|
|
1258
1306
|
}
|
|
@@ -1261,19 +1309,17 @@ async function createModuleRunner(env, webSocket, configId) {
|
|
|
1261
1309
|
},
|
|
1262
1310
|
{
|
|
1263
1311
|
async runInlinedModule(context, transformed, module) {
|
|
1264
|
-
const
|
|
1265
|
-
","
|
|
1266
|
-
)})=>{{`;
|
|
1267
|
-
const code = `${codeDefinition}${transformed}
|
|
1268
|
-
}}`;
|
|
1312
|
+
const code = `"use strict";async (${Object.keys(context).join(",")})=>{${transformed}}`;
|
|
1269
1313
|
try {
|
|
1270
1314
|
const fn = env.__VITE_UNSAFE_EVAL__.eval(code, module.id);
|
|
1271
1315
|
await fn(...Object.values(context));
|
|
1272
|
-
Object.
|
|
1273
|
-
} catch (
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1316
|
+
Object.seal(context[ssrModuleExportsKey]);
|
|
1317
|
+
} catch (error) {
|
|
1318
|
+
if (error instanceof Error) {
|
|
1319
|
+
error.message = `Error running module "${module.id}".
|
|
1320
|
+
${error.message}.`;
|
|
1321
|
+
}
|
|
1322
|
+
throw error;
|
|
1277
1323
|
}
|
|
1278
1324
|
},
|
|
1279
1325
|
async runExternalModule(filepath) {
|
|
@@ -1286,19 +1332,24 @@ async function createModuleRunner(env, webSocket, configId) {
|
|
|
1286
1332
|
)
|
|
1287
1333
|
});
|
|
1288
1334
|
}
|
|
1289
|
-
filepath = filepath.replace(/^file:\/\//, "");
|
|
1290
1335
|
return import(filepath);
|
|
1291
1336
|
}
|
|
1292
|
-
}
|
|
1337
|
+
},
|
|
1338
|
+
env
|
|
1293
1339
|
);
|
|
1294
1340
|
}
|
|
1295
|
-
async function getWorkerEntryExport(
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1341
|
+
async function getWorkerEntryExport(workerEntryPath2, exportName) {
|
|
1342
|
+
if (!moduleRunner) {
|
|
1343
|
+
throw new Error(`Module runner not initialized`);
|
|
1344
|
+
}
|
|
1345
|
+
const module = await moduleRunner.import(workerEntryPath2);
|
|
1346
|
+
const exportValue = typeof module === "object" && module !== null && exportName in module && module[exportName];
|
|
1347
|
+
if (!exportValue) {
|
|
1348
|
+
throw new Error(
|
|
1349
|
+
`"${workerEntryPath2}" does not define a "${exportName}" export.`
|
|
1350
|
+
);
|
|
1300
1351
|
}
|
|
1301
|
-
return
|
|
1352
|
+
return exportValue;
|
|
1302
1353
|
}
|
|
1303
1354
|
|
|
1304
1355
|
// src/runner-worker/index.ts
|
|
@@ -1319,7 +1370,20 @@ var DURABLE_OBJECT_KEYS = [
|
|
|
1319
1370
|
"webSocketMessage"
|
|
1320
1371
|
];
|
|
1321
1372
|
var WORKFLOW_ENTRYPOINT_KEYS = ["run"];
|
|
1322
|
-
var
|
|
1373
|
+
var workerEntryPath = "";
|
|
1374
|
+
function getRpcPropertyCallableThenable(key, property) {
|
|
1375
|
+
const fn = async function(...args) {
|
|
1376
|
+
const maybeFn = await property;
|
|
1377
|
+
if (typeof maybeFn !== "function") {
|
|
1378
|
+
throw new TypeError(`"${key}" is not a function.`);
|
|
1379
|
+
}
|
|
1380
|
+
return maybeFn(...args);
|
|
1381
|
+
};
|
|
1382
|
+
fn.then = (onFulfilled, onRejected) => property.then(onFulfilled, onRejected);
|
|
1383
|
+
fn.catch = (onRejected) => property.catch(onRejected);
|
|
1384
|
+
fn.finally = (onFinally) => property.finally(onFinally);
|
|
1385
|
+
return fn;
|
|
1386
|
+
}
|
|
1323
1387
|
function getRpcProperty(ctor, instance, key) {
|
|
1324
1388
|
const prototypeHasKey = Reflect.has(ctor.prototype, key);
|
|
1325
1389
|
if (!prototypeHasKey) {
|
|
@@ -1327,37 +1391,24 @@ function getRpcProperty(ctor, instance, key) {
|
|
|
1327
1391
|
if (instanceHasKey) {
|
|
1328
1392
|
throw new TypeError(
|
|
1329
1393
|
[
|
|
1330
|
-
`The RPC receiver's prototype does not implement
|
|
1394
|
+
`The RPC receiver's prototype does not implement "${key}", but the receiver instance does.`,
|
|
1331
1395
|
"Only properties and methods defined on the prototype can be accessed over RPC.",
|
|
1332
1396
|
`Ensure properties are declared as \`get ${key}() { ... }\` instead of \`${key} = ...\`,`,
|
|
1333
1397
|
`and methods are declared as \`${key}() { ... }\` instead of \`${key} = () => { ... }\`.`
|
|
1334
1398
|
].join("\n")
|
|
1335
1399
|
);
|
|
1336
1400
|
}
|
|
1337
|
-
throw new TypeError(`The RPC receiver does not implement
|
|
1401
|
+
throw new TypeError(`The RPC receiver does not implement "${key}".`);
|
|
1338
1402
|
}
|
|
1339
1403
|
return Reflect.get(ctor.prototype, key, instance);
|
|
1340
1404
|
}
|
|
1341
|
-
function
|
|
1342
|
-
const fn = async function(...args) {
|
|
1343
|
-
const maybeFn = await property;
|
|
1344
|
-
if (typeof maybeFn !== "function") {
|
|
1345
|
-
throw new TypeError(`'${key}' is not a function.`);
|
|
1346
|
-
}
|
|
1347
|
-
return maybeFn(...args);
|
|
1348
|
-
};
|
|
1349
|
-
fn.then = (onFulfilled, onRejected) => property.then(onFulfilled, onRejected);
|
|
1350
|
-
fn.catch = (onRejected) => property.catch(onRejected);
|
|
1351
|
-
fn.finally = (onFinally) => property.finally(onFinally);
|
|
1352
|
-
return fn;
|
|
1353
|
-
}
|
|
1354
|
-
async function getWorkerEntrypointRpcProperty(entrypoint, key) {
|
|
1405
|
+
async function getWorkerEntrypointRpcProperty(exportName, key) {
|
|
1355
1406
|
const ctor = await getWorkerEntryExport(
|
|
1356
|
-
|
|
1357
|
-
|
|
1407
|
+
workerEntryPath,
|
|
1408
|
+
exportName
|
|
1358
1409
|
);
|
|
1359
1410
|
const userEnv = stripInternalEnv(this.env);
|
|
1360
|
-
const expectedWorkerEntrypointMessage = `Expected ${
|
|
1411
|
+
const expectedWorkerEntrypointMessage = `Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`WorkerEntrypoint\` for RPC.`;
|
|
1361
1412
|
if (typeof ctor !== "function") {
|
|
1362
1413
|
throw new TypeError(expectedWorkerEntrypointMessage);
|
|
1363
1414
|
}
|
|
@@ -1371,10 +1422,8 @@ async function getWorkerEntrypointRpcProperty(entrypoint, key) {
|
|
|
1371
1422
|
}
|
|
1372
1423
|
return value;
|
|
1373
1424
|
}
|
|
1374
|
-
function createWorkerEntrypointWrapper(
|
|
1425
|
+
function createWorkerEntrypointWrapper(exportName) {
|
|
1375
1426
|
class Wrapper extends WorkerEntrypoint {
|
|
1376
|
-
/** A unique identifier used for debugging errors when config updates. */
|
|
1377
|
-
configId;
|
|
1378
1427
|
constructor(ctx, env) {
|
|
1379
1428
|
super(ctx, env);
|
|
1380
1429
|
return new Proxy(this, {
|
|
@@ -1383,12 +1432,14 @@ function createWorkerEntrypointWrapper(entrypoint) {
|
|
|
1383
1432
|
if (value !== void 0) {
|
|
1384
1433
|
return value;
|
|
1385
1434
|
}
|
|
1386
|
-
if (typeof key === "symbol" || IGNORED_KEYS.includes(key) ||
|
|
1435
|
+
if (typeof key === "symbol" || IGNORED_KEYS.includes(key) || // The class methods are accessed to determine the type of the export.
|
|
1436
|
+
// We should therefore avoid proxying `DurableObject` methods on the `WorkerEntrypoint` class.
|
|
1437
|
+
DURABLE_OBJECT_KEYS.includes(key)) {
|
|
1387
1438
|
return;
|
|
1388
1439
|
}
|
|
1389
1440
|
const property = getWorkerEntrypointRpcProperty.call(
|
|
1390
1441
|
receiver,
|
|
1391
|
-
|
|
1442
|
+
exportName,
|
|
1392
1443
|
key
|
|
1393
1444
|
);
|
|
1394
1445
|
return getRpcPropertyCallableThenable(key, property);
|
|
@@ -1401,58 +1452,51 @@ function createWorkerEntrypointWrapper(entrypoint) {
|
|
|
1401
1452
|
if (key === "fetch") {
|
|
1402
1453
|
const request = arg;
|
|
1403
1454
|
const url = new URL(request.url);
|
|
1404
|
-
let webSocket;
|
|
1405
1455
|
if (url.pathname === INIT_PATH) {
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
this.env,
|
|
1413
|
-
server,
|
|
1414
|
-
viteDevMetadata.configId
|
|
1415
|
-
);
|
|
1416
|
-
} catch (e) {
|
|
1417
|
-
return new Response(
|
|
1418
|
-
e instanceof Error ? e.message : JSON.stringify(e),
|
|
1419
|
-
{ status: 500 }
|
|
1456
|
+
const workerEntryPathHeader = request.headers.get(
|
|
1457
|
+
WORKER_ENTRY_PATH_HEADER
|
|
1458
|
+
);
|
|
1459
|
+
if (!workerEntryPathHeader) {
|
|
1460
|
+
throw new Error(
|
|
1461
|
+
`Unexpected error: "${WORKER_ENTRY_PATH_HEADER}" header not set.`
|
|
1420
1462
|
);
|
|
1421
1463
|
}
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
});
|
|
1464
|
+
workerEntryPath = workerEntryPathHeader;
|
|
1465
|
+
const stub = this.env.__VITE_RUNNER_OBJECT__.get("singleton");
|
|
1466
|
+
return stub.fetch(request);
|
|
1426
1467
|
}
|
|
1427
1468
|
}
|
|
1428
|
-
const
|
|
1469
|
+
const exportValue = await getWorkerEntryExport(
|
|
1470
|
+
workerEntryPath,
|
|
1471
|
+
exportName
|
|
1472
|
+
);
|
|
1429
1473
|
const userEnv = stripInternalEnv(this.env);
|
|
1430
|
-
if (typeof
|
|
1431
|
-
const maybeFn =
|
|
1474
|
+
if (typeof exportValue === "object" && exportValue !== null) {
|
|
1475
|
+
const maybeFn = exportValue[key];
|
|
1432
1476
|
if (typeof maybeFn !== "function") {
|
|
1433
1477
|
throw new TypeError(
|
|
1434
|
-
`Expected ${
|
|
1478
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to define a \`${key}()\` function.`
|
|
1435
1479
|
);
|
|
1436
1480
|
}
|
|
1437
|
-
return maybeFn.call(
|
|
1438
|
-
} else if (typeof
|
|
1439
|
-
const ctor =
|
|
1481
|
+
return maybeFn.call(exportValue, arg, userEnv, this.ctx);
|
|
1482
|
+
} else if (typeof exportValue === "function") {
|
|
1483
|
+
const ctor = exportValue;
|
|
1440
1484
|
const instance = new ctor(this.ctx, userEnv);
|
|
1441
1485
|
if (!(instance instanceof WorkerEntrypoint)) {
|
|
1442
1486
|
throw new TypeError(
|
|
1443
|
-
`Expected ${
|
|
1487
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`WorkerEntrypoint\`.`
|
|
1444
1488
|
);
|
|
1445
1489
|
}
|
|
1446
1490
|
const maybeFn = instance[key];
|
|
1447
1491
|
if (typeof maybeFn !== "function") {
|
|
1448
1492
|
throw new TypeError(
|
|
1449
|
-
`Expected ${
|
|
1493
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to define a \`${key}()\` method.`
|
|
1450
1494
|
);
|
|
1451
1495
|
}
|
|
1452
1496
|
return maybeFn.call(instance, arg);
|
|
1453
1497
|
} else {
|
|
1454
1498
|
return new TypeError(
|
|
1455
|
-
`Expected ${
|
|
1499
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to be an object or a class.`
|
|
1456
1500
|
);
|
|
1457
1501
|
}
|
|
1458
1502
|
};
|
|
@@ -1461,11 +1505,11 @@ function createWorkerEntrypointWrapper(entrypoint) {
|
|
|
1461
1505
|
}
|
|
1462
1506
|
var kInstance = Symbol("kInstance");
|
|
1463
1507
|
var kEnsureInstance = Symbol("kEnsureInstance");
|
|
1464
|
-
async function getDurableObjectRpcProperty(
|
|
1508
|
+
async function getDurableObjectRpcProperty(exportName, key) {
|
|
1465
1509
|
const { ctor, instance } = await this[kEnsureInstance]();
|
|
1466
|
-
if (!(instance instanceof
|
|
1510
|
+
if (!(instance instanceof DurableObject2)) {
|
|
1467
1511
|
throw new TypeError(
|
|
1468
|
-
`Expected ${
|
|
1512
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`DurableObject\` for RPC.`
|
|
1469
1513
|
);
|
|
1470
1514
|
}
|
|
1471
1515
|
const value = getRpcProperty(ctor, instance, key);
|
|
@@ -1474,8 +1518,8 @@ async function getDurableObjectRpcProperty(className, key) {
|
|
|
1474
1518
|
}
|
|
1475
1519
|
return value;
|
|
1476
1520
|
}
|
|
1477
|
-
function createDurableObjectWrapper(
|
|
1478
|
-
class Wrapper extends
|
|
1521
|
+
function createDurableObjectWrapper(exportName) {
|
|
1522
|
+
class Wrapper extends DurableObject2 {
|
|
1479
1523
|
[kInstance];
|
|
1480
1524
|
constructor(ctx, env) {
|
|
1481
1525
|
super(ctx, env);
|
|
@@ -1485,12 +1529,14 @@ function createDurableObjectWrapper(className) {
|
|
|
1485
1529
|
if (value !== void 0) {
|
|
1486
1530
|
return value;
|
|
1487
1531
|
}
|
|
1488
|
-
if (typeof key === "symbol" || IGNORED_KEYS.includes(key) ||
|
|
1532
|
+
if (typeof key === "symbol" || IGNORED_KEYS.includes(key) || // The class methods are accessed to determine the type of the export.
|
|
1533
|
+
// We should therefore avoid proxying `WorkerEntrypoint` methods on the `DurableObject` class.
|
|
1534
|
+
WORKER_ENTRYPOINT_KEYS.includes(key)) {
|
|
1489
1535
|
return;
|
|
1490
1536
|
}
|
|
1491
1537
|
const property = getDurableObjectRpcProperty.call(
|
|
1492
1538
|
receiver,
|
|
1493
|
-
|
|
1539
|
+
exportName,
|
|
1494
1540
|
key
|
|
1495
1541
|
);
|
|
1496
1542
|
return getRpcPropertyCallableThenable(key, property);
|
|
@@ -1499,12 +1545,12 @@ function createDurableObjectWrapper(className) {
|
|
|
1499
1545
|
}
|
|
1500
1546
|
async [kEnsureInstance]() {
|
|
1501
1547
|
const ctor = await getWorkerEntryExport(
|
|
1502
|
-
|
|
1503
|
-
|
|
1548
|
+
workerEntryPath,
|
|
1549
|
+
exportName
|
|
1504
1550
|
);
|
|
1505
1551
|
if (typeof ctor !== "function") {
|
|
1506
1552
|
throw new TypeError(
|
|
1507
|
-
|
|
1553
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`DurableObject\`.`
|
|
1508
1554
|
);
|
|
1509
1555
|
}
|
|
1510
1556
|
if (!this[kInstance] || this[kInstance].ctor !== ctor) {
|
|
@@ -1523,7 +1569,7 @@ function createDurableObjectWrapper(className) {
|
|
|
1523
1569
|
const maybeFn = instance[key];
|
|
1524
1570
|
if (typeof maybeFn !== "function") {
|
|
1525
1571
|
throw new TypeError(
|
|
1526
|
-
`Expected ${
|
|
1572
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to define a \`${key}()\` function.`
|
|
1527
1573
|
);
|
|
1528
1574
|
}
|
|
1529
1575
|
return maybeFn.apply(instance, args);
|
|
@@ -1531,26 +1577,26 @@ function createDurableObjectWrapper(className) {
|
|
|
1531
1577
|
}
|
|
1532
1578
|
return Wrapper;
|
|
1533
1579
|
}
|
|
1534
|
-
function createWorkflowEntrypointWrapper(
|
|
1580
|
+
function createWorkflowEntrypointWrapper(exportName) {
|
|
1535
1581
|
class Wrapper extends WorkflowEntrypoint {
|
|
1536
1582
|
}
|
|
1537
1583
|
for (const key of WORKFLOW_ENTRYPOINT_KEYS) {
|
|
1538
1584
|
Wrapper.prototype[key] = async function(...args) {
|
|
1539
1585
|
const ctor = await getWorkerEntryExport(
|
|
1540
|
-
|
|
1541
|
-
|
|
1586
|
+
workerEntryPath,
|
|
1587
|
+
exportName
|
|
1542
1588
|
);
|
|
1543
1589
|
const userEnv = stripInternalEnv(this.env);
|
|
1544
1590
|
const instance = new ctor(this.ctx, userEnv);
|
|
1545
1591
|
if (!(instance instanceof WorkflowEntrypoint)) {
|
|
1546
1592
|
throw new TypeError(
|
|
1547
|
-
`Expected ${
|
|
1593
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to be a subclass of \`WorkflowEntrypoint\`.`
|
|
1548
1594
|
);
|
|
1549
1595
|
}
|
|
1550
1596
|
const maybeFn = instance[key];
|
|
1551
1597
|
if (typeof maybeFn !== "function") {
|
|
1552
1598
|
throw new TypeError(
|
|
1553
|
-
`Expected ${
|
|
1599
|
+
`Expected "${exportName}" export of "${workerEntryPath}" to define a \`${key}()\` function.`
|
|
1554
1600
|
);
|
|
1555
1601
|
}
|
|
1556
1602
|
return maybeFn.apply(instance, args);
|
|
@@ -1558,30 +1604,8 @@ function createWorkflowEntrypointWrapper(className) {
|
|
|
1558
1604
|
}
|
|
1559
1605
|
return Wrapper;
|
|
1560
1606
|
}
|
|
1561
|
-
function getViteDevMetadata(request) {
|
|
1562
|
-
const viteDevMetadataHeader = request.headers.get(VITE_DEV_METADATA_HEADER);
|
|
1563
|
-
if (viteDevMetadataHeader === null) {
|
|
1564
|
-
throw new Error(
|
|
1565
|
-
"Unexpected internal error, vite dev metadata header not set"
|
|
1566
|
-
);
|
|
1567
|
-
}
|
|
1568
|
-
let parsedViteDevMetadataHeader;
|
|
1569
|
-
try {
|
|
1570
|
-
parsedViteDevMetadataHeader = JSON.parse(viteDevMetadataHeader);
|
|
1571
|
-
} catch {
|
|
1572
|
-
throw new Error(
|
|
1573
|
-
`Unexpected internal error, vite dev metadata header JSON parsing failed, value = ${viteDevMetadataHeader}`
|
|
1574
|
-
);
|
|
1575
|
-
}
|
|
1576
|
-
const { entryPath: entryPath2, configId } = parsedViteDevMetadataHeader;
|
|
1577
|
-
if (entryPath2 === void 0) {
|
|
1578
|
-
throw new Error(
|
|
1579
|
-
"Unexpected internal error, vite dev metadata header doesn't contain an entryPath value"
|
|
1580
|
-
);
|
|
1581
|
-
}
|
|
1582
|
-
return { entryPath: entryPath2, configId };
|
|
1583
|
-
}
|
|
1584
1607
|
export {
|
|
1608
|
+
__VITE_RUNNER_OBJECT__,
|
|
1585
1609
|
createDurableObjectWrapper,
|
|
1586
1610
|
createWorkerEntrypointWrapper,
|
|
1587
1611
|
createWorkflowEntrypointWrapper
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// src/vite-proxy-worker/index.ts
|
|
2
|
+
import { WorkerEntrypoint } from "cloudflare:workers";
|
|
3
|
+
var ViteProxyWorker = class extends WorkerEntrypoint {
|
|
4
|
+
constructor(ctx, env) {
|
|
5
|
+
super(ctx, env);
|
|
6
|
+
return new Proxy(this, {
|
|
7
|
+
get(target, prop) {
|
|
8
|
+
if (Reflect.has(target, prop)) {
|
|
9
|
+
return Reflect.get(target, prop);
|
|
10
|
+
}
|
|
11
|
+
return Reflect.get(target.env.ENTRY_USER_WORKER, prop);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async fetch(request) {
|
|
16
|
+
return this.env.__VITE_MIDDLEWARE__.fetch(request);
|
|
17
|
+
}
|
|
18
|
+
tail(events) {
|
|
19
|
+
return this.env.ENTRY_USER_WORKER.tail(
|
|
20
|
+
JSON.parse(JSON.stringify(events, tailEventsReplacer), tailEventsReviver)
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var serializedDate = "___serialized_date___";
|
|
25
|
+
function tailEventsReplacer(_, value) {
|
|
26
|
+
if (value instanceof Date) {
|
|
27
|
+
return { [serializedDate]: value.toISOString() };
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
function tailEventsReviver(_, value) {
|
|
32
|
+
if (value && typeof value === "object" && serializedDate in value) {
|
|
33
|
+
return new Date(value[serializedDate]);
|
|
34
|
+
}
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
ViteProxyWorker as default
|
|
39
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/vite-plugin",
|
|
3
|
-
"version": "1.11.
|
|
3
|
+
"version": "1.11.2",
|
|
4
4
|
"description": "Cloudflare plugin for Vite",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -40,12 +40,12 @@
|
|
|
40
40
|
"tinyglobby": "^0.2.12",
|
|
41
41
|
"unenv": "2.0.0-rc.19",
|
|
42
42
|
"ws": "8.18.0",
|
|
43
|
-
"@cloudflare/unenv-preset": "2.
|
|
44
|
-
"miniflare": "4.
|
|
45
|
-
"wrangler": "4.
|
|
43
|
+
"@cloudflare/unenv-preset": "2.6.0",
|
|
44
|
+
"miniflare": "4.20250803.0",
|
|
45
|
+
"wrangler": "4.28.1"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@cloudflare/workers-types": "^4.
|
|
48
|
+
"@cloudflare/workers-types": "^4.20250803.0",
|
|
49
49
|
"@types/node": "^22.10.1",
|
|
50
50
|
"@types/ws": "^8.5.13",
|
|
51
51
|
"magic-string": "^0.30.12",
|
|
@@ -54,14 +54,14 @@
|
|
|
54
54
|
"typescript": "^5.8.3",
|
|
55
55
|
"vite": "7.0.0",
|
|
56
56
|
"vitest": "~3.2.0",
|
|
57
|
-
"@cloudflare/containers-shared": "0.2.8",
|
|
58
57
|
"@cloudflare/mock-npm-registry": "0.0.0",
|
|
58
|
+
"@cloudflare/containers-shared": "0.2.8",
|
|
59
59
|
"@cloudflare/workers-shared": "0.18.5",
|
|
60
60
|
"@cloudflare/workers-tsconfig": "0.0.0"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
63
|
"vite": "^6.1.0 || ^7.0.0",
|
|
64
|
-
"wrangler": "^4.
|
|
64
|
+
"wrangler": "^4.28.1"
|
|
65
65
|
},
|
|
66
66
|
"publishConfig": {
|
|
67
67
|
"access": "public"
|