1- import * as httpErrors from "x/http_errors " ;
1+ import { HTTPException } from "@hono/hono/http-exception " ;
22
3- import config from "./config.ts" ;
4- import { hasKey , verify } from "./crypto.ts" ;
53import filterWebhook from "./filter.ts" ;
64import fixupEmbeds from "./formatter.ts" ;
75import { UrlConfig } from "./types.d.ts" ;
86import { parseBool , requestLog } from "./util.ts" ;
97import { sendWebhook } from "./webhook.ts" ;
108
119export default async function handle (
12- req : Request ,
13- ) : Promise < Response | [ Response , Record < string , string > ] > {
14- const url = new URL ( req . url ) ;
15-
16- // redirect to repo if `GET /`
17- if ( req . method === "GET" && config . mainRedirect && url . pathname === "/" ) {
18- return Response . redirect ( config . mainRedirect ) ;
19- }
20-
21- // everything else should be a POST
22- if ( req . method !== "POST" ) {
23- throw httpErrors . createError ( 405 ) ;
24- }
25-
26- // split url into parts
27- const [ , id , token ] = url . pathname . split ( "/" ) ;
28- if ( ! id || ! token ) {
29- throw httpErrors . createError ( 400 ) ;
30- }
31-
32- // verify signature
33- if ( hasKey ) {
34- const signature = url . searchParams . get ( "sig" ) ;
35- if ( ! signature ) throw httpErrors . createError ( 400 ) ;
36- if ( ! ( await verify ( `${ id } /${ token } ` , signature ) ) ) throw httpErrors . createError ( 403 ) ;
37- }
38-
39- // extract data
40- const urlConfig = getUrlConfig ( url . searchParams ) ;
41- const data = await req . text ( ) ;
42- const json : Record < string , any > = JSON . parse ( data ) ;
10+ json : Record < string , any > ,
11+ headers : Record < string , string > ,
12+ queryParams : Record < string , string > ,
13+ id : string ,
14+ token : string ,
15+ ) : Promise < [ Response , Record < string , string > ] > {
16+ const urlConfig = getUrlConfig ( queryParams ) ;
4317
4418 // do the thing
45- const filterReason = await filterWebhook ( req . headers , json , urlConfig ) ;
19+ const filterReason = await filterWebhook ( headers , json , urlConfig ) ;
4620 if ( filterReason !== null ) {
47- const reqLog = requestLog ( req . headers ) ;
21+ const reqLog = requestLog ( headers ) ;
4822 reqLog . debug ( `handler: ignored due to '${ filterReason } '` ) ;
49- return new Response ( `Ignored by webhook filter (reason: ${ filterReason } )` , { status : 203 } ) ;
23+ return [
24+ new Response ( `Ignored by webhook filter (reason: ${ filterReason } )` , { status : 203 } ) ,
25+ { } ,
26+ ] ;
5027 }
5128
5229 // mutate `json` in-place (fixing codeblocks etc.)
5330 fixupEmbeds ( json ) ;
5431
55- return await sendWebhook ( id , token , req . headers , json ) ;
32+ return await sendWebhook ( id , token , headers , json ) ;
5633}
5734
58- function getUrlConfig ( params : URLSearchParams ) : UrlConfig {
35+ function getUrlConfig ( params : Record < string , string > ) : UrlConfig {
5936 const config : UrlConfig = { } ;
60- for ( const [ key , value ] of params ) {
37+ for ( const [ key , value ] of Object . entries ( params ) ) {
6138 switch ( key ) {
6239 case "sig" :
6340 continue ;
@@ -71,7 +48,7 @@ function getUrlConfig(params: URLSearchParams): UrlConfig {
7148 config . commentBurstLimit = parseInt ( value ) ;
7249 break ;
7350 default :
74- throw httpErrors . createError ( 418 , `Unknown config option: ${ key } ` ) ;
51+ throw new HTTPException ( 418 , { message : `Unknown config option: ${ key } ` } ) ;
7552 }
7653 }
7754 return config ;
0 commit comments