@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,348 @@
1
+ import "node:module";
2
+ import promises from "node:fs/promises";
3
+ import node_path from "node:path";
4
+ import { pathToFileURL } from "node:url";
5
+ import { CLOUDFLARE_ASSETS_BINDING, CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY, CLOUDFLARE_RUNTIME_TYPE, CLOUDFLARE_WORKER_BUNDLE_DIRECTORY, CLOUDFLARE_WORKER_ENTRY } from "./cloudflare-output-contract.mjs";
6
+ import { createCloudflareOutputPlan } from "./cloudflare-output-plan.mjs";
7
+ const SOURCE_SCAN_FILE_PATTERN = /\.(?:[cm]?[jt]s|json)$/u;
8
+ const SOURCE_SCAN_IGNORED_DIRECTORY_NAMES = new Set([
9
+ '.codex',
10
+ '.fastcontext',
11
+ '.git',
12
+ '.mf',
13
+ '.modernjs',
14
+ '.output',
15
+ 'coverage',
16
+ 'dist',
17
+ 'dist-cloudflare',
18
+ 'node_modules',
19
+ 'repos',
20
+ 'topology'
21
+ ]);
22
+ const FORBIDDEN_MUTATION_PATTERNS = [
23
+ {
24
+ code: 'forbidden-mutation-pattern',
25
+ message: "Generated Cloudflare server worker output must not be rewritten by app scripts.",
26
+ pattern: /\.output\/server\/index\.mjs/u
27
+ },
28
+ {
29
+ code: 'forbidden-mutation-pattern',
30
+ message: "Generated Cloudflare server worker output must not be rewritten by app scripts.",
31
+ pattern: /path\.join\(\s*['"`]\.output['"`]\s*,\s*['"`]server['"`]\s*,\s*['"`]index\.mjs['"`]\s*\)/u
32
+ },
33
+ {
34
+ code: 'forbidden-mutation-pattern',
35
+ message: "Generated Cloudflare BFF worker bundles must not be rewritten by app scripts.",
36
+ pattern: /\.output\/worker\/[^'"`]*\.(?:mjs|cjs|js)/u
37
+ },
38
+ {
39
+ code: 'forbidden-mutation-pattern',
40
+ message: "Generated Cloudflare BFF worker bundles must not be rewritten by app scripts.",
41
+ pattern: /path\.join\(\s*['"`]\.output['"`]\s*,\s*['"`]worker['"`]\s*,\s*['"`][^'"`]+\.(?:mjs|cjs|js)['"`]\s*\)/u
42
+ },
43
+ {
44
+ code: 'forbidden-mutation-pattern',
45
+ message: "Drizzle entityKind worker bundle markers must be fixed in the framework toolchain, not post-build scripts.",
46
+ pattern: /replaceAll\(\s*['"`];entityKind(?:,entityKind)?;/u
47
+ },
48
+ {
49
+ code: 'forbidden-mutation-pattern',
50
+ message: "Effect BFF Cloudflare dispatch must not depend on duck-typed runtime helper probing in app scripts.",
51
+ pattern: /(?:typeof\s+[^;\n]*dispatchEffectBffRequest|['"`]dispatchEffectBffRequest['"`]\s+in\s+|\.dispatchEffectBffRequest\b)/u
52
+ },
53
+ {
54
+ code: 'forbidden-mutation-pattern',
55
+ message: "Effect BFF Cloudflare dispatch must not branch on handler.length in app scripts.",
56
+ pattern: /\bhandler\s*\.\s*length\b/u
57
+ }
58
+ ];
59
+ const pathExists = async (filePath)=>{
60
+ try {
61
+ await promises.access(filePath);
62
+ return true;
63
+ } catch {
64
+ return false;
65
+ }
66
+ };
67
+ const readJson = async (filePath)=>JSON.parse(await promises.readFile(filePath, 'utf-8'));
68
+ const addIssue = (issues, issue)=>{
69
+ issues.push(issue);
70
+ };
71
+ const assertEqual = (issues, actual, expected, issue)=>{
72
+ if (actual !== expected) addIssue(issues, issue);
73
+ };
74
+ const assertFlag = (issues, flags, flag, issue)=>{
75
+ if (!Array.isArray(flags) || !flags.includes(flag)) addIssue(issues, issue);
76
+ };
77
+ 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) : [];
78
+ const getEffectBffWorker = (manifest)=>manifest?.bff?.runtimeFramework === 'effect' && 'string' == typeof manifest.bff.worker ? manifest.bff.worker : void 0;
79
+ const getWorkerBundleReferences = (manifest)=>{
80
+ const effectBffWorker = getEffectBffWorker(manifest);
81
+ return [
82
+ ...effectBffWorker ? [
83
+ {
84
+ kind: 'effect-bff',
85
+ reference: effectBffWorker
86
+ }
87
+ ] : [],
88
+ ...getReferencedRouteWorkers(manifest).map((reference)=>({
89
+ kind: 'route',
90
+ reference
91
+ }))
92
+ ];
93
+ };
94
+ const resolveWorkerBundleReference = (issues, outputDirectory, reference, manifestPath)=>{
95
+ const workerRoot = node_path.resolve(outputDirectory, CLOUDFLARE_WORKER_BUNDLE_DIRECTORY);
96
+ const workerPath = node_path.resolve(outputDirectory, reference.reference);
97
+ const relativeToWorkerRoot = node_path.relative(workerRoot, workerPath);
98
+ if (node_path.isAbsolute(reference.reference) || relativeToWorkerRoot.startsWith('..') || node_path.isAbsolute(relativeToWorkerRoot)) {
99
+ addIssue(issues, {
100
+ code: 'invalid-manifest',
101
+ message: 'Cloudflare output manifest worker bundle references must stay under worker/.',
102
+ path: manifestPath
103
+ });
104
+ return null;
105
+ }
106
+ return {
107
+ ...reference,
108
+ path: workerPath
109
+ };
110
+ };
111
+ 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.';
112
+ const DELIVERY_UNIT_IDENTITY_FIELDS = [
113
+ 'unitId',
114
+ 'buildMarker',
115
+ 'sourceRevision'
116
+ ];
117
+ const verifyDeliveryUnitIdentity = (issues, manifest, manifestPath, declared)=>{
118
+ const stamped = manifest?.deliveryUnit;
119
+ const hasStamp = Boolean(stamped) && 'object' == typeof stamped;
120
+ if (declared) {
121
+ if (!hasStamp) return void addIssue(issues, {
122
+ code: 'missing-delivery-unit',
123
+ message: `Cloudflare worker manifest is missing the delivery-unit identity declared by the workspace topology (expected unitId ${declared.unitId}, buildMarker ${declared.buildMarker}).`,
124
+ path: manifestPath
125
+ });
126
+ for (const field of DELIVERY_UNIT_IDENTITY_FIELDS)assertEqual(issues, stamped[field], declared[field], {
127
+ code: 'delivery-unit-drift',
128
+ message: `Cloudflare worker manifest deliveryUnit.${field} must match the topology delivery-unit record (expected ${declared[field]}, received ${stamped[field] ?? 'undefined'}).`,
129
+ path: manifestPath
130
+ });
131
+ }
132
+ if (hasStamp && stamped.surfaces && 'object' == typeof stamped.surfaces) for (const surface of [
133
+ 'ui',
134
+ 'api'
135
+ ]){
136
+ const marker = stamped.surfaces[surface];
137
+ if (!marker || 'object' != typeof marker) {
138
+ addIssue(issues, {
139
+ code: 'missing-delivery-unit',
140
+ message: `Cloudflare worker manifest is missing the ${surface} delivery-unit surface marker.`,
141
+ path: manifestPath
142
+ });
143
+ continue;
144
+ }
145
+ for (const field of DELIVERY_UNIT_IDENTITY_FIELDS)assertEqual(issues, marker[field], stamped[field], {
146
+ code: 'delivery-unit-drift',
147
+ message: `Cloudflare worker manifest ${surface} surface deliveryUnit.${field} must derive from one delivery-unit record (expected ${stamped[field] ?? 'undefined'}, received ${marker[field] ?? 'undefined'}).`,
148
+ path: manifestPath
149
+ });
150
+ }
151
+ };
152
+ const verifyManifestShape = (issues, manifest, manifestPath)=>{
153
+ assertEqual(issues, manifest.runtime?.type, CLOUDFLARE_RUNTIME_TYPE, {
154
+ code: 'invalid-manifest',
155
+ message: 'Cloudflare output manifest runtime.type must be cloudflare-module-worker.',
156
+ path: manifestPath
157
+ });
158
+ assertEqual(issues, manifest.runtime?.entry, CLOUDFLARE_WORKER_ENTRY, {
159
+ code: 'invalid-manifest',
160
+ message: `Cloudflare output manifest runtime.entry must be ${CLOUDFLARE_WORKER_ENTRY}.`,
161
+ path: manifestPath
162
+ });
163
+ assertEqual(issues, manifest.runtime?.fetchExport, true, {
164
+ code: 'invalid-manifest',
165
+ message: 'Cloudflare output manifest runtime.fetchExport must be true.',
166
+ path: manifestPath
167
+ });
168
+ assertEqual(issues, manifest.runtime?.nodeListen, false, {
169
+ code: 'invalid-manifest',
170
+ message: 'Cloudflare output manifest runtime.nodeListen must be false.',
171
+ path: manifestPath
172
+ });
173
+ assertEqual(issues, manifest.assets?.binding, CLOUDFLARE_ASSETS_BINDING, {
174
+ code: 'invalid-manifest',
175
+ message: 'Cloudflare output manifest assets.binding must be ASSETS.',
176
+ path: manifestPath
177
+ });
178
+ assertEqual(issues, manifest.assets?.directory, `./${CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY}`, {
179
+ code: 'invalid-manifest',
180
+ message: 'Cloudflare output manifest assets.directory must be ./public.',
181
+ path: manifestPath
182
+ });
183
+ assertEqual(issues, manifest.assets?.runWorkerFirst, true, {
184
+ code: 'invalid-manifest',
185
+ message: 'Cloudflare output manifest assets.runWorkerFirst must be true.',
186
+ path: manifestPath
187
+ });
188
+ };
189
+ const walkFiles = async (root)=>{
190
+ if (!await pathExists(root)) return [];
191
+ const entries = await promises.readdir(root, {
192
+ withFileTypes: true
193
+ });
194
+ const files = [];
195
+ for (const entry of entries){
196
+ if (SOURCE_SCAN_IGNORED_DIRECTORY_NAMES.has(entry.name)) continue;
197
+ const entryPath = node_path.join(root, entry.name);
198
+ if (entry.isDirectory()) {
199
+ files.push(...await walkFiles(entryPath));
200
+ continue;
201
+ }
202
+ if (SOURCE_SCAN_FILE_PATTERN.test(entry.name)) files.push(entryPath);
203
+ }
204
+ return files;
205
+ };
206
+ const verifyCloudflareOutputMutationPolicy = async (options)=>{
207
+ const issues = [];
208
+ const files = (await Promise.all(options.scanRoots.map((scanRoot)=>walkFiles(node_path.resolve(scanRoot))))).flat();
209
+ for (const file of files){
210
+ const source = await promises.readFile(file, 'utf-8');
211
+ for (const forbidden of FORBIDDEN_MUTATION_PATTERNS)if (forbidden.pattern.test(source)) addIssue(issues, {
212
+ code: forbidden.code,
213
+ message: forbidden.message,
214
+ path: file
215
+ });
216
+ }
217
+ return {
218
+ ok: 0 === issues.length,
219
+ issues
220
+ };
221
+ };
222
+ const verifyWorkerImport = async (issues, entryPath)=>{
223
+ try {
224
+ const worker = (await import(`${pathToFileURL(entryPath).href}?t=${Date.now()}`)).default;
225
+ if (!worker || 'function' != typeof worker.fetch) addIssue(issues, {
226
+ code: 'worker-import-failed',
227
+ message: 'Cloudflare server entry must default-export a Worker with fetch.',
228
+ path: entryPath
229
+ });
230
+ } catch (error) {
231
+ addIssue(issues, {
232
+ code: 'worker-import-failed',
233
+ message: `Cloudflare server entry could not be imported: ${error instanceof Error ? error.message : String(error)}`,
234
+ path: entryPath
235
+ });
236
+ }
237
+ };
238
+ const verifyCloudflareOutput = async (options)=>{
239
+ const outputDirectory = node_path.resolve(options.outputDirectory);
240
+ const issues = [];
241
+ const outputPlan = createCloudflareOutputPlan(outputDirectory);
242
+ for (const relativePath of outputPlan.requiredFiles){
243
+ const filePath = node_path.join(outputDirectory, relativePath);
244
+ if (!await pathExists(filePath)) addIssue(issues, {
245
+ code: 'missing-file',
246
+ message: `Cloudflare output is missing ${relativePath}.`,
247
+ path: filePath
248
+ });
249
+ }
250
+ const manifestPath = outputPlan.paths.workerManifest;
251
+ const wranglerPath = outputPlan.paths.wranglerConfig;
252
+ const packagePath = outputPlan.paths.outputPackage;
253
+ const workerPackagePath = outputPlan.paths.workerPackage;
254
+ const manifest = await pathExists(manifestPath) ? await readJson(manifestPath) : void 0;
255
+ const wrangler = await pathExists(wranglerPath) ? await readJson(wranglerPath) : void 0;
256
+ const outputPackage = await pathExists(packagePath) ? await readJson(packagePath) : void 0;
257
+ const workerPackage = await pathExists(workerPackagePath) ? await readJson(workerPackagePath) : void 0;
258
+ if (manifest) {
259
+ verifyManifestShape(issues, manifest, manifestPath);
260
+ verifyDeliveryUnitIdentity(issues, manifest, manifestPath, options.deliveryUnit);
261
+ const workerReferences = getWorkerBundleReferences(manifest);
262
+ if (workerReferences.length > 0 && !await pathExists(workerPackagePath)) addIssue(issues, {
263
+ code: 'missing-file',
264
+ message: 'Cloudflare output is missing worker/package.json for referenced worker bundles.',
265
+ path: workerPackagePath
266
+ });
267
+ if (manifest.bff?.runtimeFramework === 'effect' && !getEffectBffWorker(manifest)) addIssue(issues, {
268
+ code: 'missing-worker-bundle',
269
+ message: 'Cloudflare Effect BFF manifest points to a missing worker bundle.'
270
+ });
271
+ for (const workerReference of workerReferences){
272
+ const resolvedWorker = resolveWorkerBundleReference(issues, outputDirectory, workerReference, manifestPath);
273
+ if (!resolvedWorker) continue;
274
+ const workerExists = await pathExists(resolvedWorker.path);
275
+ if (!workerExists) addIssue(issues, {
276
+ code: 'missing-worker-bundle',
277
+ message: missingWorkerBundleMessage(workerReference),
278
+ path: resolvedWorker.path
279
+ });
280
+ if (workerExists && 'effect-bff' === resolvedWorker.kind) {
281
+ const workerSource = await promises.readFile(resolvedWorker.path, 'utf-8');
282
+ if (workerSource.includes(';entityKind;') || workerSource.includes(';entityKind,entityKind;')) addIssue(issues, {
283
+ code: 'invalid-worker-bundle',
284
+ message: 'Cloudflare Effect BFF worker bundle contains invalid Drizzle entityKind marker references.',
285
+ path: resolvedWorker.path
286
+ });
287
+ }
288
+ }
289
+ }
290
+ if (wrangler) {
291
+ assertEqual(issues, wrangler.main, outputPlan.wrangler.main, {
292
+ code: 'invalid-wrangler',
293
+ message: `wrangler.json main must be ${outputPlan.wrangler.main}.`,
294
+ path: wranglerPath
295
+ });
296
+ assertEqual(issues, wrangler.assets?.binding, outputPlan.wrangler.assets.binding, {
297
+ code: 'invalid-wrangler',
298
+ message: 'wrangler.json assets.binding must be ASSETS.',
299
+ path: wranglerPath
300
+ });
301
+ assertEqual(issues, wrangler.assets?.directory, outputPlan.wrangler.assets.directory, {
302
+ code: 'invalid-wrangler',
303
+ message: 'wrangler.json assets.directory must be ./public.',
304
+ path: wranglerPath
305
+ });
306
+ assertEqual(issues, wrangler.assets?.run_worker_first, outputPlan.wrangler.assets.run_worker_first, {
307
+ code: 'invalid-wrangler',
308
+ message: 'wrangler.json assets.run_worker_first must be true.',
309
+ path: wranglerPath
310
+ });
311
+ for (const flag of outputPlan.wrangler.requiredCompatibilityFlags)assertFlag(issues, wrangler.compatibility_flags, flag, {
312
+ code: 'invalid-wrangler',
313
+ message: `wrangler.json compatibility_flags must include ${flag}.`,
314
+ path: wranglerPath
315
+ });
316
+ }
317
+ if (outputPackage) assertEqual(issues, outputPackage.type, outputPlan.packages.output.type, {
318
+ code: 'invalid-package',
319
+ message: '.output/package.json must declare type module.',
320
+ path: packagePath
321
+ });
322
+ if (workerPackage) assertEqual(issues, workerPackage.type, outputPlan.packages.worker.type, {
323
+ code: 'invalid-package',
324
+ message: '.output/worker/package.json must declare type commonjs.',
325
+ path: workerPackagePath
326
+ });
327
+ for (const leakedPath of outputPlan.publicLeakDirectories){
328
+ const publicPath = node_path.join(outputPlan.paths.publicAssets, leakedPath);
329
+ if (await pathExists(publicPath)) addIssue(issues, {
330
+ code: 'public-output-leak',
331
+ message: `Framework-owned ${leakedPath} output leaked into public assets.`,
332
+ path: publicPath
333
+ });
334
+ }
335
+ if (false !== options.importWorker && await pathExists(outputPlan.paths.workerEntry)) await verifyWorkerImport(issues, outputPlan.paths.workerEntry);
336
+ return {
337
+ ok: 0 === issues.length,
338
+ issues
339
+ };
340
+ };
341
+ const assertCloudflareOutput = async (options)=>{
342
+ const result = await verifyCloudflareOutput(options);
343
+ if (!result.ok) throw new Error([
344
+ 'Cloudflare output verification failed:',
345
+ ...result.issues.map((issue)=>`- ${issue.code}: ${issue.message}${issue.path ? ` (${issue.path})` : ''}`)
346
+ ].join('\n'));
347
+ };
348
+ export { assertCloudflareOutput, verifyCloudflareOutput, verifyCloudflareOutputMutationPolicy };