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

feat: reply after 9s with 202 status #190

Merged
merged 5 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
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
135 changes: 77 additions & 58 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@pika/pack": "^0.5.0",
"@pika/plugin-build-node": "^0.9.2",
"@pika/plugin-build-types": "^0.9.2",
"@sinonjs/fake-timers": "^6.0.1",
"@tsconfig/node10": "^1.0.3",
"@types/debug": "^4.1.5",
"@types/express": "^4.17.6",
Expand Down
17 changes: 17 additions & 0 deletions src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export function middleware(

debugWebhooks(`${eventName} event received (id: ${id})`);

// GitHub will abort the request if it does not receive a response within 10s
// See https://github.com/octokit/webhooks.js/issues/185
let didTimeout = false;
const timeout = setTimeout(() => {
didTimeout = true;
response.statusCode = 202;
response.end("still processing\n");
}, 9000).unref();

return getPayload(request)
.then((payload: any) => {
return verifyAndReceive(state, {
Expand All @@ -59,10 +68,18 @@ export function middleware(
})

.then(() => {
clearTimeout(timeout);

if (didTimeout) return;

response.end("ok\n");
})

.catch((error: WebhookEventHandlerError) => {
clearTimeout(timeout);

if (didTimeout) return;

const statusCode = Array.from(error)[0].status;
response.statusCode = statusCode || 500;
response.end(error.toString());
Expand Down
51 changes: 51 additions & 0 deletions test/integration/server-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import http from "http";

import FakeTimers from "@sinonjs/fake-timers";

import axios from "axios";
import getPort from "get-port";
import { promisify } from "util";
Expand Down Expand Up @@ -249,3 +252,51 @@ test("POST / with hook error", (t) => {

.catch(t.error);
});

test("POST / with timeout", async (t) => {
t.plan(1);

const clock = FakeTimers.install({
toFake: ["setTimeout"],
});

const api = new Webhooks({
secret: "mysecret",
});
const server = http.createServer(api.middleware);
const tenSecondsInMs = 10 * 1000;

api.on("push", async (event) => {
await new Promise((resolve) => setTimeout(resolve, tenSecondsInMs));
});

promisify(server.listen.bind(server))(this.port)

.then(() => {
return axios.post(`http://localhost:${this.port}`, pushEventPayload, {
headers: {
"X-GitHub-Delivery": "123e4567-e89b-12d3-a456-426655440000",
"X-GitHub-Event": "push",
"X-Hub-Signature": "sha1=f4d795e69b5d03c139cc6ea991ad3e5762d13e2f",
},
});
})

.catch(t.error)

.then((result) => {
t.is(result.status, 202);
})

.then(() => {
server.close();
clock.uninstall();
})

.catch(t.error);

await clock.nextAsync();
await clock.nextAsync();
await clock.nextAsync();
await clock.nextAsync();
});