@@ -8,6 +8,7 @@ import { shutdownController } from "../shutdown.ts";
88import { env } from "../env.ts" ;
99import { SyncResult } from "../sync/syncer.ts" ;
1010import { wrapGenericPostgresInterface } from "../sql/postgresjs.ts" ;
11+ import type { RateLimitResult } from "@rabbit-company/rate-limiter" ;
1112
1213const syncer = new PostgresSyncer ( wrapGenericPostgresInterface ) ;
1314
@@ -195,31 +196,32 @@ export function createServer(hostname: string, port: number) {
195196 limit ,
196197 ) ;
197198 }
198- function handleResponse ( res : Response ) {
199- for ( const [ key , value ] of Object . entries ( corsHeaders ) ) {
200- res . headers . set ( key , value ) ;
201- }
202- limiter . appendHeaders ( res , limit ) ;
203- return res ;
204- }
205199 if ( url . pathname === "/postgres/all" ) {
206200 if ( req . method !== "POST" ) {
207201 return new Response ( "Method not allowed" , { status : 405 } ) ;
208202 }
209203 const res = await onSync ( req ) ;
210- return handleResponse ( res ) ;
204+ return transformResponse ( res , limit ) ;
211205 } else if ( url . pathname === "/postgres/live" ) {
212206 if ( req . method !== "POST" ) {
213207 return new Response ( "Method not allowed" , { status : 405 } ) ;
214208 }
215209 const res = await onSyncLiveQuery ( req ) ;
216- return handleResponse ( res ) ;
210+ return transformResponse ( res , limit ) ;
217211 }
218212 return new Response ( "Not found" , { status : 404 } ) ;
219213 } ,
220214 ) ;
221215}
222216
217+ function transformResponse ( res : Response , limit : RateLimitResult ) : Response {
218+ for ( const [ key , value ] of Object . entries ( corsHeaders ) ) {
219+ res . headers . set ( key , value ) ;
220+ }
221+ limiter . appendHeaders ( res , limit ) ;
222+ return res ;
223+ }
224+
223225const corsHeaders = {
224226 "Access-Control-Allow-Origin" : "*" ,
225227 // cache the preflight requests for 1 day
0 commit comments