-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnatsPub.js
80 lines (69 loc) · 2.07 KB
/
natsPub.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
const readline = require('readline');
const NATS = require('tasu');
const commandLineArgs = require('command-line-args');
// Опции командной строки
const parsedArgs = initCmdlineParams();
const nats = initNATS(parsedArgs);
;(async () => {
await nats.connected();
const data = JSON.parse(await readJSONFromStdIn());
console.log(data);
if (parsedArgs.request) {
const resp = await nats.request(parsedArgs.topic, data);
console.dir(resp, {colors: true, depth: null});
} else {
nats.publish(parsedArgs.topic, data);
}
})()
.then(() => {
process.exit(0);
})
.catch(err => {
console.error(err);
process.exit(1);
});
/**
*
* @return {Promise<any>}
*/
function readJSONFromStdIn() {
const inputChunks = [];
return new Promise((resolve, reject) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (chunk) {
inputChunks.push(chunk);
});
rl.on('close', function () {
resolve(inputChunks.join(''));
});
});
}
function initCmdlineParams() {
const optionDefinitions = [
{name: 'topic', type: String, multiple: false},
{name: 'request', type: String, multiple: false},
{name: 'url', type: String, multiple: false},
{name: 'user', type: String, multiple: false},
{name: 'pass', type: String, multiple: false}
];
const parsedArgs = commandLineArgs(optionDefinitions);
parsedArgs.request = parsedArgs.request || true;
if (!parsedArgs.topic) throw new Error('Укажите NATS topic в командной строке: --topic тут_топик');
return parsedArgs;
}
function initNATS(cmdLineOptions) {
// NATS
const natsOptions = {
maxReconnectAttempts: -1,
reconnectTimeWait: 250,
url: 'nats://localhost:4222'
};
return new NATS({
...natsOptions,
...cmdLineOptions
})
}