rspack-plugin-cloudflare 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +128 -2
- package/dist/assets.d.ts +31 -0
- package/dist/assets.js +112 -0
- package/dist/assets.js.map +1 -0
- package/dist/build.d.ts +216 -0
- package/dist/build.js +140 -0
- package/dist/build.js.map +1 -0
- package/dist/config.d.ts +19 -0
- package/dist/config.js +103 -0
- package/dist/config.js.map +1 -0
- package/dist/containers.d.ts +30 -0
- package/dist/containers.js +167 -0
- package/dist/containers.js.map +1 -0
- package/dist/dev-entry.d.ts +11 -0
- package/dist/dev-entry.js +44 -0
- package/dist/dev-entry.js.map +1 -0
- package/dist/dev-worker.d.ts +73 -0
- package/dist/dev-worker.js +247 -0
- package/dist/dev-worker.js.map +1 -0
- package/dist/dev.d.ts +28 -0
- package/dist/dev.js +422 -0
- package/dist/dev.js.map +1 -0
- package/dist/http.d.ts +23 -0
- package/dist/http.js +89 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +139 -0
- package/dist/index.js.map +1 -0
- package/dist/inspector.d.ts +5 -0
- package/dist/inspector.js +41 -0
- package/dist/inspector.js.map +1 -0
- package/dist/miniflare.d.ts +1 -0
- package/dist/miniflare.js +6 -0
- package/dist/miniflare.js.map +1 -0
- package/dist/module-rules.d.ts +4 -0
- package/dist/module-rules.js +29 -0
- package/dist/module-rules.js.map +1 -0
- package/dist/nodejs-compat.d.ts +14 -0
- package/dist/nodejs-compat.js +46 -0
- package/dist/nodejs-compat.js.map +1 -0
- package/dist/options.d.ts +45 -0
- package/dist/options.js +2 -0
- package/dist/options.js.map +1 -0
- package/dist/routing.d.ts +12 -0
- package/dist/routing.js +40 -0
- package/dist/routing.js.map +1 -0
- package/dist/tunnel.d.ts +15 -0
- package/dist/tunnel.js +94 -0
- package/dist/tunnel.js.map +1 -0
- package/dist/worker-config.d.ts +42 -0
- package/dist/worker-config.js +194 -0
- package/dist/worker-config.js.map +1 -0
- package/package.json +91 -5
- package/runtime/dev-runtime.js +265 -0
- package/src/assets.ts +148 -0
- package/src/build.ts +153 -0
- package/src/config.ts +138 -0
- package/src/containers.ts +183 -0
- package/src/dev-entry.ts +50 -0
- package/src/dev-worker.ts +297 -0
- package/src/dev.ts +478 -0
- package/src/http.ts +102 -0
- package/src/index.ts +164 -0
- package/src/inspector.ts +44 -0
- package/src/miniflare.ts +7 -0
- package/src/module-rules.ts +36 -0
- package/src/nodejs-compat.ts +63 -0
- package/src/options.ts +46 -0
- package/src/routing.ts +48 -0
- package/src/tunnel.ts +106 -0
- package/src/worker-config.ts +237 -0
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import { isBuiltin } from 'node:module';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { getModuleTypes } from './module-rules.js';
|
|
6
|
+
import { getNodeJsCompat } from './nodejs-compat.js';
|
|
7
|
+
const PLUGIN_NAME = 'rspack-plugin-cloudflare';
|
|
8
|
+
const TYPESCRIPT_RE = /\.[cm]?tsx?$/;
|
|
9
|
+
// the static asset types Vite also imports as URLs; .txt stays a Text module, as wrangler treats it
|
|
10
|
+
const ASSET_RE = /\.(?:apng|avif|bmp|cur|eot|flac|gif|ico|jfif|jpe?g|jxl|m4a|mov|mp3|mp4|ogg|opus|otf|pdf|pjp|pjpeg|png|svg|ttf|vtt|wav|webm|webmanifest|webp|woff2?|aac)$/i;
|
|
11
|
+
// `?url` imports any file as a URL, like Vite
|
|
12
|
+
const URL_QUERY_RE = /\?(?:[^#]*&)?url(?:[&=#]|$)/;
|
|
13
|
+
/** Output subdirectory for assets Worker code imports, laid out the way the assets directory serves them. */
|
|
14
|
+
export const IMPORTED_ASSETS_DIR = 'imported-assets';
|
|
15
|
+
/** Copies imported `.wasm`/`.bin` files next to the bundle, where workerd loads them as their own modules. */
|
|
16
|
+
export class AdditionalModulesPlugin {
|
|
17
|
+
#files = new Map();
|
|
18
|
+
/** Every file emitted so far; dev passes these to Miniflare, since rspack's incremental asset list can leave them out. */
|
|
19
|
+
get modules() {
|
|
20
|
+
return [...this.#files].map(([fileName, { type }]) => {
|
|
21
|
+
return { fileName, type };
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/** Records a file to emit and returns the specifier the bundle should import it by. */
|
|
25
|
+
add(sourcePath, type) {
|
|
26
|
+
const hash = createHash('sha1').update(fs.readFileSync(sourcePath)).digest('hex')
|
|
27
|
+
.slice(0, 16);
|
|
28
|
+
const fileName = `${hash}-${path.basename(sourcePath)}`;
|
|
29
|
+
this.#files.set(fileName, { sourcePath, type });
|
|
30
|
+
return `./${fileName}`;
|
|
31
|
+
}
|
|
32
|
+
apply(compiler) {
|
|
33
|
+
const { Compilation, sources } = compiler.rspack;
|
|
34
|
+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation) => {
|
|
35
|
+
compilation.hooks.processAssets.tap({ name: PLUGIN_NAME, stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL }, () => {
|
|
36
|
+
for (const [fileName, { sourcePath }] of this.#files) {
|
|
37
|
+
compilation.emitAsset(fileName, new sources.RawSource(fs.readFileSync(sourcePath)));
|
|
38
|
+
compilation.fileDependencies.add(sourcePath);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function createExternals(nodeJsCompat, moduleTypes, additionalModules) {
|
|
45
|
+
return ({ request, context }, callback) => {
|
|
46
|
+
if (!request) {
|
|
47
|
+
callback();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (request.startsWith('cloudflare:')) {
|
|
51
|
+
callback(undefined, request);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (nodeJsCompat) {
|
|
55
|
+
const native = nodeJsCompat.externals.get(request);
|
|
56
|
+
if (native) {
|
|
57
|
+
callback(undefined, native);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// a builtin the preset doesn't know about yet; workerd only resolves the node: form
|
|
61
|
+
if (isBuiltin(request) && !nodeJsCompat.polyfills.has(request)) {
|
|
62
|
+
callback(undefined, request.startsWith('node:') ? request : `node:${request}`);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const filePath = request.replace(/\?.*$/, '');
|
|
67
|
+
const type = moduleTypes.get(path.extname(filePath));
|
|
68
|
+
const isModule = type === 'CompiledWasm' || type === 'Data';
|
|
69
|
+
if (isModule && !URL_QUERY_RE.test(request) && context && (filePath.startsWith('.') || path.isAbsolute(filePath))) {
|
|
70
|
+
callback(undefined, additionalModules.add(path.resolve(context, filePath), type));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
callback();
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Text modules inline as strings; with an assets directory, static files and `?url` imports become URLs served from it. */
|
|
77
|
+
function getAssetRules(worker, textExtensions) {
|
|
78
|
+
const textRule = { test: (file) => textExtensions.has(path.extname(file)), type: 'asset/source' };
|
|
79
|
+
if (!worker.assets) {
|
|
80
|
+
return [textRule];
|
|
81
|
+
}
|
|
82
|
+
const generator = {
|
|
83
|
+
filename: 'assets/[name].[contenthash:8][ext]',
|
|
84
|
+
outputPath: IMPORTED_ASSETS_DIR,
|
|
85
|
+
// served from the root of the assets directory, not the Worker's own public path
|
|
86
|
+
publicPath: '/',
|
|
87
|
+
};
|
|
88
|
+
return [
|
|
89
|
+
{ resourceQuery: URL_QUERY_RE, type: 'asset/resource', generator },
|
|
90
|
+
textRule,
|
|
91
|
+
{ test: ASSET_RE, type: 'asset/resource', generator },
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
/** Builds the rspack config for the Worker bundle: a single ESM file workerd can load as-is. */
|
|
95
|
+
export function createWorkerConfig(options) {
|
|
96
|
+
const { compiler, worker, pluginOptions, outputDir, entry, entrySource, mode, hot = false, publicPath = '/' } = options;
|
|
97
|
+
const { rspack } = compiler;
|
|
98
|
+
const moduleTypes = getModuleTypes(worker);
|
|
99
|
+
const textExtensions = new Set([...moduleTypes].filter(([, type]) => type === 'Text').map(([extension]) => extension));
|
|
100
|
+
const additionalModules = new AdditionalModulesPlugin();
|
|
101
|
+
const nodeJsCompat = getNodeJsCompat(worker);
|
|
102
|
+
const virtualModules = {};
|
|
103
|
+
const entries = [entry];
|
|
104
|
+
if (entrySource !== undefined) {
|
|
105
|
+
virtualModules[entry] = entrySource;
|
|
106
|
+
}
|
|
107
|
+
if (nodeJsCompat?.injectSource) {
|
|
108
|
+
// runs before the Worker so injected globals like `process` exist while its modules evaluate
|
|
109
|
+
const injectPath = path.join(worker.rootDir, '.wrangler', 'tmp', `rspack-${worker.name}`, 'nodejs-compat-inject.js');
|
|
110
|
+
virtualModules[injectPath] = nodeJsCompat.injectSource;
|
|
111
|
+
entries.unshift(injectPath);
|
|
112
|
+
}
|
|
113
|
+
const plugins = [
|
|
114
|
+
new rspack.DefinePlugin(worker.config.define),
|
|
115
|
+
// workerd loads a single module here, so keep dynamic imports in the entry chunk
|
|
116
|
+
new rspack.optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
|
|
117
|
+
additionalModules,
|
|
118
|
+
];
|
|
119
|
+
const polyfills = nodeJsCompat?.polyfills;
|
|
120
|
+
if (polyfills && polyfills.size > 0) {
|
|
121
|
+
// rspack reads `node:` specifiers as a URL scheme that resolve.alias never sees, so swap polyfills in before resolving
|
|
122
|
+
plugins.push(new rspack.NormalModuleReplacementPlugin(/^(?:node:)?[a-z_]/, (resource) => {
|
|
123
|
+
const polyfill = polyfills.get(resource.request);
|
|
124
|
+
if (polyfill) {
|
|
125
|
+
resource.request = polyfill;
|
|
126
|
+
}
|
|
127
|
+
}));
|
|
128
|
+
}
|
|
129
|
+
const virtualModulesPlugin = new rspack.experiments.VirtualModulesPlugin(virtualModules);
|
|
130
|
+
plugins.push(virtualModulesPlugin);
|
|
131
|
+
if (hot) {
|
|
132
|
+
plugins.push(new rspack.HotModuleReplacementPlugin());
|
|
133
|
+
}
|
|
134
|
+
let config = {
|
|
135
|
+
name: `cloudflare:${worker.name}`,
|
|
136
|
+
mode,
|
|
137
|
+
context: worker.rootDir,
|
|
138
|
+
target: ['webworker', 'es2024'],
|
|
139
|
+
entry: { index: entries },
|
|
140
|
+
output: {
|
|
141
|
+
path: outputDir,
|
|
142
|
+
filename: 'index.js',
|
|
143
|
+
module: true,
|
|
144
|
+
chunkFormat: 'module',
|
|
145
|
+
library: { type: 'module' },
|
|
146
|
+
clean: true,
|
|
147
|
+
publicPath,
|
|
148
|
+
},
|
|
149
|
+
externalsType: 'module-import',
|
|
150
|
+
externals: [createExternals(nodeJsCompat, moduleTypes, additionalModules)],
|
|
151
|
+
resolve: {
|
|
152
|
+
extensions: ['.ts', '.tsx', '.mts', '.cts', '.js', '.mjs', '.cjs', '.json'],
|
|
153
|
+
extensionAlias: { '.js': ['.ts', '.tsx', '.js'], '.mjs': ['.mts', '.mjs'] },
|
|
154
|
+
conditionNames: ['workerd', 'worker', 'browser', 'module', 'import', 'default'],
|
|
155
|
+
alias: worker.config.alias,
|
|
156
|
+
},
|
|
157
|
+
module: {
|
|
158
|
+
rules: [
|
|
159
|
+
{
|
|
160
|
+
test: TYPESCRIPT_RE,
|
|
161
|
+
loader: 'builtin:swc-loader',
|
|
162
|
+
options: {
|
|
163
|
+
jsc: {
|
|
164
|
+
parser: { syntax: 'typescript', tsx: true, decorators: true },
|
|
165
|
+
target: 'es2024',
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
type: 'javascript/auto',
|
|
169
|
+
},
|
|
170
|
+
{ oneOf: getAssetRules(worker, textExtensions) },
|
|
171
|
+
],
|
|
172
|
+
},
|
|
173
|
+
devtool: 'source-map',
|
|
174
|
+
optimization: {
|
|
175
|
+
// wrangler doesn't minify by default either, and readable stack traces matter more on the server
|
|
176
|
+
minimize: false,
|
|
177
|
+
// a failed dev build must leave the last good bundle in place, since restarting another Worker reloads it from disk
|
|
178
|
+
emitOnErrors: false,
|
|
179
|
+
},
|
|
180
|
+
plugins,
|
|
181
|
+
performance: { hints: false },
|
|
182
|
+
watchOptions: {
|
|
183
|
+
// dev's generated entry is virtual and the bundle is written under .wrangler, so the watcher would see both as changes
|
|
184
|
+
ignored: /[/\\](?:\.git|node_modules|\.wrangler)[/\\]/,
|
|
185
|
+
},
|
|
186
|
+
stats: 'errors-warnings',
|
|
187
|
+
infrastructureLogging: compiler.options.infrastructureLogging,
|
|
188
|
+
};
|
|
189
|
+
if (pluginOptions.worker) {
|
|
190
|
+
config = pluginOptions.worker(config, { name: worker.name }) ?? config;
|
|
191
|
+
}
|
|
192
|
+
return { config, virtualModules: virtualModulesPlugin, additionalModules };
|
|
193
|
+
}
|
|
194
|
+
//# sourceMappingURL=worker-config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-config.js","sourceRoot":"","sources":["../src/worker-config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAWrD,MAAM,WAAW,GAAG,0BAA0B,CAAC;AAC/C,MAAM,aAAa,GAAG,cAAc,CAAC;AACrC,oGAAoG;AACpG,MAAM,QAAQ,GAAG,2JAA2J,CAAC;AAC7K,8CAA8C;AAC9C,MAAM,YAAY,GAAG,6BAA6B,CAAC;AAEnD,6GAA6G;AAC7G,MAAM,CAAC,MAAM,mBAAmB,GAAG,iBAAiB,CAAC;AAQrD,8GAA8G;AAC9G,MAAM,OAAO,u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AAE,QAAQ,CAAC,OAAO,CAAC,qBAAqB;KAC7D,CAAC;IAEF,IAAI,aAAa,CAAC,MAAM,EAAE,CAAC;QAC1B,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,IAAI,MAAM,CAAC;IACxE,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,cAAc,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,CAAC;AAC5E,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,92 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "rspack-plugin-cloudflare",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Develop and build Cloudflare Workers with rspack: runs your Worker in workerd with HMR, using your Wrangler config",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"rspack",
|
|
7
|
+
"rspack-plugin",
|
|
8
|
+
"cloudflare",
|
|
9
|
+
"cloudflare-workers",
|
|
10
|
+
"workerd",
|
|
11
|
+
"wrangler",
|
|
12
|
+
"miniflare",
|
|
13
|
+
"hmr"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/Cherry/rspack-plugin-cloudflare#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/Cherry/rspack-plugin-cloudflare/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/Cherry/rspack-plugin-cloudflare.git"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "James Ross <james@jross.me>",
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"runtime",
|
|
35
|
+
"src"
|
|
36
|
+
],
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "rimraf dist && tsc -p tsconfig.build.json",
|
|
39
|
+
"check-types": "tsc -p tsconfig.json && tsc -p test/fixtures/basic/worker/tsconfig.json && tsc -p test/fixtures/legacy-compat/worker/tsconfig.json && tsc -p test/fixtures/multi-worker/tsconfig.json",
|
|
40
|
+
"lint": "eslint .",
|
|
41
|
+
"lint:fix": "eslint . --fix",
|
|
42
|
+
"prepack": "node --run build",
|
|
43
|
+
"test": "node --run lint && node --run check-types && node --run test:unit",
|
|
44
|
+
"test:unit": "node --run build && vitest run"
|
|
45
|
+
},
|
|
46
|
+
"engines": {
|
|
47
|
+
"node": ">=24.10"
|
|
48
|
+
},
|
|
49
|
+
"publishConfig": {
|
|
50
|
+
"access": "public"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@cloudflare/workers-types": "5.20261102.1",
|
|
54
|
+
"@nodecraft/eslint-config": "54.0.0",
|
|
55
|
+
"@rspack/cli": "2.3.0",
|
|
56
|
+
"@rspack/core": "2.3.0",
|
|
57
|
+
"@rspack/dev-server": "2.2.2",
|
|
58
|
+
"@stylistic/eslint-plugin": "5.10.0",
|
|
59
|
+
"@types/node": "26.6.6",
|
|
60
|
+
"@types/ws": "8.18.2",
|
|
61
|
+
"eslint": "10.14.0",
|
|
62
|
+
"eslint-plugin-import-newlines": "2.0.0",
|
|
63
|
+
"eslint-plugin-import-x": "4.17.1",
|
|
64
|
+
"eslint-plugin-n": "18.5.0",
|
|
65
|
+
"eslint-plugin-regexp": "3.3.3",
|
|
66
|
+
"eslint-plugin-unicorn": "79.0.0",
|
|
67
|
+
"globals": "17.14.0",
|
|
68
|
+
"miniflare": "5.20261101.0-beta",
|
|
69
|
+
"rimraf": "6.1.3",
|
|
70
|
+
"semantic-release": "25.0.9",
|
|
71
|
+
"typescript": "6.0.3",
|
|
72
|
+
"typescript-eslint": "8.72.1",
|
|
73
|
+
"vitest": "5.0.5",
|
|
74
|
+
"wrangler": "4.156.1"
|
|
75
|
+
},
|
|
76
|
+
"dependencies": {
|
|
77
|
+
"@cloudflare/unenv-preset": "2.16.2",
|
|
78
|
+
"mrmime": "2.0.1",
|
|
79
|
+
"unenv": "2.0.0-rc.24",
|
|
80
|
+
"ws": "8.22.1"
|
|
81
|
+
},
|
|
82
|
+
"peerDependencies": {
|
|
83
|
+
"@rspack/core": "^2.0.0",
|
|
84
|
+
"@rspack/dev-server": "^2.0.0",
|
|
85
|
+
"wrangler": "^4.156.0"
|
|
86
|
+
},
|
|
87
|
+
"peerDependenciesMeta": {
|
|
88
|
+
"@rspack/dev-server": {
|
|
89
|
+
"optional": true
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// Bundled into the Worker in dev only. workerd binds handlers and Durable Object classes once at startup, so every
|
|
2
|
+
// export here is a stable wrapper that looks up the user's current code on each call, letting rspack HMR swap it in place.
|
|
3
|
+
import { DurableObject, WorkerEntrypoint, WorkflowEntrypoint } from 'cloudflare:workers';
|
|
4
|
+
|
|
5
|
+
const HMR_RUNNER_BINDING = '__RSPACK_HMR_RUNNER__';
|
|
6
|
+
const HMR_TOKEN_BINDING = '__RSPACK_HMR_TOKEN__';
|
|
7
|
+
const HMR_HEADER = 'x-rspack-plugin-cloudflare-hmr';
|
|
8
|
+
const HANDLER_KEYS = ['fetch', 'scheduled', 'queue', 'email', 'tail', 'tailStream', 'trace', 'test', 'connect'];
|
|
9
|
+
const DURABLE_OBJECT_KEYS = ['fetch', 'alarm', 'connect', 'webSocketMessage', 'webSocketClose', 'webSocketError'];
|
|
10
|
+
// properties workerd probes on RPC targets that must not be treated as RPC methods
|
|
11
|
+
const IGNORED_KEYS = new Set(['then', 'self', 'constructor']);
|
|
12
|
+
|
|
13
|
+
let getWorker = () => {
|
|
14
|
+
return {};
|
|
15
|
+
};
|
|
16
|
+
let initialShape = '';
|
|
17
|
+
|
|
18
|
+
function stripEnv(env) {
|
|
19
|
+
const userEnv = { ...env };
|
|
20
|
+
delete userEnv[HMR_RUNNER_BINDING];
|
|
21
|
+
delete userEnv[HMR_TOKEN_BINDING];
|
|
22
|
+
return userEnv;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function sortNames(names) {
|
|
26
|
+
return names.toSorted((left, right) => left.localeCompare(right));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Node asks for updates through a fetch, since Miniflare's Node-side RPC into the runner can't load modules
|
|
30
|
+
function isHmrRequest(request, env) {
|
|
31
|
+
const token = env[HMR_TOKEN_BINDING];
|
|
32
|
+
return Boolean(token) && request.headers.get(HMR_HEADER) === token;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function applyHotUpdate(env) {
|
|
36
|
+
return new Response(await env[HMR_RUNNER_BINDING].get('runner').apply());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function prototypeKeys(ctor) {
|
|
40
|
+
const keys = new Set();
|
|
41
|
+
for (let proto = ctor.prototype; proto && proto !== Object.prototype; proto = Object.getPrototypeOf(proto)) {
|
|
42
|
+
for (const key of Object.getOwnPropertyNames(proto)) {
|
|
43
|
+
keys.add(key);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return sortNames([...keys]);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function kindOf(value) {
|
|
50
|
+
if (typeof value === 'function') {
|
|
51
|
+
if (value.prototype instanceof DurableObject) {
|
|
52
|
+
return `DurableObject(${prototypeKeys(value).join(',')})`;
|
|
53
|
+
}
|
|
54
|
+
if (value.prototype instanceof WorkflowEntrypoint) {
|
|
55
|
+
return 'Workflow';
|
|
56
|
+
}
|
|
57
|
+
if (value.prototype instanceof WorkerEntrypoint) {
|
|
58
|
+
return `WorkerEntrypoint(${prototypeKeys(value).join(',')})`;
|
|
59
|
+
}
|
|
60
|
+
return 'function';
|
|
61
|
+
}
|
|
62
|
+
if (value && typeof value === 'object') {
|
|
63
|
+
return `handlers(${HANDLER_KEYS.filter(key => typeof value[key] === 'function').join(',')})`;
|
|
64
|
+
}
|
|
65
|
+
return typeof value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Fingerprints what workerd saw at startup; when an update changes it, only a restart can rebind the exports. */
|
|
69
|
+
function describeExports(worker) {
|
|
70
|
+
return JSON.stringify(sortNames(Object.keys(worker)).map(name => [name, kindOf(worker[name])]));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function initialize(getter) {
|
|
74
|
+
getWorker = getter;
|
|
75
|
+
initialShape = describeExports(getter());
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function reduceError(error) {
|
|
79
|
+
return {
|
|
80
|
+
name: error?.name,
|
|
81
|
+
message: error?.message ?? String(error),
|
|
82
|
+
stack: error?.stack,
|
|
83
|
+
cause: error?.cause === undefined ? undefined : reduceError(error.cause),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Miniflare revives this response into a source-mapped Error on the Node side, the same trick `wrangler dev` uses
|
|
88
|
+
async function captureFetchError(run) {
|
|
89
|
+
try {
|
|
90
|
+
return await run();
|
|
91
|
+
} catch (error) {
|
|
92
|
+
return Response.json(reduceError(error), { status: 500, headers: { 'MF-Experimental-Error-Stack': 'true' } });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Makes an RPC property usable both awaited (`await stub.prop`) and called (`stub.method()`), like workerd's own stubs. */
|
|
97
|
+
function rpcProperty(key, valuePromise) {
|
|
98
|
+
const call = async (...args) => {
|
|
99
|
+
const value = await valuePromise;
|
|
100
|
+
if (typeof value !== 'function') {
|
|
101
|
+
throw new TypeError(`"${key}" is not a function.`);
|
|
102
|
+
}
|
|
103
|
+
return value(...args);
|
|
104
|
+
};
|
|
105
|
+
// a thenable on purpose, so `await stub.prop` works the way it does on a real workerd stub
|
|
106
|
+
// eslint-disable-next-line unicorn/no-thenable, unicorn/prefer-then-catch
|
|
107
|
+
call.then = (onFulfilled, onRejected) => valuePromise.then(onFulfilled, onRejected);
|
|
108
|
+
// a property probed but never used shouldn't surface as an unhandled rejection; real use still sees the error
|
|
109
|
+
valuePromise.catch(() => {});
|
|
110
|
+
return call;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function proxyRpc(wrapper, getInstance) {
|
|
114
|
+
return new Proxy(wrapper, {
|
|
115
|
+
get(target, key, receiver) {
|
|
116
|
+
const value = Reflect.get(target, key, receiver);
|
|
117
|
+
if (value !== undefined || typeof key === 'symbol' || IGNORED_KEYS.has(key)) {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
return rpcProperty(key, Promise.try(() => {
|
|
121
|
+
const instance = getInstance(receiver);
|
|
122
|
+
const property = instance[key];
|
|
123
|
+
return typeof property === 'function' ? property.bind(instance) : property;
|
|
124
|
+
}));
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// the default export always gets a fetch handler so hot updates can reach the Worker; this keeps workerd's own error otherwise
|
|
130
|
+
function missingFetch() {
|
|
131
|
+
throw new Error('Handler does not export a fetch() function.');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function createEntrypointWrapper(name, initial) {
|
|
135
|
+
const instanceFor = wrapper => new (getWorker()[name])(wrapper.ctx, stripEnv(wrapper.env));
|
|
136
|
+
class EntrypointWrapper extends WorkerEntrypoint {
|
|
137
|
+
constructor(ctx, env) {
|
|
138
|
+
super(ctx, env);
|
|
139
|
+
return proxyRpc(this, instanceFor);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
for (const key of HANDLER_KEYS) {
|
|
143
|
+
const hasHandler = typeof initial.prototype[key] === 'function';
|
|
144
|
+
if (key === 'fetch' && name === 'default') {
|
|
145
|
+
EntrypointWrapper.prototype.fetch = function(request) {
|
|
146
|
+
if (isHmrRequest(request, this.env)) {
|
|
147
|
+
return applyHotUpdate(this.env);
|
|
148
|
+
}
|
|
149
|
+
return captureFetchError(() => (hasHandler ? instanceFor(this).fetch(request) : missingFetch()));
|
|
150
|
+
};
|
|
151
|
+
} else if (hasHandler) {
|
|
152
|
+
EntrypointWrapper.prototype[key] = function(arg) {
|
|
153
|
+
const call = () => instanceFor(this)[key](arg);
|
|
154
|
+
return key === 'fetch' ? captureFetchError(call) : call();
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return EntrypointWrapper;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function createDurableObjectWrapper(name, initial) {
|
|
162
|
+
const states = new WeakMap();
|
|
163
|
+
// rebuilt when HMR swaps the class; storage survives, in-memory state starts over like the Vite plugin
|
|
164
|
+
const instanceFor = (wrapper) => {
|
|
165
|
+
const state = states.get(wrapper);
|
|
166
|
+
const ctor = getWorker()[name];
|
|
167
|
+
if (state.ctor !== ctor) {
|
|
168
|
+
state.ctor = ctor;
|
|
169
|
+
state.instance = new ctor(state.ctx, stripEnv(state.env));
|
|
170
|
+
}
|
|
171
|
+
return state.instance;
|
|
172
|
+
};
|
|
173
|
+
class DurableObjectWrapper extends DurableObject {
|
|
174
|
+
constructor(ctx, env) {
|
|
175
|
+
super(ctx, env);
|
|
176
|
+
const proxy = proxyRpc(this, instanceFor);
|
|
177
|
+
states.set(proxy, { ctx, env, ctor: undefined, instance: undefined });
|
|
178
|
+
return proxy;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
for (const key of DURABLE_OBJECT_KEYS) {
|
|
182
|
+
if (typeof initial.prototype[key] === 'function') {
|
|
183
|
+
DurableObjectWrapper.prototype[key] = function(...args) {
|
|
184
|
+
return instanceFor(this)[key](...args);
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return DurableObjectWrapper;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function createWorkflowWrapper(name) {
|
|
192
|
+
return class WorkflowWrapper extends WorkflowEntrypoint {
|
|
193
|
+
run(event, step) {
|
|
194
|
+
return new (getWorker()[name])(this.ctx, stripEnv(this.env)).run(event, step);
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function createHandlerWrapper(initial) {
|
|
200
|
+
const handlers = {
|
|
201
|
+
fetch(request, env, ctx) {
|
|
202
|
+
if (isHmrRequest(request, env)) {
|
|
203
|
+
return applyHotUpdate(env);
|
|
204
|
+
}
|
|
205
|
+
return captureFetchError(() => {
|
|
206
|
+
const current = getWorker().default;
|
|
207
|
+
return typeof current?.fetch === 'function' ? current.fetch(request, stripEnv(env), ctx) : missingFetch();
|
|
208
|
+
});
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
for (const key of HANDLER_KEYS) {
|
|
212
|
+
if (key === 'fetch' || typeof initial?.[key] !== 'function') {
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
handlers[key] = (arg, env, ctx) => getWorker().default[key](arg, stripEnv(env), ctx);
|
|
216
|
+
}
|
|
217
|
+
return handlers;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Wraps one export of the user's Worker so it keeps working across hot updates. */
|
|
221
|
+
export function wrapExport(name) {
|
|
222
|
+
const initial = getWorker()[name];
|
|
223
|
+
if (typeof initial === 'function') {
|
|
224
|
+
if (initial.prototype instanceof DurableObject) {
|
|
225
|
+
return createDurableObjectWrapper(name, initial);
|
|
226
|
+
}
|
|
227
|
+
if (initial.prototype instanceof WorkflowEntrypoint) {
|
|
228
|
+
return createWorkflowWrapper(name);
|
|
229
|
+
}
|
|
230
|
+
if (initial.prototype instanceof WorkerEntrypoint) {
|
|
231
|
+
return createEntrypointWrapper(name, initial);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (name === 'default') {
|
|
235
|
+
return createHandlerWrapper(initial);
|
|
236
|
+
}
|
|
237
|
+
return initial;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Applies pending rspack hot updates. It runs in a Durable Object so modules re-evaluate in an I/O context that outlives
|
|
242
|
+
* the request that triggered the update, which matters for promises they create at the top level.
|
|
243
|
+
*/
|
|
244
|
+
export class RspackHmrRunner extends DurableObject {
|
|
245
|
+
async #apply() {
|
|
246
|
+
const hot = import.meta.webpackHot;
|
|
247
|
+
if (!hot) {
|
|
248
|
+
return { reload: true, reason: 'hot module replacement is not enabled' };
|
|
249
|
+
}
|
|
250
|
+
try {
|
|
251
|
+
await hot.check(true);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
return { reload: true, reason: error?.message ?? String(error) };
|
|
254
|
+
}
|
|
255
|
+
if (describeExports(getWorker()) !== initialShape) {
|
|
256
|
+
return { reload: true, reason: 'the Worker\'s exports changed' };
|
|
257
|
+
}
|
|
258
|
+
return { reload: false };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// serialized here since it becomes the body of the response Node reads
|
|
262
|
+
async apply() {
|
|
263
|
+
return JSON.stringify(await this.#apply());
|
|
264
|
+
}
|
|
265
|
+
}
|