Skip to content

Commit e25dce1

Browse files
committed
chore: move out nested function in http.ts
1 parent 2024999 commit e25dce1

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/server/http.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { shutdownController } from "../shutdown.ts";
88
import { env } from "../env.ts";
99
import { SyncResult } from "../sync/syncer.ts";
1010
import { wrapGenericPostgresInterface } from "../sql/postgresjs.ts";
11+
import type { RateLimitResult } from "@rabbit-company/rate-limiter";
1112

1213
const 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+
223225
const corsHeaders = {
224226
"Access-Control-Allow-Origin": "*",
225227
// cache the preflight requests for 1 day

0 commit comments

Comments
 (0)