|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { |
| 3 | + mkdtempSync, |
| 4 | + mkdirSync, |
| 5 | + readFileSync, |
| 6 | + readdirSync, |
| 7 | + rmSync, |
| 8 | + writeFileSync, |
| 9 | +} from "node:fs"; |
| 10 | +import { tmpdir } from "node:os"; |
| 11 | +import { join, resolve } from "node:path"; |
| 12 | + |
| 13 | +const root = resolve(import.meta.dirname, ".."); |
| 14 | +const packageJson = JSON.parse( |
| 15 | + readFileSync(join(root, "package.json"), "utf8"), |
| 16 | +); |
| 17 | +const forbidden = [ |
| 18 | + "@modelcontextprotocol/client", |
| 19 | + "@modelcontextprotocol/server", |
| 20 | +]; |
| 21 | + |
| 22 | +for (const dependency of forbidden) { |
| 23 | + if (packageJson.peerDependencies?.[dependency]) { |
| 24 | + throw new Error(`${dependency} must not be a published peer dependency`); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +function walk(directory) { |
| 29 | + return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => { |
| 30 | + const path = join(directory, entry.name); |
| 31 | + return entry.isDirectory() ? walk(path) : [path]; |
| 32 | + }); |
| 33 | +} |
| 34 | + |
| 35 | +for (const file of walk(join(root, "dist", "src"))) { |
| 36 | + if (!/\.(?:js|d\.ts)$/.test(file)) continue; |
| 37 | + const contents = readFileSync(file, "utf8"); |
| 38 | + for (const dependency of forbidden) { |
| 39 | + const escaped = dependency.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 40 | + const importEdge = new RegExp( |
| 41 | + `(?:from\\s*["']${escaped}|import\\(["']${escaped}["']\\)|require\\(["']${escaped}["']\\))`, |
| 42 | + ); |
| 43 | + if (importEdge.test(contents)) { |
| 44 | + throw new Error( |
| 45 | + `${file} has an unintended runtime or declaration edge to ${dependency}`, |
| 46 | + ); |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +const temporaryRoot = mkdtempSync(join(tmpdir(), "ext-apps-isolation-")); |
| 52 | +try { |
| 53 | + const npmEnvironment = { |
| 54 | + ...process.env, |
| 55 | + npm_config_cache: join(temporaryRoot, "npm-cache"), |
| 56 | + }; |
| 57 | + const packOutput = JSON.parse( |
| 58 | + execFileSync( |
| 59 | + "npm", |
| 60 | + [ |
| 61 | + "pack", |
| 62 | + "--ignore-scripts", |
| 63 | + "--json", |
| 64 | + "--pack-destination", |
| 65 | + temporaryRoot, |
| 66 | + ], |
| 67 | + { cwd: root, encoding: "utf8", env: npmEnvironment }, |
| 68 | + ), |
| 69 | + ); |
| 70 | + const tarball = join(temporaryRoot, packOutput[0].filename); |
| 71 | + |
| 72 | + const consumers = [ |
| 73 | + { |
| 74 | + name: "app-only", |
| 75 | + dependencies: { "@modelcontextprotocol/ext-apps": `file:${tarball}` }, |
| 76 | + absent: forbidden, |
| 77 | + entry: |
| 78 | + 'import { App } from "@modelcontextprotocol/ext-apps"; console.log(App);', |
| 79 | + bundleAbsent: ["@modelcontextprotocol/server"], |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "server-only", |
| 83 | + dependencies: { |
| 84 | + "@modelcontextprotocol/ext-apps": `file:${tarball}`, |
| 85 | + "@modelcontextprotocol/server": "2.0.0-beta.4", |
| 86 | + }, |
| 87 | + absent: ["@modelcontextprotocol/client"], |
| 88 | + entry: |
| 89 | + 'import { registerAppTool } from "@modelcontextprotocol/ext-apps/server"; console.log(registerAppTool);', |
| 90 | + bundleAbsent: ["@modelcontextprotocol/client"], |
| 91 | + }, |
| 92 | + ]; |
| 93 | + |
| 94 | + for (const consumer of consumers) { |
| 95 | + const directory = join(temporaryRoot, consumer.name); |
| 96 | + mkdirSync(directory); |
| 97 | + writeFileSync( |
| 98 | + join(directory, "package.json"), |
| 99 | + JSON.stringify({ private: true, dependencies: consumer.dependencies }), |
| 100 | + ); |
| 101 | + execFileSync( |
| 102 | + "npm", |
| 103 | + [ |
| 104 | + "install", |
| 105 | + "--ignore-scripts", |
| 106 | + "--package-lock=false", |
| 107 | + "--no-audit", |
| 108 | + "--no-fund", |
| 109 | + ], |
| 110 | + { cwd: directory, stdio: "pipe", env: npmEnvironment }, |
| 111 | + ); |
| 112 | + |
| 113 | + writeFileSync(join(directory, "entry.mjs"), consumer.entry); |
| 114 | + const metafile = join(directory, "bundle-meta.json"); |
| 115 | + execFileSync( |
| 116 | + join(root, "node_modules", ".bin", "esbuild"), |
| 117 | + [ |
| 118 | + "entry.mjs", |
| 119 | + "--bundle", |
| 120 | + "--platform=browser", |
| 121 | + "--outfile=bundle.js", |
| 122 | + `--metafile=${metafile}`, |
| 123 | + ], |
| 124 | + { cwd: directory, stdio: "pipe" }, |
| 125 | + ); |
| 126 | + const bundleInputs = Object.keys( |
| 127 | + JSON.parse(readFileSync(metafile, "utf8")).inputs, |
| 128 | + ).join("\n"); |
| 129 | + for (const dependency of consumer.bundleAbsent) { |
| 130 | + if (bundleInputs.includes(`/node_modules/${dependency}/`)) { |
| 131 | + throw new Error(`${consumer.name} unexpectedly bundled ${dependency}`); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + for (const dependency of consumer.absent) { |
| 136 | + const packagePath = join( |
| 137 | + directory, |
| 138 | + "node_modules", |
| 139 | + ...dependency.split("/"), |
| 140 | + ); |
| 141 | + try { |
| 142 | + readFileSync(join(packagePath, "package.json")); |
| 143 | + throw new Error( |
| 144 | + `${consumer.name} unexpectedly installed ${dependency}`, |
| 145 | + ); |
| 146 | + } catch (error) { |
| 147 | + if (error?.code !== "ENOENT") throw error; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | +} finally { |
| 152 | + rmSync(temporaryRoot, { recursive: true, force: true }); |
| 153 | +} |
| 154 | + |
| 155 | +console.log("Dependency isolation checks passed."); |
0 commit comments