From bb8f3977422725ab029ea3e058b041b541e44c07 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 28 Aug 2021 18:22:26 +0200 Subject: [PATCH] Do not use a singleton for EE (#1543) (#1544) Co-authored-by: Tomas Della Vedova --- lib/Transport.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/Transport.js b/lib/Transport.js index d998e6aa1..ca9477ae6 100644 --- a/lib/Transport.js +++ b/lib/Transport.js @@ -36,13 +36,13 @@ const { 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') +const kEventEmitter = Symbol('event emitter') class Transport { constructor (opts) { @@ -71,6 +71,7 @@ class Transport { 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[kEventEmitter] = new EventEmitter() this.nodeFilter = opts.nodeFilter || defaultNodeFilter if (typeof opts.nodeSelector === 'function') { @@ -460,7 +461,7 @@ class Transport { prepareRequest() } else { // wait for product check to finish - productCheckEmitter.once('product-check', (error, status) => { + this[kEventEmitter].once('product-check', (error, status) => { if (status === false) { const err = error || new ProductNotSupportedError(result) if (this[kProductCheck] === 4) { @@ -564,48 +565,48 @@ class Transport { '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.', 'ProductNotSupportedSecurityError' ) - productCheckEmitter.emit('product-check', null, true) + this[kEventEmitter].emit('product-check', null, true) } else { this[kProductCheck] = 0 - productCheckEmitter.emit('product-check', err, false) + this[kEventEmitter].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) + return this[kEventEmitter].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) + return this[kEventEmitter].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) + return this[kEventEmitter].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) + return this[kEventEmitter].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) + return this[kEventEmitter].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) + return this[kEventEmitter].emit('product-check', null, false) } } debug('Valid Elasticsearch distribution') this[kProductCheck] = 2 - productCheckEmitter.emit('product-check', null, true) + this[kEventEmitter].emit('product-check', null, true) } }) }