Skip to content

Commit 07798b5

Browse files
authored
feat: Add endpoint for updating runtimes (#57)
1 parent 0647fc7 commit 07798b5

File tree

3 files changed

+93
-90
lines changed

3 files changed

+93
-90
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { UpdateRuntimesInputSchema } from "@/lib/api/updater/router";
2+
import { createCaller } from "@/lib/server";
3+
import { NextRequest, NextResponse } from "next/server";
4+
5+
export const POST = async (req: NextRequest) => {
6+
const body = UpdateRuntimesInputSchema.parse(await req.json());
7+
8+
const api = await createCaller();
9+
10+
await api.updater.updateRuntimes(body);
11+
12+
return NextResponse.json({ ok: true });
13+
};

apps/swc-plugins/lib/api/updater/router.ts

+80-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { publicProcedure, router } from "@/lib/base";
22
import { db } from "@/lib/prisma";
33
import { TRPCError } from "@trpc/server";
4+
import semver from "semver";
45
import { z } from "zod";
56

67
function validateToken(token: string) {
@@ -14,19 +15,24 @@ function validateToken(token: string) {
1415
});
1516
}
1617

17-
const NpmPackageVersionSchema = z.object({
18+
const PackageVersionSchema = z.object({
1819
version: z.string(),
1920
swcCoreVersion: z.string(),
2021
});
2122

22-
const NpmPackageSchema = z.object({
23+
const PackageSchema = z.object({
2324
name: z.string(),
24-
versions: z.array(NpmPackageVersionSchema),
25+
versions: z.array(PackageVersionSchema),
2526
});
2627

2728
export const UpdateWasmPluginsInputSchema = z.object({
2829
token: z.string(),
29-
pkgs: z.array(NpmPackageSchema),
30+
pkgs: z.array(PackageSchema),
31+
});
32+
33+
export const UpdateRuntimesInputSchema = z.object({
34+
token: z.string(),
35+
pkgs: z.array(PackageSchema),
3036
});
3137

3238
export const updaterRouter = router({
@@ -83,4 +89,74 @@ export const updaterRouter = router({
8389
}
8490
}
8591
}),
92+
93+
updateRuntimes: publicProcedure
94+
.input(UpdateRuntimesInputSchema)
95+
.output(z.void())
96+
.mutation(async ({ input, ctx }) => {
97+
validateToken(input.token);
98+
99+
const compatRanges = await db.compatRange.findMany({
100+
select: {
101+
id: true,
102+
from: true,
103+
to: true,
104+
},
105+
});
106+
107+
// Runtimes has so many versions so we need a faster logic.
108+
function byVersion(swcCoreVersion: string) {
109+
for (const range of compatRanges) {
110+
if (
111+
semver.gte(swcCoreVersion, range.from) &&
112+
(range.to === "*" || semver.lte(swcCoreVersion, range.to))
113+
) {
114+
return range;
115+
}
116+
}
117+
}
118+
119+
for (const pkg of input.pkgs) {
120+
const runtime = await db.swcRuntime.upsert({
121+
where: {
122+
name: pkg.name,
123+
},
124+
create: {
125+
name: pkg.name,
126+
},
127+
update: {},
128+
});
129+
130+
for (const version of pkg.versions) {
131+
const swcCoreVersion = version.swcCoreVersion;
132+
const compatRange = byVersion(swcCoreVersion);
133+
134+
if (!compatRange) {
135+
throw new TRPCError({
136+
code: "NOT_FOUND",
137+
message: `Compat range not found for SWC core version ${swcCoreVersion}`,
138+
});
139+
}
140+
141+
await db.swcRuntimeVersion.upsert({
142+
where: {
143+
runtimeId_version: {
144+
runtimeId: runtime.id,
145+
version: version.version,
146+
},
147+
},
148+
create: {
149+
runtimeId: runtime.id,
150+
version: version.version,
151+
compatRangeId: compatRange.id,
152+
swcCoreVersion,
153+
},
154+
update: {
155+
compatRangeId: compatRange.id,
156+
swcCoreVersion,
157+
},
158+
});
159+
}
160+
}
161+
}),
86162
});

apps/swc-plugins/scripts/import-runtime.mjs

-86
This file was deleted.

0 commit comments

Comments
 (0)