-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayments.ts
More file actions
executable file
·62 lines (51 loc) · 1.8 KB
/
Copy pathpayments.ts
File metadata and controls
executable file
·62 lines (51 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: GPL-3.0
// Copyright (c) 2026 The3rdWebLabs (https://github.com/the3rdweblabs)
// Author: @CYBWithFlourish (https://github.com/CYBWithFlourish)
import { Router } from "express";
import redisService from "../services/redis.js";
import logger from "../utils/logger.js";
const router = Router();
const MAX_SSE_SUBSCRIBERS = 50;
let activeSubscriberCount = 0;
/**
* Stream payment status updates for a particular nonce.
* Emits JSON payloads after each backend step (bank, walrus, settle).
*/
router.get("/stream/:nonce", async (req, res) => {
const { nonce } = req.params;
if (activeSubscriberCount >= MAX_SSE_SUBSCRIBERS) {
res.status(503).json({ error: "Too many active stream connections. Try again shortly." });
return;
}
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.setHeader("X-Accel-Buffering", "no");
res.flushHeaders();
const session = await redisService.getSession(nonce);
if (session) {
res.write(
`data: ${JSON.stringify({
status: session.status,
walrusBlobId: session.walrusBlobId,
txDigest: session.txDigest,
error: session.error
})}\n\n`
);
} else {
res.write(`data: ${JSON.stringify({ status: "EXPIRED", error: "Session expired." })}\n\n`);
}
// Subscribe to Redis pub/sub channel for this nonce
const subscriber = redisService.getClient().duplicate();
await subscriber.subscribe(`payment:${nonce}`);
activeSubscriberCount++;
subscriber.on("message", (_, msg) => {
res.write(`data: ${msg}\n\n`);
});
// Cleanup when client disconnects
req.on("close", () => {
subscriber.quit();
activeSubscriberCount = Math.max(0, activeSubscriberCount - 1);
});
});
export default router;