chore: reorder some functions

This commit is contained in:
shiftinv 2022-09-03 01:36:32 +02:00
parent 1738aa0f67
commit 5d4732072a
2 changed files with 41 additions and 41 deletions

View File

@ -11,14 +11,14 @@ const signKey = await crypto.subtle.importKey(
const encoder = new TextEncoder();
export async function verify(input: string, signature: string): Promise<boolean> {
const signatureData = hex.decode(encoder.encode(signature));
const inputData = encoder.encode(input);
return await crypto.subtle.verify("HMAC", signKey, signatureData, inputData);
}
export async function sign(input: string): Promise<string> {
const inputData = encoder.encode(input);
const sig = await crypto.subtle.sign("HMAC", signKey, inputData);
return new TextDecoder().decode(hex.encode(new Uint8Array(sig)));
}
export async function verify(input: string, signature: string): Promise<boolean> {
const signatureData = hex.decode(encoder.encode(signature));
const inputData = encoder.encode(input);
return await crypto.subtle.verify("HMAC", signKey, signatureData, inputData);
}

View File

@ -3,41 +3,6 @@ import { verify } from "./crypto.ts";
import filterWebhook from "./filter.ts";
import * as util from "./util.ts";
async function sendWebhook(
id: string,
token: string,
headers: HeadersInit,
body: string,
): Promise<Response> {
const url = `https://discord.com/api/webhooks/${id}/${token}/github?wait=1`;
log.info(`Sending webhook request to ${url}`);
const req = new Request(url, {
method: "POST",
headers: headers,
body: body,
});
return await fetch(req);
}
function getUrlConfig(params: URLSearchParams): UrlConfig {
const config: UrlConfig = {};
for (const [key, value] of params) {
switch (key) {
case "sig":
continue;
case "allowBranches":
config.allowBranches = value.split(",");
break;
case "hideTags":
config.hideTags = util.parseBool(value);
break;
default:
throw http.createHttpError(418, `Unknown config option: ${key}`);
}
}
return config;
}
export default async function handle(req: Request): Promise<Response> {
if (req.method !== "POST") {
throw http.createHttpError(405);
@ -69,3 +34,38 @@ export default async function handle(req: Request): Promise<Response> {
return await sendWebhook(id, token, req.headers, data);
}
function getUrlConfig(params: URLSearchParams): UrlConfig {
const config: UrlConfig = {};
for (const [key, value] of params) {
switch (key) {
case "sig":
continue;
case "allowBranches":
config.allowBranches = value.split(",");
break;
case "hideTags":
config.hideTags = util.parseBool(value);
break;
default:
throw http.createHttpError(418, `Unknown config option: ${key}`);
}
}
return config;
}
async function sendWebhook(
id: string,
token: string,
headers: HeadersInit,
body: string,
): Promise<Response> {
const url = `https://discord.com/api/webhooks/${id}/${token}/github?wait=1`;
log.info(`Sending webhook request to ${url}`);
const req = new Request(url, {
method: "POST",
headers: headers,
body: body,
});
return await fetch(req);
}