@bleedingdev/modern-js-app-tools 3.5.0-ultramodern.28 → 3.5.0-ultramodern.29
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/cjs/index.js +50 -44
- package/dist/cjs/plugins/backendFederationBuild.js +347 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-contract.js +91 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-plan.js +93 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier.js +402 -0
- package/dist/cjs/plugins/deploy/platforms/cloudflare.js +152 -27
- package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.mjs +30 -19
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/plugins/backendFederationBuild.mjs +296 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-contract.mjs +17 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-plan.mjs +45 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier.mjs +347 -0
- package/dist/esm/plugins/deploy/platforms/cloudflare.mjs +152 -27
- package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.mjs +30 -19
- package/dist/esm-node/index.mjs +2 -0
- package/dist/esm-node/plugins/backendFederationBuild.mjs +297 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-contract.mjs +18 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-plan.mjs +46 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier.mjs +348 -0
- package/dist/esm-node/plugins/deploy/platforms/cloudflare.mjs +152 -27
- package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.mjs +30 -19
- package/dist/types/plugins/backendFederationBuild.d.ts +11 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-contract.d.ts +13 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-plan.d.ts +33 -0
- package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier.d.ts +32 -0
- package/dist/types/types/config/cloudflareDeploy.d.ts +11 -0
- package/dist/types/types/config/deploy.d.ts +9 -2
- package/package.json +17 -11
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
import promises from "node:fs/promises";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { CLOUDFLARE_ASSETS_BINDING, CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY, CLOUDFLARE_RUNTIME_TYPE, CLOUDFLARE_WORKER_BUNDLE_DIRECTORY, CLOUDFLARE_WORKER_ENTRY } from "./cloudflare-output-contract.mjs";
|
|
5
|
+
import { createCloudflareOutputPlan } from "./cloudflare-output-plan.mjs";
|
|
6
|
+
const SOURCE_SCAN_FILE_PATTERN = /\.(?:[cm]?[jt]s|json)$/u;
|
|
7
|
+
const SOURCE_SCAN_IGNORED_DIRECTORY_NAMES = new Set([
|
|
8
|
+
'.codex',
|
|
9
|
+
'.fastcontext',
|
|
10
|
+
'.git',
|
|
11
|
+
'.mf',
|
|
12
|
+
'.modernjs',
|
|
13
|
+
'.output',
|
|
14
|
+
'coverage',
|
|
15
|
+
'dist',
|
|
16
|
+
'dist-cloudflare',
|
|
17
|
+
'node_modules',
|
|
18
|
+
'repos',
|
|
19
|
+
'topology'
|
|
20
|
+
]);
|
|
21
|
+
const FORBIDDEN_MUTATION_PATTERNS = [
|
|
22
|
+
{
|
|
23
|
+
code: 'forbidden-mutation-pattern',
|
|
24
|
+
message: "Generated Cloudflare server worker output must not be rewritten by app scripts.",
|
|
25
|
+
pattern: /\.output\/server\/index\.mjs/u
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
code: 'forbidden-mutation-pattern',
|
|
29
|
+
message: "Generated Cloudflare server worker output must not be rewritten by app scripts.",
|
|
30
|
+
pattern: /path\.join\(\s*['"`]\.output['"`]\s*,\s*['"`]server['"`]\s*,\s*['"`]index\.mjs['"`]\s*\)/u
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
code: 'forbidden-mutation-pattern',
|
|
34
|
+
message: "Generated Cloudflare BFF worker bundles must not be rewritten by app scripts.",
|
|
35
|
+
pattern: /\.output\/worker\/[^'"`]*\.(?:mjs|cjs|js)/u
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
code: 'forbidden-mutation-pattern',
|
|
39
|
+
message: "Generated Cloudflare BFF worker bundles must not be rewritten by app scripts.",
|
|
40
|
+
pattern: /path\.join\(\s*['"`]\.output['"`]\s*,\s*['"`]worker['"`]\s*,\s*['"`][^'"`]+\.(?:mjs|cjs|js)['"`]\s*\)/u
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
code: 'forbidden-mutation-pattern',
|
|
44
|
+
message: "Drizzle entityKind worker bundle markers must be fixed in the framework toolchain, not post-build scripts.",
|
|
45
|
+
pattern: /replaceAll\(\s*['"`];entityKind(?:,entityKind)?;/u
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
code: 'forbidden-mutation-pattern',
|
|
49
|
+
message: "Effect BFF Cloudflare dispatch must not depend on duck-typed runtime helper probing in app scripts.",
|
|
50
|
+
pattern: /(?:typeof\s+[^;\n]*dispatchEffectBffRequest|['"`]dispatchEffectBffRequest['"`]\s+in\s+|\.dispatchEffectBffRequest\b)/u
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
code: 'forbidden-mutation-pattern',
|
|
54
|
+
message: "Effect BFF Cloudflare dispatch must not branch on handler.length in app scripts.",
|
|
55
|
+
pattern: /\bhandler\s*\.\s*length\b/u
|
|
56
|
+
}
|
|
57
|
+
];
|
|
58
|
+
const pathExists = async (filePath)=>{
|
|
59
|
+
try {
|
|
60
|
+
await promises.access(filePath);
|
|
61
|
+
return true;
|
|
62
|
+
} catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const readJson = async (filePath)=>JSON.parse(await promises.readFile(filePath, 'utf-8'));
|
|
67
|
+
const addIssue = (issues, issue)=>{
|
|
68
|
+
issues.push(issue);
|
|
69
|
+
};
|
|
70
|
+
const assertEqual = (issues, actual, expected, issue)=>{
|
|
71
|
+
if (actual !== expected) addIssue(issues, issue);
|
|
72
|
+
};
|
|
73
|
+
const assertFlag = (issues, flags, flag, issue)=>{
|
|
74
|
+
if (!Array.isArray(flags) || !flags.includes(flag)) addIssue(issues, issue);
|
|
75
|
+
};
|
|
76
|
+
const getReferencedRouteWorkers = (manifest)=>Array.isArray(manifest?.routeSpec?.routes) ? manifest.routeSpec.routes.map((route)=>'string' == typeof route?.worker && route.worker.length > 0 ? route.worker : void 0).filter((worker)=>'string' == typeof worker) : [];
|
|
77
|
+
const getEffectBffWorker = (manifest)=>manifest?.bff?.runtimeFramework === 'effect' && 'string' == typeof manifest.bff.worker ? manifest.bff.worker : void 0;
|
|
78
|
+
const getWorkerBundleReferences = (manifest)=>{
|
|
79
|
+
const effectBffWorker = getEffectBffWorker(manifest);
|
|
80
|
+
return [
|
|
81
|
+
...effectBffWorker ? [
|
|
82
|
+
{
|
|
83
|
+
kind: 'effect-bff',
|
|
84
|
+
reference: effectBffWorker
|
|
85
|
+
}
|
|
86
|
+
] : [],
|
|
87
|
+
...getReferencedRouteWorkers(manifest).map((reference)=>({
|
|
88
|
+
kind: 'route',
|
|
89
|
+
reference
|
|
90
|
+
}))
|
|
91
|
+
];
|
|
92
|
+
};
|
|
93
|
+
const resolveWorkerBundleReference = (issues, outputDirectory, reference, manifestPath)=>{
|
|
94
|
+
const workerRoot = node_path.resolve(outputDirectory, CLOUDFLARE_WORKER_BUNDLE_DIRECTORY);
|
|
95
|
+
const workerPath = node_path.resolve(outputDirectory, reference.reference);
|
|
96
|
+
const relativeToWorkerRoot = node_path.relative(workerRoot, workerPath);
|
|
97
|
+
if (node_path.isAbsolute(reference.reference) || relativeToWorkerRoot.startsWith('..') || node_path.isAbsolute(relativeToWorkerRoot)) {
|
|
98
|
+
addIssue(issues, {
|
|
99
|
+
code: 'invalid-manifest',
|
|
100
|
+
message: 'Cloudflare output manifest worker bundle references must stay under worker/.',
|
|
101
|
+
path: manifestPath
|
|
102
|
+
});
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
...reference,
|
|
107
|
+
path: workerPath
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
const missingWorkerBundleMessage = (reference)=>'effect-bff' === reference.kind ? 'Cloudflare Effect BFF manifest points to a missing worker bundle.' : 'Cloudflare route worker manifest points to a missing worker bundle.';
|
|
111
|
+
const DELIVERY_UNIT_IDENTITY_FIELDS = [
|
|
112
|
+
'unitId',
|
|
113
|
+
'buildMarker',
|
|
114
|
+
'sourceRevision'
|
|
115
|
+
];
|
|
116
|
+
const verifyDeliveryUnitIdentity = (issues, manifest, manifestPath, declared)=>{
|
|
117
|
+
const stamped = manifest?.deliveryUnit;
|
|
118
|
+
const hasStamp = Boolean(stamped) && 'object' == typeof stamped;
|
|
119
|
+
if (declared) {
|
|
120
|
+
if (!hasStamp) return void addIssue(issues, {
|
|
121
|
+
code: 'missing-delivery-unit',
|
|
122
|
+
message: `Cloudflare worker manifest is missing the delivery-unit identity declared by the workspace topology (expected unitId ${declared.unitId}, buildMarker ${declared.buildMarker}).`,
|
|
123
|
+
path: manifestPath
|
|
124
|
+
});
|
|
125
|
+
for (const field of DELIVERY_UNIT_IDENTITY_FIELDS)assertEqual(issues, stamped[field], declared[field], {
|
|
126
|
+
code: 'delivery-unit-drift',
|
|
127
|
+
message: `Cloudflare worker manifest deliveryUnit.${field} must match the topology delivery-unit record (expected ${declared[field]}, received ${stamped[field] ?? 'undefined'}).`,
|
|
128
|
+
path: manifestPath
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
if (hasStamp && stamped.surfaces && 'object' == typeof stamped.surfaces) for (const surface of [
|
|
132
|
+
'ui',
|
|
133
|
+
'api'
|
|
134
|
+
]){
|
|
135
|
+
const marker = stamped.surfaces[surface];
|
|
136
|
+
if (!marker || 'object' != typeof marker) {
|
|
137
|
+
addIssue(issues, {
|
|
138
|
+
code: 'missing-delivery-unit',
|
|
139
|
+
message: `Cloudflare worker manifest is missing the ${surface} delivery-unit surface marker.`,
|
|
140
|
+
path: manifestPath
|
|
141
|
+
});
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
for (const field of DELIVERY_UNIT_IDENTITY_FIELDS)assertEqual(issues, marker[field], stamped[field], {
|
|
145
|
+
code: 'delivery-unit-drift',
|
|
146
|
+
message: `Cloudflare worker manifest ${surface} surface deliveryUnit.${field} must derive from one delivery-unit record (expected ${stamped[field] ?? 'undefined'}, received ${marker[field] ?? 'undefined'}).`,
|
|
147
|
+
path: manifestPath
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const verifyManifestShape = (issues, manifest, manifestPath)=>{
|
|
152
|
+
assertEqual(issues, manifest.runtime?.type, CLOUDFLARE_RUNTIME_TYPE, {
|
|
153
|
+
code: 'invalid-manifest',
|
|
154
|
+
message: 'Cloudflare output manifest runtime.type must be cloudflare-module-worker.',
|
|
155
|
+
path: manifestPath
|
|
156
|
+
});
|
|
157
|
+
assertEqual(issues, manifest.runtime?.entry, CLOUDFLARE_WORKER_ENTRY, {
|
|
158
|
+
code: 'invalid-manifest',
|
|
159
|
+
message: `Cloudflare output manifest runtime.entry must be ${CLOUDFLARE_WORKER_ENTRY}.`,
|
|
160
|
+
path: manifestPath
|
|
161
|
+
});
|
|
162
|
+
assertEqual(issues, manifest.runtime?.fetchExport, true, {
|
|
163
|
+
code: 'invalid-manifest',
|
|
164
|
+
message: 'Cloudflare output manifest runtime.fetchExport must be true.',
|
|
165
|
+
path: manifestPath
|
|
166
|
+
});
|
|
167
|
+
assertEqual(issues, manifest.runtime?.nodeListen, false, {
|
|
168
|
+
code: 'invalid-manifest',
|
|
169
|
+
message: 'Cloudflare output manifest runtime.nodeListen must be false.',
|
|
170
|
+
path: manifestPath
|
|
171
|
+
});
|
|
172
|
+
assertEqual(issues, manifest.assets?.binding, CLOUDFLARE_ASSETS_BINDING, {
|
|
173
|
+
code: 'invalid-manifest',
|
|
174
|
+
message: 'Cloudflare output manifest assets.binding must be ASSETS.',
|
|
175
|
+
path: manifestPath
|
|
176
|
+
});
|
|
177
|
+
assertEqual(issues, manifest.assets?.directory, `./${CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY}`, {
|
|
178
|
+
code: 'invalid-manifest',
|
|
179
|
+
message: 'Cloudflare output manifest assets.directory must be ./public.',
|
|
180
|
+
path: manifestPath
|
|
181
|
+
});
|
|
182
|
+
assertEqual(issues, manifest.assets?.runWorkerFirst, true, {
|
|
183
|
+
code: 'invalid-manifest',
|
|
184
|
+
message: 'Cloudflare output manifest assets.runWorkerFirst must be true.',
|
|
185
|
+
path: manifestPath
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
const walkFiles = async (root)=>{
|
|
189
|
+
if (!await pathExists(root)) return [];
|
|
190
|
+
const entries = await promises.readdir(root, {
|
|
191
|
+
withFileTypes: true
|
|
192
|
+
});
|
|
193
|
+
const files = [];
|
|
194
|
+
for (const entry of entries){
|
|
195
|
+
if (SOURCE_SCAN_IGNORED_DIRECTORY_NAMES.has(entry.name)) continue;
|
|
196
|
+
const entryPath = node_path.join(root, entry.name);
|
|
197
|
+
if (entry.isDirectory()) {
|
|
198
|
+
files.push(...await walkFiles(entryPath));
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (SOURCE_SCAN_FILE_PATTERN.test(entry.name)) files.push(entryPath);
|
|
202
|
+
}
|
|
203
|
+
return files;
|
|
204
|
+
};
|
|
205
|
+
const verifyCloudflareOutputMutationPolicy = async (options)=>{
|
|
206
|
+
const issues = [];
|
|
207
|
+
const files = (await Promise.all(options.scanRoots.map((scanRoot)=>walkFiles(node_path.resolve(scanRoot))))).flat();
|
|
208
|
+
for (const file of files){
|
|
209
|
+
const source = await promises.readFile(file, 'utf-8');
|
|
210
|
+
for (const forbidden of FORBIDDEN_MUTATION_PATTERNS)if (forbidden.pattern.test(source)) addIssue(issues, {
|
|
211
|
+
code: forbidden.code,
|
|
212
|
+
message: forbidden.message,
|
|
213
|
+
path: file
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return {
|
|
217
|
+
ok: 0 === issues.length,
|
|
218
|
+
issues
|
|
219
|
+
};
|
|
220
|
+
};
|
|
221
|
+
const verifyWorkerImport = async (issues, entryPath)=>{
|
|
222
|
+
try {
|
|
223
|
+
const worker = (await import(`${pathToFileURL(entryPath).href}?t=${Date.now()}`)).default;
|
|
224
|
+
if (!worker || 'function' != typeof worker.fetch) addIssue(issues, {
|
|
225
|
+
code: 'worker-import-failed',
|
|
226
|
+
message: 'Cloudflare server entry must default-export a Worker with fetch.',
|
|
227
|
+
path: entryPath
|
|
228
|
+
});
|
|
229
|
+
} catch (error) {
|
|
230
|
+
addIssue(issues, {
|
|
231
|
+
code: 'worker-import-failed',
|
|
232
|
+
message: `Cloudflare server entry could not be imported: ${error instanceof Error ? error.message : String(error)}`,
|
|
233
|
+
path: entryPath
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
const verifyCloudflareOutput = async (options)=>{
|
|
238
|
+
const outputDirectory = node_path.resolve(options.outputDirectory);
|
|
239
|
+
const issues = [];
|
|
240
|
+
const outputPlan = createCloudflareOutputPlan(outputDirectory);
|
|
241
|
+
for (const relativePath of outputPlan.requiredFiles){
|
|
242
|
+
const filePath = node_path.join(outputDirectory, relativePath);
|
|
243
|
+
if (!await pathExists(filePath)) addIssue(issues, {
|
|
244
|
+
code: 'missing-file',
|
|
245
|
+
message: `Cloudflare output is missing ${relativePath}.`,
|
|
246
|
+
path: filePath
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
const manifestPath = outputPlan.paths.workerManifest;
|
|
250
|
+
const wranglerPath = outputPlan.paths.wranglerConfig;
|
|
251
|
+
const packagePath = outputPlan.paths.outputPackage;
|
|
252
|
+
const workerPackagePath = outputPlan.paths.workerPackage;
|
|
253
|
+
const manifest = await pathExists(manifestPath) ? await readJson(manifestPath) : void 0;
|
|
254
|
+
const wrangler = await pathExists(wranglerPath) ? await readJson(wranglerPath) : void 0;
|
|
255
|
+
const outputPackage = await pathExists(packagePath) ? await readJson(packagePath) : void 0;
|
|
256
|
+
const workerPackage = await pathExists(workerPackagePath) ? await readJson(workerPackagePath) : void 0;
|
|
257
|
+
if (manifest) {
|
|
258
|
+
verifyManifestShape(issues, manifest, manifestPath);
|
|
259
|
+
verifyDeliveryUnitIdentity(issues, manifest, manifestPath, options.deliveryUnit);
|
|
260
|
+
const workerReferences = getWorkerBundleReferences(manifest);
|
|
261
|
+
if (workerReferences.length > 0 && !await pathExists(workerPackagePath)) addIssue(issues, {
|
|
262
|
+
code: 'missing-file',
|
|
263
|
+
message: 'Cloudflare output is missing worker/package.json for referenced worker bundles.',
|
|
264
|
+
path: workerPackagePath
|
|
265
|
+
});
|
|
266
|
+
if (manifest.bff?.runtimeFramework === 'effect' && !getEffectBffWorker(manifest)) addIssue(issues, {
|
|
267
|
+
code: 'missing-worker-bundle',
|
|
268
|
+
message: 'Cloudflare Effect BFF manifest points to a missing worker bundle.'
|
|
269
|
+
});
|
|
270
|
+
for (const workerReference of workerReferences){
|
|
271
|
+
const resolvedWorker = resolveWorkerBundleReference(issues, outputDirectory, workerReference, manifestPath);
|
|
272
|
+
if (!resolvedWorker) continue;
|
|
273
|
+
const workerExists = await pathExists(resolvedWorker.path);
|
|
274
|
+
if (!workerExists) addIssue(issues, {
|
|
275
|
+
code: 'missing-worker-bundle',
|
|
276
|
+
message: missingWorkerBundleMessage(workerReference),
|
|
277
|
+
path: resolvedWorker.path
|
|
278
|
+
});
|
|
279
|
+
if (workerExists && 'effect-bff' === resolvedWorker.kind) {
|
|
280
|
+
const workerSource = await promises.readFile(resolvedWorker.path, 'utf-8');
|
|
281
|
+
if (workerSource.includes(';entityKind;') || workerSource.includes(';entityKind,entityKind;')) addIssue(issues, {
|
|
282
|
+
code: 'invalid-worker-bundle',
|
|
283
|
+
message: 'Cloudflare Effect BFF worker bundle contains invalid Drizzle entityKind marker references.',
|
|
284
|
+
path: resolvedWorker.path
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
if (wrangler) {
|
|
290
|
+
assertEqual(issues, wrangler.main, outputPlan.wrangler.main, {
|
|
291
|
+
code: 'invalid-wrangler',
|
|
292
|
+
message: `wrangler.json main must be ${outputPlan.wrangler.main}.`,
|
|
293
|
+
path: wranglerPath
|
|
294
|
+
});
|
|
295
|
+
assertEqual(issues, wrangler.assets?.binding, outputPlan.wrangler.assets.binding, {
|
|
296
|
+
code: 'invalid-wrangler',
|
|
297
|
+
message: 'wrangler.json assets.binding must be ASSETS.',
|
|
298
|
+
path: wranglerPath
|
|
299
|
+
});
|
|
300
|
+
assertEqual(issues, wrangler.assets?.directory, outputPlan.wrangler.assets.directory, {
|
|
301
|
+
code: 'invalid-wrangler',
|
|
302
|
+
message: 'wrangler.json assets.directory must be ./public.',
|
|
303
|
+
path: wranglerPath
|
|
304
|
+
});
|
|
305
|
+
assertEqual(issues, wrangler.assets?.run_worker_first, outputPlan.wrangler.assets.run_worker_first, {
|
|
306
|
+
code: 'invalid-wrangler',
|
|
307
|
+
message: 'wrangler.json assets.run_worker_first must be true.',
|
|
308
|
+
path: wranglerPath
|
|
309
|
+
});
|
|
310
|
+
for (const flag of outputPlan.wrangler.requiredCompatibilityFlags)assertFlag(issues, wrangler.compatibility_flags, flag, {
|
|
311
|
+
code: 'invalid-wrangler',
|
|
312
|
+
message: `wrangler.json compatibility_flags must include ${flag}.`,
|
|
313
|
+
path: wranglerPath
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
if (outputPackage) assertEqual(issues, outputPackage.type, outputPlan.packages.output.type, {
|
|
317
|
+
code: 'invalid-package',
|
|
318
|
+
message: '.output/package.json must declare type module.',
|
|
319
|
+
path: packagePath
|
|
320
|
+
});
|
|
321
|
+
if (workerPackage) assertEqual(issues, workerPackage.type, outputPlan.packages.worker.type, {
|
|
322
|
+
code: 'invalid-package',
|
|
323
|
+
message: '.output/worker/package.json must declare type commonjs.',
|
|
324
|
+
path: workerPackagePath
|
|
325
|
+
});
|
|
326
|
+
for (const leakedPath of outputPlan.publicLeakDirectories){
|
|
327
|
+
const publicPath = node_path.join(outputPlan.paths.publicAssets, leakedPath);
|
|
328
|
+
if (await pathExists(publicPath)) addIssue(issues, {
|
|
329
|
+
code: 'public-output-leak',
|
|
330
|
+
message: `Framework-owned ${leakedPath} output leaked into public assets.`,
|
|
331
|
+
path: publicPath
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
if (false !== options.importWorker && await pathExists(outputPlan.paths.workerEntry)) await verifyWorkerImport(issues, outputPlan.paths.workerEntry);
|
|
335
|
+
return {
|
|
336
|
+
ok: 0 === issues.length,
|
|
337
|
+
issues
|
|
338
|
+
};
|
|
339
|
+
};
|
|
340
|
+
const assertCloudflareOutput = async (options)=>{
|
|
341
|
+
const result = await verifyCloudflareOutput(options);
|
|
342
|
+
if (!result.ok) throw new Error([
|
|
343
|
+
'Cloudflare output verification failed:',
|
|
344
|
+
...result.issues.map((issue)=>`- ${issue.code}: ${issue.message}${issue.path ? ` (${issue.path})` : ''}`)
|
|
345
|
+
].join('\n'));
|
|
346
|
+
};
|
|
347
|
+
export { assertCloudflareOutput, verifyCloudflareOutput, verifyCloudflareOutputMutationPolicy };
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import node_path from "node:path";
|
|
2
2
|
import { fs } from "@modern-js/utils";
|
|
3
3
|
import { readTemplate } from "../utils/index.mjs";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
4
|
+
import { CLOUDFLARE_ASSETS_BINDING, CLOUDFLARE_OUTPUT_PACKAGE_FILE, CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY, CLOUDFLARE_REQUIRED_COMPATIBILITY_FLAGS, CLOUDFLARE_RUNTIME_TYPE, CLOUDFLARE_WORKER_BUNDLE_DIRECTORY, CLOUDFLARE_WORKER_BUNDLE_FORMAT, CLOUDFLARE_WORKER_ENTRY, CLOUDFLARE_WRANGLER_CONFIG_FILE } from "./cloudflare-output-contract.mjs";
|
|
5
|
+
import { createCloudflareOutputPlan } from "./cloudflare-output-plan.mjs";
|
|
6
|
+
import { assertCloudflareOutput } from "./cloudflare-output-verifier.mjs";
|
|
7
|
+
const WORKER_ENTRY = CLOUDFLARE_WORKER_ENTRY;
|
|
8
|
+
const WRANGLER_CONFIG_FILE = CLOUDFLARE_WRANGLER_CONFIG_FILE;
|
|
9
|
+
const OUTPUT_PACKAGE_FILE = CLOUDFLARE_OUTPUT_PACKAGE_FILE;
|
|
10
|
+
const ASSETS_BINDING = CLOUDFLARE_ASSETS_BINDING;
|
|
9
11
|
const ROUTE_SPEC_FILE = 'route.json';
|
|
10
12
|
const ROUTE_SPEC_OUTPUT = `server/${ROUTE_SPEC_FILE}`;
|
|
11
13
|
const LOADABLE_STATS_FILE = 'loadable-stats.json';
|
|
12
14
|
const ROUTE_MANIFEST_FILE = 'routes-manifest.json';
|
|
13
|
-
const PUBLIC_ASSETS_DIRECTORY =
|
|
14
|
-
const WORKER_BUNDLE_DIRECTORY =
|
|
15
|
+
const PUBLIC_ASSETS_DIRECTORY = CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY;
|
|
16
|
+
const WORKER_BUNDLE_DIRECTORY = CLOUDFLARE_WORKER_BUNDLE_DIRECTORY;
|
|
15
17
|
const SERVER_BUNDLE_DIRECTORY = 'bundles';
|
|
16
18
|
const SERVER_OUTPUT_DIRECTORY = 'server';
|
|
17
19
|
const DEFAULT_SERVER_ONLY_PUBLIC_ASSET_EXCLUDES = [
|
|
@@ -22,10 +24,7 @@ const BFF_EFFECT_WORKER_ENTRY = `${WORKER_BUNDLE_DIRECTORY}/__modern_bff_effect.
|
|
|
22
24
|
const EFFECT_BFF_CLOUDFLARE_IMPORT_GUIDANCE = 'Ensure the Effect API entry exists at api/index.ts or bff.effect.entry, and import Cloudflare edge handlers from @modern-js/plugin-bff/effect-edge instead of lambda/Hono server helpers.';
|
|
23
25
|
const DEFAULT_COMPATIBILITY_DATE = '2026-06-02';
|
|
24
26
|
const COMPATIBILITY_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/u;
|
|
25
|
-
const REQUIRED_COMPATIBILITY_FLAGS =
|
|
26
|
-
'nodejs_compat',
|
|
27
|
-
'global_fetch_strictly_public'
|
|
28
|
-
];
|
|
27
|
+
const REQUIRED_COMPATIBILITY_FLAGS = CLOUDFLARE_REQUIRED_COMPATIBILITY_FLAGS;
|
|
29
28
|
const RESERVED_ARTIFACT_DESTINATION_FILES = new Set([
|
|
30
29
|
WRANGLER_CONFIG_FILE,
|
|
31
30
|
OUTPUT_PACKAGE_FILE
|
|
@@ -379,9 +378,48 @@ const createWranglerD1Databases = (modernConfig, configuredWranglerD1)=>{
|
|
|
379
378
|
if (!Array.isArray(d1Databases)) throw new Error('deploy.worker.d1Databases must be an array.');
|
|
380
379
|
return d1Databases.map(normalizeD1Database);
|
|
381
380
|
};
|
|
381
|
+
const normalizeServiceBinding = (service, index)=>{
|
|
382
|
+
const binding = assertNonEmptyString(service.binding, `deploy.worker.services[${index}].binding`);
|
|
383
|
+
const serviceName = assertNonEmptyString(service.service, `deploy.worker.services[${index}].service`);
|
|
384
|
+
const prefix = void 0 === service.prefix ? void 0 : assertNonEmptyString(service.prefix, `deploy.worker.services[${index}].prefix`);
|
|
385
|
+
return {
|
|
386
|
+
binding,
|
|
387
|
+
service: serviceName,
|
|
388
|
+
...void 0 === prefix ? {} : {
|
|
389
|
+
prefix
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
};
|
|
393
|
+
const createWorkerServiceBindings = (modernConfig, configuredWranglerServices)=>{
|
|
394
|
+
const services = modernConfig.deploy?.worker?.services;
|
|
395
|
+
if (void 0 === services) return configuredWranglerServices;
|
|
396
|
+
if (void 0 !== configuredWranglerServices) throw new Error('Use deploy.worker.services or deploy.worker.wrangler.services, not both.');
|
|
397
|
+
if (!Array.isArray(services)) throw new Error('deploy.worker.services must be an array.');
|
|
398
|
+
return services.map(normalizeServiceBinding);
|
|
399
|
+
};
|
|
400
|
+
const createWranglerServices = (serviceBindings)=>{
|
|
401
|
+
if (!Array.isArray(serviceBindings)) return serviceBindings;
|
|
402
|
+
if (!serviceBindings.every(isJsonRecord)) return serviceBindings;
|
|
403
|
+
return serviceBindings.map((service)=>{
|
|
404
|
+
const { prefix, ...wranglerService } = service;
|
|
405
|
+
return wranglerService;
|
|
406
|
+
});
|
|
407
|
+
};
|
|
408
|
+
const createWorkerManifestServiceBindings = (serviceBindings)=>{
|
|
409
|
+
if (!Array.isArray(serviceBindings)) return;
|
|
410
|
+
const dispatchBindings = serviceBindings.filter((service)=>isJsonRecord(service) && 'string' == typeof service.binding && 'string' == typeof service.service && 'string' == typeof service.prefix).map((service)=>({
|
|
411
|
+
binding: service.binding,
|
|
412
|
+
service: service.service,
|
|
413
|
+
prefix: service.prefix,
|
|
414
|
+
interface: 'fetch'
|
|
415
|
+
}));
|
|
416
|
+
return dispatchBindings.length > 0 ? dispatchBindings : void 0;
|
|
417
|
+
};
|
|
382
418
|
const createWranglerConfig = (appDirectory, modernConfig)=>{
|
|
383
419
|
const wrangler = getConfiguredWrangler(modernConfig);
|
|
384
420
|
const d1Databases = createWranglerD1Databases(modernConfig, wrangler.d1_databases);
|
|
421
|
+
const serviceBindings = createWorkerServiceBindings(modernConfig, wrangler.services);
|
|
422
|
+
const wranglerServices = createWranglerServices(serviceBindings);
|
|
385
423
|
return {
|
|
386
424
|
$schema: 'node_modules/wrangler/config-schema.json',
|
|
387
425
|
name: getConfiguredWorkerName(appDirectory, modernConfig),
|
|
@@ -392,6 +430,9 @@ const createWranglerConfig = (appDirectory, modernConfig)=>{
|
|
|
392
430
|
assets: createWranglerAssetsConfig(wrangler.assets),
|
|
393
431
|
...void 0 === d1Databases ? {} : {
|
|
394
432
|
d1_databases: d1Databases
|
|
433
|
+
},
|
|
434
|
+
...void 0 === wranglerServices ? {} : {
|
|
435
|
+
services: wranglerServices
|
|
395
436
|
}
|
|
396
437
|
};
|
|
397
438
|
};
|
|
@@ -454,8 +495,79 @@ const readRouteSpec = async (outputDirectory)=>{
|
|
|
454
495
|
routes: Array.isArray(routeSpec.routes) ? routeSpec.routes : []
|
|
455
496
|
};
|
|
456
497
|
};
|
|
498
|
+
const COMPACT_CONFIG_PATH = '.modernjs/ultramodern.json';
|
|
499
|
+
const ULTRAMODERN_BUILD_MODULE = 'shared/ultramodern-build.ts';
|
|
500
|
+
const nonEmptyString = (value)=>'string' == typeof value && value.length > 0 ? value : void 0;
|
|
501
|
+
const toDeliveryUnitIdentity = (value)=>{
|
|
502
|
+
if (!isRecord(value)) return;
|
|
503
|
+
const unitId = nonEmptyString(value.unitId);
|
|
504
|
+
const buildMarker = nonEmptyString(value.buildMarker);
|
|
505
|
+
const sourceRevision = nonEmptyString(value.sourceRevision);
|
|
506
|
+
if (!unitId || !buildMarker || !sourceRevision) return;
|
|
507
|
+
return {
|
|
508
|
+
unitId,
|
|
509
|
+
buildMarker,
|
|
510
|
+
sourceRevision
|
|
511
|
+
};
|
|
512
|
+
};
|
|
513
|
+
const findWorkspaceRoot = async (appDirectory)=>{
|
|
514
|
+
let current = node_path.resolve(appDirectory);
|
|
515
|
+
for(;;){
|
|
516
|
+
if (await fs.pathExists(node_path.join(current, COMPACT_CONFIG_PATH))) return current;
|
|
517
|
+
const parent = node_path.dirname(current);
|
|
518
|
+
if (parent === current) return;
|
|
519
|
+
current = parent;
|
|
520
|
+
}
|
|
521
|
+
};
|
|
522
|
+
const resolveTopologyDeliveryUnit = async (appDirectory)=>{
|
|
523
|
+
const workspaceRoot = await findWorkspaceRoot(appDirectory);
|
|
524
|
+
if (!workspaceRoot) return;
|
|
525
|
+
let compactConfig;
|
|
526
|
+
try {
|
|
527
|
+
compactConfig = await fs.readJSON(node_path.join(workspaceRoot, COMPACT_CONFIG_PATH));
|
|
528
|
+
} catch {
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
if (!isRecord(compactConfig)) return;
|
|
532
|
+
const topology = isRecord(compactConfig.topology) ? compactConfig.topology : void 0;
|
|
533
|
+
const apps = Array.isArray(topology?.apps) ? topology.apps : [];
|
|
534
|
+
const resolvedAppDirectory = node_path.resolve(appDirectory);
|
|
535
|
+
for (const app of apps){
|
|
536
|
+
if (!isRecord(app)) continue;
|
|
537
|
+
const appPath = nonEmptyString(app.path);
|
|
538
|
+
if (appPath && node_path.resolve(workspaceRoot, appPath.replace(/^\.\/+/u, '')) === resolvedAppDirectory) return toDeliveryUnitIdentity(app.deliveryUnit);
|
|
539
|
+
}
|
|
540
|
+
return toDeliveryUnitIdentity(compactConfig.deliveryUnit);
|
|
541
|
+
};
|
|
542
|
+
const resolveWorkerDeliveryUnitStamp = async (appDirectory)=>{
|
|
543
|
+
const buildModulePath = node_path.join(appDirectory, ULTRAMODERN_BUILD_MODULE);
|
|
544
|
+
if (!await fs.pathExists(buildModulePath)) return;
|
|
545
|
+
const source = await fs.readFile(buildModulePath, 'utf8');
|
|
546
|
+
const buildMarker = source.match(/\bbuild:\s*['"]([^'"]+)['"]/u)?.[1];
|
|
547
|
+
const unitId = source.match(/\bunitId:\s*['"]([^'"]+)['"]/u)?.[1];
|
|
548
|
+
const sourceRevision = source.match(/\bsourceRevision:\s*['"]([^'"]+)['"]/u)?.[1];
|
|
549
|
+
if (!buildMarker || !unitId || !sourceRevision) return;
|
|
550
|
+
const identity = {
|
|
551
|
+
unitId,
|
|
552
|
+
buildMarker,
|
|
553
|
+
sourceRevision
|
|
554
|
+
};
|
|
555
|
+
return {
|
|
556
|
+
...identity,
|
|
557
|
+
surfaces: {
|
|
558
|
+
ui: {
|
|
559
|
+
...identity,
|
|
560
|
+
surface: 'ui'
|
|
561
|
+
},
|
|
562
|
+
api: {
|
|
563
|
+
...identity,
|
|
564
|
+
surface: 'api'
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
};
|
|
457
569
|
const createMissingEffectBffWorkerError = (outputDirectory, worker)=>new Error(`Cloudflare Effect API runtime is configured, but the BFF worker bundle is missing: ${node_path.join(outputDirectory, worker)}. ${EFFECT_BFF_CLOUDFLARE_IMPORT_GUIDANCE}`);
|
|
458
|
-
const createWorkerManifest = async (outputDirectory, modernConfig, appContext)=>{
|
|
570
|
+
const createWorkerManifest = async (outputDirectory, modernConfig, appContext, deliveryUnitStamp)=>{
|
|
459
571
|
const routeSpec = await readRouteSpec(outputDirectory);
|
|
460
572
|
const routes = await Promise.all(routeSpec.routes.map(async (route)=>{
|
|
461
573
|
const worker = 'string' == typeof route.worker ? route.worker : void 0;
|
|
@@ -472,11 +584,12 @@ const createWorkerManifest = async (outputDirectory, modernConfig, appContext)=>
|
|
|
472
584
|
const primaryBffPrefix = Array.isArray(bffPrefix) ? bffPrefix[0] : bffPrefix;
|
|
473
585
|
const isEffectApi = Boolean(modernConfig.bff) && modernConfig.bff?.runtimeFramework !== 'hono';
|
|
474
586
|
const effectApiWorkerExists = await fs.pathExists(node_path.join(outputDirectory, BFF_EFFECT_WORKER_ENTRY));
|
|
587
|
+
const serviceBindings = createWorkerManifestServiceBindings(createWorkerServiceBindings(modernConfig, void 0));
|
|
475
588
|
if (isEffectApi && primaryBffPrefix && !effectApiWorkerExists) throw createMissingEffectBffWorkerError(outputDirectory, BFF_EFFECT_WORKER_ENTRY);
|
|
476
589
|
return {
|
|
477
590
|
version: 1,
|
|
478
591
|
runtime: {
|
|
479
|
-
type:
|
|
592
|
+
type: CLOUDFLARE_RUNTIME_TYPE,
|
|
480
593
|
entry: WORKER_ENTRY,
|
|
481
594
|
fetchExport: true,
|
|
482
595
|
nodeListen: false
|
|
@@ -492,7 +605,7 @@ const createWorkerManifest = async (outputDirectory, modernConfig, appContext)=>
|
|
|
492
605
|
},
|
|
493
606
|
workerBundles: {
|
|
494
607
|
directory: WORKER_BUNDLE_DIRECTORY,
|
|
495
|
-
format:
|
|
608
|
+
format: CLOUDFLARE_WORKER_BUNDLE_FORMAT,
|
|
496
609
|
importableFromModuleWorker: true,
|
|
497
610
|
requestHandlerExport: 'requestHandler'
|
|
498
611
|
},
|
|
@@ -501,12 +614,18 @@ const createWorkerManifest = async (outputDirectory, modernConfig, appContext)=>
|
|
|
501
614
|
routeManifest: ROUTE_MANIFEST_FILE
|
|
502
615
|
},
|
|
503
616
|
security: createCloudflareWorkerSecurityPolicy(modernConfig),
|
|
617
|
+
...deliveryUnitStamp ? {
|
|
618
|
+
deliveryUnit: deliveryUnitStamp
|
|
619
|
+
} : {},
|
|
504
620
|
i18n: createI18nWorkerManifest(routeSpec, appContext),
|
|
505
621
|
bff: isEffectApi && primaryBffPrefix && effectApiWorkerExists ? {
|
|
506
622
|
runtimeFramework: 'effect',
|
|
507
623
|
prefix: primaryBffPrefix,
|
|
508
624
|
worker: BFF_EFFECT_WORKER_ENTRY
|
|
509
|
-
} : void 0
|
|
625
|
+
} : void 0,
|
|
626
|
+
...void 0 === serviceBindings ? {} : {
|
|
627
|
+
serviceBindings
|
|
628
|
+
}
|
|
510
629
|
};
|
|
511
630
|
};
|
|
512
631
|
const createWorkerModuleLoaders = (manifest)=>{
|
|
@@ -558,11 +677,12 @@ const shouldCopyToWorkerBundle = (src, workerBundleDirectory)=>{
|
|
|
558
677
|
const createCloudflarePreset = ({ appContext, modernConfig })=>{
|
|
559
678
|
const { appDirectory, distDirectory } = appContext;
|
|
560
679
|
const outputDirectory = node_path.join(appDirectory, '.output');
|
|
561
|
-
const
|
|
562
|
-
const
|
|
563
|
-
const
|
|
680
|
+
const outputPlan = createCloudflareOutputPlan(outputDirectory);
|
|
681
|
+
const publicDirectory = outputPlan.paths.publicAssets;
|
|
682
|
+
const workerEntryPath = outputPlan.paths.workerEntry;
|
|
683
|
+
const workerManifestPath = outputPlan.paths.workerManifest;
|
|
564
684
|
const routeSpecOutputPath = node_path.join(outputDirectory, ROUTE_SPEC_OUTPUT);
|
|
565
|
-
const wranglerConfigPath =
|
|
685
|
+
const wranglerConfigPath = outputPlan.paths.wranglerConfig;
|
|
566
686
|
const cloudflareArtifacts = getCloudflareArtifacts(modernConfig);
|
|
567
687
|
const publicAssetExcludes = getPublicAssetExcludes(appDirectory, modernConfig);
|
|
568
688
|
return {
|
|
@@ -578,14 +698,12 @@ const createCloudflarePreset = ({ appContext, modernConfig })=>{
|
|
|
578
698
|
const routeSpecSourcePath = node_path.join(distDirectory, ROUTE_SPEC_FILE);
|
|
579
699
|
if (await fs.pathExists(routeSpecSourcePath)) await fs.copy(routeSpecSourcePath, routeSpecOutputPath);
|
|
580
700
|
const workerBundleSourceDirectory = node_path.join(distDirectory, WORKER_BUNDLE_DIRECTORY);
|
|
581
|
-
const workerBundleOutputDirectory =
|
|
701
|
+
const workerBundleOutputDirectory = outputPlan.paths.workerBundle;
|
|
582
702
|
if (await fs.pathExists(workerBundleSourceDirectory)) {
|
|
583
703
|
await fs.copy(workerBundleSourceDirectory, workerBundleOutputDirectory, {
|
|
584
704
|
filter: (src)=>shouldCopyToWorkerBundle(src, workerBundleSourceDirectory)
|
|
585
705
|
});
|
|
586
|
-
await fs.writeJSON(
|
|
587
|
-
type: 'commonjs'
|
|
588
|
-
});
|
|
706
|
+
await fs.writeJSON(outputPlan.paths.workerPackage, outputPlan.packages.worker);
|
|
589
707
|
}
|
|
590
708
|
await copyCloudflareArtifacts(appDirectory, outputDirectory, cloudflareArtifacts);
|
|
591
709
|
await copyCloudflarePublicAssets(appDirectory, publicDirectory, getCloudflarePublicAssets(modernConfig));
|
|
@@ -593,17 +711,24 @@ const createCloudflarePreset = ({ appContext, modernConfig })=>{
|
|
|
593
711
|
await fs.writeJSON(wranglerConfigPath, createWranglerConfig(appDirectory, modernConfig), {
|
|
594
712
|
spaces: 2
|
|
595
713
|
});
|
|
596
|
-
|
|
714
|
+
const deliveryUnitStamp = await resolveWorkerDeliveryUnitStamp(appDirectory);
|
|
715
|
+
await fs.writeJSON(workerManifestPath, await createWorkerManifest(outputDirectory, modernConfig, appContext, deliveryUnitStamp), {
|
|
597
716
|
spaces: 2
|
|
598
717
|
});
|
|
599
|
-
await fs.writeJSON(
|
|
600
|
-
type: 'module'
|
|
601
|
-
});
|
|
718
|
+
await fs.writeJSON(outputPlan.paths.outputPackage, outputPlan.packages.output);
|
|
602
719
|
},
|
|
603
720
|
async genEntry () {
|
|
604
721
|
const template = await readTemplate('cloudflare-entry.mjs');
|
|
605
722
|
const manifest = await fs.readJSON(workerManifestPath);
|
|
606
723
|
await fs.writeFile(workerEntryPath, template.replace('p_workerManifest', JSON.stringify(manifest, null, 2)).replace('p_workerModuleLoaders', createWorkerModuleLoaders(manifest)));
|
|
724
|
+
const topologyDeliveryUnit = await resolveTopologyDeliveryUnit(appDirectory);
|
|
725
|
+
await assertCloudflareOutput({
|
|
726
|
+
outputDirectory,
|
|
727
|
+
importWorker: false,
|
|
728
|
+
...topologyDeliveryUnit ? {
|
|
729
|
+
deliveryUnit: topologyDeliveryUnit
|
|
730
|
+
} : {}
|
|
731
|
+
});
|
|
607
732
|
}
|
|
608
733
|
};
|
|
609
734
|
};
|