-
Notifications
You must be signed in to change notification settings - Fork 727
/
Copy pathindex.js
147 lines (130 loc) · 3.99 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict'
const { EventEmitter } = require('events')
const debug = require('debug')('elasticsearch')
const Transport = require('./lib/Transport')
const Connection = require('./lib/Connection')
const ConnectionPool = require('./lib/ConnectionPool')
const Serializer = require('./lib/Serializer')
const errors = require('./lib/errors')
const { ConfigurationError } = errors
const buildApi = require('./api')
class Client extends EventEmitter {
constructor (opts = {}) {
super()
if (!opts.node && !opts.nodes) {
throw new ConfigurationError('Missing node(s) option')
}
if (opts.log === true) {
this.on('request', console.log)
this.on('response', console.log)
this.on('error', console.log)
}
const options = Object.assign({}, {
Connection,
ConnectionPool,
Transport,
Serializer,
maxRetries: 3,
requestTimeout: 30000,
pingTimeout: 3000,
sniffInterval: false,
sniffOnStart: false,
sniffEndpoint: '_nodes/_all/http',
sniffOnConnectionFault: false,
resurrectStrategy: 'ping',
randomizeHost: true,
suggestCompression: false,
ssl: null,
agent: null,
nodeFilter: null,
nodeWeighter: null,
nodeSelector: 'round-robin'
}, opts)
this.serializer = new options.Serializer()
this.connectionPool = new options.ConnectionPool({
pingTimeout: options.pingTimeout,
resurrectStrategy: options.resurrectStrategy,
randomizeHost: options.randomizeHost,
ssl: options.ssl,
agent: options.agent,
nodeFilter: options.nodeFilter,
nodeWeighter: options.nodeWeighter,
nodeSelector: options.nodeSelector,
Connection: options.Connection,
emit: this.emit.bind(this)
})
// Add the connections before initialize the Transport
this.connectionPool.addConnection(options.node || options.nodes)
this.transport = new options.Transport({
emit: this.emit.bind(this),
connectionPool: this.connectionPool,
serializer: this.serializer,
maxRetries: options.maxRetries,
requestTimeout: options.requestTimeout,
sniffInterval: options.sniffInterval,
sniffOnStart: options.sniffOnStart,
sniffOnConnectionFault: options.sniffOnConnectionFault,
sniffEndpoint: options.sniffEndpoint,
suggestCompression: options.suggestCompression
})
const apis = buildApi({
makeRequest: this.transport.request.bind(this.transport),
result: { body: null, statusCode: null, headers: null, warnings: null },
ConfigurationError
})
Object.keys(apis).forEach(api => {
this[api] = apis[api]
})
}
extend (name, fn) {
var [namespace, method] = name.split('.')
if (method == null) {
method = namespace
namespace = null
}
if (namespace != null) {
if (this[namespace] != null && this[namespace][method] != null) {
throw new Error(`The method "${method}" already exists on namespace "${namespace}"`)
}
this[namespace] = this[namespace] || {}
this[namespace][method] = fn({
makeRequest: this.transport.request.bind(this.transport),
result: { body: null, statusCode: null, headers: null, warnings: null },
ConfigurationError
})
} else {
if (this[method] != null) {
throw new Error(`The method "${method}" already exists`)
}
this[method] = fn({
makeRequest: this.transport.request.bind(this.transport),
result: { body: null, statusCode: null, headers: null, warnings: null },
ConfigurationError
})
}
}
close (callback) {
if (callback == null) {
return new Promise((resolve, reject) => {
this.close(resolve)
})
}
debug('Closing the client')
this.connectionPool.empty(callback)
}
}
const events = {
RESPONSE: 'response',
REQUEST: 'request',
SNIFF: 'sniff',
RESURRECT: 'resurrect'
}
module.exports = {
Client,
Transport,
ConnectionPool,
Connection,
Serializer,
events,
errors
}