From 675abe6eec0b5d6eaf265359a08ec84fb88dd914 Mon Sep 17 00:00:00 2001 From: Samuel Papineau Date: Wed, 8 Nov 2023 21:58:25 -0500 Subject: [PATCH] Changed remaining new Response() to toText() --- src/fetch/GET.ts | 6 +++--- src/fetch/POST.ts | 11 ++++++----- src/health.ts | 4 +++- src/websocket/subscribe.ts | 1 + 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/fetch/GET.ts b/src/fetch/GET.ts index e547d81..57fc053 100644 --- a/src/fetch/GET.ts +++ b/src/fetch/GET.ts @@ -7,7 +7,7 @@ import { logger } from "../logger.js"; import openapi from "./openapi.js"; import swaggerHtml from "../../swagger/index.html" import swaggerFavicon from "../../swagger/favicon.png" -import { toFile, toJSON } from "./cors.js"; +import { toFile, toJSON, toText } from "./cors.js"; export default async function (req: Request, server: Server) { const { pathname, searchParams} = new URL(req.url); @@ -26,11 +26,11 @@ export default async function (req: Request, server: Server) { if ( pathname === "/" ) return toFile(Bun.file(swaggerHtml)); if ( pathname === "/favicon.png" ) return toFile(Bun.file(swaggerFavicon)); if ( pathname === "/health") return checkHealth(); - if ( pathname === "/metrics") return new Response(await prometheus.registry.metrics()); + if ( pathname === "/metrics") return toText(await prometheus.registry.metrics()); if ( pathname === "/moduleHash") return toJSON(sqlite.selectAll(db, "moduleHash")); if ( pathname === "/moduleHashByChain") return toJSON(sqlite.selectAll(db, "moduleHashByChain")); if ( pathname === "/traceId") return toJSON(sqlite.selectAll(db, "traceId")); if ( pathname === "/chain") return toJSON(sqlite.selectAll(db, "chain")); if ( pathname === "/openapi") return toJSON(openapi); - return new Response("Not found", { status: 400 }); + return toText("Not found", 400 ); } \ No newline at end of file diff --git a/src/fetch/POST.ts b/src/fetch/POST.ts index 7bbbef4..313a462 100644 --- a/src/fetch/POST.ts +++ b/src/fetch/POST.ts @@ -5,6 +5,7 @@ import { logger } from "../logger.js"; import { verify } from "../verify.js"; import { PUBLIC_KEY } from "../config.js"; import { Server } from "bun"; +import { toText } from "./cors.js"; export default async function (req: Request, server: Server) { // get headers and body from POST request @@ -21,7 +22,7 @@ export default async function (req: Request, server: Server) { } catch (e) { logger.error(e); prometheus.webhook_message_received_errors.inc(1); - return new Response(e.message, { status: 400 }); + return toText(e.message, 400 ); } // verify request @@ -29,7 +30,7 @@ export default async function (req: Request, server: Server) { const isVerified = verify(msg, signature, PUBLIC_KEY); if (!isVerified) { prometheus.webhook_message_received_errors.inc(1); - return new Response("invalid request signature", { status: 401 }); + return toText("invalid request signature", 401 ); } const json = JSON.parse(body); @@ -37,7 +38,7 @@ export default async function (req: Request, server: Server) { if (json?.message == "PING") { const message = JSON.parse(body).message; logger.info('PING WebHook handshake', {message}); - return new Response("OK"); + return toText("OK"); } // Get data from Substreams metadata const { clock, manifest, session } = json; @@ -55,7 +56,7 @@ export default async function (req: Request, server: Server) { } catch (e) { logger.error(e); prometheus.webhook_message_received_errors.inc(1); - return new Response(e.message, { status: 400 }); + return toText(e.message, 400 ); } // publish message to subscribers @@ -84,5 +85,5 @@ export default async function (req: Request, server: Server) { sqlite.replace(db, "moduleHashByChain", `${chain}:${moduleHash}`, timestamp); sqlite.replace(db, "traceId", `${chain}:${traceId}`, timestamp); - return new Response("OK"); + return toText("OK"); } \ No newline at end of file diff --git a/src/health.ts b/src/health.ts index 8e3a8c0..18b17da 100644 --- a/src/health.ts +++ b/src/health.ts @@ -1,4 +1,6 @@ +import { toText } from "./fetch/cors.js"; + // https://github.com/pinax-network/substreams-sink-websockets/issues/2#issuecomment-1746121519 export function checkHealth() { - return new Response("OK"); + return toText("OK"); }; \ No newline at end of file diff --git a/src/websocket/subscribe.ts b/src/websocket/subscribe.ts index f41ce85..ff31c89 100644 --- a/src/websocket/subscribe.ts +++ b/src/websocket/subscribe.ts @@ -1,3 +1,4 @@ + import { ServerWebSocket } from "bun"; import { logger } from "../logger.js"; import { ServerWebSocketData, db } from "../../index.js";