|
| 1 | +import type { WirePackage } from './normalize.js'; |
| 2 | + |
| 3 | +/** |
| 4 | + * A best-effort description of the stack a build was produced with, derived |
| 5 | + * entirely from the lockfile (ground truth) plus the build-time environment. |
| 6 | + * |
| 7 | + * The disclosure widget reads this from `window.__PATCHSTACK_STACK__` (injected |
| 8 | + * by `mark-build`) and reports it to Patchstack, so we learn how the sites we |
| 9 | + * protect are actually built and hosted — across every "vibe" platform — without |
| 10 | + * shipping a runtime probe onto the host server. Every field is a coarse label |
| 11 | + * or a bare key *name*: no versions beyond the framework, and never an env value. |
| 12 | + */ |
| 13 | +export interface StackDescriptor { |
| 14 | + /** App / meta-framework, e.g. "next", "nuxt", "tanstack-start", "remix". */ |
| 15 | + framework: string | null; |
| 16 | + /** UI runtime, e.g. "react", "vue", "svelte", "solid". */ |
| 17 | + ui: string | null; |
| 18 | + /** Build tool / bundler, e.g. "vite", "webpack", "rspack". */ |
| 19 | + bundler: string | null; |
| 20 | + /** Deployment-runtime hint from deps, e.g. "cloudflare-workers", "vercel". */ |
| 21 | + runtime: string | null; |
| 22 | + /** The vibe/builder platform that generated the project, e.g. "lovable". */ |
| 23 | + builder: string | null; |
| 24 | + /** Package ecosystem the manifest came from. */ |
| 25 | + ecosystem: 'npm'; |
| 26 | + /** Hosting-related build-environment variable NAMES (never their values). */ |
| 27 | + hostingEnvKeys: string[]; |
| 28 | +} |
| 29 | + |
| 30 | +type Category = 'framework' | 'ui' | 'bundler' | 'runtime' | 'builder'; |
| 31 | + |
| 32 | +interface StackRule { |
| 33 | + category: Category; |
| 34 | + /** Exact package name that signals this label. */ |
| 35 | + pkg: string; |
| 36 | + label: string; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Package → stack-label registry. First match per category wins, so order |
| 41 | + * within a category is priority order (most specific first). Add a row to teach |
| 42 | + * the connector a new framework, bundler, or vibe platform. |
| 43 | + */ |
| 44 | +const STACK_RULES: readonly StackRule[] = [ |
| 45 | + // Meta-frameworks (most specific first). |
| 46 | + { category: 'framework', pkg: '@tanstack/react-start', label: 'tanstack-start' }, |
| 47 | + { category: 'framework', pkg: '@tanstack/start', label: 'tanstack-start' }, |
| 48 | + { category: 'framework', pkg: 'next', label: 'next' }, |
| 49 | + { category: 'framework', pkg: 'nuxt', label: 'nuxt' }, |
| 50 | + { category: 'framework', pkg: '@remix-run/react', label: 'remix' }, |
| 51 | + { category: 'framework', pkg: '@remix-run/node', label: 'remix' }, |
| 52 | + { category: 'framework', pkg: 'react-router', label: 'react-router' }, |
| 53 | + { category: 'framework', pkg: 'astro', label: 'astro' }, |
| 54 | + { category: 'framework', pkg: '@sveltejs/kit', label: 'sveltekit' }, |
| 55 | + { category: 'framework', pkg: '@builder.io/qwik-city', label: 'qwik-city' }, |
| 56 | + { category: 'framework', pkg: 'gatsby', label: 'gatsby' }, |
| 57 | + { category: 'framework', pkg: 'express', label: 'express' }, |
| 58 | + { category: 'framework', pkg: 'fastify', label: 'fastify' }, |
| 59 | + |
| 60 | + // UI runtimes. |
| 61 | + { category: 'ui', pkg: '@angular/core', label: 'angular' }, |
| 62 | + { category: 'ui', pkg: 'react-dom', label: 'react' }, |
| 63 | + { category: 'ui', pkg: 'react', label: 'react' }, |
| 64 | + { category: 'ui', pkg: 'vue', label: 'vue' }, |
| 65 | + { category: 'ui', pkg: 'svelte', label: 'svelte' }, |
| 66 | + { category: 'ui', pkg: 'solid-js', label: 'solid' }, |
| 67 | + { category: 'ui', pkg: 'preact', label: 'preact' }, |
| 68 | + |
| 69 | + // Bundlers / build tools. |
| 70 | + { category: 'bundler', pkg: 'vite', label: 'vite' }, |
| 71 | + { category: 'bundler', pkg: '@rspack/core', label: 'rspack' }, |
| 72 | + { category: 'bundler', pkg: 'webpack', label: 'webpack' }, |
| 73 | + { category: 'bundler', pkg: 'parcel', label: 'parcel' }, |
| 74 | + { category: 'bundler', pkg: 'rollup', label: 'rollup' }, |
| 75 | + { category: 'bundler', pkg: 'esbuild', label: 'esbuild' }, |
| 76 | + |
| 77 | + // Deployment-runtime hints from build deps. |
| 78 | + { category: 'runtime', pkg: 'wrangler', label: 'cloudflare-workers' }, |
| 79 | + { category: 'runtime', pkg: '@cloudflare/workers-types', label: 'cloudflare-workers' }, |
| 80 | + { category: 'runtime', pkg: '@cloudflare/vite-plugin', label: 'cloudflare-workers' }, |
| 81 | + { category: 'runtime', pkg: '@vercel/node', label: 'vercel' }, |
| 82 | + { category: 'runtime', pkg: '@netlify/functions', label: 'netlify' }, |
| 83 | + { category: 'runtime', pkg: '@netlify/blobs', label: 'netlify' }, |
| 84 | + |
| 85 | + // Vibe / builder platforms — the "learn from each platform" signal. |
| 86 | + { category: 'builder', pkg: 'lovable-tagger', label: 'lovable' }, |
| 87 | + { category: 'builder', pkg: '@replit/vite-plugin-runtime-error-modal', label: 'replit' }, |
| 88 | + { category: 'builder', pkg: '@replit/vite-plugin-cartographer', label: 'replit' }, |
| 89 | +]; |
| 90 | + |
| 91 | +/** Build-environment variable-name patterns that fingerprint a host, from server-insight. */ |
| 92 | +const HOSTING_ENV_PATTERNS: readonly RegExp[] = [ |
| 93 | + /^CF_/, |
| 94 | + /^CLOUDFLARE_/, |
| 95 | + /^VERCEL/, |
| 96 | + /^NETLIFY/, |
| 97 | + /^AWS_(LAMBDA|REGION|EXECUTION)/, |
| 98 | + /^FLY_/, |
| 99 | + /^RENDER/, |
| 100 | + /^RAILWAY_/, |
| 101 | + /^DENO_/, |
| 102 | + /^EDGE_RUNTIME/, |
| 103 | + /^DYNO$/, |
| 104 | + /^K_SERVICE$/, |
| 105 | + /^GAE_/, |
| 106 | + /^FUNCTION_/, |
| 107 | +]; |
| 108 | + |
| 109 | +/** |
| 110 | + * Return the sorted NAMES of hosting-related environment variables present in |
| 111 | + * `env`. Only names are surfaced — never values — so a build fingerprint can |
| 112 | + * distinguish "deployed on Cloudflare" from "deployed on Vercel" without ever |
| 113 | + * disclosing a secret. |
| 114 | + */ |
| 115 | +export function collectHostingEnvKeys(env: NodeJS.ProcessEnv = process.env): string[] { |
| 116 | + return Object.keys(env) |
| 117 | + .filter((key) => HOSTING_ENV_PATTERNS.some((pattern) => pattern.test(key))) |
| 118 | + .sort(); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Derive a {@link StackDescriptor} from the wire packages and the build |
| 123 | + * environment. Never throws: an unrecognised stack just yields nulls. |
| 124 | + */ |
| 125 | +export function detectStack( |
| 126 | + packages: readonly WirePackage[], |
| 127 | + env: NodeJS.ProcessEnv = process.env, |
| 128 | +): StackDescriptor { |
| 129 | + const present = new Set(packages.map((pkg) => pkg.name)); |
| 130 | + |
| 131 | + const firstMatch = (category: Category): string | null => { |
| 132 | + for (const rule of STACK_RULES) { |
| 133 | + if (rule.category === category && present.has(rule.pkg)) { |
| 134 | + return rule.label; |
| 135 | + } |
| 136 | + } |
| 137 | + return null; |
| 138 | + }; |
| 139 | + |
| 140 | + return { |
| 141 | + framework: firstMatch('framework'), |
| 142 | + ui: firstMatch('ui'), |
| 143 | + bundler: firstMatch('bundler'), |
| 144 | + runtime: firstMatch('runtime'), |
| 145 | + builder: firstMatch('builder'), |
| 146 | + ecosystem: 'npm', |
| 147 | + hostingEnvKeys: collectHostingEnvKeys(env), |
| 148 | + }; |
| 149 | +} |
| 150 | + |
| 151 | +/** True when the descriptor carries no useful signal (nothing worth injecting). */ |
| 152 | +export function isEmptyStack(stack: StackDescriptor): boolean { |
| 153 | + return ( |
| 154 | + stack.framework === null && |
| 155 | + stack.ui === null && |
| 156 | + stack.bundler === null && |
| 157 | + stack.runtime === null && |
| 158 | + stack.builder === null && |
| 159 | + stack.hostingEnvKeys.length === 0 |
| 160 | + ); |
| 161 | +} |
0 commit comments