The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import * as NodeURL from "node:url"; |
| 2 | |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { assert, describe, it } from "@effect/vitest"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | |
| 10 | import { findEsmImportsOfExternalPackages } from "./cli-executable-imports.ts"; |
| 11 | |
| 12 | import { |
| 13 | isRuntimeExternalCliDependency, |
| 14 | findInlinedExternalPackages, |
| 15 | selectCliRuntimeExternalDependencies, |
| 16 | shouldBundleCliDependency, |
| 17 | } from "./cli-external-packages.ts"; |
| 18 | |
| 19 | // Native bindings live in optionalDependencies; checking dependencies alone misses them. |
| 20 | const PackageManifest = Schema.Struct({ |
| 21 | dependencies: Schema.optional(Schema.Record(Schema.String, Schema.String)), |
| 22 | optionalDependencies: Schema.optional(Schema.Record(Schema.String, Schema.String)), |
| 23 | peerDependencies: Schema.optional(Schema.Record(Schema.String, Schema.String)), |
| 24 | peerDependenciesMeta: Schema.optional( |
| 25 | Schema.Record(Schema.String, Schema.Struct({ optional: Schema.optional(Schema.Boolean) })), |
| 26 | ), |
| 27 | }); |
| 28 | type PackageManifest = typeof PackageManifest.Type; |
| 29 | |
| 30 | const decodeManifest = Schema.decodeUnknownSync(Schema.fromJsonString(PackageManifest)); |
| 31 | |
| 32 | describe("shouldBundleCliDependency", () => { |
| 33 | it("leaves native addons and their dlopen wrappers external", () => { |
| 34 | for (const id of [ |
| 35 | "node-pty", |
| 36 | "ffi-rs", |
| 37 | "@yuuang/ffi-rs-win32-x64-msvc", |
| 38 | "@ff-labs/fff-node", |
| 39 | "@napi-rs/keyring", |
| 40 | "transcribe-cpp", |
| 41 | "@transcribe-cpp/win32-x64-cpu-vulkan", |
| 42 | "koffi", |
| 43 | "@clerk/electron-passkeys", |
| 44 | "node-addon-api", |
| 45 | ]) { |
| 46 | assert.strictEqual(shouldBundleCliDependency(id), false, id); |
| 47 | } |
| 48 | }); |
| 49 | |
| 50 | it("treats prefix-matched siblings as external", () => { |
| 51 | assert.strictEqual(shouldBundleCliDependency("node-gyp-build-optional-packages"), false); |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | it.layer(NodeServices.layer)("external package dependency closure", (it) => { |
| 56 | // Read the pnpm store directly: isolation hides transitive names and exports maps can block package.json. |
| 57 | const readInstalledPackages = Effect.gen(function* () { |
| 58 | const fileSystem = yield* FileSystem.FileSystem; |
| 59 | const path = yield* Path.Path; |
| 60 | const storeDir = path.resolve( |
| 61 | path.dirname(NodeURL.fileURLToPath(import.meta.url)), |
| 62 | "../../node_modules/.pnpm", |
| 63 | ); |
| 64 | |
| 65 | // Paths under store files such as lock.yaml raise ENOTDIR, not absence. |
| 66 | const isPresent = (candidate: string) => |
| 67 | fileSystem.exists(candidate).pipe(Effect.orElseSucceed(() => false)); |
| 68 | |
| 69 | const installed = new Map<string, PackageManifest>(); |
| 70 | if (!(yield* isPresent(storeDir))) return installed; |
| 71 | |
| 72 | for (const entry of yield* fileSystem.readDirectory(storeDir)) { |
| 73 | const modulesDir = path.join(storeDir, entry, "node_modules"); |
| 74 | if (!(yield* isPresent(modulesDir))) continue; |
| 75 | |
| 76 | for (const owner of yield* fileSystem.readDirectory(modulesDir)) { |
| 77 | const names = owner.startsWith("@") |
| 78 | ? (yield* fileSystem.readDirectory(path.join(modulesDir, owner))).map( |
| 79 | (scoped) => `${owner}/${scoped}`, |
| 80 | ) |
| 81 | : [owner]; |
| 82 | |
| 83 | for (const name of names) { |
| 84 | if (installed.has(name)) continue; |
| 85 | const manifestPath = path.join(modulesDir, name, "package.json"); |
| 86 | if (!(yield* isPresent(manifestPath))) continue; |
| 87 | installed.set(name, decodeManifest(yield* fileSystem.readFileString(manifestPath))); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | return installed; |
| 92 | }).pipe(Effect.cached, Effect.runSync); |
| 93 | |
| 94 | const isRuntimeExternal = isRuntimeExternalCliDependency; |
| 95 | |
| 96 | it.effect("keeps every runtime dependency of an external package external too", () => |
| 97 | Effect.gen(function* () { |
| 98 | const installed = yield* readInstalledPackages; |
| 99 | const violations: string[] = []; |
| 100 | const seen = new Set<string>(); |
| 101 | const queue = [...installed.keys()].filter(isRuntimeExternal); |
| 102 | |
| 103 | for (const name of queue) { |
| 104 | if (seen.has(name)) continue; |
| 105 | seen.add(name); |
| 106 | |
| 107 | const manifest = installed.get(name); |
| 108 | if (!manifest) continue; |
| 109 | |
| 110 | const declared = { |
| 111 | ...manifest.dependencies, |
| 112 | ...manifest.optionalDependencies, |
| 113 | ...manifest.peerDependencies, |
| 114 | }; |
| 115 | for (const dependency of Object.keys(declared)) { |
| 116 | if (!isRuntimeExternal(dependency)) { |
| 117 | violations.push(`${name} -> ${dependency}`); |
| 118 | } |
| 119 | if (!seen.has(dependency)) queue.push(dependency); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | assert.deepStrictEqual( |
| 124 | violations, |
| 125 | [], |
| 126 | `these dependencies of external packages would be bundled away and fail to resolve under WSL: ${violations.join(", ")}`, |
| 127 | ); |
| 128 | }), |
| 129 | ); |
| 130 | |
| 131 | // The stage installs only externals the server lists, so a required peer must be reachable from them. |
| 132 | it.effect("stages a provider for every required peer of a staged external", () => |
| 133 | Effect.gen(function* () { |
| 134 | const fileSystem = yield* FileSystem.FileSystem; |
| 135 | const path = yield* Path.Path; |
| 136 | const installed = yield* readInstalledPackages; |
| 137 | const server = decodeManifest( |
| 138 | yield* fileSystem.readFileString( |
| 139 | path.resolve( |
| 140 | path.dirname(NodeURL.fileURLToPath(import.meta.url)), |
| 141 | "../../apps/server/package.json", |
| 142 | ), |
| 143 | ), |
| 144 | ); |
| 145 | |
| 146 | const staged = new Set<string>(); |
| 147 | const queue = Object.keys(selectCliRuntimeExternalDependencies(server.dependencies ?? {})); |
| 148 | for (const name of queue) { |
| 149 | if (staged.has(name)) continue; |
| 150 | staged.add(name); |
| 151 | const manifest = installed.get(name); |
| 152 | queue.push( |
| 153 | ...Object.keys({ ...manifest?.dependencies, ...manifest?.optionalDependencies }), |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | const missing = [...staged].flatMap((name) => { |
| 158 | const manifest = installed.get(name); |
| 159 | return Object.keys(manifest?.peerDependencies ?? {}) |
| 160 | .filter((peer) => !manifest?.peerDependenciesMeta?.[peer]?.optional && !staged.has(peer)) |
| 161 | .map((peer) => `${name} -> ${peer}`); |
| 162 | }); |
| 163 | |
| 164 | assert.deepStrictEqual( |
| 165 | missing, |
| 166 | [], |
| 167 | `these peers of staged external packages are missing from the packaged app; add them to apps/server dependencies: ${missing.join(", ")}`, |
| 168 | ); |
| 169 | }), |
| 170 | ); |
| 171 | }); |
| 172 | |
| 173 | describe("findInlinedExternalPackages", () => { |
| 174 | const region = (path: string) => `//#region ${path} |
| 175 | var x = 1; |
| 176 | //#endregion |
| 177 | `; |
| 178 | |
| 179 | it("flags an external package that was inlined", () => { |
| 180 | const source = |
| 181 | region("../../node_modules/.pnpm/node-addon-api@7.1.1/node_modules/node-addon-api/index.js") + |
| 182 | region( |
| 183 | "../../node_modules/.pnpm/node-gyp-build-optional-packages@5.2.2/node_modules/node-gyp-build-optional-packages/index.js", |
| 184 | ); |
| 185 | const result = findInlinedExternalPackages(source); |
| 186 | |
| 187 | assert.deepStrictEqual(result.inlined, ["node-addon-api", "node-gyp-build-optional-packages"]); |
| 188 | assert.strictEqual(result.regionCount, 2); |
| 189 | }); |
| 190 | |
| 191 | it("reports the packages that were inlined, not just the violations", () => { |
| 192 | const source = |
| 193 | region("../../node_modules/.pnpm/effect@4.0.0/node_modules/effect/dist/index.js") + |
| 194 | region("../../node_modules/.pnpm/yaml@2.4.0/node_modules/yaml/dist/index.js") + |
| 195 | region("../../src/server/main.ts"); |
| 196 | const result = findInlinedExternalPackages(source); |
| 197 | |
| 198 | assert.deepStrictEqual(result.inlinedPackages, ["effect", "yaml"]); |
| 199 | assert.deepStrictEqual(result.inlined, []); |
| 200 | }); |
| 201 | |
| 202 | it("reads the plain `// <path>` markers newer Rolldown emits", () => { |
| 203 | const source = [ |
| 204 | "// \0rolldown/runtime.js", |
| 205 | "var x = 1;", |
| 206 | "// ../../node_modules/.pnpm/node-addon-api@7.1.1/node_modules/node-addon-api/index.js", |
| 207 | "var y = 2; // trailing comment", |
| 208 | "// ../../src/server/main.ts", |
| 209 | "", |
| 210 | ].join("\n"); |
| 211 | const result = findInlinedExternalPackages(source); |
| 212 | |
| 213 | assert.strictEqual(result.regionCount, 3); |
| 214 | assert.deepStrictEqual(result.inlined, ["node-addon-api"]); |
| 215 | }); |
| 216 | }); |
| 217 | |
| 218 | describe("findEsmImportsOfExternalPackages", () => { |
| 219 | it("flags static and dynamic imports of file-backed packages", () => { |
| 220 | const source = [ |
| 221 | 'import { FileFinder } from "@ff-labs/fff-node";', |
| 222 | 'import * as fs from "fs";', |
| 223 | 'import { createRequire } from "node:module";', |
| 224 | 'const pty = () => import("node-pty");', |
| 225 | 'const data = () => import("@ff-labs/fff-bin-linux-x64-gnu", { with: { type: "json" } });', |
| 226 | 'const lazy = () => import(/* @vite-ignore */ "ffi-rs");', |
| 227 | 'const local = () => import("./chunk-abc.mjs");', |
| 228 | ].join("\n"); |
| 229 | |
| 230 | assert.deepStrictEqual(findEsmImportsOfExternalPackages(source), [ |
| 231 | "@ff-labs/fff-bin-linux-x64-gnu", |
| 232 | "@ff-labs/fff-node", |
| 233 | "ffi-rs", |
| 234 | "node-pty", |
| 235 | ]); |
| 236 | }); |
| 237 | |
| 238 | it("flags side-effect imports and re-exports too", () => { |
| 239 | const source = ['import "msgpackr-extract";', 'export { load } from "ffi-rs";'].join("\n"); |
| 240 | assert.deepStrictEqual(findEsmImportsOfExternalPackages(source), [ |
| 241 | "ffi-rs", |
| 242 | "msgpackr-extract", |
| 243 | ]); |
| 244 | }); |
| 245 | |
| 246 | it("ignores imports inside generated extension source and comments", () => { |
| 247 | const source = [ |
| 248 | 'const extension = `import { Type } from "typebox";\nimport type { ExtensionAPI } from "@earendil-works/pi-coding-agent";`;', |
| 249 | '// import "comment-only";', |
| 250 | "const example = 'import(\"string-only\")';", |
| 251 | 'const interpolated = `source ${import("real-package")}`;', |
| 252 | ].join("\n"); |
| 253 | assert.deepStrictEqual(findEsmImportsOfExternalPackages(source), ["real-package"]); |
| 254 | }); |
| 255 | |
| 256 | it("allows optional dynamic Bun built-ins but rejects static imports", () => { |
| 257 | assert.deepStrictEqual( |
| 258 | findEsmImportsOfExternalPackages('const load = () => import("bun:sqlite");'), |
| 259 | [], |
| 260 | ); |
| 261 | assert.deepStrictEqual( |
| 262 | findEsmImportsOfExternalPackages('import { Database } from "bun:sqlite";'), |
| 263 | ["bun:sqlite"], |
| 264 | ); |
| 265 | }); |
| 266 | |
| 267 | it("does not mistake createRequire calls for imports", () => { |
| 268 | const source = 'const { FileFinder } = createRequire(import.meta.url)("@ff-labs/fff-node");'; |
| 269 | assert.deepStrictEqual(findEsmImportsOfExternalPackages(source), []); |
| 270 | }); |
| 271 | }); |