Skip to content

Commit ef7e95a

Browse files
committed
Do not use a singleton for EE (#1543)
1 parent f2a0008 commit ef7e95a

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') {
@@ -460,7 +461,7 @@ class Transport {
460461
prepareRequest()
461462
} else {
462463
// wait for product check to finish
463-
productCheckEmitter.once('product-check', (error, status) => {
464+
this[kEventEmitter].once('product-check', (error, status) => {
464465
if (status === false) {
465466
const err = error || new ProductNotSupportedError(result)
466467
if (this[kProductCheck] === 4) {
@@ -564,48 +565,48 @@ class Transport {
564565
'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.',
565566
'ProductNotSupportedSecurityError'
566567
)
567-
productCheckEmitter.emit('product-check', null, true)
568+
this[kEventEmitter].emit('product-check', null, true)
568569
} else {
569570
this[kProductCheck] = 0
570-
productCheckEmitter.emit('product-check', err, false)
571+
this[kEventEmitter].emit('product-check', err, false)
571572
}
572573
} else {
573574
debug('Checking elasticsearch version', result.body, result.headers)
574575
if (result.body.version == null || typeof result.body.version.number !== 'string') {
575576
debug('Can\'t access Elasticsearch version')
576-
return productCheckEmitter.emit('product-check', null, false)
577+
return this[kEventEmitter].emit('product-check', null, false)
577578
}
578579
const tagline = result.body.tagline
579580
const version = result.body.version.number.split('.')
580581
const major = Number(version[0])
581582
const minor = Number(version[1])
582583
if (major < 6) {
583-
return productCheckEmitter.emit('product-check', null, false)
584+
return this[kEventEmitter].emit('product-check', null, false)
584585
} else if (major >= 6 && major < 7) {
585586
if (tagline !== 'You Know, for Search') {
586587
debug('Bad tagline')
587-
return productCheckEmitter.emit('product-check', null, false)
588+
return this[kEventEmitter].emit('product-check', null, false)
588589
}
589590
} else if (major === 7 && minor < 14) {
590591
if (tagline !== 'You Know, for Search') {
591592
debug('Bad tagline')
592-
return productCheckEmitter.emit('product-check', null, false)
593+
return this[kEventEmitter].emit('product-check', null, false)
593594
}
594595

595596
if (result.body.version.build_flavor !== 'default') {
596597
debug('Bad build_flavor')
597598
this[kProductCheck] = 4
598-
return productCheckEmitter.emit('product-check', null, false)
599+
return this[kEventEmitter].emit('product-check', null, false)
599600
}
600601
} else {
601602
if (result.headers['x-elastic-product'] !== 'Elasticsearch') {
602603
debug('x-elastic-product not recognized')
603-
return productCheckEmitter.emit('product-check', null, false)
604+
return this[kEventEmitter].emit('product-check', null, false)
604605
}
605606
}
606607
debug('Valid Elasticsearch distribution')
607608
this[kProductCheck] = 2
608-
productCheckEmitter.emit('product-check', null, true)
609+
this[kEventEmitter].emit('product-check', null, true)
609610
}
610611
})
611612
}

0 commit comments

Comments
 (0)