@bleedingdev/modern-js-app-tools 3.5.0-ultramodern.4 → 3.5.0-ultramodern.41

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