Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

made changes to support events of Buffer type #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/scalekit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,22 @@ export default class ScalekitClient {
* @param {string} payload The payload
* @return {boolean} Returns true if the payload is valid.
*/
verifyWebhookPayload(secret: string, headers: Record<string, string>, payload: string): boolean {
verifyWebhookPayload(secret: string, headers: Record<string, string>, payload: string | Buffer): boolean {
const webhookId = headers['webhook-id'];
const webhookTimestamp = headers['webhook-timestamp'];
const webhookSignature = headers['webhook-signature'];
if (!webhookId || !webhookTimestamp || !webhookSignature) {
throw new Error("Missing required headers");
}
if (typeof payload === "string") {
// Do nothing, already a string
} else if (payload.constructor.name === "Buffer") {
payload = payload.toString();
} else {
throw new Error(
"Expected payload to be of type string or Buffer."
);
}
const timestamp = this.verifyTimestamp(webhookTimestamp);
const data = `${webhookId}.${Math.floor(timestamp.getTime() / 1000)}.${payload}`;
const secretBytes = Buffer.from(secret.split("_")[1], 'base64');
Expand Down
Loading