|
| 1 | +import { LoaderFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { prisma } from "~/db.server"; |
| 4 | +import { |
| 5 | + authenticateProjectApiKeyOrPersonalAccessToken, |
| 6 | + authenticatedEnvironmentForAuthentication, |
| 7 | +} from "~/services/apiAuth.server"; |
| 8 | +import zlib from "node:zlib"; |
| 9 | + |
| 10 | +const ParamsSchema = z.object({ |
| 11 | + projectRef: z.string(), |
| 12 | + envSlug: z.string(), |
| 13 | + version: z.string(), |
| 14 | +}); |
| 15 | + |
| 16 | +export async function loader({ params, request }: LoaderFunctionArgs) { |
| 17 | + const parsedParams = ParamsSchema.safeParse(params); |
| 18 | + |
| 19 | + if (!parsedParams.success) { |
| 20 | + return json({ error: "Invalid params" }, { status: 400 }); |
| 21 | + } |
| 22 | + |
| 23 | + const authenticationResult = await authenticateProjectApiKeyOrPersonalAccessToken(request); |
| 24 | + |
| 25 | + if (!authenticationResult) { |
| 26 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 27 | + } |
| 28 | + |
| 29 | + const environment = await authenticatedEnvironmentForAuthentication( |
| 30 | + authenticationResult, |
| 31 | + parsedParams.data.projectRef, |
| 32 | + parsedParams.data.envSlug |
| 33 | + ); |
| 34 | + |
| 35 | + // Find the background worker and tasks and files |
| 36 | + const backgroundWorker = await prisma.backgroundWorker.findFirst({ |
| 37 | + where: { |
| 38 | + runtimeEnvironmentId: environment.id, |
| 39 | + version: parsedParams.data.version, |
| 40 | + }, |
| 41 | + include: { |
| 42 | + tasks: true, |
| 43 | + files: { |
| 44 | + include: { |
| 45 | + tasks: { |
| 46 | + select: { |
| 47 | + slug: true, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }); |
| 54 | + |
| 55 | + if (!backgroundWorker) { |
| 56 | + return json({ error: "Background worker not found" }, { status: 404 }); |
| 57 | + } |
| 58 | + |
| 59 | + return json({ |
| 60 | + id: backgroundWorker.friendlyId, |
| 61 | + version: backgroundWorker.version, |
| 62 | + cliVersion: backgroundWorker.cliVersion, |
| 63 | + sdkVersion: backgroundWorker.sdkVersion, |
| 64 | + contentHash: backgroundWorker.contentHash, |
| 65 | + createdAt: backgroundWorker.createdAt, |
| 66 | + updatedAt: backgroundWorker.updatedAt, |
| 67 | + tasks: backgroundWorker.tasks.map((task) => ({ |
| 68 | + id: task.slug, |
| 69 | + exportName: task.exportName, |
| 70 | + filePath: task.filePath, |
| 71 | + source: task.triggerSource, |
| 72 | + retryConfig: task.retryConfig, |
| 73 | + queueConfig: task.queueConfig, |
| 74 | + })), |
| 75 | + files: backgroundWorker.files.map((file) => ({ |
| 76 | + id: file.friendlyId, |
| 77 | + filePath: file.filePath, |
| 78 | + contentHash: file.contentHash, |
| 79 | + contents: decompressContent(file.contents), |
| 80 | + tasks: Array.from(new Set(file.tasks.map((task) => task.slug))), |
| 81 | + })), |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function decompressContent(compressedBuffer: Buffer): string { |
| 86 | + // First, we need to decode the base64 Buffer to get the actual compressed data |
| 87 | + const decodedBuffer = Buffer.from(compressedBuffer.toString("utf-8"), "base64"); |
| 88 | + |
| 89 | + // Decompress the data |
| 90 | + const decompressedData = zlib.inflateSync(decodedBuffer); |
| 91 | + |
| 92 | + // Convert the decompressed data to string |
| 93 | + return decompressedData.toString(); |
| 94 | +} |
0 commit comments