UNPKG

@upstash/qstash

Version:

Official Typescript client for QStash

55 lines (53 loc) 1.79 kB
import "./chunk-M7SEEFAC.mjs"; import { Receiver, serve } from "./chunk-QYBCXZKB.mjs"; // platforms/svelte.ts var verifySignatureSvelte = (handler, config) => { const currentSigningKey = config?.currentSigningKey ?? process.env.QSTASH_CURRENT_SIGNING_KEY; const nextSigningKey = config?.nextSigningKey ?? process.env.QSTASH_NEXT_SIGNING_KEY; if (!currentSigningKey && !nextSigningKey && !process.env.QSTASH_REGION) { throw new Error( "currentSigningKey and nextSigningKey are required, either in the config or as env variables (QSTASH_CURRENT_SIGNING_KEY and QSTASH_NEXT_SIGNING_KEY)" ); } const receiver = new Receiver({ currentSigningKey, nextSigningKey }); const wrappedHandler = async (event) => { const signature = event.request.headers.get("upstash-signature"); if (!signature) { return new Response("`Upstash-Signature` header is missing", { status: 403 }); } if (typeof signature !== "string") { throw new TypeError("`Upstash-Signature` header is not a string"); } const upstashRegion = event.request.headers.get("upstash-region"); const cloneRequest = event.request.clone(); const body = await cloneRequest.text(); const isValid = await receiver.verify({ signature, body, clockTolerance: config?.clockTolerance, upstashRegion: upstashRegion ?? void 0 }); if (!isValid) { return new Response("invalid signature", { status: 403 }); } return handler(event); }; return wrappedHandler; }; var serve2 = (routeFunction, options) => { const handler = async ({ request }) => { const serveMethod = serve(routeFunction, options); return await serveMethod(request); }; return handler; }; export { serve2 as serve, verifySignatureSvelte };