@bleedingdev/modern-js-app-tools-extensions 3.8.3-ultramodern.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/cjs/cloudflare/artifacts.js +127 -0
- package/dist/cjs/cloudflare/constants.js +141 -0
- package/dist/cjs/cloudflare/delivery-unit.js +161 -0
- package/dist/cjs/cloudflare/i18n-worker.js +135 -0
- package/dist/cjs/cloudflare/index.js +156 -0
- package/dist/cjs/cloudflare/security-policies.js +205 -0
- package/dist/cjs/cloudflare/types.js +18 -0
- package/dist/cjs/cloudflare/utils.js +66 -0
- package/dist/cjs/cloudflare/worker-manifest.js +239 -0
- package/dist/cjs/cloudflare/wrangler-config.js +205 -0
- package/dist/cjs/cloudflare-builder.js +394 -0
- package/dist/cjs/cloudflare-output-contract.js +108 -0
- package/dist/cjs/cloudflare-output-plan.js +93 -0
- package/dist/cjs/cloudflare-output-verifier/identity.js +92 -0
- package/dist/cjs/cloudflare-output-verifier/index.js +188 -0
- package/dist/cjs/cloudflare-output-verifier/issues.js +54 -0
- package/dist/cjs/cloudflare-output-verifier/manifest.js +89 -0
- package/dist/cjs/cloudflare-output-verifier/mutation-policy.js +151 -0
- package/dist/cjs/cloudflare-output-verifier/worker-bundles.js +378 -0
- package/dist/cjs/config.js +18 -0
- package/dist/cjs/css-extract-runtime-plugin.js +74 -0
- package/dist/cjs/index.js +125 -0
- package/dist/cjs/read-template.js +56 -0
- package/dist/cjs/release-envelope/canonical.js +68 -0
- package/dist/cjs/release-envelope/framework-output.js +649 -0
- package/dist/cjs/release-envelope/index.js +399 -0
- package/dist/cjs/release-envelope/plugin.js +102 -0
- package/dist/cjs/release-envelope/types.js +51 -0
- package/dist/cjs/release-identity.js +110 -0
- package/dist/cjs/templates/cloudflare-entry.001-bootstrap-security.mjs +251 -0
- package/dist/cjs/templates/cloudflare-entry.002-assets-routes.mjs +191 -0
- package/dist/cjs/templates/cloudflare-entry.003-i18n-locales.mjs +514 -0
- package/dist/cjs/templates/cloudflare-entry.004-rendering-css.mjs +1194 -0
- package/dist/cjs/templates/cloudflare-entry.005-worker-dispatch.mjs +421 -0
- package/dist/cjs/templates/cloudflare-entry.006-fetch-handler.mjs +93 -0
- package/dist/cjs/templates/cloudflare-worker-fs-promises.mjs +11 -0
- package/dist/cjs/templates/cloudflare-worker-loadable-server.mjs +281 -0
- package/dist/cjs/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +14 -0
- package/dist/cjs/templates/cloudflare-worker-path.mjs +98 -0
- package/dist/esm/cloudflare/artifacts.mjs +64 -0
- package/dist/esm/cloudflare/constants.mjs +34 -0
- package/dist/esm/cloudflare/delivery-unit.mjs +110 -0
- package/dist/esm/cloudflare/i18n-worker.mjs +97 -0
- package/dist/esm/cloudflare/index.mjs +108 -0
- package/dist/esm/cloudflare/security-policies.mjs +167 -0
- package/dist/esm/cloudflare/types.mjs +0 -0
- package/dist/esm/cloudflare/utils.mjs +12 -0
- package/dist/esm/cloudflare/worker-manifest.mjs +179 -0
- package/dist/esm/cloudflare/wrangler-config.mjs +151 -0
- package/dist/esm/cloudflare-builder.mjs +327 -0
- package/dist/esm/cloudflare-output-contract.mjs +31 -0
- package/dist/esm/cloudflare-output-plan.mjs +45 -0
- package/dist/esm/cloudflare-output-verifier/identity.mjs +54 -0
- package/dist/esm/cloudflare-output-verifier/index.mjs +133 -0
- package/dist/esm/cloudflare-output-verifier/issues.mjs +10 -0
- package/dist/esm/cloudflare-output-verifier/manifest.mjs +51 -0
- package/dist/esm/cloudflare-output-verifier/mutation-policy.mjs +99 -0
- package/dist/esm/cloudflare-output-verifier/worker-bundles.mjs +313 -0
- package/dist/esm/config.mjs +0 -0
- package/dist/esm/css-extract-runtime-plugin.mjs +32 -0
- package/dist/esm/index.mjs +10 -0
- package/dist/esm/read-template.mjs +5 -0
- package/dist/esm/release-envelope/canonical.mjs +21 -0
- package/dist/esm/release-envelope/framework-output.mjs +573 -0
- package/dist/esm/release-envelope/index.mjs +335 -0
- package/dist/esm/release-envelope/plugin.mjs +54 -0
- package/dist/esm/release-envelope/types.mjs +7 -0
- package/dist/esm/release-identity.mjs +66 -0
- package/dist/esm/templates/cloudflare-entry.001-bootstrap-security.mjs +251 -0
- package/dist/esm/templates/cloudflare-entry.002-assets-routes.mjs +191 -0
- package/dist/esm/templates/cloudflare-entry.003-i18n-locales.mjs +514 -0
- package/dist/esm/templates/cloudflare-entry.004-rendering-css.mjs +1194 -0
- package/dist/esm/templates/cloudflare-entry.005-worker-dispatch.mjs +421 -0
- package/dist/esm/templates/cloudflare-entry.006-fetch-handler.mjs +93 -0
- package/dist/esm/templates/cloudflare-worker-fs-promises.mjs +11 -0
- package/dist/esm/templates/cloudflare-worker-loadable-server.mjs +281 -0
- package/dist/esm/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +14 -0
- package/dist/esm/templates/cloudflare-worker-path.mjs +98 -0
- package/dist/esm-node/cloudflare/artifacts.mjs +65 -0
- package/dist/esm-node/cloudflare/constants.mjs +35 -0
- package/dist/esm-node/cloudflare/delivery-unit.mjs +111 -0
- package/dist/esm-node/cloudflare/i18n-worker.mjs +98 -0
- package/dist/esm-node/cloudflare/index.mjs +109 -0
- package/dist/esm-node/cloudflare/security-policies.mjs +168 -0
- package/dist/esm-node/cloudflare/types.mjs +1 -0
- package/dist/esm-node/cloudflare/utils.mjs +13 -0
- package/dist/esm-node/cloudflare/worker-manifest.mjs +180 -0
- package/dist/esm-node/cloudflare/wrangler-config.mjs +152 -0
- package/dist/esm-node/cloudflare-builder.mjs +328 -0
- package/dist/esm-node/cloudflare-output-contract.mjs +32 -0
- package/dist/esm-node/cloudflare-output-plan.mjs +46 -0
- package/dist/esm-node/cloudflare-output-verifier/identity.mjs +55 -0
- package/dist/esm-node/cloudflare-output-verifier/index.mjs +134 -0
- package/dist/esm-node/cloudflare-output-verifier/issues.mjs +11 -0
- package/dist/esm-node/cloudflare-output-verifier/manifest.mjs +52 -0
- package/dist/esm-node/cloudflare-output-verifier/mutation-policy.mjs +100 -0
- package/dist/esm-node/cloudflare-output-verifier/worker-bundles.mjs +314 -0
- package/dist/esm-node/config.mjs +1 -0
- package/dist/esm-node/css-extract-runtime-plugin.mjs +33 -0
- package/dist/esm-node/index.mjs +11 -0
- package/dist/esm-node/read-template.mjs +9 -0
- package/dist/esm-node/release-envelope/canonical.mjs +22 -0
- package/dist/esm-node/release-envelope/framework-output.mjs +574 -0
- package/dist/esm-node/release-envelope/index.mjs +336 -0
- package/dist/esm-node/release-envelope/plugin.mjs +55 -0
- package/dist/esm-node/release-envelope/types.mjs +8 -0
- package/dist/esm-node/release-identity.mjs +67 -0
- package/dist/esm-node/templates/cloudflare-entry.001-bootstrap-security.mjs +251 -0
- package/dist/esm-node/templates/cloudflare-entry.002-assets-routes.mjs +191 -0
- package/dist/esm-node/templates/cloudflare-entry.003-i18n-locales.mjs +514 -0
- package/dist/esm-node/templates/cloudflare-entry.004-rendering-css.mjs +1194 -0
- package/dist/esm-node/templates/cloudflare-entry.005-worker-dispatch.mjs +421 -0
- package/dist/esm-node/templates/cloudflare-entry.006-fetch-handler.mjs +93 -0
- package/dist/esm-node/templates/cloudflare-worker-fs-promises.mjs +11 -0
- package/dist/esm-node/templates/cloudflare-worker-loadable-server.mjs +281 -0
- package/dist/esm-node/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +14 -0
- package/dist/esm-node/templates/cloudflare-worker-path.mjs +98 -0
- package/dist/types/cloudflare/artifacts.d.ts +15 -0
- package/dist/types/cloudflare/constants.d.ts +24 -0
- package/dist/types/cloudflare/delivery-unit.d.ts +23 -0
- package/dist/types/cloudflare/i18n-worker.d.ts +22 -0
- package/dist/types/cloudflare/index.d.ts +2 -0
- package/dist/types/cloudflare/security-policies.d.ts +52 -0
- package/dist/types/cloudflare/types.d.ts +28 -0
- package/dist/types/cloudflare/utils.d.ts +6 -0
- package/dist/types/cloudflare/worker-manifest.d.ts +149 -0
- package/dist/types/cloudflare/wrangler-config.d.ts +51 -0
- package/dist/types/cloudflare-builder.d.ts +59 -0
- package/dist/types/cloudflare-output-contract.d.ts +14 -0
- package/dist/types/cloudflare-output-plan.d.ts +34 -0
- package/dist/types/cloudflare-output-verifier/identity.d.ts +8 -0
- package/dist/types/cloudflare-output-verifier/index.d.ts +18 -0
- package/dist/types/cloudflare-output-verifier/issues.d.ts +14 -0
- package/dist/types/cloudflare-output-verifier/manifest.d.ts +2 -0
- package/dist/types/cloudflare-output-verifier/mutation-policy.d.ts +14 -0
- package/dist/types/cloudflare-output-verifier/worker-bundles.d.ts +20 -0
- package/dist/types/config.d.ts +175 -0
- package/dist/types/css-extract-runtime-plugin.d.ts +6 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/read-template.d.ts +2 -0
- package/dist/types/release-envelope/canonical.d.ts +5 -0
- package/dist/types/release-envelope/framework-output.d.ts +32 -0
- package/dist/types/release-envelope/index.d.ts +6 -0
- package/dist/types/release-envelope/plugin.d.ts +30 -0
- package/dist/types/release-envelope/types.d.ts +62 -0
- package/dist/types/release-identity.d.ts +16 -0
- package/dist/types/templates/cloudflare-entry.001-bootstrap-security.d.mts +1 -0
- package/dist/types/templates/cloudflare-entry.002-assets-routes.d.mts +1 -0
- package/dist/types/templates/cloudflare-entry.003-i18n-locales.d.mts +1 -0
- package/dist/types/templates/cloudflare-entry.004-rendering-css.d.mts +1 -0
- package/dist/types/templates/cloudflare-entry.005-worker-dispatch.d.mts +1 -0
- package/dist/types/templates/cloudflare-entry.006-fetch-handler.d.mts +4 -0
- package/dist/types/templates/cloudflare-worker-fs-promises.d.mts +5 -0
- package/dist/types/templates/cloudflare-worker-loadable-server.d.mts +48 -0
- package/dist/types/templates/cloudflare-worker-mf-ssr-runtime-plugin.d.mts +13 -0
- package/dist/types/templates/cloudflare-worker-path.d.mts +21 -0
- package/package.json +131 -0
- package/src/cloudflare/artifacts.ts +165 -0
- package/src/cloudflare/constants.ts +50 -0
- package/src/cloudflare/delivery-unit.ts +195 -0
- package/src/cloudflare/i18n-worker.ts +197 -0
- package/src/cloudflare/index.ts +259 -0
- package/src/cloudflare/security-policies.ts +194 -0
- package/src/cloudflare/types.ts +33 -0
- package/src/cloudflare/utils.ts +40 -0
- package/src/cloudflare/worker-manifest.ts +313 -0
- package/src/cloudflare/wrangler-config.ts +338 -0
- package/src/cloudflare-builder.ts +653 -0
- package/src/cloudflare-output-contract.ts +30 -0
- package/src/cloudflare-output-plan.ts +108 -0
- package/src/cloudflare-output-verifier/identity.ts +87 -0
- package/src/cloudflare-output-verifier/index.ts +277 -0
- package/src/cloudflare-output-verifier/issues.ts +54 -0
- package/src/cloudflare-output-verifier/manifest.ts +76 -0
- package/src/cloudflare-output-verifier/mutation-policy.ts +159 -0
- package/src/cloudflare-output-verifier/worker-bundles.ts +664 -0
- package/src/config.ts +196 -0
- package/src/css-extract-runtime-plugin.ts +70 -0
- package/src/index.ts +10 -0
- package/src/read-template.ts +8 -0
- package/src/release-envelope/canonical.ts +58 -0
- package/src/release-envelope/framework-output.ts +1121 -0
- package/src/release-envelope/index.ts +656 -0
- package/src/release-envelope/plugin.ts +125 -0
- package/src/release-envelope/types.ts +83 -0
- package/src/release-identity.ts +134 -0
- package/src/templates/cloudflare-entry.001-bootstrap-security.mjs +251 -0
- package/src/templates/cloudflare-entry.002-assets-routes.mjs +191 -0
- package/src/templates/cloudflare-entry.003-i18n-locales.mjs +514 -0
- package/src/templates/cloudflare-entry.004-rendering-css.mjs +1194 -0
- package/src/templates/cloudflare-entry.005-worker-dispatch.mjs +421 -0
- package/src/templates/cloudflare-entry.006-fetch-handler.mjs +93 -0
- package/src/templates/cloudflare-worker-fs-promises.mjs +11 -0
- package/src/templates/cloudflare-worker-loadable-server.mjs +281 -0
- package/src/templates/cloudflare-worker-mf-ssr-runtime-plugin.mjs +14 -0
- package/src/templates/cloudflare-worker-path.mjs +98 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import promises from "node:fs/promises";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { createCloudflareOutputPlan } from "../cloudflare-output-plan.mjs";
|
|
4
|
+
import { verifyDeliveryUnitIdentity } from "./identity.mjs";
|
|
5
|
+
import { addIssue, assertEqual, assertFlag } from "./issues.mjs";
|
|
6
|
+
import { verifyManifestShape } from "./manifest.mjs";
|
|
7
|
+
import { getEffectBffWorker, getWorkerBundleReferences, missingWorkerBundleMessage, resolveWorkerBundleReference, verifyWorkerBundleReferences, verifyWorkerImport } from "./worker-bundles.mjs";
|
|
8
|
+
const pathExists = async (filePath)=>{
|
|
9
|
+
try {
|
|
10
|
+
await promises.access(filePath);
|
|
11
|
+
return true;
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const readJson = async (filePath)=>JSON.parse(await promises.readFile(filePath, 'utf-8'));
|
|
17
|
+
const verifyCloudflareOutput = async (options)=>{
|
|
18
|
+
const outputDirectory = node_path.resolve(options.outputDirectory);
|
|
19
|
+
const issues = [];
|
|
20
|
+
const outputPlan = createCloudflareOutputPlan(outputDirectory);
|
|
21
|
+
for (const relativePath of outputPlan.requiredFiles){
|
|
22
|
+
const filePath = node_path.join(outputDirectory, relativePath);
|
|
23
|
+
if (!await pathExists(filePath)) addIssue(issues, {
|
|
24
|
+
code: 'missing-file',
|
|
25
|
+
message: `Cloudflare output is missing ${relativePath}.`,
|
|
26
|
+
path: filePath
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
const manifestPath = outputPlan.paths.workerManifest;
|
|
30
|
+
const wranglerPath = outputPlan.paths.wranglerConfig;
|
|
31
|
+
const packagePath = outputPlan.paths.outputPackage;
|
|
32
|
+
const workerPackagePath = outputPlan.paths.workerPackage;
|
|
33
|
+
const manifest = await pathExists(manifestPath) ? await readJson(manifestPath) : void 0;
|
|
34
|
+
const wrangler = await pathExists(wranglerPath) ? await readJson(wranglerPath) : void 0;
|
|
35
|
+
const outputPackage = await pathExists(packagePath) ? await readJson(packagePath) : void 0;
|
|
36
|
+
const workerPackage = await pathExists(workerPackagePath) ? await readJson(workerPackagePath) : void 0;
|
|
37
|
+
if (manifest) {
|
|
38
|
+
verifyManifestShape(issues, manifest, manifestPath);
|
|
39
|
+
verifyDeliveryUnitIdentity(issues, manifest, manifestPath, options.deliveryUnit);
|
|
40
|
+
const workerReferences = getWorkerBundleReferences(manifest);
|
|
41
|
+
const providedWorkerPackages = new Set(workerPackage?.dependencies && 'object' == typeof workerPackage.dependencies ? Object.keys(workerPackage.dependencies) : []);
|
|
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
|
+
await verifyWorkerBundleReferences(issues, resolvedWorker, workerSource, {
|
|
63
|
+
providedPackages: providedWorkerPackages,
|
|
64
|
+
workerRoot: outputPlan.paths.workerBundle
|
|
65
|
+
});
|
|
66
|
+
if ('effect-bff' === resolvedWorker.kind && (workerSource.includes(';entityKind;') || workerSource.includes(';entityKind,entityKind;'))) addIssue(issues, {
|
|
67
|
+
code: 'invalid-worker-bundle',
|
|
68
|
+
message: 'Cloudflare Effect BFF worker bundle contains invalid Drizzle entityKind marker references.',
|
|
69
|
+
path: resolvedWorker.path
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (wrangler) {
|
|
75
|
+
assertEqual(issues, wrangler.main, outputPlan.wrangler.main, {
|
|
76
|
+
code: 'invalid-wrangler',
|
|
77
|
+
message: `wrangler.json main must be ${outputPlan.wrangler.main}.`,
|
|
78
|
+
path: wranglerPath
|
|
79
|
+
});
|
|
80
|
+
assertEqual(issues, wrangler.assets?.binding, outputPlan.wrangler.assets.binding, {
|
|
81
|
+
code: 'invalid-wrangler',
|
|
82
|
+
message: 'wrangler.json assets.binding must be ASSETS.',
|
|
83
|
+
path: wranglerPath
|
|
84
|
+
});
|
|
85
|
+
assertEqual(issues, wrangler.assets?.directory, outputPlan.wrangler.assets.directory, {
|
|
86
|
+
code: 'invalid-wrangler',
|
|
87
|
+
message: 'wrangler.json assets.directory must be ./public.',
|
|
88
|
+
path: wranglerPath
|
|
89
|
+
});
|
|
90
|
+
assertEqual(issues, wrangler.assets?.run_worker_first, outputPlan.wrangler.assets.run_worker_first, {
|
|
91
|
+
code: 'invalid-wrangler',
|
|
92
|
+
message: 'wrangler.json assets.run_worker_first must be true.',
|
|
93
|
+
path: wranglerPath
|
|
94
|
+
});
|
|
95
|
+
for (const flag of outputPlan.wrangler.requiredCompatibilityFlags)assertFlag(issues, wrangler.compatibility_flags, flag, {
|
|
96
|
+
code: 'invalid-wrangler',
|
|
97
|
+
message: `wrangler.json compatibility_flags must include ${flag}.`,
|
|
98
|
+
path: wranglerPath
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (outputPackage) assertEqual(issues, outputPackage.type, outputPlan.packages.output.type, {
|
|
102
|
+
code: 'invalid-package',
|
|
103
|
+
message: '.output/package.json must declare type module.',
|
|
104
|
+
path: packagePath
|
|
105
|
+
});
|
|
106
|
+
if (workerPackage) assertEqual(issues, workerPackage.type, outputPlan.packages.worker.type, {
|
|
107
|
+
code: 'invalid-package',
|
|
108
|
+
message: '.output/worker/package.json must declare type commonjs.',
|
|
109
|
+
path: workerPackagePath
|
|
110
|
+
});
|
|
111
|
+
for (const leakedPath of outputPlan.publicLeakDirectories){
|
|
112
|
+
const publicPath = node_path.join(outputPlan.paths.publicAssets, leakedPath);
|
|
113
|
+
if (await pathExists(publicPath)) addIssue(issues, {
|
|
114
|
+
code: 'public-output-leak',
|
|
115
|
+
message: `Framework-owned ${leakedPath} output leaked into public assets.`,
|
|
116
|
+
path: publicPath
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
if (false !== options.importWorker && await pathExists(outputPlan.paths.workerEntry)) await verifyWorkerImport(issues, outputPlan.paths.workerEntry, manifest);
|
|
120
|
+
return {
|
|
121
|
+
ok: 0 === issues.length,
|
|
122
|
+
issues
|
|
123
|
+
};
|
|
124
|
+
};
|
|
125
|
+
const assertCloudflareOutput = async (options)=>{
|
|
126
|
+
const result = await verifyCloudflareOutput(options);
|
|
127
|
+
if (!result.ok) throw new Error([
|
|
128
|
+
'Cloudflare output verification failed:',
|
|
129
|
+
...result.issues.map((issue)=>`- ${issue.code}: ${issue.message}${issue.path ? ` (${issue.path})` : ''}`)
|
|
130
|
+
].join('\n'));
|
|
131
|
+
};
|
|
132
|
+
export { verifyCloudflareOutputMutationPolicy } from "./mutation-policy.mjs";
|
|
133
|
+
export { assertCloudflareOutput, verifyCloudflareOutput };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const addIssue = (issues, issue)=>{
|
|
2
|
+
issues.push(issue);
|
|
3
|
+
};
|
|
4
|
+
const assertEqual = (issues, actual, expected, issue)=>{
|
|
5
|
+
if (actual !== expected) addIssue(issues, issue);
|
|
6
|
+
};
|
|
7
|
+
const assertFlag = (issues, flags, flag, issue)=>{
|
|
8
|
+
if (!Array.isArray(flags) || !flags.includes(flag)) addIssue(issues, issue);
|
|
9
|
+
};
|
|
10
|
+
export { addIssue, assertEqual, assertFlag };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { BFF_EFFECT_WORKER_DISPATCHER_EXPORT } from "../cloudflare/constants.mjs";
|
|
2
|
+
import { CLOUDFLARE_ASSETS_BINDING, CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY, CLOUDFLARE_RUNTIME_TYPE, CLOUDFLARE_WORKER_ENTRY } from "../cloudflare-output-contract.mjs";
|
|
3
|
+
import { addIssue, assertEqual } from "./issues.mjs";
|
|
4
|
+
const verifyManifestShape = (issues, manifest, manifestPath)=>{
|
|
5
|
+
assertEqual(issues, manifest.runtime?.type, CLOUDFLARE_RUNTIME_TYPE, {
|
|
6
|
+
code: 'invalid-manifest',
|
|
7
|
+
message: 'Cloudflare output manifest runtime.type must be cloudflare-module-worker.',
|
|
8
|
+
path: manifestPath
|
|
9
|
+
});
|
|
10
|
+
assertEqual(issues, manifest.runtime?.entry, CLOUDFLARE_WORKER_ENTRY, {
|
|
11
|
+
code: 'invalid-manifest',
|
|
12
|
+
message: `Cloudflare output manifest runtime.entry must be ${CLOUDFLARE_WORKER_ENTRY}.`,
|
|
13
|
+
path: manifestPath
|
|
14
|
+
});
|
|
15
|
+
assertEqual(issues, manifest.runtime?.fetchExport, true, {
|
|
16
|
+
code: 'invalid-manifest',
|
|
17
|
+
message: 'Cloudflare output manifest runtime.fetchExport must be true.',
|
|
18
|
+
path: manifestPath
|
|
19
|
+
});
|
|
20
|
+
assertEqual(issues, manifest.runtime?.nodeListen, false, {
|
|
21
|
+
code: 'invalid-manifest',
|
|
22
|
+
message: 'Cloudflare output manifest runtime.nodeListen must be false.',
|
|
23
|
+
path: manifestPath
|
|
24
|
+
});
|
|
25
|
+
assertEqual(issues, manifest.assets?.binding, CLOUDFLARE_ASSETS_BINDING, {
|
|
26
|
+
code: 'invalid-manifest',
|
|
27
|
+
message: 'Cloudflare output manifest assets.binding must be ASSETS.',
|
|
28
|
+
path: manifestPath
|
|
29
|
+
});
|
|
30
|
+
assertEqual(issues, manifest.assets?.directory, `./${CLOUDFLARE_PUBLIC_ASSETS_DIRECTORY}`, {
|
|
31
|
+
code: 'invalid-manifest',
|
|
32
|
+
message: 'Cloudflare output manifest assets.directory must be ./public.',
|
|
33
|
+
path: manifestPath
|
|
34
|
+
});
|
|
35
|
+
assertEqual(issues, manifest.assets?.runWorkerFirst, true, {
|
|
36
|
+
code: 'invalid-manifest',
|
|
37
|
+
message: 'Cloudflare output manifest assets.runWorkerFirst must be true.',
|
|
38
|
+
path: manifestPath
|
|
39
|
+
});
|
|
40
|
+
if (!Array.isArray(manifest.routeSpec?.routes)) addIssue(issues, {
|
|
41
|
+
code: 'invalid-manifest',
|
|
42
|
+
message: 'Cloudflare output manifest routeSpec.routes must be array.',
|
|
43
|
+
path: manifestPath
|
|
44
|
+
});
|
|
45
|
+
if (manifest.bff?.runtimeFramework === 'effect') assertEqual(issues, manifest.bff.dispatcherExport, BFF_EFFECT_WORKER_DISPATCHER_EXPORT, {
|
|
46
|
+
code: 'invalid-manifest',
|
|
47
|
+
message: `Cloudflare Effect BFF manifest dispatcherExport must be ${BFF_EFFECT_WORKER_DISPATCHER_EXPORT}.`,
|
|
48
|
+
path: manifestPath
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
export { verifyManifestShape };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import promises from "node:fs/promises";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { addIssue } from "./issues.mjs";
|
|
4
|
+
const SOURCE_SCAN_FILE_PATTERN = /\.(?:[cm]?[jt]s|json)$/u;
|
|
5
|
+
const SOURCE_SCAN_IGNORED_DIRECTORY_NAMES = new Set([
|
|
6
|
+
'.codex',
|
|
7
|
+
'.fastcontext',
|
|
8
|
+
'.git',
|
|
9
|
+
'.mf',
|
|
10
|
+
'.modernjs',
|
|
11
|
+
'.output',
|
|
12
|
+
'coverage',
|
|
13
|
+
'dist',
|
|
14
|
+
'dist-cloudflare',
|
|
15
|
+
'node_modules',
|
|
16
|
+
'repos',
|
|
17
|
+
'topology'
|
|
18
|
+
]);
|
|
19
|
+
const FORBIDDEN_MUTATION_PATTERNS = [
|
|
20
|
+
{
|
|
21
|
+
code: 'forbidden-mutation-pattern',
|
|
22
|
+
message: "Generated Cloudflare server worker output must not be rewritten by app scripts.",
|
|
23
|
+
pattern: /\.output\/server\/index\.mjs/u
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
code: 'forbidden-mutation-pattern',
|
|
27
|
+
message: "Generated Cloudflare server worker output must not be rewritten by app scripts.",
|
|
28
|
+
pattern: /path\.(?:join|resolve)\(\s*['"`]\.output['"`]\s*,\s*['"`]server['"`]\s*,\s*['"`]index\.mjs['"`]\s*\)/u
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
code: 'forbidden-mutation-pattern',
|
|
32
|
+
message: "Generated Cloudflare BFF worker bundles must not be rewritten by app scripts.",
|
|
33
|
+
pattern: /\.output\/worker\/[^'"`]*\.(?:mjs|cjs|js)/u
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
code: 'forbidden-mutation-pattern',
|
|
37
|
+
message: "Generated Cloudflare BFF worker bundles must not be rewritten by app scripts.",
|
|
38
|
+
pattern: /path\.(?:join|resolve)\(\s*['"`]\.output['"`]\s*,\s*['"`]worker['"`]\s*,\s*['"`][^'"`]+\.(?:mjs|cjs|js)['"`]\s*\)/u
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
code: 'forbidden-mutation-pattern',
|
|
42
|
+
message: "Drizzle entityKind worker bundle markers must be fixed in the framework toolchain, not post-build scripts.",
|
|
43
|
+
pattern: /replaceAll\(\s*['"`];entityKind(?:,entityKind)?;/u
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
code: 'forbidden-mutation-pattern',
|
|
47
|
+
message: "Effect BFF Cloudflare dispatch must not depend on duck-typed runtime helper probing in app scripts.",
|
|
48
|
+
pattern: /(?:typeof\s+[^;\n]*dispatchEffectBffRequest|['"`]dispatchEffectBffRequest['"`]\s+in\s+|\.dispatchEffectBffRequest\b)/u
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
code: 'forbidden-mutation-pattern',
|
|
52
|
+
message: "Effect BFF Cloudflare dispatch must not branch on handler.length in app scripts.",
|
|
53
|
+
pattern: /\bhandler\s*\.\s*length\b/u
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
const pathExists = async (filePath)=>{
|
|
57
|
+
try {
|
|
58
|
+
await promises.access(filePath);
|
|
59
|
+
return true;
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const walkFiles = async (root)=>{
|
|
65
|
+
if (!await pathExists(root)) return [];
|
|
66
|
+
const entries = await promises.readdir(root, {
|
|
67
|
+
withFileTypes: true
|
|
68
|
+
});
|
|
69
|
+
const files = [];
|
|
70
|
+
for (const entry of entries){
|
|
71
|
+
if (SOURCE_SCAN_IGNORED_DIRECTORY_NAMES.has(entry.name)) continue;
|
|
72
|
+
const entryPath = node_path.join(root, entry.name);
|
|
73
|
+
if (entry.isDirectory()) {
|
|
74
|
+
files.push(...await walkFiles(entryPath));
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (SOURCE_SCAN_FILE_PATTERN.test(entry.name)) files.push(entryPath);
|
|
78
|
+
}
|
|
79
|
+
return files;
|
|
80
|
+
};
|
|
81
|
+
const verifyCloudflareOutputMutationPolicy = async (options)=>{
|
|
82
|
+
const issues = [];
|
|
83
|
+
const excludedFiles = new Set((options.excludePaths ?? []).map((entry)=>node_path.resolve(entry)));
|
|
84
|
+
const files = (await Promise.all(options.scanRoots.map((scanRoot)=>walkFiles(node_path.resolve(scanRoot))))).flat();
|
|
85
|
+
for (const file of files){
|
|
86
|
+
if (excludedFiles.has(file)) continue;
|
|
87
|
+
const source = await promises.readFile(file, 'utf-8');
|
|
88
|
+
for (const forbidden of FORBIDDEN_MUTATION_PATTERNS)if (forbidden.pattern.test(source)) addIssue(issues, {
|
|
89
|
+
code: forbidden.code,
|
|
90
|
+
message: forbidden.message,
|
|
91
|
+
path: file
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
ok: 0 === issues.length,
|
|
96
|
+
issues
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
export { verifyCloudflareOutputMutationPolicy, walkFiles };
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import promises from "node:fs/promises";
|
|
2
|
+
import node_path from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { isDeepStrictEqual } from "node:util";
|
|
5
|
+
import { parse } from "@babel/parser";
|
|
6
|
+
import traverse from "@babel/traverse";
|
|
7
|
+
import { CLOUDFLARE_WORKER_BUNDLE_DIRECTORY, CLOUDFLARE_WORKER_NODE_BUILTINS } from "../cloudflare-output-contract.mjs";
|
|
8
|
+
import { addIssue } from "./issues.mjs";
|
|
9
|
+
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) : [];
|
|
10
|
+
const getEffectBffWorker = (manifest)=>manifest?.bff?.runtimeFramework === 'effect' && 'string' == typeof manifest.bff.worker ? manifest.bff.worker : void 0;
|
|
11
|
+
const getWorkerBundleReferences = (manifest)=>{
|
|
12
|
+
const effectBffWorker = getEffectBffWorker(manifest);
|
|
13
|
+
return [
|
|
14
|
+
...effectBffWorker ? [
|
|
15
|
+
{
|
|
16
|
+
dispatcherExport: 'string' == typeof manifest.bff?.dispatcherExport ? manifest.bff.dispatcherExport : void 0,
|
|
17
|
+
kind: 'effect-bff',
|
|
18
|
+
reference: effectBffWorker
|
|
19
|
+
}
|
|
20
|
+
] : [],
|
|
21
|
+
...getReferencedRouteWorkers(manifest).map((reference)=>({
|
|
22
|
+
kind: 'route',
|
|
23
|
+
reference
|
|
24
|
+
}))
|
|
25
|
+
];
|
|
26
|
+
};
|
|
27
|
+
const resolveWorkerBundleReference = (issues, outputDirectory, reference, manifestPath)=>{
|
|
28
|
+
const workerRoot = node_path.resolve(outputDirectory, CLOUDFLARE_WORKER_BUNDLE_DIRECTORY);
|
|
29
|
+
const workerPath = node_path.resolve(outputDirectory, reference.reference);
|
|
30
|
+
const relativeToWorkerRoot = node_path.relative(workerRoot, workerPath);
|
|
31
|
+
if (node_path.isAbsolute(reference.reference) || relativeToWorkerRoot.startsWith('..') || node_path.isAbsolute(relativeToWorkerRoot)) {
|
|
32
|
+
addIssue(issues, {
|
|
33
|
+
code: 'invalid-manifest',
|
|
34
|
+
message: 'Cloudflare output manifest worker bundle references must stay under worker/.',
|
|
35
|
+
path: manifestPath
|
|
36
|
+
});
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
...reference,
|
|
41
|
+
path: workerPath
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
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.';
|
|
45
|
+
const isAstNode = (value)=>null !== value && 'object' == typeof value && 'type' in value && 'string' == typeof value.type;
|
|
46
|
+
const getNodeName = (node)=>{
|
|
47
|
+
if (!isAstNode(node)) return;
|
|
48
|
+
return 'Identifier' === node.type && 'string' == typeof node.name ? node.name : 'StringLiteral' === node.type && 'string' == typeof node.value ? node.value : void 0;
|
|
49
|
+
};
|
|
50
|
+
const getStringLiteralValue = (node)=>isAstNode(node) && 'StringLiteral' === node.type && 'string' == typeof node.value ? node.value : void 0;
|
|
51
|
+
const collectBindingNames = (node, names)=>{
|
|
52
|
+
if (!isAstNode(node)) return;
|
|
53
|
+
const name = getNodeName(node);
|
|
54
|
+
if (name) return void names.add(name);
|
|
55
|
+
if ('ObjectPattern' === node.type && Array.isArray(node.properties)) {
|
|
56
|
+
for (const property of node.properties)if (isAstNode(property)) collectBindingNames('RestElement' === property.type ? property.argument : property.value, names);
|
|
57
|
+
} else if ('ArrayPattern' === node.type && Array.isArray(node.elements)) for (const element of node.elements)collectBindingNames(element, names);
|
|
58
|
+
else if ('AssignmentPattern' === node.type || 'RestElement' === node.type) collectBindingNames('AssignmentPattern' === node.type ? node.left : node.argument, names);
|
|
59
|
+
};
|
|
60
|
+
const getMemberPropertyName = (node)=>{
|
|
61
|
+
if ('MemberExpression' !== node.type) return;
|
|
62
|
+
return true === node.computed ? getStringLiteralValue(node.property) : getNodeName(node.property);
|
|
63
|
+
};
|
|
64
|
+
const isAmbientModuleRequire = (node, modulePath)=>isAstNode(node) && 'MemberExpression' === node.type && isAstNode(node.object) && 'Identifier' === node.object.type && 'module' === node.object.name && void 0 === modulePath.scope.getBinding('module') && 'require' === getMemberPropertyName(node);
|
|
65
|
+
const isModuleExports = (node)=>isAstNode(node) && 'MemberExpression' === node.type && isAstNode(node.object) && 'Identifier' === node.object.type && 'module' === node.object.name && 'exports' === getMemberPropertyName(node);
|
|
66
|
+
const collectCommonJsExports = (statement, exports)=>{
|
|
67
|
+
if ('ExpressionStatement' !== statement.type || !isAstNode(statement.expression) || 'AssignmentExpression' !== statement.expression.type) return;
|
|
68
|
+
const { left, right } = statement.expression;
|
|
69
|
+
if (isAstNode(left) && 'MemberExpression' === left.type && (isAstNode(left.object) && 'Identifier' === left.object.type && 'exports' === left.object.name || isModuleExports(left.object))) {
|
|
70
|
+
const exportName = getMemberPropertyName(left);
|
|
71
|
+
if (exportName) exports.add(exportName);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (isModuleExports(left) && isAstNode(right) && 'ObjectExpression' === right.type && Array.isArray(right.properties)) {
|
|
75
|
+
for (const property of right.properties)if (isAstNode(property)) {
|
|
76
|
+
const exportName = getNodeName(property.key);
|
|
77
|
+
if (exportName) exports.add(exportName);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
const collectEsmExports = (statement, exports)=>{
|
|
82
|
+
if ('ExportDefaultDeclaration' === statement.type) return void exports.add('default');
|
|
83
|
+
if ('ExportNamedDeclaration' !== statement.type) return;
|
|
84
|
+
if (isAstNode(statement.declaration)) {
|
|
85
|
+
const declaration = statement.declaration;
|
|
86
|
+
const declarationName = getNodeName(declaration.id);
|
|
87
|
+
if (declarationName) exports.add(declarationName);
|
|
88
|
+
if ('VariableDeclaration' === declaration.type && Array.isArray(declaration.declarations)) {
|
|
89
|
+
for (const declarator of declaration.declarations)if (isAstNode(declarator)) collectBindingNames(declarator.id, exports);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (Array.isArray(statement.specifiers)) {
|
|
93
|
+
for (const specifier of statement.specifiers)if (isAstNode(specifier)) {
|
|
94
|
+
const exportName = getNodeName(specifier.exported);
|
|
95
|
+
if (exportName) exports.add(exportName);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
const getStaticImport = (modulePath)=>{
|
|
100
|
+
const node = modulePath.node;
|
|
101
|
+
if (('ImportDeclaration' === node.type || 'ExportNamedDeclaration' === node.type || 'ExportAllDeclaration' === node.type) && isAstNode(node.source)) return getStringLiteralValue(node.source);
|
|
102
|
+
if ('ImportExpression' === node.type && isAstNode(node.source)) return getStringLiteralValue(node.source);
|
|
103
|
+
if ('CallExpression' === node.type && Array.isArray(node.arguments) && 1 === node.arguments.length && isAstNode(node.callee) && ('Identifier' === node.callee.type && 'require' === node.callee.name && void 0 === modulePath.scope.getBinding('require') || isAmbientModuleRequire(node.callee, modulePath) || 'Import' === node.callee.type)) return getStringLiteralValue(node.arguments[0]);
|
|
104
|
+
};
|
|
105
|
+
const isAmbientLoaderWrite = (modulePath)=>{
|
|
106
|
+
let targetPath = modulePath;
|
|
107
|
+
while(true){
|
|
108
|
+
const parentPath = targetPath.parentPath;
|
|
109
|
+
if (null === parentPath) return false;
|
|
110
|
+
if (parentPath.isAssignmentExpression()) return 'left' === targetPath.key;
|
|
111
|
+
if (parentPath.isUpdateExpression()) return 'argument' === targetPath.key;
|
|
112
|
+
if (parentPath.isForInStatement() || parentPath.isForOfStatement()) return 'left' === targetPath.key;
|
|
113
|
+
if (parentPath.isObjectProperty()) {
|
|
114
|
+
if ('value' !== targetPath.key) return false;
|
|
115
|
+
targetPath = parentPath;
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (parentPath.isObjectPattern() || parentPath.isArrayPattern() || parentPath.isRestElement() || parentPath.isAssignmentPattern()) {
|
|
119
|
+
targetPath = parentPath;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const analyzeWorkerModule = (source)=>{
|
|
126
|
+
const ast = parse(source, {
|
|
127
|
+
sourceType: 'unambiguous'
|
|
128
|
+
});
|
|
129
|
+
const exports = new Set();
|
|
130
|
+
const imports = new Set();
|
|
131
|
+
let unsafeCommonJsLoaderUsage = false;
|
|
132
|
+
let unsafeDynamicImport = false;
|
|
133
|
+
for (const statement of ast.program.body){
|
|
134
|
+
collectEsmExports(statement, exports);
|
|
135
|
+
collectCommonJsExports(statement, exports);
|
|
136
|
+
}
|
|
137
|
+
traverse(ast, {
|
|
138
|
+
enter (modulePath) {
|
|
139
|
+
const node = modulePath.node;
|
|
140
|
+
if (('ImportExpression' === node.type || 'CallExpression' === node.type && isAstNode(node.callee) && 'Import' === node.callee.type) && void 0 === getStaticImport(modulePath)) unsafeDynamicImport = true;
|
|
141
|
+
if (modulePath.isReferencedIdentifier({
|
|
142
|
+
name: 'require'
|
|
143
|
+
}) && void 0 === modulePath.scope.getBinding('require')) {
|
|
144
|
+
const parentPath = modulePath.parentPath;
|
|
145
|
+
const isStaticRequireCall = parentPath?.isCallExpression() && 'callee' === modulePath.key && void 0 !== getStaticImport(parentPath);
|
|
146
|
+
const isAvailabilityCheck = parentPath?.isUnaryExpression({
|
|
147
|
+
operator: 'typeof'
|
|
148
|
+
});
|
|
149
|
+
if (!isStaticRequireCall && !isAvailabilityCheck) unsafeCommonJsLoaderUsage = true;
|
|
150
|
+
}
|
|
151
|
+
if ('Identifier' === node.type && isAmbientLoaderWrite(modulePath) && ('require' === node.name || 'module' === node.name) && void 0 === modulePath.scope.getBinding(node.name)) unsafeCommonJsLoaderUsage = true;
|
|
152
|
+
if (modulePath.isReferencedIdentifier({
|
|
153
|
+
name: 'module'
|
|
154
|
+
}) && void 0 === modulePath.scope.getBinding('module')) {
|
|
155
|
+
const parentPath = modulePath.parentPath;
|
|
156
|
+
const isExportsAccess = null !== parentPath && isModuleExports(parentPath.node);
|
|
157
|
+
const isStaticModuleRequireCall = null !== parentPath && isAmbientModuleRequire(parentPath.node, parentPath) && parentPath.parentPath?.isCallExpression() === true && 'callee' === parentPath.key && void 0 !== getStaticImport(parentPath.parentPath);
|
|
158
|
+
if (!isExportsAccess && !isStaticModuleRequireCall) unsafeCommonJsLoaderUsage = true;
|
|
159
|
+
}
|
|
160
|
+
if (isAmbientModuleRequire(node, modulePath)) {
|
|
161
|
+
const parentPath = modulePath.parentPath;
|
|
162
|
+
const isStaticRequireCall = parentPath?.isCallExpression() && 'callee' === modulePath.key && void 0 !== getStaticImport(parentPath);
|
|
163
|
+
if (!isStaticRequireCall) unsafeCommonJsLoaderUsage = true;
|
|
164
|
+
}
|
|
165
|
+
const specifier = getStaticImport(modulePath);
|
|
166
|
+
if (void 0 !== specifier) imports.add(specifier);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
exports,
|
|
171
|
+
imports,
|
|
172
|
+
unsafeCommonJsLoaderUsage,
|
|
173
|
+
unsafeDynamicImport
|
|
174
|
+
};
|
|
175
|
+
};
|
|
176
|
+
const getPackageName = (specifier)=>specifier.startsWith('@') ? specifier.split('/').slice(0, 2).join('/') : specifier.split('/')[0];
|
|
177
|
+
const CLOUDFLARE_WORKER_NODE_BUILTIN_SPECIFIERS = new Set(CLOUDFLARE_WORKER_NODE_BUILTINS.map((builtin)=>`node:${builtin}`));
|
|
178
|
+
const isInsideDirectory = (directory, filePath)=>{
|
|
179
|
+
const relative = node_path.relative(directory, filePath);
|
|
180
|
+
return 0 === relative.length || !relative.startsWith('..') && !node_path.isAbsolute(relative);
|
|
181
|
+
};
|
|
182
|
+
const resolveWorkerModule = async (importer, specifier)=>{
|
|
183
|
+
const unresolvedPath = node_path.resolve(node_path.dirname(importer), specifier);
|
|
184
|
+
const candidates = [
|
|
185
|
+
unresolvedPath,
|
|
186
|
+
`${unresolvedPath}.js`,
|
|
187
|
+
`${unresolvedPath}.mjs`,
|
|
188
|
+
`${unresolvedPath}.cjs`,
|
|
189
|
+
node_path.join(unresolvedPath, 'index.js'),
|
|
190
|
+
node_path.join(unresolvedPath, 'index.mjs'),
|
|
191
|
+
node_path.join(unresolvedPath, 'index.cjs')
|
|
192
|
+
];
|
|
193
|
+
for (const candidate of candidates)try {
|
|
194
|
+
if ((await promises.stat(candidate)).isFile()) return await promises.realpath(candidate);
|
|
195
|
+
} catch {}
|
|
196
|
+
};
|
|
197
|
+
const verifyWorkerModuleClosure = async (issues, entryPath, entrySource, contract)=>{
|
|
198
|
+
const visited = new Set();
|
|
199
|
+
let entryAnalysis;
|
|
200
|
+
const [realWorkerRoot, realEntryPath] = await Promise.all([
|
|
201
|
+
promises.realpath(contract.workerRoot),
|
|
202
|
+
promises.realpath(entryPath)
|
|
203
|
+
]);
|
|
204
|
+
if (!isInsideDirectory(realWorkerRoot, realEntryPath)) return void addIssue(issues, {
|
|
205
|
+
code: 'invalid-worker-bundle',
|
|
206
|
+
message: 'Cloudflare worker bundles must not resolve outside the staged worker directory.',
|
|
207
|
+
path: entryPath
|
|
208
|
+
});
|
|
209
|
+
const visitModule = async (modulePath, source, isEntry = false)=>{
|
|
210
|
+
if (visited.has(modulePath)) return;
|
|
211
|
+
visited.add(modulePath);
|
|
212
|
+
let analysis;
|
|
213
|
+
try {
|
|
214
|
+
analysis = analyzeWorkerModule(source ?? await promises.readFile(modulePath, 'utf-8'));
|
|
215
|
+
} catch (error) {
|
|
216
|
+
addIssue(issues, {
|
|
217
|
+
code: 'invalid-worker-bundle',
|
|
218
|
+
message: `Cloudflare worker bundle module could not be parsed: ${error instanceof Error ? error.message : String(error)}`,
|
|
219
|
+
path: modulePath
|
|
220
|
+
});
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (isEntry) entryAnalysis = analysis;
|
|
224
|
+
if (analysis.unsafeCommonJsLoaderUsage) addIssue(issues, {
|
|
225
|
+
code: 'invalid-worker-bundle',
|
|
226
|
+
message: 'Cloudflare worker bundles must use exactly one string-literal specifier in ambient CommonJS loader calls and must not pass or alias the loader.',
|
|
227
|
+
path: modulePath
|
|
228
|
+
});
|
|
229
|
+
if (analysis.unsafeDynamicImport) addIssue(issues, {
|
|
230
|
+
code: 'invalid-worker-bundle',
|
|
231
|
+
message: 'Cloudflare worker bundles must not contain non-static dynamic module imports.',
|
|
232
|
+
path: modulePath
|
|
233
|
+
});
|
|
234
|
+
for (const specifier of analysis.imports){
|
|
235
|
+
if (0 === specifier.length) {
|
|
236
|
+
addIssue(issues, {
|
|
237
|
+
code: 'invalid-worker-bundle',
|
|
238
|
+
message: 'Cloudflare worker bundle module specifiers must not be empty.',
|
|
239
|
+
path: modulePath
|
|
240
|
+
});
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (!specifier.startsWith('.') && !node_path.isAbsolute(specifier)) {
|
|
244
|
+
if (CLOUDFLARE_WORKER_NODE_BUILTIN_SPECIFIERS.has(specifier)) continue;
|
|
245
|
+
if (specifier.startsWith('node:')) {
|
|
246
|
+
addIssue(issues, {
|
|
247
|
+
code: 'invalid-worker-bundle',
|
|
248
|
+
message: `Cloudflare worker bundle import "${specifier}" is not a supported Worker node: builtin.`,
|
|
249
|
+
path: modulePath
|
|
250
|
+
});
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (!contract.providedPackages.has(getPackageName(specifier))) addIssue(issues, {
|
|
254
|
+
code: 'invalid-worker-bundle',
|
|
255
|
+
message: `Cloudflare worker bundle import "${specifier}" is not provided by worker/package.json dependencies.`,
|
|
256
|
+
path: modulePath
|
|
257
|
+
});
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
const resolved = await resolveWorkerModule(modulePath, specifier);
|
|
261
|
+
if (resolved && !isInsideDirectory(realWorkerRoot, resolved)) {
|
|
262
|
+
addIssue(issues, {
|
|
263
|
+
code: 'invalid-worker-bundle',
|
|
264
|
+
message: 'Cloudflare worker bundles must not resolve outside the staged worker directory.',
|
|
265
|
+
path: modulePath
|
|
266
|
+
});
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
if (!resolved) {
|
|
270
|
+
addIssue(issues, {
|
|
271
|
+
code: 'invalid-worker-bundle',
|
|
272
|
+
message: `Cloudflare worker bundle import "${specifier}" does not resolve inside worker/.`,
|
|
273
|
+
path: modulePath
|
|
274
|
+
});
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
await visitModule(resolved);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
await visitModule(realEntryPath, entrySource, true);
|
|
281
|
+
return entryAnalysis;
|
|
282
|
+
};
|
|
283
|
+
const verifyWorkerBundleReferences = async (issues, worker, source, contract)=>{
|
|
284
|
+
const entryAnalysis = await verifyWorkerModuleClosure(issues, worker.path, source, contract);
|
|
285
|
+
if ('effect-bff' === worker.kind && 'string' == typeof worker.dispatcherExport && !entryAnalysis?.exports.has(worker.dispatcherExport)) addIssue(issues, {
|
|
286
|
+
code: 'invalid-worker-bundle',
|
|
287
|
+
message: 'Cloudflare Effect BFF worker bundle must expose its manifest dispatcherExport.',
|
|
288
|
+
path: worker.path
|
|
289
|
+
});
|
|
290
|
+
};
|
|
291
|
+
const verifyWorkerImport = async (issues, entryPath, expectedManifest)=>{
|
|
292
|
+
try {
|
|
293
|
+
const workerModule = await import(`${pathToFileURL(entryPath).href}?t=${Date.now()}`);
|
|
294
|
+
const worker = workerModule.default;
|
|
295
|
+
if (!worker || 'function' != typeof worker.fetch) addIssue(issues, {
|
|
296
|
+
code: 'worker-import-failed',
|
|
297
|
+
message: 'Cloudflare server entry must default-export a Worker with fetch.',
|
|
298
|
+
path: entryPath
|
|
299
|
+
});
|
|
300
|
+
if (void 0 !== expectedManifest && !isDeepStrictEqual(workerModule.modernWorkerManifest, expectedManifest)) addIssue(issues, {
|
|
301
|
+
code: 'worker-import-failed',
|
|
302
|
+
message: 'Cloudflare server entry runtime manifest must exactly match modern-worker-manifest.json.',
|
|
303
|
+
path: entryPath
|
|
304
|
+
});
|
|
305
|
+
} catch (error) {
|
|
306
|
+
addIssue(issues, {
|
|
307
|
+
code: 'worker-import-failed',
|
|
308
|
+
message: `Cloudflare server entry could not be imported: ${error instanceof Error ? error.message : String(error)}`,
|
|
309
|
+
path: entryPath
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
};
|
|
313
|
+
export { getEffectBffWorker, getWorkerBundleReferences, missingWorkerBundleMessage, resolveWorkerBundleReference, verifyWorkerBundleReferences, verifyWorkerImport };
|
|
File without changes
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const PLUGIN_NAME = 'ModernjsCssExtractRuntimePlugin';
|
|
2
|
+
const CSS_LOADING_RUNTIME_MODULE = 'webpack/runtime/css loading';
|
|
3
|
+
const CSS_EXTRACT_STYLESHEET_MATCH = '\t\tif (tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref)) return tag;';
|
|
4
|
+
const CSS_EXTRACT_STYLESHEET_MATCH_WITH_NORMALIZED_URL = `\t\tvar absoluteHref = tag.href;
|
|
5
|
+
\t\tvar absoluteFullHref = new URL(fullhref, document.baseURI).href;
|
|
6
|
+
\t\tif (tag.rel === "stylesheet" && (dataHref === href || dataHref === fullhref || absoluteHref === absoluteFullHref)) return tag;`;
|
|
7
|
+
const isAutomaticPublicPath = (publicPath)=>'auto' === publicPath || 'auto/' === publicPath;
|
|
8
|
+
const normalizeCssExtractRuntimeStylesheetLookup = (source)=>{
|
|
9
|
+
if (!source.includes('__webpack_require__.miniCssF')) return source;
|
|
10
|
+
const occurrences = source.split(CSS_EXTRACT_STYLESHEET_MATCH).length - 1;
|
|
11
|
+
if (1 !== occurrences) throw new Error(`Unsupported CssExtractRspackPlugin runtime shape: expected one stylesheet lookup, found ${occurrences}.`);
|
|
12
|
+
return source.replace(CSS_EXTRACT_STYLESHEET_MATCH, CSS_EXTRACT_STYLESHEET_MATCH_WITH_NORMALIZED_URL);
|
|
13
|
+
};
|
|
14
|
+
class CssExtractRuntimePlugin {
|
|
15
|
+
apply(compiler) {
|
|
16
|
+
if (!isAutomaticPublicPath(compiler.options.output.publicPath)) return;
|
|
17
|
+
compiler.hooks.thisCompilation.tap(PLUGIN_NAME, (compilation)=>{
|
|
18
|
+
compilation.hooks.runtimeModule.tap(PLUGIN_NAME, (runtimeModule)=>{
|
|
19
|
+
if (runtimeModule.identifier() !== CSS_LOADING_RUNTIME_MODULE) return;
|
|
20
|
+
const source = runtimeModule.source;
|
|
21
|
+
if (!source) return;
|
|
22
|
+
const runtimeSource = source.source;
|
|
23
|
+
if ('string' != typeof runtimeSource) return;
|
|
24
|
+
source.source = normalizeCssExtractRuntimeStylesheetLookup(runtimeSource);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
constructor(){
|
|
29
|
+
this.name = PLUGIN_NAME;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export { CssExtractRuntimePlugin, normalizeCssExtractRuntimeStylesheetLookup };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./cloudflare/index.mjs";
|
|
2
|
+
export * from "./cloudflare-builder.mjs";
|
|
3
|
+
export * from "./cloudflare-output-contract.mjs";
|
|
4
|
+
export * from "./cloudflare-output-plan.mjs";
|
|
5
|
+
export * from "./cloudflare-output-verifier/index.mjs";
|
|
6
|
+
export * from "./config.mjs";
|
|
7
|
+
export * from "./css-extract-runtime-plugin.mjs";
|
|
8
|
+
export * from "./release-envelope/index.mjs";
|
|
9
|
+
export * from "./release-envelope/plugin.mjs";
|
|
10
|
+
export * from "./release-identity.mjs";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import node_path from "node:path";
|
|
2
|
+
import { fs } from "@modern-js/utils";
|
|
3
|
+
const getTemplatePath = (file)=>node_path.join(__dirname, 'templates', file);
|
|
4
|
+
const readTemplate = async (file)=>(await fs.readFile(getTemplatePath(file))).toString();
|
|
5
|
+
export { getTemplatePath, readTemplate };
|