Skip to content

Commit

Permalink
feat(swc-plugins): Add Wasm plugin data (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Aug 27, 2024
1 parent 4f667b2 commit 0647fc7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
13 changes: 13 additions & 0 deletions apps/swc-plugins/app/api/update/wasm-plugins/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { UpdateWasmPluginsInputSchema } from "@/lib/api/updater/router";
import { createCaller } from "@/lib/server";
import { NextRequest, NextResponse } from "next/server";

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

const api = await createCaller();

await api.updater.updateWasmPlugins(body);

return NextResponse.json({ ok: true });
};
3 changes: 3 additions & 0 deletions apps/swc-plugins/lib/api/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import { router } from "@/lib/base";
import { inferRouterInputs, inferRouterOutputs } from "@trpc/server";
import { compatRangeRouter } from "./compatRange/router";
import { runtimeRouter } from "./runtimes/router";
import { updaterRouter } from "./updater/router";
import { userRouter } from "./users/router";

export const apiRouter = router({
users: userRouter,

runtime: runtimeRouter,
compatRange: compatRangeRouter,

updater: updaterRouter,
});

export type ApiRouter = typeof apiRouter;
Expand Down
3 changes: 3 additions & 0 deletions apps/swc-plugins/lib/api/runtimes/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const runtimeRouter = router({
version: true,
compatRangeId: true,
},
orderBy: {
version: "desc",
},
});

return versions;
Expand Down
10 changes: 7 additions & 3 deletions apps/swc-plugins/lib/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,18 @@ export function defineAbilitiesFor({ user }: { user: User | null }): Abilities {

const factory = createCallerFactory(apiRouter);

export const createCaller = async () => {
export const createCaller = async (ctx?: Context) => {
if (ctx) {
return factory(ctx);
}

const user: User | null = await getCurrentUser();

const abilities = defineAbilitiesFor({
user,
});

const ctx: Context = {
const newCtx: Context = {
getAccessToken() {
const h = headers();
const auth = h.get("authorization");
Expand All @@ -172,5 +176,5 @@ export const createCaller = async () => {
responseHeaders: null,
isAdmin: false,
};
return factory(ctx);
return factory(newCtx);
};
86 changes: 86 additions & 0 deletions apps/swc-plugins/lib/api/updater/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { publicProcedure, router } from "@/lib/base";
import { db } from "@/lib/prisma";
import { TRPCError } from "@trpc/server";
import { z } from "zod";

function validateToken(token: string) {
if (token === process.env.CRAWL_SECRET) {
return;
}

throw new TRPCError({
code: "UNAUTHORIZED",
message: "Invalid token",
});
}

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

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

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

export const updaterRouter = router({
updateWasmPlugins: publicProcedure
.input(UpdateWasmPluginsInputSchema)
.output(z.void())
.mutation(async ({ input, ctx }) => {
validateToken(input.token);

const api = await (await import("@/lib/api/server")).createCaller(ctx);

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

for (const version of pkg.versions) {
const swcCoreVersion = version.swcCoreVersion;
const compatRange = await api.compatRange.byCoreVersion({
version: swcCoreVersion,
});

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

await db.swcPluginVersion.upsert({
where: {
pluginId_version: {
pluginId: plugin.id,
version: version.version,
},
},
create: {
pluginId: plugin.id,
version: version.version,
compatRangeId: compatRange.id,
swcCoreVersion,
},
update: {
compatRangeId: compatRange.id,
swcCoreVersion,
},
});
}
}
}),
});

0 comments on commit 0647fc7

Please sign in to comment.