Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 29, 2021
1 parent b03b504 commit 84277eb
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/middleware/node/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { onUnhandledRequestDefault } from "./on-unhandled-request-default";
import { getMissingHeaders } from "./get-missing-headers";
import { getPayload } from "./get-payload";

export function middleware(
export async function middleware(
webhooks: Webhooks,
options: Required<MiddlewareOptions>,
request: IncomingMessage,
Expand Down Expand Up @@ -55,31 +55,27 @@ export function middleware(
response.end("still processing\n");
}, 9000).unref();

return getPayload(request)
.then((payload) => {
return webhooks.verifyAndReceive({
id: id,
name: eventName as any,
payload: payload as any,
signature: signatureSHA256,
});
})
try {
const payload = await getPayload(request);

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

if (didTimeout) return;
await webhooks.verifyAndReceive({
id: id,
name: eventName as any,
payload: payload as any,
signature: signatureSHA256,
});
clearTimeout(timeout);

response.end("ok\n");
})
if (didTimeout) return;

.catch((error: WebhookEventHandlerError) => {
clearTimeout(timeout);
response.end("ok\n");
} catch (error) {
clearTimeout(timeout);

if (didTimeout) return;
if (didTimeout) return;

const statusCode = Array.from(error)[0].status;
response.statusCode = statusCode || 500;
response.end(error.toString());
});
const statusCode = Array.from(error as WebhookEventHandlerError)[0].status;
response.statusCode = statusCode || 500;
response.end(error.toString());
}
}

0 comments on commit 84277eb

Please sign in to comment.