Skip to content

Commit

Permalink
Changed remaining new Response() to toText()
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelpapineau committed Nov 9, 2023
1 parent b969672 commit 675abe6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/fetch/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 );
}
11 changes: 6 additions & 5 deletions src/fetch/POST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,23 +22,23 @@ 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
const msg = Buffer.from(timestamp + body);
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);

// Webhook handshake (not WebSocket related)
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;
Expand All @@ -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
Expand Down Expand Up @@ -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");
}
4 changes: 3 additions & 1 deletion src/health.ts
Original file line number Diff line number Diff line change
@@ -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");
};
1 change: 1 addition & 0 deletions src/websocket/subscribe.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import { ServerWebSocket } from "bun";
import { logger } from "../logger.js";
import { ServerWebSocketData, db } from "../../index.js";
Expand Down

0 comments on commit 675abe6

Please sign in to comment.