@bleedingdev/modern-js-app-tools 3.5.0-ultramodern.3 → 3.5.0-ultramodern.30

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.
Files changed (32) hide show
  1. package/dist/cjs/builder/generator/getBuilderEnvironments.js +1 -0
  2. package/dist/cjs/index.js +50 -44
  3. package/dist/cjs/plugins/backendFederationBuild.js +347 -0
  4. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-contract.js +91 -0
  5. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-plan.js +93 -0
  6. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier.js +402 -0
  7. package/dist/cjs/plugins/deploy/platforms/cloudflare.js +272 -29
  8. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.mjs +346 -43
  9. package/dist/esm/builder/generator/getBuilderEnvironments.mjs +1 -0
  10. package/dist/esm/index.mjs +2 -0
  11. package/dist/esm/plugins/backendFederationBuild.mjs +296 -0
  12. package/dist/esm/plugins/deploy/platforms/cloudflare-output-contract.mjs +17 -0
  13. package/dist/esm/plugins/deploy/platforms/cloudflare-output-plan.mjs +45 -0
  14. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier.mjs +347 -0
  15. package/dist/esm/plugins/deploy/platforms/cloudflare.mjs +272 -29
  16. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.mjs +346 -43
  17. package/dist/esm-node/builder/generator/getBuilderEnvironments.mjs +1 -0
  18. package/dist/esm-node/index.mjs +2 -0
  19. package/dist/esm-node/plugins/backendFederationBuild.mjs +297 -0
  20. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-contract.mjs +18 -0
  21. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-plan.mjs +46 -0
  22. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier.mjs +348 -0
  23. package/dist/esm-node/plugins/deploy/platforms/cloudflare.mjs +272 -29
  24. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.mjs +346 -43
  25. package/dist/types/plugins/backendFederationBuild.d.ts +11 -0
  26. package/dist/types/plugins/deploy/platforms/cloudflare-output-contract.d.ts +13 -0
  27. package/dist/types/plugins/deploy/platforms/cloudflare-output-plan.d.ts +33 -0
  28. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier.d.ts +32 -0
  29. package/dist/types/types/config/cloudflareDeploy.d.ts +22 -0
  30. package/dist/types/types/config/deploy.d.ts +15 -2
  31. package/lib/types.d.ts +2 -3
  32. 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 };