-
Notifications
You must be signed in to change notification settings - Fork 729
/
Copy pathTransport.js
684 lines (617 loc) · 23.1 KB
/
Transport.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
'use strict'
const debug = require('debug')('elasticsearch')
const os = require('os')
const { gzip, unzip, createGzip } = require('zlib')
const buffer = require('buffer')
const ms = require('ms')
const { EventEmitter } = require('events')
const {
ConnectionError,
RequestAbortedError,
NoLivingConnectionsError,
ResponseError,
ConfigurationError,
ProductNotSupportedError
} = require('./errors')
const noop = () => {}
const productCheckEmitter = new EventEmitter()
const clientVersion = require('../package.json').version
const userAgent = `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${process.version})`
const MAX_BUFFER_LENGTH = buffer.constants.MAX_LENGTH
const MAX_STRING_LENGTH = buffer.constants.MAX_STRING_LENGTH
const kProductCheck = Symbol('product check')
const kApiVersioning = Symbol('api versioning')
class Transport {
constructor (opts) {
if (typeof opts.compression === 'string' && opts.compression !== 'gzip') {
throw new ConfigurationError(`Invalid compression: '${opts.compression}'`)
}
this.emit = opts.emit
this.connectionPool = opts.connectionPool
this.serializer = opts.serializer
this.maxRetries = opts.maxRetries
this.requestTimeout = toMs(opts.requestTimeout)
this.suggestCompression = opts.suggestCompression === true
this.compression = opts.compression || false
this.context = opts.context || null
this.headers = Object.assign({},
{ 'user-agent': userAgent },
opts.suggestCompression === true ? { 'accept-encoding': 'gzip,deflate' } : null,
lowerCaseHeaders(opts.headers)
)
this.sniffInterval = opts.sniffInterval
this.sniffOnConnectionFault = opts.sniffOnConnectionFault
this.sniffEndpoint = opts.sniffEndpoint
this.generateRequestId = opts.generateRequestId || generateRequestId()
this.name = opts.name
this.opaqueIdPrefix = opts.opaqueIdPrefix
this[kProductCheck] = 0 // 0 = to be checked, 1 = checking, 2 = checked-ok, 3 checked-notok, 4 checked-nodefault
this[kApiVersioning] = process.env.ELASTIC_CLIENT_APIVERSIONING === 'true'
this.nodeFilter = opts.nodeFilter || defaultNodeFilter
if (typeof opts.nodeSelector === 'function') {
this.nodeSelector = opts.nodeSelector
} else if (opts.nodeSelector === 'round-robin') {
this.nodeSelector = roundRobinSelector()
} else if (opts.nodeSelector === 'random') {
this.nodeSelector = randomSelector
} else {
this.nodeSelector = roundRobinSelector()
}
this._sniffEnabled = typeof this.sniffInterval === 'number'
this._nextSniff = this._sniffEnabled ? (Date.now() + this.sniffInterval) : 0
this._isSniffing = false
if (opts.sniffOnStart === true) {
// timer needed otherwise it will clash
// with the product check testing
setTimeout(() => {
this.sniff({ reason: Transport.sniffReasons.SNIFF_ON_START })
}, 10)
}
}
request (params, options, callback) {
options = options || {}
if (typeof options === 'function') {
callback = options
options = {}
}
let p = null
// promises support
if (callback === undefined) {
let onFulfilled = null
let onRejected = null
p = new Promise((resolve, reject) => {
onFulfilled = resolve
onRejected = reject
})
callback = function callback (err, result) {
err ? onRejected(err) : onFulfilled(result)
}
}
const meta = {
context: null,
request: {
params: null,
options: null,
id: options.id || this.generateRequestId(params, options)
},
name: this.name,
connection: null,
attempts: 0,
aborted: false
}
if (this.context != null && options.context != null) {
meta.context = Object.assign({}, this.context, options.context)
} else if (this.context != null) {
meta.context = this.context
} else if (options.context != null) {
meta.context = options.context
}
const result = {
body: null,
statusCode: null,
headers: null,
meta
}
Object.defineProperty(result, 'warnings', {
get () {
return this.headers && this.headers.warning
? this.headers.warning.split(/(?!\B"[^"]*),(?![^"]*"\B)/)
: null
}
})
// We should not retry if we are sending a stream body, because we should store in memory
// a copy of the stream to be able to send it again, but since we don't know in advance
// the size of the stream, we risk to take too much memory.
// Furthermore, copying everytime the stream is very a expensive operation.
const maxRetries = isStream(params.body) || isStream(params.bulkBody)
? 0
: (typeof options.maxRetries === 'number' ? options.maxRetries : this.maxRetries)
const compression = options.compression !== undefined ? options.compression : this.compression
let request = { abort: noop }
const transportReturn = {
then (onFulfilled, onRejected) {
return p.then(onFulfilled, onRejected)
},
catch (onRejected) {
return p.catch(onRejected)
},
abort () {
meta.aborted = true
request.abort()
debug('Aborting request', params)
return this
},
finally (onFinally) {
return p.finally(onFinally)
}
}
const makeRequest = () => {
if (meta.aborted === true) {
return process.nextTick(callback, new RequestAbortedError(), result)
}
meta.connection = this.getConnection({ requestId: meta.request.id })
if (meta.connection == null) {
return process.nextTick(callback, new NoLivingConnectionsError(), result)
}
this.emit('request', null, result)
// perform the actual http request
request = meta.connection.request(params, onResponse)
}
const onConnectionError = (err) => {
if (err.name !== 'RequestAbortedError') {
// if there is an error in the connection
// let's mark the connection as dead
this.connectionPool.markDead(meta.connection)
if (this.sniffOnConnectionFault === true) {
this.sniff({
reason: Transport.sniffReasons.SNIFF_ON_CONNECTION_FAULT,
requestId: meta.request.id
})
}
// retry logic
if (meta.attempts < maxRetries) {
meta.attempts++
debug(`Retrying request, there are still ${maxRetries - meta.attempts} attempts`, params)
makeRequest()
return
}
}
err.meta = result
this.emit('response', err, result)
return callback(err, result)
}
const onResponse = (err, response) => {
if (err !== null) {
return onConnectionError(err)
}
result.statusCode = response.statusCode
result.headers = response.headers
if (options.asStream === true) {
result.body = response
this.emit('response', null, result)
callback(null, result)
return
}
const contentEncoding = (result.headers['content-encoding'] || '').toLowerCase()
const isCompressed = contentEncoding.indexOf('gzip') > -1 || contentEncoding.indexOf('deflate') > -1
const isVectorTile = (result.headers['content-type'] || '').indexOf('application/vnd.mapbox-vector-tile') > -1
/* istanbul ignore else */
if (result.headers['content-length'] !== undefined) {
const contentLength = Number(result.headers['content-length'])
if (isCompressed && contentLength > MAX_BUFFER_LENGTH) {
response.destroy()
return onConnectionError(
new RequestAbortedError(`The content length (${contentLength}) is bigger than the maximum allowed buffer (${MAX_BUFFER_LENGTH})`, result)
)
} else if (contentLength > MAX_STRING_LENGTH) {
response.destroy()
return onConnectionError(
new RequestAbortedError(`The content length (${contentLength}) is bigger than the maximum allowed string (${MAX_STRING_LENGTH})`, result)
)
}
}
// if the response is compressed, we must handle it
// as buffer for allowing decompression later
// while if it's a vector tile, we should return it as buffer
let payload = isCompressed || isVectorTile ? [] : ''
const onData = isCompressed || isVectorTile
? chunk => { payload.push(chunk) }
: chunk => { payload += chunk }
const onEnd = err => {
response.removeListener('data', onData)
response.removeListener('end', onEnd)
response.removeListener('error', onEnd)
response.removeListener('aborted', onAbort)
if (err) {
return onConnectionError(new ConnectionError(err.message))
}
if (isCompressed) {
unzip(Buffer.concat(payload), onBody)
} else {
onBody(null, isVectorTile ? Buffer.concat(payload) : payload)
}
}
const onAbort = () => {
response.destroy()
onEnd(new Error('Response aborted while reading the body'))
}
if (!isCompressed && !isVectorTile) {
response.setEncoding('utf8')
}
this.emit('deserialization', null, result)
response.on('data', onData)
response.on('error', onEnd)
response.on('end', onEnd)
response.on('aborted', onAbort)
}
const onBody = (err, payload) => {
if (err) {
this.emit('response', err, result)
return callback(err, result)
}
const isVectorTile = (result.headers['content-type'] || '').indexOf('application/vnd.mapbox-vector-tile') > -1
if (Buffer.isBuffer(payload) && !isVectorTile) {
payload = payload.toString()
}
const isHead = params.method === 'HEAD'
// we should attempt the payload deserialization only if:
// - a `content-type` is defined and is equal to `application/json`
// - the request is not a HEAD request
// - the payload is not an empty string
if (result.headers['content-type'] !== undefined &&
(result.headers['content-type'].indexOf('application/json') > -1 ||
result.headers['content-type'].indexOf('application/vnd.elasticsearch+json') > -1) &&
isHead === false &&
payload !== ''
) {
try {
result.body = this.serializer.deserialize(payload)
} catch (err) {
this.emit('response', err, result)
return callback(err, result)
}
} else {
// cast to boolean if the request method was HEAD and there was no error
result.body = isHead === true && result.statusCode < 400 ? true : payload
}
// we should ignore the statusCode if the user has configured the `ignore` field with
// the statusCode we just got or if the request method is HEAD and the statusCode is 404
const ignoreStatusCode = (Array.isArray(options.ignore) && options.ignore.indexOf(result.statusCode) > -1) ||
(isHead === true && result.statusCode === 404)
if (ignoreStatusCode === false &&
(result.statusCode === 502 || result.statusCode === 503 || result.statusCode === 504)) {
// if the statusCode is 502/3/4 we should run our retry strategy
// and mark the connection as dead
this.connectionPool.markDead(meta.connection)
// retry logic (we shoukd not retry on "429 - Too Many Requests")
if (meta.attempts < maxRetries && result.statusCode !== 429) {
meta.attempts++
debug(`Retrying request, there are still ${maxRetries - meta.attempts} attempts`, params)
makeRequest()
return
}
} else {
// everything has worked as expected, let's mark
// the connection as alive (or confirm it)
this.connectionPool.markAlive(meta.connection)
}
if (ignoreStatusCode === false && result.statusCode >= 400) {
const error = new ResponseError(result)
this.emit('response', error, result)
callback(error, result)
} else {
// cast to boolean if the request method was HEAD
if (isHead === true && result.statusCode === 404) {
result.body = false
}
this.emit('response', null, result)
callback(null, result)
}
}
const prepareRequest = () => {
this.emit('serialization', null, result)
const headers = Object.assign({}, this.headers, lowerCaseHeaders(options.headers))
if (options.opaqueId !== undefined) {
headers['x-opaque-id'] = this.opaqueIdPrefix !== null
? this.opaqueIdPrefix + options.opaqueId
: options.opaqueId
}
// handle json body
if (params.body != null) {
if (shouldSerialize(params.body) === true) {
try {
params.body = this.serializer.serialize(params.body)
} catch (err) {
this.emit('request', err, result)
process.nextTick(callback, err, result)
return transportReturn
}
}
if (params.body !== '') {
headers['content-type'] = headers['content-type'] || (this[kApiVersioning] ? 'application/vnd.elasticsearch+json; compatible-with=7' : 'application/json')
}
// handle ndjson body
} else if (params.bulkBody != null) {
if (shouldSerialize(params.bulkBody) === true) {
try {
params.body = this.serializer.ndserialize(params.bulkBody)
} catch (err) {
this.emit('request', err, result)
process.nextTick(callback, err, result)
return transportReturn
}
} else {
params.body = params.bulkBody
}
if (params.body !== '') {
headers['content-type'] = headers['content-type'] || (this[kApiVersioning] ? 'application/vnd.elasticsearch+x-ndjson; compatible-with=7' : 'application/x-ndjson')
}
}
params.headers = headers
// serializes the querystring
if (options.querystring == null) {
params.querystring = this.serializer.qserialize(params.querystring)
} else {
params.querystring = this.serializer.qserialize(
Object.assign({}, params.querystring, options.querystring)
)
}
// handles request timeout
params.timeout = toMs(options.requestTimeout || this.requestTimeout)
if (options.asStream === true) params.asStream = true
meta.request.params = params
meta.request.options = options
// handle compression
if (params.body !== '' && params.body != null) {
if (isStream(params.body) === true) {
if (compression === 'gzip') {
params.headers['content-encoding'] = compression
params.body = params.body.pipe(createGzip())
}
makeRequest()
} else if (compression === 'gzip') {
gzip(params.body, (err, buffer) => {
/* istanbul ignore next */
if (err) {
this.emit('request', err, result)
return callback(err, result)
}
params.headers['content-encoding'] = compression
params.headers['content-length'] = '' + Buffer.byteLength(buffer)
params.body = buffer
makeRequest()
})
} else {
params.headers['content-length'] = '' + Buffer.byteLength(params.body)
makeRequest()
}
} else {
makeRequest()
}
}
// still need to check the product or waiting for the check to finish
if (this[kProductCheck] === 0 || this[kProductCheck] === 1) {
// let pass info requests
if (params.method === 'GET' && params.path === '/') {
prepareRequest()
} else {
// wait for product check to finish
productCheckEmitter.once('product-check', (error, status) => {
if (status === false) {
const err = error || new ProductNotSupportedError(result)
if (this[kProductCheck] === 4) {
err.message = 'The client noticed that the server is not a supported distribution of Elasticsearch'
}
this.emit('request', err, result)
process.nextTick(callback, err, result)
} else {
prepareRequest()
}
})
// the very first request triggers the product check
if (this[kProductCheck] === 0) {
this.productCheck()
}
}
// the product check is finished and it's not Elasticsearch
} else if (this[kProductCheck] === 3 || this[kProductCheck] === 4) {
const err = new ProductNotSupportedError(result)
if (this[kProductCheck] === 4) {
err.message = 'The client noticed that the server is not a supported distribution of Elasticsearch'
}
this.emit('request', err, result)
process.nextTick(callback, err, result)
// the product check finished and it's Elasticsearch
} else {
prepareRequest()
}
return transportReturn
}
getConnection (opts) {
const now = Date.now()
if (this._sniffEnabled === true && now > this._nextSniff) {
this.sniff({ reason: Transport.sniffReasons.SNIFF_INTERVAL, requestId: opts.requestId })
}
return this.connectionPool.getConnection({
filter: this.nodeFilter,
selector: this.nodeSelector,
requestId: opts.requestId,
name: this.name,
now
})
}
sniff (opts, callback = noop) {
if (this._isSniffing === true) return
this._isSniffing = true
debug('Started sniffing request')
if (typeof opts === 'function') {
callback = opts
opts = { reason: Transport.sniffReasons.DEFAULT }
}
const { reason } = opts
const request = {
method: 'GET',
path: this.sniffEndpoint
}
this.request(request, { id: opts.requestId }, (err, result) => {
this._isSniffing = false
if (this._sniffEnabled === true) {
this._nextSniff = Date.now() + this.sniffInterval
}
if (err != null) {
debug('Sniffing errored', err)
result.meta.sniff = { hosts: [], reason }
this.emit('sniff', err, result)
return callback(err)
}
debug('Sniffing ended successfully', result.body)
const protocol = result.meta.connection.url.protocol || /* istanbul ignore next */ 'http:'
const hosts = this.connectionPool.nodesToHost(result.body.nodes, protocol)
this.connectionPool.update(hosts)
result.meta.sniff = { hosts, reason }
this.emit('sniff', null, result)
callback(null, hosts)
})
}
productCheck () {
debug('Start product check')
this[kProductCheck] = 1
this.request({
method: 'GET',
path: '/'
}, (err, result) => {
this[kProductCheck] = 3
if (err) {
debug('Product check failed', err)
if (err.statusCode === 401 || err.statusCode === 403) {
this[kProductCheck] = 2
process.emitWarning('The client is unable to verify that the server is Elasticsearch due to security privileges on the server side. Some functionality may not be compatible if the server is running an unsupported product.')
productCheckEmitter.emit('product-check', null, true)
} else {
this[kProductCheck] = 0
productCheckEmitter.emit('product-check', err, false)
}
} else {
debug('Checking elasticsearch version', result.body, result.headers)
if (result.body.version == null || typeof result.body.version.number !== 'string') {
debug('Can\'t access Elasticsearch version')
return productCheckEmitter.emit('product-check', null, false)
}
const tagline = result.body.tagline
const version = result.body.version.number.split('.')
const major = Number(version[0])
const minor = Number(version[1])
if (major < 6) {
return productCheckEmitter.emit('product-check', null, false)
} else if (major >= 6 && major < 7) {
if (tagline !== 'You Know, for Search') {
debug('Bad tagline')
return productCheckEmitter.emit('product-check', null, false)
}
} else if (major === 7 && minor < 14) {
if (tagline !== 'You Know, for Search') {
debug('Bad tagline')
return productCheckEmitter.emit('product-check', null, false)
}
if (result.body.version.build_flavor !== 'default') {
debug('Bad build_flavor')
this[kProductCheck] = 4
return productCheckEmitter.emit('product-check', null, false)
}
} else {
if (result.headers['x-elastic-product'] !== 'Elasticsearch') {
debug('x-elastic-product not recognized')
return productCheckEmitter.emit('product-check', null, false)
}
}
debug('Valid Elasticsearch distribution')
this[kProductCheck] = 2
productCheckEmitter.emit('product-check', null, true)
}
})
}
}
Transport.sniffReasons = {
SNIFF_ON_START: 'sniff-on-start',
SNIFF_INTERVAL: 'sniff-interval',
SNIFF_ON_CONNECTION_FAULT: 'sniff-on-connection-fault',
// TODO: find a better name
DEFAULT: 'default'
}
function toMs (time) {
if (typeof time === 'string') {
return ms(time)
}
return time
}
function shouldSerialize (obj) {
return typeof obj !== 'string' &&
typeof obj.pipe !== 'function' &&
Buffer.isBuffer(obj) === false
}
function isStream (obj) {
return obj != null && typeof obj.pipe === 'function'
}
function defaultNodeFilter (node) {
// avoid master only nodes
if (node.roles.master === true &&
node.roles.data === false &&
node.roles.ingest === false) {
return false
}
return true
}
function roundRobinSelector () {
let current = -1
return function _roundRobinSelector (connections) {
if (++current >= connections.length) {
current = 0
}
return connections[current]
}
}
function randomSelector (connections) {
const index = Math.floor(Math.random() * connections.length)
return connections[index]
}
function generateRequestId () {
const maxInt = 2147483647
let nextReqId = 0
return function genReqId (params, options) {
return (nextReqId = (nextReqId + 1) & maxInt)
}
}
function lowerCaseHeaders (oldHeaders) {
if (oldHeaders == null) return oldHeaders
const newHeaders = {}
for (const header in oldHeaders) {
newHeaders[header.toLowerCase()] = oldHeaders[header]
}
return newHeaders
}
module.exports = Transport
module.exports.internals = {
defaultNodeFilter,
roundRobinSelector,
randomSelector,
generateRequestId,
lowerCaseHeaders
}