-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
121 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
// remove type imports from http for Deno compatibility | ||
// see https://github.com/octokit/octokit.js/issues/2075#issuecomment-817361886 | ||
// import { IncomingMessage, ServerResponse } from "http"; | ||
type IncomingMessage = any; | ||
type ServerResponse = any; | ||
|
||
import type { WebhookEventName } from "@octokit/webhooks-types"; | ||
|
||
import type { Webhooks } from "../../index.js"; | ||
import type { WebhookEventHandlerError } from "../../types.js"; | ||
import type { MiddlewareOptions } from "./types.js"; | ||
import { getMissingHeaders } from "./get-missing-headers.js"; | ||
import { getPayload } from "./get-payload.js"; | ||
|
||
type Handler = ( | ||
request: IncomingMessage, | ||
response: ServerResponse, | ||
) => Promise<void>; | ||
export function createNodeHandler( | ||
webhooks: Webhooks, | ||
options: Required<MiddlewareOptions>, | ||
): Handler { | ||
return async function handler( | ||
request: IncomingMessage, | ||
response: ServerResponse, | ||
): Promise<void> { | ||
// Check if the Content-Type header is `application/json` and allow for charset to be specified in it | ||
// Otherwise, return a 415 Unsupported Media Type error | ||
// See https://github.com/octokit/webhooks.js/issues/158 | ||
if ( | ||
!request.headers["content-type"] || | ||
!request.headers["content-type"].startsWith("application/json") | ||
) { | ||
response.writeHead(415, { | ||
"content-type": "application/json", | ||
accept: "application/json", | ||
}); | ||
response.end( | ||
JSON.stringify({ | ||
error: `Unsupported "Content-Type" header value. Must be "application/json"`, | ||
}), | ||
); | ||
return; | ||
} | ||
|
||
const missingHeaders = getMissingHeaders(request).join(", "); | ||
|
||
if (missingHeaders) { | ||
response.writeHead(400, { | ||
"content-type": "application/json", | ||
}); | ||
response.end( | ||
JSON.stringify({ | ||
error: `Required headers missing: ${missingHeaders}`, | ||
}), | ||
); | ||
|
||
return; | ||
} | ||
|
||
const eventName = request.headers["x-github-event"] as WebhookEventName; | ||
const signatureSHA256 = request.headers["x-hub-signature-256"] as string; | ||
const id = request.headers["x-github-delivery"] as string; | ||
|
||
options.log.debug(`${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(); | ||
|
||
try { | ||
// If request.body already exists we dont need to wait for getPayload | ||
// See https://github.com/octokit/webhooks.js/pull/23 | ||
|
||
const payload = request.body || (await getPayload(request)); | ||
|
||
await webhooks.verifyAndReceive({ | ||
id: id, | ||
name: eventName as any, | ||
payload, | ||
signature: signatureSHA256, | ||
}); | ||
clearTimeout(timeout); | ||
|
||
if (didTimeout) return; | ||
|
||
response.end("ok\n"); | ||
return; | ||
} catch (error) { | ||
clearTimeout(timeout); | ||
|
||
if (didTimeout) return; | ||
|
||
const err = Array.from(error as WebhookEventHandlerError)[0]; | ||
const errorMessage = err.message | ||
? `${err.name}: ${err.message}` | ||
: "Error: An Unspecified error occurred"; | ||
response.statusCode = | ||
typeof err.status !== "undefined" ? err.status : 500; | ||
|
||
options.log.error(error); | ||
|
||
response.end( | ||
JSON.stringify({ | ||
error: errorMessage, | ||
}), | ||
); | ||
|
||
return; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters