Skip to content

Commit

Permalink
Updated latest cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelpapineau committed Dec 5, 2023
1 parent 57af741 commit e0d4046
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/fetch/GET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import swaggerFavicon from "../../swagger/favicon.png"
import { toFile, toJSON, toText } from "./cors.js";
import { handleMessages, selectMessages } from "./messages.js";
import { checkHealth } from "./health.js";
import { latestCursor } from "./cursor.js";

export default async function (req: Request, server: Server) {
const { pathname, searchParams} = new URL(req.url);

// Bun automatically returns a 101 Switching Protocols
// if the upgrade succeeds
const key = req.headers.get("sec-websocket-key")
const chain = searchParams.get("chain")
const chain = searchParams.get("chain");
const moduleHash = searchParams.get("moduleHash");
const payload = JSON.parse(selectMessages(db, 1, "desc", searchParams.get("chain"), searchParams.get("moduleHash"))[0].payload)
const success = server.upgrade(req, {data: {key, chain, moduleHash}});
if (success) {
logger.info('upgrade', {key, chain, moduleHash});
Expand All @@ -35,7 +35,7 @@ export default async function (req: Request, server: Server) {
if ( pathname === "/chain") return toJSON(sqlite.selectAll(db, "chain"));
if ( pathname === "/openapi") return toJSON(openapi);
if ( pathname === "/messages") return handleMessages(req);
if ( pathname === "/cursor/latest") return toText(payload.cursor);
if ( pathname === "/cursor/latest") return latestCursor(req);

return toText("Not found", 400 );
}
17 changes: 17 additions & 0 deletions src/fetch/cursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { handleMessages, selectMessages } from "./messages.js";
import * as sqlite from "../sqlite.js";
import { db } from "../../index.js";
import { toText } from "./cors.js";


export function latestCursor(req: Request) {
const { searchParams} = new URL(req.url);
const chain = searchParams.get("chain");
const moduleHash = searchParams.get("moduleHash");

if ( !sqlite.exists(db, "chain", chain) && chain != null) throw new Error(`Chain [${chain}] not found.`);
if ( !sqlite.exists(db, "moduleHash", moduleHash) && moduleHash != null) throw new Error(`ModuleHash [${moduleHash}] not found.`);

const payload = selectMessages(db, 1, "desc", searchParams.get("chain"), searchParams.get("moduleHash"))[0]?.payload;
return toText(JSON.parse(payload)?.cursor)
}

0 comments on commit e0d4046

Please sign in to comment.