|
| 1 | +import { publicProcedure, router } from "@/lib/base"; |
| 2 | +import { db } from "@/lib/prisma"; |
| 3 | +import { TRPCError } from "@trpc/server"; |
| 4 | +import { z } from "zod"; |
| 5 | + |
| 6 | +function validateToken(token: string) { |
| 7 | + if (token === process.env.CRAWL_SECRET) { |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + throw new TRPCError({ |
| 12 | + code: "UNAUTHORIZED", |
| 13 | + message: "Invalid token", |
| 14 | + }); |
| 15 | +} |
| 16 | + |
| 17 | +const NpmPackageVersionSchema = z.object({ |
| 18 | + version: z.string(), |
| 19 | + swcCoreVersion: z.string(), |
| 20 | +}); |
| 21 | + |
| 22 | +const NpmPackageSchema = z.object({ |
| 23 | + name: z.string(), |
| 24 | + versions: z.array(NpmPackageVersionSchema), |
| 25 | +}); |
| 26 | + |
| 27 | +export const UpdateWasmPluginsInputSchema = z.object({ |
| 28 | + token: z.string(), |
| 29 | + pkgs: z.array(NpmPackageSchema), |
| 30 | +}); |
| 31 | + |
| 32 | +export const updaterRouter = router({ |
| 33 | + updateWasmPlugins: publicProcedure |
| 34 | + .input(UpdateWasmPluginsInputSchema) |
| 35 | + .output(z.void()) |
| 36 | + .mutation(async ({ input, ctx }) => { |
| 37 | + validateToken(input.token); |
| 38 | + |
| 39 | + const api = await (await import("@/lib/api/server")).createCaller(ctx); |
| 40 | + |
| 41 | + for (const pkg of input.pkgs) { |
| 42 | + const plugin = await db.swcPlugin.upsert({ |
| 43 | + where: { |
| 44 | + name: pkg.name, |
| 45 | + }, |
| 46 | + create: { |
| 47 | + name: pkg.name, |
| 48 | + }, |
| 49 | + update: {}, |
| 50 | + }); |
| 51 | + |
| 52 | + for (const version of pkg.versions) { |
| 53 | + const swcCoreVersion = version.swcCoreVersion; |
| 54 | + const compatRange = await api.compatRange.byCoreVersion({ |
| 55 | + version: swcCoreVersion, |
| 56 | + }); |
| 57 | + |
| 58 | + if (!compatRange) { |
| 59 | + throw new TRPCError({ |
| 60 | + code: "NOT_FOUND", |
| 61 | + message: `Compat range not found for SWC core version ${swcCoreVersion}`, |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + await db.swcPluginVersion.upsert({ |
| 66 | + where: { |
| 67 | + pluginId_version: { |
| 68 | + pluginId: plugin.id, |
| 69 | + version: version.version, |
| 70 | + }, |
| 71 | + }, |
| 72 | + create: { |
| 73 | + pluginId: plugin.id, |
| 74 | + version: version.version, |
| 75 | + compatRangeId: compatRange.id, |
| 76 | + swcCoreVersion, |
| 77 | + }, |
| 78 | + update: { |
| 79 | + compatRangeId: compatRange.id, |
| 80 | + swcCoreVersion, |
| 81 | + }, |
| 82 | + }); |
| 83 | + } |
| 84 | + } |
| 85 | + }), |
| 86 | +}); |
0 commit comments