The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { gunzipSync } from "node:zlib"; |
| 2 | import { createComputeArchive } from "@alchemy.run/prisma/ComputeArchive"; |
| 3 | import { |
| 4 | runBuildCommand, |
| 5 | runComputeAutoBuild, |
| 6 | runComputeStaticBuild, |
| 7 | } from "@alchemy.run/prisma/ComputeBuild"; |
| 8 | import { describe, expect, it } from "@alchemy.run/test"; |
| 9 | import { findAvailablePort } from "alchemy/Util/Node"; |
| 10 | import { PlatformServices } from "alchemy/Util/PlatformServices"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as FileSystem from "effect/FileSystem"; |
| 13 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 14 | import * as HttpClient from "effect/http/HttpClient"; |
| 15 | import * as Path from "effect/Path"; |
| 16 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 17 | import { ChildProcessSpawner } from "effect/process/ChildProcessSpawner"; |
| 18 | import * as Schedule from "effect/Schedule"; |
| 19 | |
| 20 | const inspectBuildEnvironmentCommand = [ |
| 21 | JSON.stringify(process.execPath), |
| 22 | "-e", |
| 23 | JSON.stringify( |
| 24 | "process.stdout.write(JSON.stringify({ serviceToken: process.env.PRISMA_SERVICE_TOKEN ?? null, apiToken: process.env.PRISMA_API_TOKEN ?? null, inherited: process.env.ALCHEMY_BUILD_INHERITED_SENTINEL ?? null }))", |
| 25 | ), |
| 26 | ].join(" "); |
| 27 | |
| 28 | const restoreProcessEnv = (name: string, value: string | undefined) => { |
| 29 | if (value === undefined) { |
| 30 | delete process.env[name]; |
| 31 | } else { |
| 32 | process.env[name] = value; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | describe( |
| 37 | "Prisma Compute auto-build", |
| 38 | { tags: ["unit", "provider:prisma", "provider:prisma:compute", "local"] }, |
| 39 | () => { |
| 40 | it.effect("builds a Bun app from package.json main", () => |
| 41 | Effect.gen(function* () { |
| 42 | const fs = yield* FileSystem.FileSystem; |
| 43 | const path = yield* Path.Path; |
| 44 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-bun-" }); |
| 45 | yield* fs.makeDirectory(path.join(root, "src")); |
| 46 | yield* fs.writeFileString( |
| 47 | path.join(root, "package.json"), |
| 48 | JSON.stringify({ main: "src/server.ts" }), |
| 49 | ); |
| 50 | yield* fs.writeFileString(path.join(root, "src", "server.ts"), "console.log('auto bun');"); |
| 51 | |
| 52 | const artifact = yield* runComputeAutoBuild({ appPath: root, framework: "bun" }); |
| 53 | const entrypointText = yield* fs.readFileString( |
| 54 | path.join(artifact.directory, artifact.entrypoint), |
| 55 | ); |
| 56 | |
| 57 | expect(artifact.entrypoint).toBe("server.js"); |
| 58 | expect(entrypointText).toContain("auto bun"); |
| 59 | |
| 60 | yield* artifact.cleanup; |
| 61 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 62 | }).pipe(Effect.provide(PlatformServices)), |
| 63 | ); |
| 64 | |
| 65 | it.effect("builds a NestJS app from local CLI output", () => |
| 66 | Effect.gen(function* () { |
| 67 | const fs = yield* FileSystem.FileSystem; |
| 68 | const path = yield* Path.Path; |
| 69 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-nest-" }); |
| 70 | const binDir = path.join(root, "node_modules", ".bin"); |
| 71 | const nestBin = path.join(binDir, "nest"); |
| 72 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 73 | yield* fs.writeFileString( |
| 74 | path.join(root, "package.json"), |
| 75 | JSON.stringify({ dependencies: { "@nestjs/core": "0.0.0-test" } }), |
| 76 | ); |
| 77 | yield* fs.writeFileString(nestBin, "#!/usr/bin/env sh\nexit 0\n"); |
| 78 | yield* fs.chmod(nestBin, 0o755); |
| 79 | yield* fs.makeDirectory(path.join(root, "dist"), { recursive: true }); |
| 80 | yield* fs.writeFileString( |
| 81 | path.join(root, "dist", "main.js"), |
| 82 | "console.log('nest server');", |
| 83 | ); |
| 84 | |
| 85 | const artifact = yield* runComputeAutoBuild({ appPath: root, framework: "nestjs" }); |
| 86 | |
| 87 | expect(artifact.entrypoint).toBe("dist/main.js"); |
| 88 | expect(artifact.defaultPort).toBe(3000); |
| 89 | expect(yield* fs.readFileString(path.join(artifact.directory, "dist/main.js"))).toContain( |
| 90 | "nest server", |
| 91 | ); |
| 92 | |
| 93 | yield* artifact.cleanup; |
| 94 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 95 | }).pipe(Effect.provide(PlatformServices)), |
| 96 | ); |
| 97 | |
| 98 | it.effect("auto-detects NestJS before the Bun fallback", () => |
| 99 | Effect.gen(function* () { |
| 100 | const fs = yield* FileSystem.FileSystem; |
| 101 | const path = yield* Path.Path; |
| 102 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-nest-detect-" }); |
| 103 | const binDir = path.join(root, "node_modules", ".bin"); |
| 104 | const nestBin = path.join(binDir, "nest"); |
| 105 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 106 | yield* fs.writeFileString( |
| 107 | path.join(root, "package.json"), |
| 108 | JSON.stringify({ dependencies: { "@nestjs/core": "0.0.0-test" } }), |
| 109 | ); |
| 110 | yield* fs.writeFileString(nestBin, "#!/usr/bin/env sh\nexit 0\n"); |
| 111 | yield* fs.chmod(nestBin, 0o755); |
| 112 | yield* fs.makeDirectory(path.join(root, "dist", "src"), { recursive: true }); |
| 113 | yield* fs.writeFileString( |
| 114 | path.join(root, "dist", "src", "main.js"), |
| 115 | "console.log('auto nest');", |
| 116 | ); |
| 117 | |
| 118 | const artifact = yield* runComputeAutoBuild({ appPath: root }); |
| 119 | |
| 120 | expect(artifact.entrypoint).toBe("dist/src/main.js"); |
| 121 | |
| 122 | yield* artifact.cleanup; |
| 123 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 124 | }).pipe(Effect.provide(PlatformServices)), |
| 125 | ); |
| 126 | |
| 127 | it.effect("forwards output limits to framework build commands", () => |
| 128 | Effect.gen(function* () { |
| 129 | const fs = yield* FileSystem.FileSystem; |
| 130 | const path = yield* Path.Path; |
| 131 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-build-limit-" }); |
| 132 | const binDir = path.join(root, "node_modules", ".bin"); |
| 133 | const nestBin = path.join(binDir, "nest"); |
| 134 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 135 | yield* fs.writeFileString( |
| 136 | path.join(root, "package.json"), |
| 137 | JSON.stringify({ dependencies: { "@nestjs/core": "0.0.0-test" } }), |
| 138 | ); |
| 139 | yield* fs.writeFileString(nestBin, "#!/usr/bin/env sh\nprintf '123456789'\n"); |
| 140 | yield* fs.chmod(nestBin, 0o755); |
| 141 | |
| 142 | const error = yield* runComputeAutoBuild({ |
| 143 | appPath: root, |
| 144 | framework: "nestjs", |
| 145 | outputLimitBytes: 8, |
| 146 | }).pipe(Effect.flip); |
| 147 | |
| 148 | expect((error as Error).message).toContain( |
| 149 | "Build stdout exceeded the 8 byte output safety limit", |
| 150 | ); |
| 151 | }).pipe(Effect.provide(PlatformServices)), |
| 152 | ); |
| 153 | |
| 154 | it.live("forwards deadlines to framework build commands", () => |
| 155 | Effect.gen(function* () { |
| 156 | const fs = yield* FileSystem.FileSystem; |
| 157 | const path = yield* Path.Path; |
| 158 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-build-timeout-" }); |
| 159 | const binDir = path.join(root, "node_modules", ".bin"); |
| 160 | const nestBin = path.join(binDir, "nest"); |
| 161 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 162 | yield* fs.writeFileString( |
| 163 | path.join(root, "package.json"), |
| 164 | JSON.stringify({ dependencies: { "@nestjs/core": "0.0.0-test" } }), |
| 165 | ); |
| 166 | yield* fs.writeFileString(nestBin, "#!/usr/bin/env sh\nsleep 10\n"); |
| 167 | yield* fs.chmod(nestBin, 0o755); |
| 168 | |
| 169 | const error = yield* runComputeAutoBuild({ |
| 170 | appPath: root, |
| 171 | framework: "nestjs", |
| 172 | timeoutSeconds: 0.05, |
| 173 | }).pipe(Effect.flip); |
| 174 | |
| 175 | expect((error as Error).message).toContain("Build command timed out after 0.05 seconds"); |
| 176 | }).pipe(Effect.provide(PlatformServices)), |
| 177 | ); |
| 178 | |
| 179 | it.effect("resolves NestJS config output and stages traced dependencies", () => |
| 180 | Effect.gen(function* () { |
| 181 | const fs = yield* FileSystem.FileSystem; |
| 182 | const path = yield* Path.Path; |
| 183 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-nest-trace-" }); |
| 184 | const binDir = path.join(root, "node_modules", ".bin"); |
| 185 | const nestBin = path.join(binDir, "nest"); |
| 186 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 187 | yield* fs.writeFileString( |
| 188 | path.join(root, "package.json"), |
| 189 | JSON.stringify({ dependencies: { "@nestjs/core": "0.0.0-test" } }), |
| 190 | ); |
| 191 | yield* fs.writeFileString( |
| 192 | path.join(root, "nest-cli.json"), |
| 193 | JSON.stringify({ sourceRoot: "app", entryFile: "bootstrap" }), |
| 194 | ); |
| 195 | yield* fs.writeFileString( |
| 196 | path.join(root, "tsconfig.json"), |
| 197 | [ |
| 198 | "{", |
| 199 | " // Keep URLs with // intact while reading compiler options.", |
| 200 | ' "compilerOptions": {', |
| 201 | ' "outDir": "build",', |
| 202 | ' "sourceMappingURL": "https://example.com//maps"', |
| 203 | " }", |
| 204 | "}", |
| 205 | ].join("\n"), |
| 206 | ); |
| 207 | yield* fs.writeFileString(nestBin, "#!/usr/bin/env sh\nexit 0\n"); |
| 208 | yield* fs.chmod(nestBin, 0o755); |
| 209 | |
| 210 | const usedDep = path.join(root, "node_modules", "used-dep"); |
| 211 | const unusedDep = path.join(root, "node_modules", "unused-dep"); |
| 212 | yield* fs.makeDirectory(usedDep, { recursive: true }); |
| 213 | yield* fs.writeFileString( |
| 214 | path.join(usedDep, "package.json"), |
| 215 | JSON.stringify({ name: "used-dep", main: "index.js" }), |
| 216 | ); |
| 217 | yield* fs.writeFileString(path.join(usedDep, "index.js"), "module.exports = 'used';"); |
| 218 | yield* fs.makeDirectory(unusedDep, { recursive: true }); |
| 219 | yield* fs.writeFileString( |
| 220 | path.join(unusedDep, "package.json"), |
| 221 | JSON.stringify({ name: "unused-dep", main: "index.js" }), |
| 222 | ); |
| 223 | yield* fs.writeFileString(path.join(unusedDep, "index.js"), "module.exports = 'unused';"); |
| 224 | yield* fs.makeDirectory(path.join(root, "build", "app"), { recursive: true }); |
| 225 | yield* fs.writeFileString( |
| 226 | path.join(root, "build", "app", "bootstrap.js"), |
| 227 | "const used = require('used-dep'); console.log(used);", |
| 228 | ); |
| 229 | |
| 230 | const artifact = yield* runComputeAutoBuild({ appPath: root, framework: "nestjs" }); |
| 231 | |
| 232 | expect(artifact.entrypoint).toBe("build/app/bootstrap.js"); |
| 233 | expect( |
| 234 | yield* fs.exists(path.join(artifact.directory, "node_modules", "used-dep", "index.js")), |
| 235 | ).toBe(true); |
| 236 | expect(yield* fs.exists(path.join(artifact.directory, "node_modules", "unused-dep"))).toBe( |
| 237 | false, |
| 238 | ); |
| 239 | |
| 240 | yield* artifact.cleanup; |
| 241 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 242 | }).pipe(Effect.provide(PlatformServices)), |
| 243 | ); |
| 244 | |
| 245 | it.effect("uses a project-local framework CLI and copies Next.js extras", () => |
| 246 | Effect.gen(function* () { |
| 247 | const fs = yield* FileSystem.FileSystem; |
| 248 | const path = yield* Path.Path; |
| 249 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-next-" }); |
| 250 | const binDir = path.join(root, "node_modules", ".bin"); |
| 251 | const nextBin = path.join(binDir, "next"); |
| 252 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 253 | yield* fs.writeFileString( |
| 254 | path.join(root, "package.json"), |
| 255 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 256 | ); |
| 257 | yield* fs.writeFileString( |
| 258 | nextBin, |
| 259 | [ |
| 260 | "#!/bin/sh", |
| 261 | "mkdir -p .next/standalone .next/static public", |
| 262 | "printf 'next server' > .next/standalone/server.js", |
| 263 | "printf 'next static' > .next/static/app.js", |
| 264 | "printf 'next public' > public/asset.txt", |
| 265 | "", |
| 266 | ].join("\n"), |
| 267 | ); |
| 268 | yield* fs.chmod(nextBin, 0o755); |
| 269 | |
| 270 | const artifact = yield* runComputeAutoBuild({ appPath: root, framework: "nextjs" }); |
| 271 | |
| 272 | expect(artifact.entrypoint).toBe("server.js"); |
| 273 | expect(artifact.defaultPort).toBe(3000); |
| 274 | expect(yield* fs.readFileString(path.join(artifact.directory, "server.js"))).toBe( |
| 275 | "next server", |
| 276 | ); |
| 277 | expect( |
| 278 | yield* fs.readFileString(path.join(artifact.directory, ".next", "static", "app.js")), |
| 279 | ).toBe("next static"); |
| 280 | expect(yield* fs.readFileString(path.join(artifact.directory, "public", "asset.txt"))).toBe( |
| 281 | "next public", |
| 282 | ); |
| 283 | |
| 284 | yield* artifact.cleanup; |
| 285 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 286 | }).pipe(Effect.provide(PlatformServices)), |
| 287 | ); |
| 288 | |
| 289 | it.effect("uses an installed workspace framework CLI", () => |
| 290 | Effect.gen(function* () { |
| 291 | const fs = yield* FileSystem.FileSystem; |
| 292 | const path = yield* Path.Path; |
| 293 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-workspace-cli-" }); |
| 294 | const appPath = path.join(root, "apps", "web"); |
| 295 | const binDir = path.join(root, "node_modules", ".bin"); |
| 296 | const nextBin = path.join(binDir, "next"); |
| 297 | yield* fs.makeDirectory(appPath, { recursive: true }); |
| 298 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 299 | yield* fs.writeFileString( |
| 300 | path.join(root, "package.json"), |
| 301 | JSON.stringify({ workspaces: ["apps/*"] }), |
| 302 | ); |
| 303 | yield* fs.writeFileString( |
| 304 | path.join(appPath, "package.json"), |
| 305 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 306 | ); |
| 307 | yield* fs.writeFileString( |
| 308 | nextBin, |
| 309 | [ |
| 310 | "#!/bin/sh", |
| 311 | "mkdir -p .next/standalone", |
| 312 | "printf 'workspace next server' > .next/standalone/server.js", |
| 313 | "", |
| 314 | ].join("\n"), |
| 315 | ); |
| 316 | yield* fs.chmod(nextBin, 0o755); |
| 317 | |
| 318 | const artifact = yield* runComputeAutoBuild({ appPath, framework: "nextjs" }); |
| 319 | |
| 320 | expect(yield* fs.readFileString(path.join(artifact.directory, "server.js"))).toBe( |
| 321 | "workspace next server", |
| 322 | ); |
| 323 | yield* artifact.cleanup; |
| 324 | }).pipe(Effect.provide(PlatformServices)), |
| 325 | ); |
| 326 | |
| 327 | it.effect("never downloads a missing framework CLI", () => |
| 328 | Effect.gen(function* () { |
| 329 | const fs = yield* FileSystem.FileSystem; |
| 330 | const path = yield* Path.Path; |
| 331 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-no-network-cli-" }); |
| 332 | const fakeBin = path.join(root, "fake-bin"); |
| 333 | const fallbackMarker = path.join(root, "network-fallback-ran"); |
| 334 | yield* fs.makeDirectory(fakeBin, { recursive: true }); |
| 335 | yield* fs.writeFileString( |
| 336 | path.join(root, "package.json"), |
| 337 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 338 | ); |
| 339 | for (const executable of ["npx", "bunx"]) { |
| 340 | const executablePath = path.join(fakeBin, executable); |
| 341 | yield* fs.writeFileString( |
| 342 | executablePath, |
| 343 | `#!/bin/sh\nprintf invoked > ${JSON.stringify(fallbackMarker)}\nexit 127\n`, |
| 344 | ); |
| 345 | yield* fs.chmod(executablePath, 0o755); |
| 346 | } |
| 347 | |
| 348 | const error = yield* runComputeAutoBuild({ |
| 349 | appPath: root, |
| 350 | framework: "nextjs", |
| 351 | env: { PATH: fakeBin }, |
| 352 | }).pipe(Effect.flip); |
| 353 | |
| 354 | expect(error).toBeInstanceOf(Error); |
| 355 | expect((error as Error).message).toContain("Could not find an installed Next.js CLI"); |
| 356 | expect(yield* fs.exists(fallbackMarker)).toBe(false); |
| 357 | }).pipe(Effect.provide(PlatformServices)), |
| 358 | ); |
| 359 | |
| 360 | it.effect("rejects framework symlinks that escape the output root", () => |
| 361 | Effect.gen(function* () { |
| 362 | const fs = yield* FileSystem.FileSystem; |
| 363 | const path = yield* Path.Path; |
| 364 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-link-escape-" }); |
| 365 | const binDir = path.join(root, "node_modules", ".bin"); |
| 366 | const nextBin = path.join(binDir, "next"); |
| 367 | const standalone = path.join(root, ".next", "standalone"); |
| 368 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 369 | yield* fs.makeDirectory(standalone, { recursive: true }); |
| 370 | yield* fs.writeFileString( |
| 371 | path.join(root, "package.json"), |
| 372 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 373 | ); |
| 374 | yield* fs.writeFileString(nextBin, "#!/bin/sh\nexit 0\n"); |
| 375 | yield* fs.chmod(nextBin, 0o755); |
| 376 | yield* fs.writeFileString(path.join(standalone, "server.js"), "console.log('server');"); |
| 377 | yield* fs.writeFileString(path.join(root, "outside.txt"), "secret"); |
| 378 | yield* fs.symlink("../../outside.txt", path.join(standalone, "outside.txt")); |
| 379 | |
| 380 | const error = yield* runComputeAutoBuild({ appPath: root, framework: "nextjs" }).pipe( |
| 381 | Effect.flip, |
| 382 | ); |
| 383 | |
| 384 | expect(error).toBeInstanceOf(Error); |
| 385 | expect((error as Error).message).toContain( |
| 386 | "Compute artifact path escapes its staging root", |
| 387 | ); |
| 388 | }).pipe(Effect.provide(PlatformServices)), |
| 389 | ); |
| 390 | |
| 391 | it.effect("rejects a framework output directory outside the application", () => |
| 392 | Effect.gen(function* () { |
| 393 | const fs = yield* FileSystem.FileSystem; |
| 394 | const path = yield* Path.Path; |
| 395 | const root = yield* fs.makeTempDirectory({ |
| 396 | prefix: "alchemy-prisma-auto-output-root-escape-", |
| 397 | }); |
| 398 | const outside = yield* fs.makeTempDirectory({ |
| 399 | prefix: "alchemy-prisma-auto-external-output-", |
| 400 | }); |
| 401 | const binDir = path.join(root, "node_modules", ".bin"); |
| 402 | const nextBin = path.join(binDir, "next"); |
| 403 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 404 | yield* fs.makeDirectory(path.join(root, ".next"), { recursive: true }); |
| 405 | yield* fs.writeFileString( |
| 406 | path.join(root, "package.json"), |
| 407 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 408 | ); |
| 409 | yield* fs.writeFileString(nextBin, "#!/bin/sh\nexit 0\n"); |
| 410 | yield* fs.chmod(nextBin, 0o755); |
| 411 | yield* fs.writeFileString(path.join(outside, "server.js"), "console.log('server');"); |
| 412 | yield* fs.symlink(outside, path.join(root, ".next", "standalone")); |
| 413 | |
| 414 | const error = yield* runComputeAutoBuild({ appPath: root, framework: "nextjs" }).pipe( |
| 415 | Effect.flip, |
| 416 | ); |
| 417 | |
| 418 | expect(error).toBeInstanceOf(Error); |
| 419 | expect((error as Error).message).toContain( |
| 420 | "Compute artifact path escapes its staging root", |
| 421 | ); |
| 422 | }).pipe(Effect.provide(PlatformServices)), |
| 423 | ); |
| 424 | |
| 425 | it.effect("rejects an oversized framework output file before copying it", () => |
| 426 | Effect.gen(function* () { |
| 427 | const fs = yield* FileSystem.FileSystem; |
| 428 | const path = yield* Path.Path; |
| 429 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-oversized-file-" }); |
| 430 | const binDir = path.join(root, "node_modules", ".bin"); |
| 431 | const nextBin = path.join(binDir, "next"); |
| 432 | const standalone = path.join(root, ".next", "standalone"); |
| 433 | const server = path.join(standalone, "server.js"); |
| 434 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 435 | yield* fs.makeDirectory(standalone, { recursive: true }); |
| 436 | yield* fs.writeFileString( |
| 437 | path.join(root, "package.json"), |
| 438 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 439 | ); |
| 440 | yield* fs.writeFileString(nextBin, "#!/bin/sh\nexit 0\n"); |
| 441 | yield* fs.chmod(nextBin, 0o755); |
| 442 | yield* fs.writeFileString(server, ""); |
| 443 | yield* fs.truncate(server, 128 * 1024 * 1024 + 1); |
| 444 | |
| 445 | const error = yield* runComputeAutoBuild({ appPath: root, framework: "nextjs" }).pipe( |
| 446 | Effect.flip, |
| 447 | ); |
| 448 | |
| 449 | expect(error).toBeInstanceOf(Error); |
| 450 | expect((error as Error).message).toContain("134217728 byte per-file safety limit"); |
| 451 | }).pipe(Effect.provide(PlatformServices)), |
| 452 | ); |
| 453 | |
| 454 | it.effect("detects nested Next.js standalone entrypoints in monorepos", () => |
| 455 | Effect.gen(function* () { |
| 456 | const fs = yield* FileSystem.FileSystem; |
| 457 | const path = yield* Path.Path; |
| 458 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-next-monorepo-" }); |
| 459 | const binDir = path.join(root, "node_modules", ".bin"); |
| 460 | const nextBin = path.join(binDir, "next"); |
| 461 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 462 | yield* fs.writeFileString( |
| 463 | path.join(root, "package.json"), |
| 464 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 465 | ); |
| 466 | yield* fs.writeFileString( |
| 467 | nextBin, |
| 468 | [ |
| 469 | "#!/bin/sh", |
| 470 | "mkdir -p .next/standalone/examples/prisma-nextjs .next/standalone/node_modules/pkg .next/static public", |
| 471 | "printf 'nested next server' > .next/standalone/examples/prisma-nextjs/server.js", |
| 472 | "printf 'dependency server' > .next/standalone/node_modules/pkg/server.js", |
| 473 | "printf 'next static' > .next/static/app.js", |
| 474 | "printf 'next public' > public/asset.txt", |
| 475 | "", |
| 476 | ].join("\n"), |
| 477 | ); |
| 478 | yield* fs.chmod(nextBin, 0o755); |
| 479 | |
| 480 | const artifact = yield* runComputeAutoBuild({ appPath: root, framework: "nextjs" }); |
| 481 | |
| 482 | expect(artifact.entrypoint).toBe("examples/prisma-nextjs/server.js"); |
| 483 | expect(yield* fs.readFileString(path.join(artifact.directory, artifact.entrypoint))).toBe( |
| 484 | "nested next server", |
| 485 | ); |
| 486 | expect( |
| 487 | yield* fs.readFileString( |
| 488 | path.join(artifact.directory, "examples", "prisma-nextjs", ".next", "static", "app.js"), |
| 489 | ), |
| 490 | ).toBe("next static"); |
| 491 | expect( |
| 492 | yield* fs.readFileString( |
| 493 | path.join(artifact.directory, "examples", "prisma-nextjs", "public", "asset.txt"), |
| 494 | ), |
| 495 | ).toBe("next public"); |
| 496 | |
| 497 | yield* artifact.cleanup; |
| 498 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 499 | }).pipe(Effect.provide(PlatformServices)), |
| 500 | ); |
| 501 | |
| 502 | it.effect("rejects ambiguous Next.js standalone entrypoints", () => |
| 503 | Effect.gen(function* () { |
| 504 | const fs = yield* FileSystem.FileSystem; |
| 505 | const path = yield* Path.Path; |
| 506 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-auto-next-ambiguous-" }); |
| 507 | const binDir = path.join(root, "node_modules", ".bin"); |
| 508 | const nextBin = path.join(binDir, "next"); |
| 509 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 510 | yield* fs.writeFileString( |
| 511 | path.join(root, "package.json"), |
| 512 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 513 | ); |
| 514 | yield* fs.writeFileString( |
| 515 | nextBin, |
| 516 | [ |
| 517 | "#!/bin/sh", |
| 518 | "mkdir -p .next/standalone/apps/web .next/standalone/apps/admin", |
| 519 | "printf 'web server' > .next/standalone/apps/web/server.js", |
| 520 | "printf 'admin server' > .next/standalone/apps/admin/server.js", |
| 521 | "", |
| 522 | ].join("\n"), |
| 523 | ); |
| 524 | yield* fs.chmod(nextBin, 0o755); |
| 525 | |
| 526 | const error = yield* runComputeAutoBuild({ appPath: root, framework: "nextjs" }).pipe( |
| 527 | Effect.flip, |
| 528 | ); |
| 529 | |
| 530 | expect(error).toBeInstanceOf(Error); |
| 531 | expect((error as Error).message).toContain("multiple server.js files"); |
| 532 | }).pipe(Effect.provide(PlatformServices)), |
| 533 | ); |
| 534 | |
| 535 | it.effect("materializes Bun package aliases for Next.js standalone output", () => |
| 536 | Effect.gen(function* () { |
| 537 | const fs = yield* FileSystem.FileSystem; |
| 538 | const path = yield* Path.Path; |
| 539 | const root = yield* fs.makeTempDirectory({ |
| 540 | prefix: "alchemy-prisma-auto-next-bun-aliases-", |
| 541 | }); |
| 542 | const binDir = path.join(root, "node_modules", ".bin"); |
| 543 | const nextBin = path.join(binDir, "next"); |
| 544 | yield* fs.makeDirectory(binDir, { recursive: true }); |
| 545 | yield* fs.writeFileString( |
| 546 | path.join(root, "package.json"), |
| 547 | JSON.stringify({ dependencies: { next: "0.0.0-test" } }), |
| 548 | ); |
| 549 | yield* fs.writeFileString( |
| 550 | nextBin, |
| 551 | [ |
| 552 | "#!/bin/sh", |
| 553 | "set -eu", |
| 554 | "mkdir -p .next/standalone/examples/prisma-nextjs", |
| 555 | "mkdir -p .next/standalone/node_modules/.bun/@swc+helpers@0.5.15/node_modules/@swc/helpers", |
| 556 | "mkdir -p .next/standalone/node_modules/.bun/node_modules/@swc", |
| 557 | "printf 'nested next server' > .next/standalone/examples/prisma-nextjs/server.js", |
| 558 | 'printf \'{"name":"@swc/helpers"}\' > .next/standalone/node_modules/.bun/@swc+helpers@0.5.15/node_modules/@swc/helpers/package.json', |
| 559 | "ln -s ../../@swc+helpers@0.5.15/node_modules/@swc/helpers .next/standalone/node_modules/.bun/node_modules/@swc/helpers", |
| 560 | "", |
| 561 | ].join("\n"), |
| 562 | ); |
| 563 | yield* fs.chmod(nextBin, 0o755); |
| 564 | |
| 565 | const artifact = yield* runComputeAutoBuild({ appPath: root, framework: "nextjs" }); |
| 566 | |
| 567 | expect(artifact.entrypoint).toBe("examples/prisma-nextjs/server.js"); |
| 568 | expect( |
| 569 | yield* fs.readFileString( |
| 570 | path.join(artifact.directory, "node_modules", "@swc", "helpers", "package.json"), |
| 571 | ), |
| 572 | ).toBe('{"name":"@swc/helpers"}'); |
| 573 | |
| 574 | yield* artifact.cleanup; |
| 575 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 576 | }).pipe(Effect.provide(PlatformServices)), |
| 577 | ); |
| 578 | |
| 579 | it.live("auto-detects a Vite static SPA", () => |
| 580 | Effect.gen(function* () { |
| 581 | const fs = yield* FileSystem.FileSystem; |
| 582 | const path = yield* Path.Path; |
| 583 | const { root } = yield* copyStaticBuildFixture; |
| 584 | const artifact = yield* runComputeAutoBuild({ appPath: root }); |
| 585 | yield* Effect.addFinalizer(() => artifact.cleanup); |
| 586 | const index = yield* fs.readFileString(path.join(root, "dist/index.html")); |
| 587 | |
| 588 | expect(artifact.entrypoint).toBe("server.mjs"); |
| 589 | expect(artifact.defaultPort).toBe(8080); |
| 590 | expect(artifact.archiveIgnorePrefix).toBe("public"); |
| 591 | expect(artifact.requiredFiles).toEqual(["public/index.html"]); |
| 592 | expect(index).toContain("<h1>app shell</h1>"); |
| 593 | expect(index).toContain("/assets/app.js"); |
| 594 | expect( |
| 595 | yield* fs.readFileString(path.join(artifact.directory, "public", "index.html")), |
| 596 | ).toBe(index); |
| 597 | |
| 598 | yield* withStaticSiteServer(artifact.directory, (origin) => |
| 599 | Effect.gen(function* () { |
| 600 | const response = yield* HttpClient.get(`${origin}/dashboard`); |
| 601 | expect(response.status).toBe(200); |
| 602 | expect(yield* response.text).toBe(index); |
| 603 | }), |
| 604 | ); |
| 605 | |
| 606 | yield* artifact.cleanup; |
| 607 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 608 | }).pipe(Effect.provide(PlatformServices)), |
| 609 | ); |
| 610 | |
| 611 | it.live("detects TanStack Start before the Vite fallback", () => |
| 612 | Effect.gen(function* () { |
| 613 | const fs = yield* FileSystem.FileSystem; |
| 614 | const path = yield* Path.Path; |
| 615 | const { root } = yield* copyStaticBuildFixture; |
| 616 | // Declaring Start without its plugins must not silently deploy a SPA. |
| 617 | yield* fs.writeFileString( |
| 618 | path.join(root, "package.json"), |
| 619 | JSON.stringify({ |
| 620 | type: "module", |
| 621 | dependencies: { "@tanstack/react-start": "*" }, |
| 622 | devDependencies: { vite: "*" }, |
| 623 | }), |
| 624 | ); |
| 625 | |
| 626 | const error = yield* runComputeAutoBuild({ appPath: root }).pipe(Effect.flip); |
| 627 | |
| 628 | expect((error as Error).message).toContain( |
| 629 | "TanStack Start build did not produce a Nitro node server entrypoint at .output/server/index.mjs", |
| 630 | ); |
| 631 | expect(yield* fs.readFileString(path.join(root, "dist/index.html"))).toContain( |
| 632 | "/assets/app.js", |
| 633 | ); |
| 634 | expect(yield* fs.exists(path.join(root, ".output/server/index.mjs"))).toBe(false); |
| 635 | }).pipe(Effect.provide(PlatformServices)), |
| 636 | ); |
| 637 | |
| 638 | it.effect("bounds retained build stdout", () => |
| 639 | Effect.gen(function* () { |
| 640 | const error = yield* runBuildCommand({ |
| 641 | command: "printf '123456789'", |
| 642 | outputLimitBytes: 8, |
| 643 | }).pipe(Effect.flip); |
| 644 | |
| 645 | expect(error).toBeInstanceOf(Error); |
| 646 | expect((error as Error).message).toContain( |
| 647 | "Build stdout exceeded the 8 byte output safety limit", |
| 648 | ); |
| 649 | }).pipe(Effect.provide(PlatformServices)), |
| 650 | ); |
| 651 | |
| 652 | it.effect("bounds retained build stderr", () => |
| 653 | Effect.gen(function* () { |
| 654 | const error = yield* runBuildCommand({ |
| 655 | command: "printf '123456789' >&2", |
| 656 | outputLimitBytes: 8, |
| 657 | }).pipe(Effect.flip); |
| 658 | |
| 659 | expect(error).toBeInstanceOf(Error); |
| 660 | expect((error as Error).message).toContain( |
| 661 | "Build stderr exceeded the 8 byte output safety limit", |
| 662 | ); |
| 663 | }).pipe(Effect.provide(PlatformServices)), |
| 664 | ); |
| 665 | |
| 666 | it.effect("does not expose failed build output in errors", () => |
| 667 | Effect.gen(function* () { |
| 668 | const error = yield* runBuildCommand({ |
| 669 | command: "printf 'BUILD_SECRET_SENTINEL' >&2; exit 17", |
| 670 | }).pipe(Effect.flip); |
| 671 | |
| 672 | expect(error).toBeInstanceOf(Error); |
| 673 | expect((error as Error).message).toContain("exit code 17"); |
| 674 | expect((error as Error).message).toContain("stderr: 21 bytes"); |
| 675 | expect((error as Error).message).not.toContain("BUILD_SECRET_SENTINEL"); |
| 676 | }).pipe(Effect.provide(PlatformServices)), |
| 677 | ); |
| 678 | |
| 679 | it.effect("withholds ambient Prisma credentials from build commands", () => |
| 680 | Effect.gen(function* () { |
| 681 | const previousServiceToken = process.env.PRISMA_SERVICE_TOKEN; |
| 682 | const previousApiToken = process.env.PRISMA_API_TOKEN; |
| 683 | const previousInherited = process.env.ALCHEMY_BUILD_INHERITED_SENTINEL; |
| 684 | process.env.PRISMA_SERVICE_TOKEN = "ambient-service-token"; |
| 685 | process.env.PRISMA_API_TOKEN = "ambient-api-token"; |
| 686 | process.env.ALCHEMY_BUILD_INHERITED_SENTINEL = "inherited-value"; |
| 687 | |
| 688 | try { |
| 689 | const result = yield* runBuildCommand({ command: inspectBuildEnvironmentCommand }); |
| 690 | |
| 691 | expect(JSON.parse(result.stdout)).toEqual({ |
| 692 | serviceToken: null, |
| 693 | apiToken: null, |
| 694 | inherited: "inherited-value", |
| 695 | }); |
| 696 | } finally { |
| 697 | restoreProcessEnv("PRISMA_SERVICE_TOKEN", previousServiceToken); |
| 698 | restoreProcessEnv("PRISMA_API_TOKEN", previousApiToken); |
| 699 | restoreProcessEnv("ALCHEMY_BUILD_INHERITED_SENTINEL", previousInherited); |
| 700 | } |
| 701 | }).pipe(Effect.provide(PlatformServices)), |
| 702 | ); |
| 703 | |
| 704 | it.effect("allows explicitly configured Prisma build credentials", () => |
| 705 | Effect.gen(function* () { |
| 706 | const result = yield* runBuildCommand({ |
| 707 | command: inspectBuildEnvironmentCommand, |
| 708 | env: { |
| 709 | PRISMA_SERVICE_TOKEN: "explicit-service-token", |
| 710 | PRISMA_API_TOKEN: "explicit-api-token", |
| 711 | }, |
| 712 | }); |
| 713 | |
| 714 | expect(JSON.parse(result.stdout)).toEqual({ |
| 715 | serviceToken: "explicit-service-token", |
| 716 | apiToken: "explicit-api-token", |
| 717 | inherited: null, |
| 718 | }); |
| 719 | }).pipe(Effect.provide(PlatformServices)), |
| 720 | ); |
| 721 | |
| 722 | it.live("interrupts a silent build at its configured deadline", () => |
| 723 | Effect.gen(function* () { |
| 724 | const error = yield* runBuildCommand({ command: "sleep 10", timeoutSeconds: 0.05 }).pipe( |
| 725 | Effect.flip, |
| 726 | ); |
| 727 | |
| 728 | expect(error).toBeInstanceOf(Error); |
| 729 | expect((error as Error).message).toContain("Build command timed out after 0.05 seconds"); |
| 730 | }).pipe(Effect.provide(PlatformServices)), |
| 731 | ); |
| 732 | |
| 733 | it.live("kills descendant processes when a build times out", () => |
| 734 | Effect.gen(function* () { |
| 735 | if (process.platform === "win32") return; |
| 736 | const fs = yield* FileSystem.FileSystem; |
| 737 | const path = yield* Path.Path; |
| 738 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-build-process-group-" }); |
| 739 | const marker = path.join(root, "descendant-survived"); |
| 740 | |
| 741 | yield* runBuildCommand({ |
| 742 | command: `(sleep 0.4; printf survived > ${JSON.stringify(marker)}) & sleep 10`, |
| 743 | timeoutSeconds: 0.05, |
| 744 | }).pipe(Effect.flip); |
| 745 | yield* Effect.sleep("700 millis"); |
| 746 | |
| 747 | expect(yield* fs.exists(marker)).toBe(false); |
| 748 | }).pipe(Effect.provide(PlatformServices)), |
| 749 | ); |
| 750 | |
| 751 | it.live("reaps descendants left by a successful build shell", () => |
| 752 | Effect.gen(function* () { |
| 753 | if (process.platform === "win32") return; |
| 754 | const fs = yield* FileSystem.FileSystem; |
| 755 | const path = yield* Path.Path; |
| 756 | const root = yield* fs.makeTempDirectory({ |
| 757 | prefix: "alchemy-prisma-build-success-process-group-", |
| 758 | }); |
| 759 | const marker = path.join(root, "descendant-survived"); |
| 760 | |
| 761 | yield* runBuildCommand({ |
| 762 | command: `(sleep 0.4; printf survived > ${JSON.stringify(marker)}) >/dev/null 2>&1 &`, |
| 763 | }); |
| 764 | yield* Effect.sleep("700 millis"); |
| 765 | |
| 766 | expect(yield* fs.exists(marker)).toBe(false); |
| 767 | }).pipe(Effect.provide(PlatformServices)), |
| 768 | ); |
| 769 | }, |
| 770 | ); |
| 771 | |
| 772 | describe( |
| 773 | "Prisma Compute static-site build", |
| 774 | { tags: ["provider:prisma", "provider:prisma:compute", "local"] }, |
| 775 | () => { |
| 776 | it.live( |
| 777 | "packages a prebuilt index without running a command", |
| 778 | () => |
| 779 | Effect.gen(function* () { |
| 780 | const fs = yield* FileSystem.FileSystem; |
| 781 | const path = yield* Path.Path; |
| 782 | const root = yield* fs.makeTempDirectoryScoped({ |
| 783 | prefix: "alchemy-prisma-static-index-", |
| 784 | }); |
| 785 | yield* fs.writeFileString(path.join(root, "index.html"), "hello static"); |
| 786 | |
| 787 | const artifact = yield* runComputeStaticBuild({ appPath: root, outdir: "." }); |
| 788 | yield* Effect.addFinalizer(() => artifact.cleanup); |
| 789 | |
| 790 | yield* withStaticSiteServer(artifact.directory, (origin) => |
| 791 | Effect.gen(function* () { |
| 792 | const response = yield* HttpClient.get(`${origin}/`); |
| 793 | expect(response.status).toBe(200); |
| 794 | expect(yield* response.text).toBe("hello static"); |
| 795 | }), |
| 796 | ); |
| 797 | |
| 798 | yield* artifact.cleanup; |
| 799 | }).pipe(Effect.provide(PlatformServices)), |
| 800 | { tags: ["unit"] }, |
| 801 | ); |
| 802 | |
| 803 | it.live("packages and serves static files with SPA fallback", () => |
| 804 | Effect.gen(function* () { |
| 805 | const fs = yield* FileSystem.FileSystem; |
| 806 | const path = yield* Path.Path; |
| 807 | const { root, command } = yield* copyStaticBuildFixture; |
| 808 | const artifact = yield* runComputeStaticBuild({ |
| 809 | appPath: root, |
| 810 | command, |
| 811 | outdir: "dist", |
| 812 | spa: true, |
| 813 | }); |
| 814 | yield* Effect.addFinalizer(() => artifact.cleanup); |
| 815 | const index = yield* fs.readFileString(path.join(root, "dist/index.html")); |
| 816 | const script = yield* fs.readFileString(path.join(root, "dist/assets/app.js")); |
| 817 | const scriptStat = yield* fs.stat(path.join(root, "dist/assets/app.js")); |
| 818 | const stylesheet = yield* fs.readFileString(path.join(root, "dist/assets/index.css")); |
| 819 | const docs = yield* fs.readFileString(path.join(root, "dist/docs/index.html")); |
| 820 | |
| 821 | expect(artifact.requiredFiles).toEqual(["public/index.html"]); |
| 822 | expect(artifact.entrypoint).toBe("server.mjs"); |
| 823 | expect(artifact.defaultPort).toBe(8080); |
| 824 | expect(index).toContain("<h1>app shell</h1>"); |
| 825 | expect(script).toMatch(/console\.log\(["'`]asset["'`]\)/); |
| 826 | expect( |
| 827 | yield* fs.readFileString(path.join(artifact.directory, "public", "index.html")), |
| 828 | ).toBe(index); |
| 829 | expect(yield* fs.exists(path.join(root, "server.mjs"))).toBe(false); |
| 830 | |
| 831 | const archive = yield* createComputeArchive({ |
| 832 | directory: artifact.directory, |
| 833 | entrypoint: artifact.entrypoint, |
| 834 | ignore: ["assets/*.map", "*.mjs"], |
| 835 | ignorePrefix: artifact.archiveIgnorePrefix, |
| 836 | requiredFiles: artifact.requiredFiles, |
| 837 | }); |
| 838 | const excludedIndexError = yield* createComputeArchive({ |
| 839 | directory: artifact.directory, |
| 840 | entrypoint: artifact.entrypoint, |
| 841 | ignore: ["index.html"], |
| 842 | ignorePrefix: artifact.archiveIgnorePrefix, |
| 843 | requiredFiles: artifact.requiredFiles, |
| 844 | }).pipe(Effect.flip); |
| 845 | expect(excludedIndexError.message).toContain( |
| 846 | "Required file not found in compute artifact: public/index.html", |
| 847 | ); |
| 848 | const extracted = yield* extractStaticArchive(archive); |
| 849 | expect(yield* fs.exists(path.join(extracted, "server.mjs"))).toBe(true); |
| 850 | expect(yield* fs.exists(path.join(extracted, "public/assets/app.mjs"))).toBe(true); |
| 851 | expect(yield* fs.exists(path.join(extracted, "public/assets/app.js.map"))).toBe(false); |
| 852 | |
| 853 | for (const directory of [artifact.directory, extracted]) { |
| 854 | yield* withStaticSiteServer(directory, (origin) => |
| 855 | Effect.gen(function* () { |
| 856 | const rootResponse = yield* HttpClient.get(`${origin}/`); |
| 857 | expect(rootResponse.status).toBe(200); |
| 858 | expect(rootResponse.headers["content-type"]).toContain("text/html"); |
| 859 | expect(yield* rootResponse.text).toBe(index); |
| 860 | |
| 861 | const assetResponse = yield* HttpClient.get(`${origin}/assets/app.js`); |
| 862 | expect(assetResponse.status).toBe(200); |
| 863 | expect(assetResponse.headers["content-type"]).toContain("javascript"); |
| 864 | expect(yield* assetResponse.text).toBe(script); |
| 865 | |
| 866 | const cssResponse = yield* HttpClient.get(`${origin}/assets/index.css`); |
| 867 | expect(cssResponse.status).toBe(200); |
| 868 | expect(cssResponse.headers["content-type"]).toContain("text/css"); |
| 869 | expect(yield* cssResponse.text).toBe(stylesheet); |
| 870 | |
| 871 | const directoryResponse = yield* HttpClient.get(`${origin}/docs/`); |
| 872 | expect(directoryResponse.status).toBe(200); |
| 873 | expect(yield* directoryResponse.text).toBe(docs); |
| 874 | yield* assertDirectoryRedirects(origin); |
| 875 | |
| 876 | const directoryHead = yield* HttpClient.head(`${origin}/docs/`); |
| 877 | expect(directoryHead.status).toBe(200); |
| 878 | expect(directoryHead.headers["content-type"]).toContain("text/html"); |
| 879 | expect(yield* directoryHead.text).toBe(""); |
| 880 | |
| 881 | const encodedResponse = yield* HttpClient.get(`${origin}/docs%20%231.html`); |
| 882 | expect(encodedResponse.status).toBe(200); |
| 883 | expect(yield* encodedResponse.text).toBe("encoded asset\n"); |
| 884 | |
| 885 | const fallbackResponse = yield* HttpClient.get(`${origin}/dashboard/settings`); |
| 886 | expect(fallbackResponse.status).toBe(200); |
| 887 | expect(yield* fallbackResponse.text).toBe(index); |
| 888 | |
| 889 | const fallbackHead = yield* HttpClient.head(`${origin}/dashboard/settings`); |
| 890 | expect(fallbackHead.status).toBe(200); |
| 891 | expect(yield* fallbackHead.text).toBe(""); |
| 892 | |
| 893 | const headResponse = yield* HttpClient.head(`${origin}/assets/app.js`); |
| 894 | expect(headResponse.status).toBe(200); |
| 895 | expect(headResponse.headers["content-length"]).toBe(String(scriptStat.size)); |
| 896 | expect(headResponse.headers["content-type"]).toBe( |
| 897 | assetResponse.headers["content-type"], |
| 898 | ); |
| 899 | expect(yield* headResponse.text).toBe(""); |
| 900 | |
| 901 | const postResponse = yield* HttpClient.post(`${origin}/`); |
| 902 | expect(postResponse.status).toBe(405); |
| 903 | expect(postResponse.headers["allow"]).toBe("GET, HEAD"); |
| 904 | expect(yield* postResponse.text).toBe("Method Not Allowed"); |
| 905 | |
| 906 | for (const invalidPath of [ |
| 907 | "/..%2Fsecret", |
| 908 | "/%2e%2e%2fsecret", |
| 909 | "/%00", |
| 910 | "/%E0%A4%A", |
| 911 | "/%ZZ", |
| 912 | ]) { |
| 913 | const invalidResponse = yield* HttpClient.get(`${origin}${invalidPath}`); |
| 914 | expect(invalidResponse.status).toBe(400); |
| 915 | expect(yield* invalidResponse.text).toBe("Bad Request"); |
| 916 | } |
| 917 | }), |
| 918 | ); |
| 919 | } |
| 920 | |
| 921 | yield* artifact.cleanup; |
| 922 | expect(yield* fs.exists(artifact.directory)).toBe(false); |
| 923 | }).pipe(Effect.provide(PlatformServices)), |
| 924 | ); |
| 925 | |
| 926 | it.live("serves a custom root page without enabling SPA fallback", () => |
| 927 | Effect.gen(function* () { |
| 928 | const fs = yield* FileSystem.FileSystem; |
| 929 | const path = yield* Path.Path; |
| 930 | const { root, command } = yield* copyStaticBuildFixture; |
| 931 | const artifact = yield* runComputeStaticBuild({ |
| 932 | appPath: root, |
| 933 | command, |
| 934 | outdir: "dist", |
| 935 | indexPage: "home.html", |
| 936 | }); |
| 937 | yield* Effect.addFinalizer(() => artifact.cleanup); |
| 938 | const docs = yield* fs.readFileString(path.join(root, "dist/docs/index.html")); |
| 939 | expect(artifact.requiredFiles).toEqual(["public/home.html"]); |
| 940 | const archive = yield* createComputeArchive({ |
| 941 | directory: artifact.directory, |
| 942 | entrypoint: artifact.entrypoint, |
| 943 | requiredFiles: artifact.requiredFiles, |
| 944 | }); |
| 945 | const excludedIndexError = yield* createComputeArchive({ |
| 946 | directory: artifact.directory, |
| 947 | entrypoint: artifact.entrypoint, |
| 948 | ignore: ["home.html"], |
| 949 | ignorePrefix: artifact.archiveIgnorePrefix, |
| 950 | requiredFiles: artifact.requiredFiles, |
| 951 | }).pipe(Effect.flip); |
| 952 | expect(excludedIndexError.message).toContain( |
| 953 | "Required file not found in compute artifact: public/home.html", |
| 954 | ); |
| 955 | const extracted = yield* extractStaticArchive(archive); |
| 956 | |
| 957 | for (const directory of [artifact.directory, extracted]) { |
| 958 | yield* withStaticSiteServer(directory, (origin) => |
| 959 | Effect.gen(function* () { |
| 960 | const rootResponse = yield* HttpClient.get(`${origin}/`); |
| 961 | expect(rootResponse.status).toBe(200); |
| 962 | expect(yield* rootResponse.text).toBe("static home\n"); |
| 963 | |
| 964 | const rootHead = yield* HttpClient.head(`${origin}/`); |
| 965 | expect(rootHead.status).toBe(200); |
| 966 | expect(rootHead.headers["content-length"]).toBe("12"); |
| 967 | expect(yield* rootHead.text).toBe(""); |
| 968 | |
| 969 | const directoryResponse = yield* HttpClient.get(`${origin}/docs/`); |
| 970 | expect(directoryResponse.status).toBe(200); |
| 971 | expect(yield* directoryResponse.text).toBe(docs); |
| 972 | yield* assertDirectoryRedirects(origin); |
| 973 | |
| 974 | const missingResponse = yield* HttpClient.get(`${origin}/missing`); |
| 975 | expect(missingResponse.status).toBe(404); |
| 976 | expect(yield* missingResponse.text).toBe("Not Found"); |
| 977 | |
| 978 | const missingHead = yield* HttpClient.head(`${origin}/missing`); |
| 979 | expect(missingHead.status).toBe(404); |
| 980 | expect(yield* missingHead.text).toBe(""); |
| 981 | }), |
| 982 | ); |
| 983 | } |
| 984 | |
| 985 | yield* artifact.cleanup; |
| 986 | }).pipe(Effect.provide(PlatformServices)), |
| 987 | ); |
| 988 | |
| 989 | it.effect( |
| 990 | "scopes archive ignores to the static output", |
| 991 | () => |
| 992 | Effect.gen(function* () { |
| 993 | const fs = yield* FileSystem.FileSystem; |
| 994 | const path = yield* Path.Path; |
| 995 | const root = yield* fs.makeTempDirectory({ |
| 996 | prefix: "alchemy-prisma-static-site-ignore-", |
| 997 | }); |
| 998 | yield* fs.makeDirectory(path.join(root, "dist", "assets"), { recursive: true }); |
| 999 | yield* fs.writeFileString(path.join(root, "dist", "index.html"), "static home"); |
| 1000 | yield* fs.writeFileString(path.join(root, "dist", "assets", "app.mjs"), "app"); |
| 1001 | yield* fs.writeFileString(path.join(root, "dist", "assets", "app.js.map"), "source map"); |
| 1002 | |
| 1003 | const artifact = yield* runComputeStaticBuild({ |
| 1004 | appPath: root, |
| 1005 | command: "true", |
| 1006 | outdir: "dist", |
| 1007 | }); |
| 1008 | const archive = yield* createComputeArchive({ |
| 1009 | directory: artifact.directory, |
| 1010 | entrypoint: artifact.entrypoint, |
| 1011 | ignore: ["assets/*.map", "*.mjs"], |
| 1012 | ignorePrefix: artifact.archiveIgnorePrefix, |
| 1013 | requiredFiles: artifact.requiredFiles, |
| 1014 | }).pipe(Effect.ensuring(artifact.cleanup)); |
| 1015 | const tarText = yield* Effect.sync(() => new TextDecoder().decode(gunzipSync(archive))); |
| 1016 | |
| 1017 | expect(tarText).toContain("bundle/server.mjs"); |
| 1018 | expect(tarText).toContain("bundle/public/index.html"); |
| 1019 | expect(tarText).toContain("bundle/public/assets/app.mjs"); |
| 1020 | expect(tarText).not.toContain("bundle/public/assets/app.js.map"); |
| 1021 | }).pipe(Effect.provide(PlatformServices)), |
| 1022 | { tags: ["unit"] }, |
| 1023 | ); |
| 1024 | |
| 1025 | it.effect( |
| 1026 | "rejects unsafe output and index paths", |
| 1027 | () => |
| 1028 | Effect.gen(function* () { |
| 1029 | const fs = yield* FileSystem.FileSystem; |
| 1030 | const root = yield* fs.makeTempDirectory({ prefix: "alchemy-prisma-static-site-paths-" }); |
| 1031 | |
| 1032 | const outdirError = yield* runComputeStaticBuild({ |
| 1033 | appPath: root, |
| 1034 | command: "true", |
| 1035 | outdir: "../dist", |
| 1036 | }).pipe(Effect.flip); |
| 1037 | expect((outdirError as Error).message).toContain("outdir must be a relative path"); |
| 1038 | |
| 1039 | const indexError = yield* runComputeStaticBuild({ |
| 1040 | appPath: root, |
| 1041 | command: "true", |
| 1042 | outdir: "dist", |
| 1043 | indexPage: "../index.html", |
| 1044 | }).pipe(Effect.flip); |
| 1045 | expect((indexError as Error).message).toContain("indexPage must be a relative file path"); |
| 1046 | }).pipe(Effect.provide(PlatformServices)), |
| 1047 | { tags: ["unit"] }, |
| 1048 | ); |
| 1049 | |
| 1050 | it.effect( |
| 1051 | "fails when the build output or index page is missing", |
| 1052 | () => |
| 1053 | Effect.gen(function* () { |
| 1054 | const fs = yield* FileSystem.FileSystem; |
| 1055 | const path = yield* Path.Path; |
| 1056 | const root = yield* fs.makeTempDirectory({ |
| 1057 | prefix: "alchemy-prisma-static-site-missing-", |
| 1058 | }); |
| 1059 | |
| 1060 | const outputError = yield* runComputeStaticBuild({ |
| 1061 | appPath: root, |
| 1062 | command: "true", |
| 1063 | outdir: "dist", |
| 1064 | }).pipe(Effect.flip); |
| 1065 | expect((outputError as Error).message).toContain("did not produce an output directory"); |
| 1066 | |
| 1067 | yield* fs.makeDirectory(path.join(root, "dist")); |
| 1068 | const indexError = yield* runComputeStaticBuild({ |
| 1069 | appPath: root, |
| 1070 | command: "true", |
| 1071 | outdir: "dist", |
| 1072 | }).pipe(Effect.flip); |
| 1073 | expect((indexError as Error).message).toContain("did not produce index.html"); |
| 1074 | }).pipe(Effect.provide(PlatformServices)), |
| 1075 | { tags: ["unit"] }, |
| 1076 | ); |
| 1077 | }, |
| 1078 | ); |
| 1079 | |
| 1080 | const assertDirectoryRedirects = Effect.fn(function* (origin: string) { |
| 1081 | const query = "?from=%2Fclient%2Froute&next=%2F%2Fevil.example"; |
| 1082 | for (const pathname of ["/docs", "//docs", "/%2Fdocs"]) { |
| 1083 | for (const method of ["GET", "HEAD"] as const) { |
| 1084 | const url = `${origin}${pathname}${query}`; |
| 1085 | const response = yield* (method === "GET" ? HttpClient.get(url) : HttpClient.head(url)).pipe( |
| 1086 | Effect.provideService(FetchHttpClient.RequestInit, { redirect: "manual" }), |
| 1087 | ); |
| 1088 | expect(response.status).toBe(301); |
| 1089 | const location = response.headers["location"]; |
| 1090 | expect(location).toBe(pathname === "/%2Fdocs" ? `/%2Fdocs/${query}` : `/docs/${query}`); |
| 1091 | const target = yield* Effect.sync(() => new URL(location!, origin)); |
| 1092 | expect(target.origin).toBe(origin); |
| 1093 | expect(target.search).toBe(query); |
| 1094 | expect(decodeURIComponent(target.pathname).replace(/^\/+/, "/")).toBe("/docs/"); |
| 1095 | expect(yield* response.text).toBe(""); |
| 1096 | |
| 1097 | const assetUrl = yield* Effect.sync(() => new URL("./style.css", target).href); |
| 1098 | const asset = yield* HttpClient.get(assetUrl); |
| 1099 | expect(asset.status).toBe(200); |
| 1100 | expect(asset.headers["content-type"]).toContain("text/css"); |
| 1101 | expect(yield* asset.text).toContain(".directory-marker"); |
| 1102 | } |
| 1103 | } |
| 1104 | }); |
| 1105 | |
| 1106 | const copyStaticBuildFixture = Effect.gen(function* () { |
| 1107 | const fs = yield* FileSystem.FileSystem; |
| 1108 | const path = yield* Path.Path; |
| 1109 | const fixture = yield* path.fromFileUrl( |
| 1110 | new URL("./fixtures/compute-static-build/", import.meta.url), |
| 1111 | ); |
| 1112 | const nodeModules = yield* path.fromFileUrl(new URL("../../node_modules/", import.meta.url)); |
| 1113 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-prisma-static-build-" }); |
| 1114 | yield* fs.copy(fixture, root); |
| 1115 | yield* fs.symlink(nodeModules, path.join(root, "node_modules")); |
| 1116 | const executable = yield* Effect.sync(() => process.execPath); |
| 1117 | return { |
| 1118 | root, |
| 1119 | command: `${JSON.stringify(executable)} ${JSON.stringify(path.join(nodeModules, "vite/bin/vite.js"))} build`, |
| 1120 | }; |
| 1121 | }); |
| 1122 | |
| 1123 | const extractStaticArchive = Effect.fn(function* (archive: Uint8Array) { |
| 1124 | const fs = yield* FileSystem.FileSystem; |
| 1125 | const path = yield* Path.Path; |
| 1126 | const spawner = yield* ChildProcessSpawner; |
| 1127 | const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-prisma-static-archive-" }); |
| 1128 | const archivePath = path.join(root, "bundle.tar.gz"); |
| 1129 | yield* fs.writeFile(archivePath, archive); |
| 1130 | const handle = yield* spawner.spawn( |
| 1131 | ChildProcess.make("tar", ["-xzf", archivePath, "-C", root], { |
| 1132 | stdin: "ignore", |
| 1133 | stdout: "ignore", |
| 1134 | stderr: "inherit", |
| 1135 | }), |
| 1136 | ); |
| 1137 | expect(yield* handle.exitCode).toBe(0); |
| 1138 | return path.join(root, "bundle"); |
| 1139 | }); |
| 1140 | |
| 1141 | const withStaticSiteServer = <A, E, R>( |
| 1142 | directory: string, |
| 1143 | use: (origin: string) => Effect.Effect<A, E, R>, |
| 1144 | ) => |
| 1145 | Effect.scoped( |
| 1146 | Effect.gen(function* () { |
| 1147 | const spawner = yield* ChildProcessSpawner; |
| 1148 | const port = yield* findAvailablePort(); |
| 1149 | const executable = yield* Effect.sync(() => process.execPath); |
| 1150 | yield* spawner.spawn( |
| 1151 | ChildProcess.make(executable, ["server.mjs"], { |
| 1152 | cwd: directory, |
| 1153 | env: { PORT: String(port) }, |
| 1154 | stdin: "ignore", |
| 1155 | stdout: "ignore", |
| 1156 | stderr: "inherit", |
| 1157 | killSignal: "SIGKILL", |
| 1158 | }), |
| 1159 | ); |
| 1160 | const origin = `http://127.0.0.1:${port}`; |
| 1161 | yield* HttpClient.head(`${origin}/`).pipe( |
| 1162 | Effect.flatMap((response) => |
| 1163 | response.status === 200 |
| 1164 | ? Effect.void |
| 1165 | : Effect.fail(new Error(`Static server readiness returned ${response.status}`)), |
| 1166 | ), |
| 1167 | Effect.timeout("1 second"), |
| 1168 | Effect.retry({ schedule: Schedule.spaced("100 millis"), times: 10 }), |
| 1169 | ); |
| 1170 | return yield* use(origin); |
| 1171 | }), |
| 1172 | ).pipe(Effect.provide(FetchHttpClient.layer)); |