UNPKG

@netronk/mdx-remote

Version:

The remote MDX files adapter for Netronk Gen

86 lines (84 loc) 2.42 kB
// src/github/next/github-webhook.ts import { revalidatePath } from "next/cache"; function createGithubWebhookAPI({ ref = process.env.VERCEL_GIT_COMMIT_REF, secret, paths = [ { path: "/docs/[[...slug]]", type: "page" }, { path: "/docs", type: "layout" } ] }) { const encoder = new TextEncoder(); async function verifySignature(header, payload) { const parts = header.split("="); const sigHex = parts[1]; const algorithm = { name: "HMAC", hash: { name: "SHA-256" } }; const keyBytes = encoder.encode(secret); const extractable = false; const key = await crypto.subtle.importKey( "raw", keyBytes, algorithm, extractable, ["sign", "verify"] ); const sigBytes = hexToBytes(sigHex); const dataBytes = encoder.encode(payload); return crypto.subtle.verify(algorithm.name, key, sigBytes, dataBytes); } return { async POST(request) { const body = await request.text(); if (secret) { const signature = request.headers.get("x-hub-signature-256"); if (!signature || !await verifySignature(signature, body)) { return new Response("Unauthorized", { status: 401 }); } } const githubEvent = request.headers.get("x-github-event"); if (githubEvent === "push") { const data = JSON.parse(body); if (ref === void 0 || data.ref === `refs/heads/${ref}`) { for (const path of paths) { revalidatePath(path.path, path.type); } } } return new Response("Accepted", { status: 202 }); } }; } function hexToBytes(hex) { const len = hex.length / 2; const bytes = new Uint8Array(len); let index = 0; for (let i = 0; i < hex.length; i += 2) { const c = hex.slice(i, i + 2); const b = Number.parseInt(c, 16); bytes[index] = b; index += 1; } return bytes; } // src/github/next/hot-reload.ts import dynamic from "next/dynamic"; import { createElement } from "react"; var Client = dynamic(() => import("../../hot-reload.client-7JNYSI6H.js"), { ssr: false }); function initHotReload({ port = 3001, revokeUrl = "/api/revoke" } = {}) { if (process.env.NODE_ENV !== "development") return { component: null }; return { component: createElement(Client, { url: `ws://localhost:${port.toString()}`, revokeUrl }) }; } export { createGithubWebhookAPI, initHotReload };