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

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 (116) hide show
  1. package/dist/cjs/plugins/backend-federation/codegen.js +208 -0
  2. package/dist/cjs/plugins/backend-federation/config.js +217 -0
  3. package/dist/cjs/plugins/backend-federation/emit.js +139 -0
  4. package/dist/cjs/plugins/backendFederationBuild.js +4 -298
  5. package/dist/cjs/plugins/deploy/platforms/cloudflare/artifacts.js +127 -0
  6. package/dist/cjs/plugins/deploy/platforms/cloudflare/constants.js +137 -0
  7. package/dist/cjs/plugins/deploy/platforms/cloudflare/delivery-unit.js +118 -0
  8. package/dist/cjs/plugins/deploy/platforms/cloudflare/i18n-worker.js +135 -0
  9. package/dist/cjs/plugins/deploy/platforms/cloudflare/index.js +126 -0
  10. package/dist/cjs/plugins/deploy/platforms/cloudflare/security-policies.js +223 -0
  11. package/dist/cjs/plugins/deploy/platforms/cloudflare/types.js +18 -0
  12. package/dist/cjs/plugins/deploy/platforms/cloudflare/utils.js +66 -0
  13. package/dist/cjs/plugins/deploy/platforms/cloudflare/worker-manifest.js +178 -0
  14. package/dist/cjs/plugins/deploy/platforms/cloudflare/wrangler-config.js +189 -0
  15. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/identity.js +83 -0
  16. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/index.js +184 -0
  17. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/issues.js +54 -0
  18. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/manifest.js +83 -0
  19. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.js +158 -0
  20. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.js +137 -0
  21. package/dist/cjs/plugins/deploy/platforms/cloudflare-output-verifier.js +23 -363
  22. package/dist/cjs/plugins/deploy/platforms/cloudflare.js +2 -745
  23. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
  24. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
  25. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
  26. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
  27. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
  28. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
  29. package/dist/esm/plugins/backend-federation/codegen.mjs +157 -0
  30. package/dist/esm/plugins/backend-federation/config.mjs +141 -0
  31. package/dist/esm/plugins/backend-federation/emit.mjs +88 -0
  32. package/dist/esm/plugins/backendFederationBuild.mjs +1 -296
  33. package/dist/esm/plugins/deploy/platforms/cloudflare/artifacts.mjs +64 -0
  34. package/dist/esm/plugins/deploy/platforms/cloudflare/constants.mjs +33 -0
  35. package/dist/esm/plugins/deploy/platforms/cloudflare/delivery-unit.mjs +67 -0
  36. package/dist/esm/plugins/deploy/platforms/cloudflare/i18n-worker.mjs +97 -0
  37. package/dist/esm/plugins/deploy/platforms/cloudflare/index.mjs +78 -0
  38. package/dist/esm/plugins/deploy/platforms/cloudflare/security-policies.mjs +167 -0
  39. package/dist/esm/plugins/deploy/platforms/cloudflare/types.mjs +0 -0
  40. package/dist/esm/plugins/deploy/platforms/cloudflare/utils.mjs +12 -0
  41. package/dist/esm/plugins/deploy/platforms/cloudflare/worker-manifest.mjs +118 -0
  42. package/dist/esm/plugins/deploy/platforms/cloudflare/wrangler-config.mjs +135 -0
  43. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/identity.mjs +45 -0
  44. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/index.mjs +129 -0
  45. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/issues.mjs +10 -0
  46. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/manifest.mjs +45 -0
  47. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.mjs +97 -0
  48. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.mjs +68 -0
  49. package/dist/esm/plugins/deploy/platforms/cloudflare-output-verifier.mjs +1 -347
  50. package/dist/esm/plugins/deploy/platforms/cloudflare.mjs +1 -735
  51. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
  52. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
  53. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
  54. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
  55. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
  56. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
  57. package/dist/esm-node/plugins/backend-federation/codegen.mjs +158 -0
  58. package/dist/esm-node/plugins/backend-federation/config.mjs +142 -0
  59. package/dist/esm-node/plugins/backend-federation/emit.mjs +89 -0
  60. package/dist/esm-node/plugins/backendFederationBuild.mjs +1 -296
  61. package/dist/esm-node/plugins/deploy/platforms/cloudflare/artifacts.mjs +65 -0
  62. package/dist/esm-node/plugins/deploy/platforms/cloudflare/constants.mjs +34 -0
  63. package/dist/esm-node/plugins/deploy/platforms/cloudflare/delivery-unit.mjs +68 -0
  64. package/dist/esm-node/plugins/deploy/platforms/cloudflare/i18n-worker.mjs +98 -0
  65. package/dist/esm-node/plugins/deploy/platforms/cloudflare/index.mjs +79 -0
  66. package/dist/esm-node/plugins/deploy/platforms/cloudflare/security-policies.mjs +168 -0
  67. package/dist/esm-node/plugins/deploy/platforms/cloudflare/types.mjs +1 -0
  68. package/dist/esm-node/plugins/deploy/platforms/cloudflare/utils.mjs +13 -0
  69. package/dist/esm-node/plugins/deploy/platforms/cloudflare/worker-manifest.mjs +119 -0
  70. package/dist/esm-node/plugins/deploy/platforms/cloudflare/wrangler-config.mjs +136 -0
  71. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/identity.mjs +46 -0
  72. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/index.mjs +130 -0
  73. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/issues.mjs +11 -0
  74. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/manifest.mjs +46 -0
  75. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.mjs +98 -0
  76. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.mjs +69 -0
  77. package/dist/esm-node/plugins/deploy/platforms/cloudflare-output-verifier.mjs +1 -347
  78. package/dist/esm-node/plugins/deploy/platforms/cloudflare.mjs +1 -735
  79. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.mjs +22 -0
  80. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.mjs +0 -0
  81. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.mjs +0 -0
  82. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.mjs +0 -0
  83. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.mjs +0 -0
  84. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.006-fetch-handler.mjs +32 -0
  85. package/dist/types/plugins/backend-federation/codegen.d.ts +82 -0
  86. package/dist/types/plugins/backend-federation/config.d.ts +103 -0
  87. package/dist/types/plugins/backend-federation/emit.d.ts +13 -0
  88. package/dist/types/plugins/backendFederationBuild.d.ts +2 -11
  89. package/dist/types/plugins/deploy/platforms/cloudflare/artifacts.d.ts +15 -0
  90. package/dist/types/plugins/deploy/platforms/cloudflare/constants.d.ts +23 -0
  91. package/dist/types/plugins/deploy/platforms/cloudflare/delivery-unit.d.ts +23 -0
  92. package/dist/types/plugins/deploy/platforms/cloudflare/i18n-worker.d.ts +21 -0
  93. package/dist/types/plugins/deploy/platforms/cloudflare/index.d.ts +2 -0
  94. package/dist/types/plugins/deploy/platforms/cloudflare/security-policies.d.ts +88 -0
  95. package/dist/types/plugins/deploy/platforms/cloudflare/types.d.ts +5 -0
  96. package/dist/types/plugins/deploy/platforms/cloudflare/utils.d.ts +6 -0
  97. package/dist/types/plugins/deploy/platforms/cloudflare/worker-manifest.d.ts +100 -0
  98. package/dist/types/plugins/deploy/platforms/cloudflare/wrangler-config.d.ts +39 -0
  99. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/identity.d.ts +4 -0
  100. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/index.d.ts +18 -0
  101. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/issues.d.ts +14 -0
  102. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/manifest.d.ts +2 -0
  103. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/mutation-policy.d.ts +13 -0
  104. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier/worker-bundles.d.ts +16 -0
  105. package/dist/types/plugins/deploy/platforms/cloudflare-output-verifier.d.ts +1 -32
  106. package/dist/types/plugins/deploy/platforms/cloudflare.d.ts +1 -2
  107. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.001-bootstrap-security.d.mts +1 -0
  108. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.002-assets-routes.d.mts +1 -0
  109. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.003-i18n-locales.d.mts +1 -0
  110. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.004-rendering-css.d.mts +1 -0
  111. package/dist/types/plugins/deploy/platforms/templates/cloudflare-entry.005-worker-dispatch.d.mts +1 -0
  112. package/package.json +11 -11
  113. package/dist/cjs/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -879
  114. package/dist/esm/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -879
  115. package/dist/esm-node/plugins/deploy/platforms/templates/cloudflare-entry.mjs +0 -879
  116. /package/dist/types/plugins/deploy/platforms/templates/{cloudflare-entry.d.mts → cloudflare-entry.006-fetch-handler.d.mts} +0 -0
@@ -0,0 +1,168 @@
1
+ import "node:module";
2
+ const DEFAULT_SECURITY_HEADERS = {
3
+ referrerPolicy: 'strict-origin-when-cross-origin',
4
+ contentTypeOptions: 'nosniff',
5
+ permissionsPolicy: 'camera=(), geolocation=(), microphone=(), payment=(), usb=()'
6
+ };
7
+ const DEFAULT_CORS_ALLOWED_METHODS = [
8
+ 'GET',
9
+ 'HEAD',
10
+ 'POST',
11
+ 'PUT',
12
+ 'PATCH',
13
+ 'DELETE',
14
+ 'OPTIONS'
15
+ ];
16
+ const DEFAULT_CSP_DIRECTIVES = {
17
+ 'base-uri': [
18
+ "'self'"
19
+ ],
20
+ 'connect-src': [
21
+ "'self'",
22
+ 'https:',
23
+ 'http:',
24
+ 'wss:',
25
+ 'ws:'
26
+ ],
27
+ 'default-src': [
28
+ "'self'"
29
+ ],
30
+ 'font-src': [
31
+ "'self'",
32
+ 'data:',
33
+ 'https:',
34
+ 'http:'
35
+ ],
36
+ 'form-action': [
37
+ "'self'"
38
+ ],
39
+ 'frame-ancestors': [
40
+ "'self'"
41
+ ],
42
+ 'img-src': [
43
+ "'self'",
44
+ 'data:',
45
+ 'blob:',
46
+ 'https:',
47
+ 'http:'
48
+ ],
49
+ 'manifest-src': [
50
+ "'self'",
51
+ 'https:',
52
+ 'http:'
53
+ ],
54
+ 'object-src': [
55
+ "'none'"
56
+ ],
57
+ "script-src": [
58
+ "'self'",
59
+ "'unsafe-inline'",
60
+ "'unsafe-eval'",
61
+ 'https:',
62
+ 'http:',
63
+ 'blob:'
64
+ ],
65
+ 'style-src': [
66
+ "'self'",
67
+ "'unsafe-inline'",
68
+ 'https:',
69
+ 'http:'
70
+ ],
71
+ 'worker-src': [
72
+ "'self'",
73
+ 'blob:'
74
+ ]
75
+ };
76
+ const normalizeDirectiveValues = (value)=>{
77
+ const values = Array.isArray(value) ? value : [
78
+ value
79
+ ];
80
+ return [
81
+ ...new Set(values.map((entry)=>entry.trim()).filter(Boolean))
82
+ ];
83
+ };
84
+ const appendDirectiveValues = (directives, name, values)=>{
85
+ if (!values?.length) return;
86
+ directives[name] = normalizeDirectiveValues([
87
+ ...directives[name] ?? [],
88
+ ...values
89
+ ]);
90
+ };
91
+ const createContentSecurityPolicy = (config)=>{
92
+ const mode = config?.mode ?? 'report-only';
93
+ if ('off' === mode) return {
94
+ mode,
95
+ directives: {},
96
+ reason: config?.reason
97
+ };
98
+ const directives = Object.fromEntries(Object.entries(DEFAULT_CSP_DIRECTIVES).map(([name, values])=>[
99
+ name,
100
+ [
101
+ ...values
102
+ ]
103
+ ]));
104
+ for (const [name, value] of Object.entries(config?.directives ?? {}))if (false === value) delete directives[name];
105
+ else directives[name] = normalizeDirectiveValues(value);
106
+ if (config?.frameAncestors === false) delete directives['frame-ancestors'];
107
+ else if (config?.frameAncestors) directives['frame-ancestors'] = normalizeDirectiveValues(config.frameAncestors);
108
+ appendDirectiveValues(directives, "script-src", config?.additionalScriptSrc);
109
+ appendDirectiveValues(directives, 'style-src', config?.additionalStyleSrc);
110
+ appendDirectiveValues(directives, 'connect-src', config?.additionalConnectSrc);
111
+ appendDirectiveValues(directives, 'img-src', config?.additionalImgSrc);
112
+ if (config?.reportUri) directives['report-uri'] = [
113
+ config.reportUri
114
+ ];
115
+ return {
116
+ mode,
117
+ directives,
118
+ reason: config?.reason
119
+ };
120
+ };
121
+ const createNoindexPolicy = (noindex)=>{
122
+ if (false === noindex) return {
123
+ workersDev: false,
124
+ localhost: false,
125
+ previewHostnames: []
126
+ };
127
+ if (true === noindex || void 0 === noindex) return {
128
+ workersDev: true,
129
+ localhost: true,
130
+ previewHostnames: []
131
+ };
132
+ return {
133
+ workersDev: noindex.workersDev ?? true,
134
+ localhost: noindex.localhost ?? true,
135
+ previewHostnames: noindex.previewHostnames ?? [],
136
+ reason: noindex.reason
137
+ };
138
+ };
139
+ const createCloudflareWorkerCorsPolicy = (cors)=>({
140
+ assets: cors?.assets ?? true,
141
+ allowedOrigins: normalizeDirectiveValues(cors?.allowedOrigins ?? []),
142
+ allowedMethods: cors?.allowedMethods?.length ? normalizeDirectiveValues(cors.allowedMethods.map((method)=>method.toUpperCase())) : DEFAULT_CORS_ALLOWED_METHODS,
143
+ allowedHeaders: cors?.allowedHeaders?.length ? normalizeDirectiveValues(cors.allowedHeaders) : [
144
+ '*'
145
+ ],
146
+ reason: cors?.reason
147
+ });
148
+ const createCloudflareWorkerSecurityPolicy = (modernConfig)=>{
149
+ const security = modernConfig.deploy?.worker?.security;
150
+ if (security?.enabled === false) return {
151
+ enabled: false,
152
+ cors: createCloudflareWorkerCorsPolicy(security.cors),
153
+ reason: security.reason
154
+ };
155
+ return {
156
+ enabled: true,
157
+ headers: {
158
+ referrerPolicy: security?.headers?.referrerPolicy ?? DEFAULT_SECURITY_HEADERS.referrerPolicy,
159
+ contentTypeOptions: security?.headers?.contentTypeOptions ?? DEFAULT_SECURITY_HEADERS.contentTypeOptions,
160
+ permissionsPolicy: security?.headers?.permissionsPolicy ?? DEFAULT_SECURITY_HEADERS.permissionsPolicy
161
+ },
162
+ contentSecurityPolicy: createContentSecurityPolicy(security?.contentSecurityPolicy),
163
+ noindex: createNoindexPolicy(security?.noindex),
164
+ cors: createCloudflareWorkerCorsPolicy(security?.cors),
165
+ reason: security?.reason
166
+ };
167
+ };
168
+ export { DEFAULT_CORS_ALLOWED_METHODS, DEFAULT_CSP_DIRECTIVES, DEFAULT_SECURITY_HEADERS, createCloudflareWorkerCorsPolicy, createCloudflareWorkerSecurityPolicy, createContentSecurityPolicy, createNoindexPolicy };
@@ -0,0 +1 @@
1
+ import "node:module";
@@ -0,0 +1,13 @@
1
+ import "node:module";
2
+ import node_path from "node:path";
3
+ const isRecord = (value)=>null !== value && 'object' == typeof value && !Array.isArray(value);
4
+ const normalizeRelativePath = (value, label, scope = 'app output', options = {})=>{
5
+ if ('string' != typeof value) throw new Error(`${label} must be a relative path inside the ${scope}.`);
6
+ const normalized = value.trim().replace(/\\/g, '/').replace(/^\.\/+/u, '').replace(/\/+$/u, '');
7
+ const segments = normalized.split('/');
8
+ const isRootDestination = '.' === normalized;
9
+ if (!normalized || node_path.isAbsolute(normalized) || !options.allowRoot && isRootDestination || segments.includes('..')) throw new Error(`${label} must be a relative path inside the ${scope}.`);
10
+ return normalized;
11
+ };
12
+ const isJsonRecord = (value)=>'object' == typeof value && null !== value && !Array.isArray(value);
13
+ export { isJsonRecord, isRecord, normalizeRelativePath };
@@ -0,0 +1,119 @@
1
+ import "node:module";
2
+ import node_path from "node:path";
3
+ import { fs } from "@modern-js/utils";
4
+ import { readRouteSpec } from "./artifacts.mjs";
5
+ import { ASSETS_BINDING, BFF_EFFECT_WORKER_ENTRY, CLOUDFLARE_RUNTIME_TYPE, CLOUDFLARE_WORKER_BUNDLE_FORMAT, DEFAULT_SERVER_ONLY_PUBLIC_ASSET_EXCLUDES, EFFECT_BFF_CLOUDFLARE_IMPORT_GUIDANCE, LOADABLE_STATS_FILE, PUBLIC_ASSETS_DIRECTORY, ROUTE_MANIFEST_FILE, ROUTE_SPEC_FILE, ROUTE_SPEC_OUTPUT, SERVER_BUNDLE_DIRECTORY, WORKER_BUNDLE_DIRECTORY, WORKER_ENTRY } from "./constants.mjs";
6
+ import { createI18nWorkerManifest } from "./i18n-worker.mjs";
7
+ import { createCloudflareWorkerSecurityPolicy } from "./security-policies.mjs";
8
+ import { normalizeRelativePath } from "./utils.mjs";
9
+ import { createWorkerManifestServiceBindings, createWorkerServiceBindings } from "./wrangler-config.mjs";
10
+ 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}`);
11
+ const createWorkerManifest = async (outputDirectory, modernConfig, appContext, deliveryUnitStamp)=>{
12
+ const routeSpec = await readRouteSpec(outputDirectory);
13
+ const routes = await Promise.all(routeSpec.routes.map(async (route)=>{
14
+ const worker = 'string' == typeof route.worker ? route.worker : void 0;
15
+ return {
16
+ urlPath: route.urlPath,
17
+ entryName: route.entryName,
18
+ entryPath: route.entryPath,
19
+ isSSR: Boolean(route.isSSR),
20
+ worker,
21
+ workerExists: worker ? await fs.pathExists(node_path.join(outputDirectory, worker)) : false
22
+ };
23
+ }));
24
+ const bffPrefix = modernConfig.bff?.prefix;
25
+ const primaryBffPrefix = Array.isArray(bffPrefix) ? bffPrefix[0] : bffPrefix;
26
+ const isEffectApi = Boolean(modernConfig.bff) && modernConfig.bff?.runtimeFramework !== 'hono';
27
+ const effectApiWorkerExists = await fs.pathExists(node_path.join(outputDirectory, BFF_EFFECT_WORKER_ENTRY));
28
+ const serviceBindings = createWorkerManifestServiceBindings(createWorkerServiceBindings(modernConfig, void 0));
29
+ if (isEffectApi && primaryBffPrefix && !effectApiWorkerExists) throw createMissingEffectBffWorkerError(outputDirectory, BFF_EFFECT_WORKER_ENTRY);
30
+ return {
31
+ version: 1,
32
+ runtime: {
33
+ type: CLOUDFLARE_RUNTIME_TYPE,
34
+ entry: WORKER_ENTRY,
35
+ fetchExport: true,
36
+ nodeListen: false
37
+ },
38
+ assets: {
39
+ binding: ASSETS_BINDING,
40
+ directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
41
+ runWorkerFirst: true
42
+ },
43
+ routeSpec: {
44
+ file: ROUTE_SPEC_OUTPUT,
45
+ routes
46
+ },
47
+ workerBundles: {
48
+ directory: WORKER_BUNDLE_DIRECTORY,
49
+ format: CLOUDFLARE_WORKER_BUNDLE_FORMAT,
50
+ importableFromModuleWorker: true,
51
+ requestHandlerExport: 'requestHandler'
52
+ },
53
+ resources: {
54
+ loadableStats: LOADABLE_STATS_FILE,
55
+ routeManifest: ROUTE_MANIFEST_FILE
56
+ },
57
+ security: createCloudflareWorkerSecurityPolicy(modernConfig),
58
+ ...deliveryUnitStamp ? {
59
+ deliveryUnit: deliveryUnitStamp
60
+ } : {},
61
+ i18n: createI18nWorkerManifest(routeSpec, appContext),
62
+ bff: isEffectApi && primaryBffPrefix && effectApiWorkerExists ? {
63
+ runtimeFramework: 'effect',
64
+ prefix: primaryBffPrefix,
65
+ worker: BFF_EFFECT_WORKER_ENTRY
66
+ } : void 0,
67
+ ...void 0 === serviceBindings ? {} : {
68
+ serviceBindings
69
+ }
70
+ };
71
+ };
72
+ const createWorkerModuleLoaders = (manifest)=>{
73
+ const imports = new Map();
74
+ for (const route of manifest.routeSpec.routes)if (route.worker && route.workerExists) {
75
+ const importPath = `../${String(route.worker).replace(/^\/+/u, '')}`;
76
+ imports.set(route.worker, `() => import(${JSON.stringify(importPath)})`);
77
+ }
78
+ if (manifest.bff?.worker) {
79
+ const importPath = `../${String(manifest.bff.worker).replace(/^\/+/u, '')}`;
80
+ imports.set(manifest.bff.worker, `() => import(${JSON.stringify(importPath)})`);
81
+ }
82
+ if (0 === imports.size) return '{}';
83
+ const loaders = [
84
+ ...imports.entries()
85
+ ].map(([worker, loader])=>`${JSON.stringify(worker)}: ${loader}`);
86
+ return `{\n${loaders.join(',\n')}\n}`;
87
+ };
88
+ const getPublicAssetExcludes = (appDirectory, modernConfig)=>[
89
+ ...DEFAULT_SERVER_ONLY_PUBLIC_ASSET_EXCLUDES.filter((directory)=>{
90
+ try {
91
+ return fs.statSync(node_path.join(appDirectory, directory)).isDirectory();
92
+ } catch {
93
+ return false;
94
+ }
95
+ }),
96
+ ...modernConfig.deploy?.worker?.publicAssetExcludes ?? []
97
+ ].map((entry)=>normalizeRelativePath(entry, 'deploy.worker.publicAssetExcludes'));
98
+ const shouldCopyToPublicAssets = (src, distDirectory, publicAssetExcludes)=>{
99
+ const relativePath = node_path.relative(distDirectory, src);
100
+ if (!relativePath) return true;
101
+ const normalizedRelativePath = relativePath.replace(/\\/g, '/');
102
+ const [topLevelDirectory] = normalizedRelativePath.split('/');
103
+ const basename = normalizedRelativePath.split('/').pop() ?? '';
104
+ return normalizedRelativePath !== ROUTE_SPEC_FILE && topLevelDirectory !== WORKER_BUNDLE_DIRECTORY && topLevelDirectory !== SERVER_BUNDLE_DIRECTORY && '.env' !== basename && !basename.startsWith('.env.') && !publicAssetExcludes.some((exclude)=>normalizedRelativePath === exclude || normalizedRelativePath.startsWith(`${exclude}/`));
105
+ };
106
+ const shouldCopyToWorkerBundle = (src, workerBundleDirectory)=>{
107
+ const relativePath = node_path.relative(workerBundleDirectory, src);
108
+ if (!relativePath) return true;
109
+ const normalizedRelativePath = relativePath.replace(/\\/g, '/');
110
+ const basename = node_path.basename(normalizedRelativePath);
111
+ if (basename.startsWith('.') || normalizedRelativePath.includes('/.')) return false;
112
+ if (fs.statSync(src).isDirectory()) return true;
113
+ return [
114
+ '.cjs',
115
+ '.js',
116
+ '.mjs'
117
+ ].includes(node_path.extname(normalizedRelativePath));
118
+ };
119
+ export { createWorkerManifest, createWorkerModuleLoaders, getPublicAssetExcludes, shouldCopyToPublicAssets, shouldCopyToWorkerBundle };
@@ -0,0 +1,136 @@
1
+ import "node:module";
2
+ import node_path from "node:path";
3
+ import { ASSETS_BINDING, COMPATIBILITY_DATE_PATTERN, DEFAULT_COMPATIBILITY_DATE, PUBLIC_ASSETS_DIRECTORY, REQUIRED_COMPATIBILITY_FLAGS, WORKER_ENTRY } from "./constants.mjs";
4
+ import { isJsonRecord, normalizeRelativePath } from "./utils.mjs";
5
+ const getCompatibilityDate = (modernConfig)=>{
6
+ const configuredDate = modernConfig.deploy?.worker?.compatibilityDate?.trim();
7
+ const compatibilityDate = configuredDate || DEFAULT_COMPATIBILITY_DATE;
8
+ if (!COMPATIBILITY_DATE_PATTERN.test(compatibilityDate)) throw new Error(`deploy.worker.compatibilityDate must use YYYY-MM-DD, received ${JSON.stringify(compatibilityDate)}.`);
9
+ return compatibilityDate;
10
+ };
11
+ const getWorkerName = (appDirectory)=>{
12
+ const basename = node_path.basename(appDirectory);
13
+ return basename.replace(/[^a-zA-Z0-9-_]/g, '-') || 'modern-cloudflare-worker';
14
+ };
15
+ const getConfiguredWorkerName = (appDirectory, modernConfig)=>{
16
+ const configuredName = modernConfig.deploy?.worker?.name?.trim();
17
+ return configuredName || getWorkerName(appDirectory);
18
+ };
19
+ const getConfiguredWrangler = (modernConfig)=>{
20
+ const wrangler = modernConfig.deploy?.worker?.wrangler;
21
+ if (void 0 === wrangler) return {};
22
+ if (!isJsonRecord(wrangler)) throw new Error('deploy.worker.wrangler must be a JSON object.');
23
+ return wrangler;
24
+ };
25
+ const createWranglerCompatibilityFlags = (configuredFlags)=>{
26
+ if (void 0 === configuredFlags) return [
27
+ ...REQUIRED_COMPATIBILITY_FLAGS
28
+ ];
29
+ if (!Array.isArray(configuredFlags) || configuredFlags.some((flag)=>'string' != typeof flag)) throw new Error('deploy.worker.wrangler.compatibility_flags must be an array of strings.');
30
+ return [
31
+ ...new Set([
32
+ ...configuredFlags,
33
+ ...REQUIRED_COMPATIBILITY_FLAGS
34
+ ])
35
+ ];
36
+ };
37
+ const createWranglerAssetsConfig = (configuredAssets)=>{
38
+ if (void 0 !== configuredAssets && !isJsonRecord(configuredAssets)) throw new Error('deploy.worker.wrangler.assets must be an object.');
39
+ return {
40
+ ...isJsonRecord(configuredAssets) ? configuredAssets : {},
41
+ directory: `./${PUBLIC_ASSETS_DIRECTORY}`,
42
+ binding: ASSETS_BINDING,
43
+ run_worker_first: true
44
+ };
45
+ };
46
+ const assertNonEmptyString = (value, label)=>{
47
+ if ('string' != typeof value || 0 === value.trim().length) throw new Error(`${label} must be a non-empty string.`);
48
+ return value.trim();
49
+ };
50
+ const normalizeD1Database = (database, index)=>{
51
+ const binding = assertNonEmptyString(database.binding, `deploy.worker.d1Databases[${index}].binding`);
52
+ const databaseName = assertNonEmptyString(database.databaseName, `deploy.worker.d1Databases[${index}].databaseName`);
53
+ const databaseId = assertNonEmptyString(database.databaseId, `deploy.worker.d1Databases[${index}].databaseId`);
54
+ const migrationsDir = void 0 === database.migrationsDir ? void 0 : normalizeRelativePath(database.migrationsDir, `deploy.worker.d1Databases[${index}].migrationsDir`, 'app root');
55
+ const previewDatabaseId = void 0 === database.previewDatabaseId ? void 0 : assertNonEmptyString(database.previewDatabaseId, `deploy.worker.d1Databases[${index}].previewDatabaseId`);
56
+ return {
57
+ binding,
58
+ database_name: databaseName,
59
+ database_id: databaseId,
60
+ ...void 0 === migrationsDir ? {} : {
61
+ migrations_dir: migrationsDir
62
+ },
63
+ ...void 0 === previewDatabaseId ? {} : {
64
+ preview_database_id: previewDatabaseId
65
+ },
66
+ ...void 0 === database.remote ? {} : {
67
+ remote: database.remote
68
+ }
69
+ };
70
+ };
71
+ const createWranglerD1Databases = (modernConfig, configuredWranglerD1)=>{
72
+ const d1Databases = modernConfig.deploy?.worker?.d1Databases;
73
+ if (void 0 === d1Databases) return configuredWranglerD1;
74
+ if (void 0 !== configuredWranglerD1) throw new Error('Use deploy.worker.d1Databases or deploy.worker.wrangler.d1_databases, not both.');
75
+ if (!Array.isArray(d1Databases)) throw new Error('deploy.worker.d1Databases must be an array.');
76
+ return d1Databases.map(normalizeD1Database);
77
+ };
78
+ const normalizeServiceBinding = (service, index)=>{
79
+ const binding = assertNonEmptyString(service.binding, `deploy.worker.services[${index}].binding`);
80
+ const serviceName = assertNonEmptyString(service.service, `deploy.worker.services[${index}].service`);
81
+ const prefix = void 0 === service.prefix ? void 0 : assertNonEmptyString(service.prefix, `deploy.worker.services[${index}].prefix`);
82
+ return {
83
+ binding,
84
+ service: serviceName,
85
+ ...void 0 === prefix ? {} : {
86
+ prefix
87
+ }
88
+ };
89
+ };
90
+ const createWorkerServiceBindings = (modernConfig, configuredWranglerServices)=>{
91
+ const services = modernConfig.deploy?.worker?.services;
92
+ if (void 0 === services) return configuredWranglerServices;
93
+ if (void 0 !== configuredWranglerServices) throw new Error('Use deploy.worker.services or deploy.worker.wrangler.services, not both.');
94
+ if (!Array.isArray(services)) throw new Error('deploy.worker.services must be an array.');
95
+ return services.map(normalizeServiceBinding);
96
+ };
97
+ const createWranglerServices = (serviceBindings)=>{
98
+ if (!Array.isArray(serviceBindings)) return serviceBindings;
99
+ if (!serviceBindings.every(isJsonRecord)) return serviceBindings;
100
+ return serviceBindings.map((service)=>{
101
+ const { prefix, ...wranglerService } = service;
102
+ return wranglerService;
103
+ });
104
+ };
105
+ const createWorkerManifestServiceBindings = (serviceBindings)=>{
106
+ if (!Array.isArray(serviceBindings)) return;
107
+ const dispatchBindings = serviceBindings.filter((service)=>isJsonRecord(service) && 'string' == typeof service.binding && 'string' == typeof service.service && 'string' == typeof service.prefix).map((service)=>({
108
+ binding: service.binding,
109
+ service: service.service,
110
+ prefix: service.prefix,
111
+ interface: 'fetch'
112
+ }));
113
+ return dispatchBindings.length > 0 ? dispatchBindings : void 0;
114
+ };
115
+ const createWranglerConfig = (appDirectory, modernConfig)=>{
116
+ const wrangler = getConfiguredWrangler(modernConfig);
117
+ const d1Databases = createWranglerD1Databases(modernConfig, wrangler.d1_databases);
118
+ const serviceBindings = createWorkerServiceBindings(modernConfig, wrangler.services);
119
+ const wranglerServices = createWranglerServices(serviceBindings);
120
+ return {
121
+ $schema: 'node_modules/wrangler/config-schema.json',
122
+ name: getConfiguredWorkerName(appDirectory, modernConfig),
123
+ compatibility_date: getCompatibilityDate(modernConfig),
124
+ ...wrangler,
125
+ main: WORKER_ENTRY,
126
+ compatibility_flags: createWranglerCompatibilityFlags(wrangler.compatibility_flags),
127
+ assets: createWranglerAssetsConfig(wrangler.assets),
128
+ ...void 0 === d1Databases ? {} : {
129
+ d1_databases: d1Databases
130
+ },
131
+ ...void 0 === wranglerServices ? {} : {
132
+ services: wranglerServices
133
+ }
134
+ };
135
+ };
136
+ export { createWorkerManifestServiceBindings, createWorkerServiceBindings, createWranglerConfig };
@@ -0,0 +1,46 @@
1
+ import "node:module";
2
+ import { DELIVERY_UNIT_IDENTITY_FIELDS, deliveryUnitIdentityFieldValue } from "@modern-js/utils/universal";
3
+ import { addIssue, assertEqual } from "./issues.mjs";
4
+ const verifyDeliveryUnitIdentity = (issues, manifest, manifestPath, declared)=>{
5
+ const stamped = manifest?.deliveryUnit;
6
+ const hasStamp = Boolean(stamped) && 'object' == typeof stamped;
7
+ if (declared) {
8
+ if (!hasStamp) return void addIssue(issues, {
9
+ code: 'missing-delivery-unit',
10
+ message: `Cloudflare worker manifest is missing the delivery-unit identity declared by the workspace topology (expected unitId ${declared.unitId}, buildMarker ${declared.buildMarker}).`,
11
+ path: manifestPath
12
+ });
13
+ for (const field of DELIVERY_UNIT_IDENTITY_FIELDS){
14
+ const stampedValue = deliveryUnitIdentityFieldValue(stamped, field);
15
+ assertEqual(issues, stampedValue, declared[field], {
16
+ code: 'delivery-unit-drift',
17
+ message: `Cloudflare worker manifest deliveryUnit.${field} must match the topology delivery-unit record (expected ${declared[field]}, received ${stampedValue ?? 'undefined'}).`,
18
+ path: manifestPath
19
+ });
20
+ }
21
+ }
22
+ if (hasStamp && stamped.surfaces && 'object' == typeof stamped.surfaces) for (const surface of [
23
+ 'ui',
24
+ 'api'
25
+ ]){
26
+ const marker = stamped.surfaces[surface];
27
+ if (!marker || 'object' != typeof marker) {
28
+ addIssue(issues, {
29
+ code: 'missing-delivery-unit',
30
+ message: `Cloudflare worker manifest is missing the ${surface} delivery-unit surface marker.`,
31
+ path: manifestPath
32
+ });
33
+ continue;
34
+ }
35
+ for (const field of DELIVERY_UNIT_IDENTITY_FIELDS){
36
+ const markerValue = deliveryUnitIdentityFieldValue(marker, field);
37
+ const stampedValue = deliveryUnitIdentityFieldValue(stamped, field);
38
+ assertEqual(issues, markerValue, stampedValue, {
39
+ code: 'delivery-unit-drift',
40
+ message: `Cloudflare worker manifest ${surface} surface deliveryUnit.${field} must derive from one delivery-unit record (expected ${stampedValue ?? 'undefined'}, received ${markerValue ?? 'undefined'}).`,
41
+ path: manifestPath
42
+ });
43
+ }
44
+ }
45
+ };
46
+ export { verifyDeliveryUnitIdentity };
@@ -0,0 +1,130 @@
1
+ import "node:module";
2
+ import promises from "node:fs/promises";
3
+ import node_path from "node:path";
4
+ import { createCloudflareOutputPlan } from "../cloudflare-output-plan.mjs";
5
+ import { verifyDeliveryUnitIdentity } from "./identity.mjs";
6
+ import { addIssue, assertEqual, assertFlag } from "./issues.mjs";
7
+ import { verifyManifestShape } from "./manifest.mjs";
8
+ import { getEffectBffWorker, getWorkerBundleReferences, missingWorkerBundleMessage, resolveWorkerBundleReference, verifyWorkerBundleReferences, verifyWorkerImport } from "./worker-bundles.mjs";
9
+ const pathExists = async (filePath)=>{
10
+ try {
11
+ await promises.access(filePath);
12
+ return true;
13
+ } catch {
14
+ return false;
15
+ }
16
+ };
17
+ const readJson = async (filePath)=>JSON.parse(await promises.readFile(filePath, 'utf-8'));
18
+ const verifyCloudflareOutput = async (options)=>{
19
+ const outputDirectory = node_path.resolve(options.outputDirectory);
20
+ const issues = [];
21
+ const outputPlan = createCloudflareOutputPlan(outputDirectory);
22
+ for (const relativePath of outputPlan.requiredFiles){
23
+ const filePath = node_path.join(outputDirectory, relativePath);
24
+ if (!await pathExists(filePath)) addIssue(issues, {
25
+ code: 'missing-file',
26
+ message: `Cloudflare output is missing ${relativePath}.`,
27
+ path: filePath
28
+ });
29
+ }
30
+ const manifestPath = outputPlan.paths.workerManifest;
31
+ const wranglerPath = outputPlan.paths.wranglerConfig;
32
+ const packagePath = outputPlan.paths.outputPackage;
33
+ const workerPackagePath = outputPlan.paths.workerPackage;
34
+ const manifest = await pathExists(manifestPath) ? await readJson(manifestPath) : void 0;
35
+ const wrangler = await pathExists(wranglerPath) ? await readJson(wranglerPath) : void 0;
36
+ const outputPackage = await pathExists(packagePath) ? await readJson(packagePath) : void 0;
37
+ const workerPackage = await pathExists(workerPackagePath) ? await readJson(workerPackagePath) : void 0;
38
+ if (manifest) {
39
+ verifyManifestShape(issues, manifest, manifestPath);
40
+ verifyDeliveryUnitIdentity(issues, manifest, manifestPath, options.deliveryUnit);
41
+ const workerReferences = getWorkerBundleReferences(manifest);
42
+ if (workerReferences.length > 0 && !await pathExists(workerPackagePath)) addIssue(issues, {
43
+ code: 'missing-file',
44
+ message: 'Cloudflare output is missing worker/package.json for referenced worker bundles.',
45
+ path: workerPackagePath
46
+ });
47
+ if (manifest.bff?.runtimeFramework === 'effect' && !getEffectBffWorker(manifest)) addIssue(issues, {
48
+ code: 'missing-worker-bundle',
49
+ message: 'Cloudflare Effect BFF manifest points to a missing worker bundle.'
50
+ });
51
+ for (const workerReference of workerReferences){
52
+ const resolvedWorker = resolveWorkerBundleReference(issues, outputDirectory, workerReference, manifestPath);
53
+ if (!resolvedWorker) continue;
54
+ const workerExists = await pathExists(resolvedWorker.path);
55
+ if (!workerExists) addIssue(issues, {
56
+ code: 'missing-worker-bundle',
57
+ message: missingWorkerBundleMessage(workerReference),
58
+ path: resolvedWorker.path
59
+ });
60
+ if (workerExists) {
61
+ const workerSource = await promises.readFile(resolvedWorker.path, 'utf-8');
62
+ verifyWorkerBundleReferences(issues, resolvedWorker, workerSource);
63
+ if ('effect-bff' === resolvedWorker.kind && (workerSource.includes(';entityKind;') || workerSource.includes(';entityKind,entityKind;'))) addIssue(issues, {
64
+ code: 'invalid-worker-bundle',
65
+ message: 'Cloudflare Effect BFF worker bundle contains invalid Drizzle entityKind marker references.',
66
+ path: resolvedWorker.path
67
+ });
68
+ }
69
+ }
70
+ }
71
+ if (wrangler) {
72
+ assertEqual(issues, wrangler.main, outputPlan.wrangler.main, {
73
+ code: 'invalid-wrangler',
74
+ message: `wrangler.json main must be ${outputPlan.wrangler.main}.`,
75
+ path: wranglerPath
76
+ });
77
+ assertEqual(issues, wrangler.assets?.binding, outputPlan.wrangler.assets.binding, {
78
+ code: 'invalid-wrangler',
79
+ message: 'wrangler.json assets.binding must be ASSETS.',
80
+ path: wranglerPath
81
+ });
82
+ assertEqual(issues, wrangler.assets?.directory, outputPlan.wrangler.assets.directory, {
83
+ code: 'invalid-wrangler',
84
+ message: 'wrangler.json assets.directory must be ./public.',
85
+ path: wranglerPath
86
+ });
87
+ assertEqual(issues, wrangler.assets?.run_worker_first, outputPlan.wrangler.assets.run_worker_first, {
88
+ code: 'invalid-wrangler',
89
+ message: 'wrangler.json assets.run_worker_first must be true.',
90
+ path: wranglerPath
91
+ });
92
+ for (const flag of outputPlan.wrangler.requiredCompatibilityFlags)assertFlag(issues, wrangler.compatibility_flags, flag, {
93
+ code: 'invalid-wrangler',
94
+ message: `wrangler.json compatibility_flags must include ${flag}.`,
95
+ path: wranglerPath
96
+ });
97
+ }
98
+ if (outputPackage) assertEqual(issues, outputPackage.type, outputPlan.packages.output.type, {
99
+ code: 'invalid-package',
100
+ message: '.output/package.json must declare type module.',
101
+ path: packagePath
102
+ });
103
+ if (workerPackage) assertEqual(issues, workerPackage.type, outputPlan.packages.worker.type, {
104
+ code: 'invalid-package',
105
+ message: '.output/worker/package.json must declare type commonjs.',
106
+ path: workerPackagePath
107
+ });
108
+ for (const leakedPath of outputPlan.publicLeakDirectories){
109
+ const publicPath = node_path.join(outputPlan.paths.publicAssets, leakedPath);
110
+ if (await pathExists(publicPath)) addIssue(issues, {
111
+ code: 'public-output-leak',
112
+ message: `Framework-owned ${leakedPath} output leaked into public assets.`,
113
+ path: publicPath
114
+ });
115
+ }
116
+ if (false !== options.importWorker && await pathExists(outputPlan.paths.workerEntry)) await verifyWorkerImport(issues, outputPlan.paths.workerEntry);
117
+ return {
118
+ ok: 0 === issues.length,
119
+ issues
120
+ };
121
+ };
122
+ const assertCloudflareOutput = async (options)=>{
123
+ const result = await verifyCloudflareOutput(options);
124
+ if (!result.ok) throw new Error([
125
+ 'Cloudflare output verification failed:',
126
+ ...result.issues.map((issue)=>`- ${issue.code}: ${issue.message}${issue.path ? ` (${issue.path})` : ''}`)
127
+ ].join('\n'));
128
+ };
129
+ export { verifyCloudflareOutputMutationPolicy } from "./mutation-policy.mjs";
130
+ export { assertCloudflareOutput, verifyCloudflareOutput };
@@ -0,0 +1,11 @@
1
+ import "node:module";
2
+ const addIssue = (issues, issue)=>{
3
+ issues.push(issue);
4
+ };
5
+ const assertEqual = (issues, actual, expected, issue)=>{
6
+ if (actual !== expected) addIssue(issues, issue);
7
+ };
8
+ const assertFlag = (issues, flags, flag, issue)=>{
9
+ if (!Array.isArray(flags) || !flags.includes(flag)) addIssue(issues, issue);
10
+ };
11
+ export { addIssue, assertEqual, assertFlag };