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