Skip to content

Commit

Permalink
feat: Add endpoint for updating runtimes (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Aug 27, 2024
1 parent 0647fc7 commit 07798b5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 90 deletions.
13 changes: 13 additions & 0 deletions apps/swc-plugins/app/api/update/runtimes/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { UpdateRuntimesInputSchema } from "@/lib/api/updater/router";
import { createCaller } from "@/lib/server";
import { NextRequest, NextResponse } from "next/server";

export const POST = async (req: NextRequest) => {
const body = UpdateRuntimesInputSchema.parse(await req.json());

const api = await createCaller();

await api.updater.updateRuntimes(body);

return NextResponse.json({ ok: true });
};
84 changes: 80 additions & 4 deletions apps/swc-plugins/lib/api/updater/router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { publicProcedure, router } from "@/lib/base";
import { db } from "@/lib/prisma";
import { TRPCError } from "@trpc/server";
import semver from "semver";
import { z } from "zod";

function validateToken(token: string) {
Expand All @@ -14,19 +15,24 @@ function validateToken(token: string) {
});
}

const NpmPackageVersionSchema = z.object({
const PackageVersionSchema = z.object({
version: z.string(),
swcCoreVersion: z.string(),
});

const NpmPackageSchema = z.object({
const PackageSchema = z.object({
name: z.string(),
versions: z.array(NpmPackageVersionSchema),
versions: z.array(PackageVersionSchema),
});

export const UpdateWasmPluginsInputSchema = z.object({
token: z.string(),
pkgs: z.array(NpmPackageSchema),
pkgs: z.array(PackageSchema),
});

export const UpdateRuntimesInputSchema = z.object({
token: z.string(),
pkgs: z.array(PackageSchema),
});

export const updaterRouter = router({
Expand Down Expand Up @@ -83,4 +89,74 @@ export const updaterRouter = router({
}
}
}),

updateRuntimes: publicProcedure
.input(UpdateRuntimesInputSchema)
.output(z.void())
.mutation(async ({ input, ctx }) => {
validateToken(input.token);

const compatRanges = await db.compatRange.findMany({
select: {
id: true,
from: true,
to: true,
},
});

// Runtimes has so many versions so we need a faster logic.
function byVersion(swcCoreVersion: string) {
for (const range of compatRanges) {
if (
semver.gte(swcCoreVersion, range.from) &&
(range.to === "*" || semver.lte(swcCoreVersion, range.to))
) {
return range;
}
}
}

for (const pkg of input.pkgs) {
const runtime = await db.swcRuntime.upsert({
where: {
name: pkg.name,
},
create: {
name: pkg.name,
},
update: {},
});

for (const version of pkg.versions) {
const swcCoreVersion = version.swcCoreVersion;
const compatRange = byVersion(swcCoreVersion);

if (!compatRange) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Compat range not found for SWC core version ${swcCoreVersion}`,
});
}

await db.swcRuntimeVersion.upsert({
where: {
runtimeId_version: {
runtimeId: runtime.id,
version: version.version,
},
},
create: {
runtimeId: runtime.id,
version: version.version,
compatRangeId: compatRange.id,
swcCoreVersion,
},
update: {
compatRangeId: compatRange.id,
swcCoreVersion,
},
});
}
}
}),
});
86 changes: 0 additions & 86 deletions apps/swc-plugins/scripts/import-runtime.mjs

This file was deleted.

0 comments on commit 07798b5

Please sign in to comment.