-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathsvelte.ts
81 lines (76 loc) · 2.68 KB
/
svelte.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import type { RequestHandler } from "@sveltejs/kit";
import { Receiver } from "../src";
import type { RouteFunction, WorkflowServeOptions } from "../src/client/workflow";
import { serve as serveBase } from "../src/client/workflow";
type VerifySignatureConfig = {
currentSigningKey: string;
nextSigningKey: string;
clockTolerance?: number;
};
export const verifySignatureSvelte = <
Parameters extends Partial<Record<string, string>> = Partial<Record<string, string>>,
RouteId extends string | null = string | null,
>(
handler: RequestHandler<Parameters, RouteId>,
config: VerifySignatureConfig
) => {
const currentSigningKey = config.currentSigningKey;
if (!currentSigningKey) {
throw new Error("currentSigningKey is required, either in the config or from the env");
}
const nextSigningKey = config.nextSigningKey;
if (!nextSigningKey) {
throw new Error("nextSigningKey is required, either in the config or from the env");
}
const receiver = new Receiver({
currentSigningKey,
nextSigningKey,
});
const wrappedHandler: RequestHandler<Parameters, RouteId> = 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 cloneRequest = event.request.clone();
const body = await cloneRequest.text();
const isValid = await receiver.verify({
signature,
body,
clockTolerance: config.clockTolerance,
});
if (!isValid) {
return new Response("invalid signature", { status: 403 });
}
return handler(event);
};
return wrappedHandler;
};
/**
* Serve method to serve a Upstash Workflow in a Nextjs project
*
* See for options https://upstash.com/docs/qstash/workflows/basics/serve
*
* @param routeFunction workflow function
* @param options workflow options
* @returns
*
* @deprecated as of version 2.7.17. Will be removed in qstash-js 3.0.0.
* Please use https://github.com/upstash/workflow-js
* Migration Guide: https://upstash.com/docs/workflow/migration
*/
export const serve = <TInitialPayload = unknown>(
routeFunction: RouteFunction<TInitialPayload>,
options: Omit<WorkflowServeOptions<Response, TInitialPayload>, "onStepFinish"> & {
env: WorkflowServeOptions["env"];
}
): RequestHandler => {
const handler: RequestHandler = async ({ request }) => {
// eslint-disable-next-line @typescript-eslint/no-deprecated
const serveMethod = serveBase<TInitialPayload>(routeFunction, options);
return await serveMethod(request);
};
return handler;
};