Skip to content

Commit 7502600

Browse files
committed
Do not use a singleton for EE (#1543)
1 parent 8c0cd05 commit 7502600

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

lib/Transport.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ const {
3636

3737
const noop = () => {}
3838

39-
const productCheckEmitter = new EventEmitter()
4039
const clientVersion = require('../package.json').version
4140
const userAgent = `elasticsearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${process.version})`
4241
const MAX_BUFFER_LENGTH = buffer.constants.MAX_LENGTH
4342
const MAX_STRING_LENGTH = buffer.constants.MAX_STRING_LENGTH
4443
const kProductCheck = Symbol('product check')
4544
const kApiVersioning = Symbol('api versioning')
45+
const kEventEmitter = Symbol('event emitter')
4646

4747
class Transport {
4848
constructor (opts) {
@@ -71,6 +71,7 @@ class Transport {
7171
this.opaqueIdPrefix = opts.opaqueIdPrefix
7272
this[kProductCheck] = 0 // 0 = to be checked, 1 = checking, 2 = checked-ok, 3 checked-notok, 4 checked-nodefault
7373
this[kApiVersioning] = process.env.ELASTIC_CLIENT_APIVERSIONING === 'true'
74+
this[kEventEmitter] = new EventEmitter()
7475

7576
this.nodeFilter = opts.nodeFilter || defaultNodeFilter
7677
if (typeof opts.nodeSelector === 'function') {
@@ -455,7 +456,7 @@ class Transport {
455456
prepareRequest()
456457
} else {
457458
// wait for product check to finish
458-
productCheckEmitter.once('product-check', (error, status) => {
459+
this[kEventEmitter].once('product-check', (error, status) => {
459460
if (status === false) {
460461
const err = error || new ProductNotSupportedError(result)
461462
if (this[kProductCheck] === 4) {
@@ -556,48 +557,48 @@ class Transport {
556557
if (err.statusCode === 401 || err.statusCode === 403) {
557558
this[kProductCheck] = 2
558559
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.')
559-
productCheckEmitter.emit('product-check', null, true)
560+
this[kEventEmitter].emit('product-check', null, true)
560561
} else {
561562
this[kProductCheck] = 0
562-
productCheckEmitter.emit('product-check', err, false)
563+
this[kEventEmitter].emit('product-check', err, false)
563564
}
564565
} else {
565566
debug('Checking elasticsearch version', result.body, result.headers)
566567
if (result.body.version == null || typeof result.body.version.number !== 'string') {
567568
debug('Can\'t access Elasticsearch version')
568-
return productCheckEmitter.emit('product-check', null, false)
569+
return this[kEventEmitter].emit('product-check', null, false)
569570
}
570571
const tagline = result.body.tagline
571572
const version = result.body.version.number.split('.')
572573
const major = Number(version[0])
573574
const minor = Number(version[1])
574575
if (major < 6) {
575-
return productCheckEmitter.emit('product-check', null, false)
576+
return this[kEventEmitter].emit('product-check', null, false)
576577
} else if (major >= 6 && major < 7) {
577578
if (tagline !== 'You Know, for Search') {
578579
debug('Bad tagline')
579-
return productCheckEmitter.emit('product-check', null, false)
580+
return this[kEventEmitter].emit('product-check', null, false)
580581
}
581582
} else if (major === 7 && minor < 14) {
582583
if (tagline !== 'You Know, for Search') {
583584
debug('Bad tagline')
584-
return productCheckEmitter.emit('product-check', null, false)
585+
return this[kEventEmitter].emit('product-check', null, false)
585586
}
586587

587588
if (result.body.version.build_flavor !== 'default') {
588589
debug('Bad build_flavor')
589590
this[kProductCheck] = 4
590-
return productCheckEmitter.emit('product-check', null, false)
591+
return this[kEventEmitter].emit('product-check', null, false)
591592
}
592593
} else {
593594
if (result.headers['x-elastic-product'] !== 'Elasticsearch') {
594595
debug('x-elastic-product not recognized')
595-
return productCheckEmitter.emit('product-check', null, false)
596+
return this[kEventEmitter].emit('product-check', null, false)
596597
}
597598
}
598599
debug('Valid Elasticsearch distribution')
599600
this[kProductCheck] = 2
600-
productCheckEmitter.emit('product-check', null, true)
601+
this[kEventEmitter].emit('product-check', null, true)
601602
}
602603
})
603604
}

0 commit comments

Comments
 (0)