-
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.
improve performance of headers check
- Loading branch information
Showing
5 changed files
with
52 additions
and
34 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,45 @@ | ||
// remove type imports from http for Deno compatibility | ||
// see https://github.com/octokit/octokit.js/issues/24#issuecomment-817361886 | ||
// import type { IncomingMessage } from "node:http"; | ||
type IncomingMessage = any; | ||
type OutgoingMessage = any; | ||
|
||
const webhookHeadersMissingMessage = { | ||
"x-github-event": JSON.stringify({ | ||
error: `Required header missing: x-github-event`, | ||
}), | ||
"x-hub-signature-256": JSON.stringify({ | ||
error: `Required header missing: x-github-signature-256`, | ||
}), | ||
"x-github-delivery": JSON.stringify({ | ||
error: `Required header missing: x-github-delivery`, | ||
}), | ||
}; | ||
|
||
function sendMissindHeaderResponse( | ||
response: OutgoingMessage, | ||
missingHeader: keyof typeof webhookHeadersMissingMessage, | ||
) { | ||
response.writeHead(400, { | ||
"content-type": "application/json", | ||
}); | ||
response.end(webhookHeadersMissingMessage[missingHeader]); | ||
} | ||
|
||
// https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#delivery-headers | ||
export function validateHeaders( | ||
request: IncomingMessage, | ||
response: OutgoingMessage, | ||
) { | ||
if ("x-github-event" in request.headers === false) { | ||
sendMissindHeaderResponse(response, "x-github-event"); | ||
} else if ("x-hub-signature-256" in request.headers === false) { | ||
sendMissindHeaderResponse(response, "x-hub-signature-256"); | ||
} else if ("x-github-delivery" in request.headers === false) { | ||
sendMissindHeaderResponse(response, "x-github-delivery"); | ||
} else { | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
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