-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
55 lines (43 loc) · 1.31 KB
/
example.js
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
import { commander, setup, prometheus, http, logger, fileCursor } from "./dist/index.js";
const pkg = {
name: "substreams-sink",
version: "0.0.1",
description: "Substreams Sink long description",
}
// Setup CLI using Commander
const program = commander.program(pkg);
const command = commander.addRunOptions(program);
logger.setName(pkg.name);
// Custom Prometheus Counters
const customCounter = prometheus.registerCounter("custom_counter");
command.action(async options => {
// Get cursor from file
const cursor = fileCursor.readCursor("cursor.lock");
// Setup sink for Block Emitter
const { emitter } = await setup({...options, cursor});
emitter.on("session", (session) => {
console.log(session);
});
emitter.on("progress", (progress) => {
console.log(progress);
});
// Stream Blocks
emitter.on("anyMessage", (message, cursor, clock) => {
customCounter?.inc(1);
console.log(message);
console.log(cursor);
console.log(clock);
});
// Setup HTTP server & Prometheus metrics
http.listen(options);
// Save new cursor on each new block emitted
fileCursor.onCursor(emitter, "cursor.lock");
// Close HTTP server on close
emitter.on("close", () => {
http.server.close();
console.log("✅ finished");
})
// Start the stream
emitter.start();
})
program.parse();