diff --git a/lib/crypto.ts b/lib/crypto.ts index 0dc6d9a..0476e1c 100644 --- a/lib/crypto.ts +++ b/lib/crypto.ts @@ -13,7 +13,7 @@ async function getKey(): Promise { _signKey = await crypto.subtle.importKey( "raw", - hex.decode(new TextEncoder().encode(config.signKey)), + hex.decodeHex(config.signKey), { name: "HMAC", hash: "SHA-256" }, false, ["sign", "verify"], @@ -26,12 +26,12 @@ export async function sign(input: string): Promise { const key = await getKey(); const inputData = encoder.encode(input); const sig = await crypto.subtle.sign("HMAC", key, inputData); - return new TextDecoder().decode(hex.encode(new Uint8Array(sig))); + return hex.encodeHex(sig); } export async function verify(input: string, signature: string): Promise { const key = await getKey(); - const signatureData = hex.decode(encoder.encode(signature)); const inputData = encoder.encode(input); - return await crypto.subtle.verify("HMAC", key, signatureData, inputData); + const sig = hex.decodeHex(signature); + return await crypto.subtle.verify("HMAC", key, sig, inputData); } diff --git a/lib/handler.ts b/lib/handler.ts index e4efeae..d918c49 100644 --- a/lib/handler.ts +++ b/lib/handler.ts @@ -1,4 +1,4 @@ -import { http } from "../deps.ts"; +import { httpErrors } from "../deps.ts"; import config from "./config.ts"; import { hasKey, verify } from "./crypto.ts"; import filterWebhook from "./filter.ts"; @@ -19,20 +19,20 @@ export default async function handle( // everything else should be a POST if (req.method !== "POST") { - throw http.createHttpError(405); + throw httpErrors.createError(405); } // split url into parts const [, id, token] = url.pathname.split("/"); if (!id || !token) { - throw http.createHttpError(400); + throw httpErrors.createError(400); } // verify signature if (hasKey) { const signature = url.searchParams.get("sig"); - if (!signature) throw http.createHttpError(400); - if (!(await verify(`${id}/${token}`, signature))) throw http.createHttpError(403); + if (!signature) throw httpErrors.createError(400); + if (!(await verify(`${id}/${token}`, signature))) throw httpErrors.createError(403); } // extract data @@ -70,7 +70,7 @@ function getUrlConfig(params: URLSearchParams): UrlConfig { config.commentBurstLimit = parseInt(value); break; default: - throw http.createHttpError(418, `Unknown config option: ${key}`); + throw httpErrors.createError(418, `Unknown config option: ${key}`); } } return config; diff --git a/lib/manager.ts b/lib/manager.ts index c3c5f86..ee4df85 100644 --- a/lib/manager.ts +++ b/lib/manager.ts @@ -1,4 +1,4 @@ -import { Lock, log, redis, TTLCache } from "../deps.ts"; +import { log, Mutex, redis, TTLCache } from "../deps.ts"; import config from "./config.ts"; const KEY_EXPIRY = 3; // seconds @@ -22,12 +22,12 @@ class LocalCommentManager implements CommentManager { class RedisCommentManager implements CommentManager { private connectOptions: redis.RedisConnectOptions; - private lock: Lock; + private lock: Mutex; private _redis: Promise | undefined; constructor(options: redis.RedisConnectOptions) { this.connectOptions = options; - this.lock = new Lock(); + this.lock = new Mutex(); } // manual lazy init, redis.createLazyClient currently doesn't support pipelines :/ diff --git a/main.ts b/main.ts index 4e84a6b..74057c6 100644 --- a/main.ts +++ b/main.ts @@ -1,12 +1,12 @@ -import { http, log } from "./deps.ts"; +import { http, httpErrors, log } from "./deps.ts"; import config from "./lib/config.ts"; import handler from "./lib/handler.ts"; import { requestLog } from "./lib/util.ts"; -async function setupLogs(): Promise { - await log.setup({ +function setupLogs() { + log.setup({ handlers: { - console: new log.handlers.ConsoleHandler("DEBUG", { + console: new log.ConsoleHandler("DEBUG", { formatter: (rec) => `${rec.datetime.toISOString()} [${rec.levelName}] ${rec.msg}`, }), }, @@ -32,7 +32,7 @@ async function handleRequest(req: Request, connInfo: http.ConnInfo): Promise