Skip to content

Commit

Permalink
chore: add benchmarks (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak authored Jul 19, 2024
1 parent 1e94d0f commit 114b397
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"version": "0.0.0-development",
"description": "Methods to handle GitHub Webhook requests",
"scripts": {
"bench": "vitest bench --run",
"build": "node scripts/build.mjs && tsc -p tsconfig.json",
"lint": "prettier --check '{src,test,scripts}/**/*' README.md package.json",
"lint:fix": "prettier --write '{src,test,scripts}/**/*' README.md package.json",
Expand Down
19 changes: 19 additions & 0 deletions test/benchmark-sign.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { bench, describe } from "vitest";
import { verify as verifyNode } from "../src/index.ts";
import { verify as verifyWeb } from "../src/web.ts";
import { toNormalizedJsonString } from "./common.ts";

describe("sign", () => {
const eventPayload = toNormalizedJsonString({
foo: "bar",
});
const secret = "mysecret";

bench("node", async () => {
await signNode(secret, JSON.stringify(eventPayload));
});

bench("web", async () => {
await signWeb(secret, JSON.stringify(eventPayload));
});
});
22 changes: 22 additions & 0 deletions test/benchmark-verify.bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { bench, describe } from "vitest";
import { verify as verifyNode } from "../src/index.ts";
import { verify as verifyWeb } from "../src/web.ts";
import { toNormalizedJsonString } from "./common.ts";

describe("verify", () => {
const eventPayload = toNormalizedJsonString({
foo: "bar",
});
const secret = "mysecret";

const signatureSHA256 =
"sha256=e3eccac34c43c7dc1cbb905488b1b81347fcc700a7b025697a9d07862256023f";

bench("node", async () => {
await verifyNode(secret, eventPayload, signatureSHA256);
});

bench("web", async () => {
await verifyWeb(secret, eventPayload, signatureSHA256);
});
});
7 changes: 7 additions & 0 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function toNormalizedJsonString(payload: object) {
// GitHub sends its JSON with no indentation and no line breaks at the end
const payloadString = JSON.stringify(payload);
return payloadString.replace(/[^\\]\\u[\da-f]{4}/g, (s) => {
return s.substr(0, 3) + s.substr(3).toUpperCase();
});
}
17 changes: 5 additions & 12 deletions test/verify.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { describe, it, expect } from "vitest";
import { verify, verifyWithFallback } from "../src/index.ts";

function toNormalizedJsonString(payload: object) {
// GitHub sends its JSON with an indentation of 2 spaces and a line break at the end
const payloadString = JSON.stringify(payload, null, 2) + "\n";
return payloadString.replace(/[^\\]\\u[\da-f]{4}/g, (s) => {
return s.substr(0, 3) + s.substr(3).toUpperCase();
});
}
import { toNormalizedJsonString } from "./common.ts";

const JSONeventPayload = { foo: "bar" };
const eventPayload = toNormalizedJsonString(JSONeventPayload);
const secret = "mysecret";
const signatureSHA256 =
"sha256=e3eccac34c43c7dc1cbb905488b1b81347fcc700a7b025697a9d07862256023f";
"sha256=4864d2759938a15468b5df9ade20bf161da9b4f737ea61794142f3484236bda3";

describe("verify", () => {
it("is a function", () => {
Expand Down Expand Up @@ -78,23 +71,23 @@ describe("verify", () => {
toNormalizedJsonString({
foo: "Foo\n\u001b[34mbar: ♥♥♥♥♥♥♥♥\nthis-is-lost\u001b[0m\u001b[2K",
}),
"sha256=9dacf9003316b09be07df56d86a2d0d6872e42a1e6c72c3bad9ff915a7c5603e",
"sha256=afecc3caa27548bb90d51a50384cb2868b9a3327b4ad6a01c9bd4ed0f8b0b12c",
);
expect(signatureMatchesLowerCaseSequence).toBe(true);
const signatureMatchesUpperCaseSequence = await verify(
"development",
toNormalizedJsonString({
foo: "Foo\n\u001B[34mbar: ♥♥♥♥♥♥♥♥\nthis-is-lost\u001B[0m\u001B[2K",
}),
"sha256=9dacf9003316b09be07df56d86a2d0d6872e42a1e6c72c3bad9ff915a7c5603e",
"sha256=afecc3caa27548bb90d51a50384cb2868b9a3327b4ad6a01c9bd4ed0f8b0b12c",
);
expect(signatureMatchesUpperCaseSequence).toBe(true);
const signatureMatchesEscapedSequence = await verify(
"development",
toNormalizedJsonString({
foo: "\\u001b",
}),
"sha256=87316067e2011fae39998b18c46a14d83b3e7c3ffdd88fb2ee5afb7d11288e60",
"sha256=6f8326efbacfbd04e870cea25b5652e635be8c9807f2fd5348ef60753c9e96ed",
);
expect(signatureMatchesEscapedSequence).toBe(true);
});
Expand Down

0 comments on commit 114b397

Please sign in to comment.