Skip to content

Commit

Permalink
chore: add example for benchmarks (#1913)
Browse files Browse the repository at this point in the history
chore: add example
  • Loading branch information
Uzlopak committed Nov 16, 2023
1 parent 54ca972 commit 1c7a2d3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
70 changes: 70 additions & 0 deletions example/node-middleware.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const { createServer } = require("http");
const { createNodeMiddleware } = require('../lib/create-node-middleware');
const { createProbot } = require('../lib/create-probot');
const { sign } = require("@octokit/webhooks-methods");
const WebhookExamples = require("@octokit/webhooks-examples");

process.env.APP_ID = "123";
process.env.PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA1c7+9z5Pad7OejecsQ0bu3aozN3tihPmljnnudb9G3HECdnH
lWu2/a1gB9JW5TBQ+AVpum9Okx7KfqkfBKL9mcHgSL0yWMdjMfNOqNtrQqKlN4kE
p6RD++7sGbzbfZ9arwrlD/HSDAWGdGGJTSOBM6pHehyLmSC3DJoR/CTu0vTGTWXQ
rO64Z8tyXQPtVPb/YXrcUhbBp8i72b9Xky0fD6PkEebOy0Ip58XVAn2UPNlNOSPS
ye+Qjtius0Md4Nie4+X8kwVI2Qjk3dSm0sw/720KJkdVDmrayeljtKBx6AtNQsSX
gzQbeMmiqFFkwrG1+zx6E7H7jqIQ9B6bvWKXGwIDAQABAoIBAD8kBBPL6PPhAqUB
K1r1/gycfDkUCQRP4DbZHt+458JlFHm8QL6VstKzkrp8mYDRhffY0WJnYJL98tr4
4tohsDbqFGwmw2mIaHjl24LuWXyyP4xpAGDpl9IcusjXBxLQLp2m4AKXbWpzb0OL
Ulrfc1ZooPck2uz7xlMIZOtLlOPjLz2DuejVe24JcwwHzrQWKOfA11R/9e50DVse
hnSH/w46Q763y4I0E3BIoUMsolEKzh2ydAAyzkgabGQBUuamZotNfvJoDXeCi1LD
8yNCWyTlYpJZJDDXooBU5EAsCvhN1sSRoaXWrlMSDB7r/E+aQyKua4KONqvmoJuC
21vSKeECgYEA7yW6wBkVoNhgXnk8XSZv3W+Q0xtdVpidJeNGBWnczlZrummt4xw3
xs6zV+rGUDy59yDkKwBKjMMa42Mni7T9Fx8+EKUuhVK3PVQyajoyQqFwT1GORJNz
c/eYQ6VYOCSC8OyZmsBM2p+0D4FF2/abwSPMmy0NgyFLCUFVc3OECpkCgYEA5OAm
I3wt5s+clg18qS7BKR2DuOFWrzNVcHYXhjx8vOSWV033Oy3yvdUBAhu9A1LUqpwy
Ma+unIgxmvmUMQEdyHQMcgBsVs10dR/g2xGjMLcwj6kn+xr3JVIZnbRT50YuPhf+
ns1ScdhP6upo9I0/sRsIuN96Gb65JJx94gQ4k9MCgYBO5V6gA2aMQvZAFLUicgzT
u/vGea+oYv7tQfaW0J8E/6PYwwaX93Y7Q3QNXCoCzJX5fsNnoFf36mIThGHGiHY6
y5bZPPWFDI3hUMa1Hu/35XS85kYOP6sGJjf4kTLyirEcNKJUWH7CXY+00cwvTkOC
S4Iz64Aas8AilIhRZ1m3eQKBgQCUW1s9azQRxgeZGFrzC3R340LL530aCeta/6FW
CQVOJ9nv84DLYohTVqvVowdNDTb+9Epw/JDxtDJ7Y0YU0cVtdxPOHcocJgdUGHrX
ZcJjRIt8w8g/s4X6MhKasBYm9s3owALzCuJjGzUKcDHiO2DKu1xXAb0SzRcTzUCn
7daCswKBgQDOYPZ2JGmhibqKjjLFm0qzpcQ6RPvPK1/7g0NInmjPMebP0K6eSPx0
9/49J6WTD++EajN7FhktUSYxukdWaCocAQJTDNYP0K88G4rtC2IYy5JFn9SWz5oh
x//0u+zd/R/QRUzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
-----END RSA PRIVATE KEY-----`;
process.env.WEBHOOK_SECRET = "secret";
process.env.WEBHOOK_PATH = "/";

const pushEvent = JSON.stringify((
WebhookExamples.filter(
(event) => event.name === "push",
)[0]
).examples[0]);

const appFn = (app) => {
app.on("issues.opened", async (context) => {
const issueComment = context.issue({
body: "Thanks for opening this issue!",
});
return context.octokit.issues.createComment(issueComment);
});

app.onAny(async (context) => {
context.log.info({ event: context.name, action: context.payload.action });
});

app.onError(async (error) => {
app.log.error(error);
});
};

const middleware = createNodeMiddleware(appFn, { probot: createProbot() });

const server = createServer(middleware);

server.listen(3000, async () => {
console.log("Probot started http://localhost:3000/")
console.log(`autocannon -m POST -b '${pushEvent}' -H content-type=application/json -H x-github-event=push -H x-github-delivery=1 -H x-hub-signature-256=${await sign("secret", pushEvent)} http://127.0.0.1:3000/`)
});
3 changes: 2 additions & 1 deletion src/create-node-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { createNodeMiddleware as createWebhooksMiddleware } from "@octokit/webho

import type { ApplicationFunction, MiddlewareOptions } from "./types";
import { defaultWebhooksPath } from "./server/server";
import { createProbot } from ".";

export function createNodeMiddleware(
appFn: ApplicationFunction,
{ probot, webhooksPath }: MiddlewareOptions,
{ probot = createProbot(), webhooksPath }: MiddlewareOptions,
): RequestListener {
probot.load(appFn);

Expand Down

0 comments on commit 1c7a2d3

Please sign in to comment.