fix: load app function only once when using createNodeMiddleware (#1432)

This commit is contained in:
Gregor Martynus 2020-12-07 16:36:52 -08:00
parent 8c01e90ecc
commit 60b702bd05
3 changed files with 30 additions and 21 deletions

View File

@ -1,7 +1,6 @@
import { RequestListener, IncomingMessage, ServerResponse } from "http";
import { RequestListener } from "http";
import { Deprecation } from "deprecation";
import { NextFunction } from "express";
import { ApplicationFunction } from "./types";
import { MiddlewareOptions } from "./types";
@ -10,11 +9,6 @@ export function createNodeMiddleware(
appFn: ApplicationFunction,
{ probot, Probot }: MiddlewareOptions
): RequestListener {
return (
request: IncomingMessage,
response: ServerResponse,
next?: NextFunction
) => {
if (Probot) {
probot = new Probot();
probot.log.warn(
@ -25,6 +19,6 @@ export function createNodeMiddleware(
}
probot.load(appFn);
probot.webhooks.middleware(request, response, next);
};
return probot.webhooks.middleware;
}

View File

@ -1,4 +1,4 @@
import { createServer } from "http";
import { createServer, IncomingMessage, ServerResponse } from "http";
import Stream from "stream";
import pino from "pino";
@ -6,7 +6,7 @@ import getPort from "get-port";
import got from "got";
import { sign } from "@octokit/webhooks";
import { createNodeMiddleware, createProbot } from "../src";
import { createNodeMiddleware, createProbot, Probot } from "../src";
import { ApplicationFunction } from "../src/types";
const APP_ID = "1";
@ -75,4 +75,19 @@ describe("createNodeMiddleware", () => {
server.close();
});
test("loads app only once", async () => {
let counter = 0;
const appFn = () => {
counter++;
};
const middleware = createNodeMiddleware(appFn, {
probot: new Probot(),
});
middleware({} as IncomingMessage, { end() {} } as ServerResponse);
middleware({} as IncomingMessage, { end() {} } as ServerResponse);
expect(counter).toEqual(1);
});
});

View File

@ -1,5 +1,6 @@
import Stream from "stream";
import { join } from "path";
import { IncomingMessage, ServerResponse } from "http";
import { Webhooks } from "@octokit/webhooks";
import pino from "pino";
@ -13,7 +14,6 @@ import {
getOptions,
createNodeMiddleware,
} from "../src";
import { IncomingMessage, ServerResponse } from "http";
const pushEvent = require("./fixtures/webhook/push.json");