-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(swc-plugins): Add Wasm plugin data (#56)
- Loading branch information
Showing
5 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}); | ||
} | ||
} | ||
}), | ||
}); |