-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (54 loc) · 1.92 KB
/
index.ts
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
63
64
65
66
67
import { createHash, download } from "substreams";
import { run, logger, RunOptions } from "substreams-sink";
import pkg from "./package.json";
import { RabbitMq } from "./src/rabbitmq";
logger.setName(pkg.name);
export { logger };
// default user options
export const DEFAULT_USERNAME = 'guest';
export const DEFAULT_PASSWORD = 'guest';
export const DEFAULT_ADDRESS = 'localhost';
export const DEFAULT_PORT = 5672;
export const DEFAULT_EXCHANGE_TYPE = 'direct';
export const DEFAULT_EXCHANGE_DURABLE = false;
// Custom user options interface
interface ActionOptions extends RunOptions {
address: string;
port: number;
username: string;
password: string;
exchangeType: string;
exchangeDurable: boolean;
routingKey: string;
}
export async function action(manifest: string, moduleName: string, options: ActionOptions) {
// Download substreams and create hash
const spkg = await download(manifest);
const hash = createHash(spkg);
// Get command options
const { address, port, username, password, exchangeType, exchangeDurable, routingKey } = options;
// Initialize RabbitMQ
const rabbitMq = new RabbitMq(username, password, address, port);
await rabbitMq.init(options.substreamsEndpoint!, exchangeType, { durable: exchangeDurable });
logger.info(`Connecting to RabbitMQ: ${address}:${port}`);
// Run substreams
const substreams = run(spkg, moduleName, options);
substreams.on("anyMessage", async (message) => {
let opts: any | undefined;
switch (exchangeType) {
case "headers": {
opts = { headers: { hash, moduleName } };
break;
}
case "topic": {
opts = { routingKey };
break;
}
default: {
break;
}
}
rabbitMq.sendToQueue(message, opts);
});
substreams.start(options.delayBeforeStart);
}