-
Notifications
You must be signed in to change notification settings - Fork 44
/
_client.js
67 lines (58 loc) · 1.57 KB
/
_client.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
'use strict';
var formatUrl = require('url').format;
var argv = require('./argv');
var through2 = require('through2');
var parse = require('url').parse;
var elasticsearch = require('elasticsearch');
var Client = elasticsearch.Client;
var NoConnections = elasticsearch.errors.NoConnections;
var RequestTimeout = elasticsearch.errors.RequestTimeout;
var url = argv.url;
if (!url) {
var host = String(argv.host);
var proto = host.includes('//') ? '' : '//';
var parsed = parse(proto + host, false, true);
url = formatUrl({
hostname: parsed.hostname,
port: parsed.port,
auth: argv.auth
});
}
var makeUseable;
var usable = new Promise(function (resolve) {
makeUseable = resolve;
});
var ms = 5000;
var client = module.exports = new Client({
log: {
type: 'stream',
level: argv.trace ? 'trace' : 'warning',
stream: through2(function (chunk, enc, cb) {
usable.then(function () {
process.stdout.write(chunk, enc);
cb();
});
})
},
host: url,
ssl: argv.insecure ? { rejectUnauthorized: false, pfx: [] } : {}
});
client.usable = usable;
client.ping({
requestTimeout: ms
})
.then(function () {
makeUseable();
})
.catch(function (err) {
var notAlive = err instanceof NoConnections;
var timeout = err instanceof RequestTimeout;
if (notAlive || timeout) {
console.error('Unable to connect to elasticsearch at %s within %d seconds', host, ms / 1000);
} else {
console.log('unknown ping error', err);
}
client.close();
// prevent the promise from ever resolving or rejecting
return new Promise(() => {});
});