From e2d2a2afc2aca72557afcdcf9eaa53699e2eb053 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 20:44:58 +0600 Subject: [PATCH 01/27] refactor: addConnection Signed-off-by: Timur Bolotov --- lib/pool/BaseConnectionPool.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index c2a901f64..afc1c9b6f 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -32,6 +32,7 @@ const { URL } = require('url'); const debug = require('debug')('opensearch'); const Connection = require('../Connection'); +const { ConfigurationError } = require('../errors'); const noop = () => {}; class BaseConnectionPool { @@ -102,22 +103,28 @@ class BaseConnectionPool { */ addConnection(opts) { if (Array.isArray(opts)) { - return opts.forEach((o) => this.addConnection(o)); - } - - if (typeof opts === 'string') { - opts = this.urlToHost(opts); - } + opts.forEach((o) => this.addConnection(o)); + } else { + const connectionId = opts?.id; + const connectionUrl = opts?.url?.href; + + const connectionById = this.connections.find((c) => c.id === connectionId); + const connectionByUrl = this.connections.find((c) => c.id === connectionUrl); + + if (connectionById || connectionByUrl) { + throw new ConfigurationError( + `Connection with id '${opts.id || opts.url.href}' is already present` + ); + } - const connectionById = this.connections.find((c) => c.id === opts.id); - const connectionByUrl = this.connections.find((c) => c.id === opts.url.href); + if (typeof opts === 'string') { + opts = this.urlToHost(opts); + } - if (connectionById || connectionByUrl) { - throw new Error(`Connection with id '${opts.id || opts.url.href}' is already present`); + const connection = this.createConnection(opts); + this.update([...this.connections, connection]); + return this.connections[this.size - 1]; } - - this.update([...this.connections, opts]); - return this.connections[this.size - 1]; } /** From 1478ac4a8723b16dea6b3ae1438e535d104540ca Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 20:45:19 +0600 Subject: [PATCH 02/27] refactor: improve types Signed-off-by: Timur Bolotov --- lib/pool/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pool/index.d.ts b/lib/pool/index.d.ts index fe6954362..81fe4a7e1 100644 --- a/lib/pool/index.d.ts +++ b/lib/pool/index.d.ts @@ -31,7 +31,7 @@ import { URL } from 'url'; import { SecureContextOptions } from 'tls'; -import Connection, { AgentOptions } from '../Connection'; +import Connection, { AgentOptions, ConnectionOptions } from '../Connection'; import { nodeFilterFn, nodeSelectorFn } from '../Transport'; interface BaseConnectionPoolOptions { @@ -121,7 +121,7 @@ declare class BaseConnectionPool { * @param {object|string} host * @returns {ConnectionPool} */ - addConnection(opts: any): Connection; + addConnection(opts: ConnectionOptions | ConnectionOptions[]): Connection; /** * Removes a new connection to the pool. * @@ -155,7 +155,7 @@ declare class BaseConnectionPool { * @param {string} url * @returns {object} host */ - urlToHost(url: string): { url: URL }; + urlToHost(url: string): ConnectionOptions; } declare class ConnectionPool extends BaseConnectionPool { From 974a81c20a9802d99826bd5d85dd3bfff25cae53 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 20:45:56 +0600 Subject: [PATCH 03/27] fix: connection type tests Signed-off-by: Timur Bolotov --- test/types/connection-pool.test-d.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/types/connection-pool.test-d.ts b/test/types/connection-pool.test-d.ts index 1b35c41db..ffb527f2f 100644 --- a/test/types/connection-pool.test-d.ts +++ b/test/types/connection-pool.test-d.ts @@ -30,6 +30,7 @@ import { expectType, expectAssignable } from 'tsd'; import { URL } from 'url'; import { BaseConnectionPool, ConnectionPool, CloudConnectionPool, Connection } from '../../'; +import { ConnectionOptions } from '../../lib/Connection'; { const pool = new BaseConnectionPool({ @@ -59,12 +60,12 @@ import { BaseConnectionPool, ConnectionPool, CloudConnectionPool, Connection } f now: Date.now(), }) ); - expectType(pool.addConnection({})); + expectType(pool.addConnection({ url: new URL('url') })); expectType(pool.removeConnection(new Connection())); expectType(pool.empty()); expectType(pool.update([])); expectType(pool.nodesToHost([], 'https')); - expectType<{ url: URL }>(pool.urlToHost('url')); + expectType(pool.urlToHost('url')); } { @@ -99,12 +100,12 @@ import { BaseConnectionPool, ConnectionPool, CloudConnectionPool, Connection } f now: Date.now(), }) ); - expectType(pool.addConnection({})); + expectType(pool.addConnection({ url: new URL('url') })); expectAssignable(pool.removeConnection(new Connection())); expectAssignable(pool.empty()); expectAssignable(pool.update([])); expectType(pool.nodesToHost([], 'https')); - expectType<{ url: URL }>(pool.urlToHost('url')); + expectType(pool.urlToHost('url')); expectType( pool.resurrect({ now: Date.now(), From e5dbcead3fdb03beeb28986d06e45d9b0b8630f0 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 21:06:54 +0600 Subject: [PATCH 04/27] feat: addConnection test with only URL provided Signed-off-by: Timur Bolotov --- test/unit/base-connection-pool.test.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/unit/base-connection-pool.test.js b/test/unit/base-connection-pool.test.js index 8af26a393..62952367d 100644 --- a/test/unit/base-connection-pool.test.js +++ b/test/unit/base-connection-pool.test.js @@ -70,6 +70,15 @@ test('API', (t) => { t.end(); }); + t.test('addConnection with only URL', (t) => { + const pool = new BaseConnectionPool({ Connection }); + const href = 'http://localhost:9200/'; + pool.addConnection({ url: new URL(href) }); + t.ok(pool.connections.find((c) => c.id === href) instanceof Connection); + t.equal(pool.connections.find((c) => c.id === href).status, Connection.statuses.ALIVE); + t.end(); + }); + t.test('markDead', (t) => { const pool = new BaseConnectionPool({ Connection, sniffEnabled: true }); const href = 'http://localhost:9200/'; From 74507234a23a0f549fad028b811c7f134abbf0fa Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 21:11:26 +0600 Subject: [PATCH 05/27] refactor: optimization Signed-off-by: Timur Bolotov --- lib/pool/BaseConnectionPool.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index afc1c9b6f..636a784be 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -105,20 +105,22 @@ class BaseConnectionPool { if (Array.isArray(opts)) { opts.forEach((o) => this.addConnection(o)); } else { + if (typeof opts === 'string') { + opts = this.urlToHost(opts); + } + const connectionId = opts?.id; const connectionUrl = opts?.url?.href; - const connectionById = this.connections.find((c) => c.id === connectionId); - const connectionByUrl = this.connections.find((c) => c.id === connectionUrl); + if (connectionId || connectionUrl) { + const connectionById = this.connections.find((c) => c.id === connectionId); + const connectionByUrl = this.connections.find((c) => c.id === connectionUrl); - if (connectionById || connectionByUrl) { - throw new ConfigurationError( - `Connection with id '${opts.id || opts.url.href}' is already present` - ); - } - - if (typeof opts === 'string') { - opts = this.urlToHost(opts); + if (connectionById || connectionByUrl) { + throw new ConfigurationError( + `Connection with id '${opts.id || opts.url.href}' is already present` + ); + } } const connection = this.createConnection(opts); From fc2b6f8f18734ddd33318c1b0af426276f1b6bf3 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 21:16:49 +0600 Subject: [PATCH 06/27] chore: use vars Signed-off-by: Timur Bolotov --- lib/pool/BaseConnectionPool.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index 636a784be..e9a2aaa07 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -118,7 +118,7 @@ class BaseConnectionPool { if (connectionById || connectionByUrl) { throw new ConfigurationError( - `Connection with id '${opts.id || opts.url.href}' is already present` + `Connection with id '${connectionId || connectionUrl}' is already present` ); } } From c045560216d8cfc8422f11ed0fae40aae4e0633e Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 21:22:58 +0600 Subject: [PATCH 07/27] refactor: make callback optional Signed-off-by: Timur Bolotov --- lib/pool/BaseConnectionPool.js | 2 +- test/unit/base-connection-pool.test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index e9a2aaa07..780bae2bd 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -145,7 +145,7 @@ class BaseConnectionPool { * * @returns {ConnectionPool} */ - empty(callback) { + empty(callback = noop) { debug('Emptying the connection pool'); let openConnections = this.size; this.connections.forEach((connection) => { diff --git a/test/unit/base-connection-pool.test.js b/test/unit/base-connection-pool.test.js index 62952367d..34b9bcf3a 100644 --- a/test/unit/base-connection-pool.test.js +++ b/test/unit/base-connection-pool.test.js @@ -131,6 +131,15 @@ test('API', (t) => { }); }); + t.test('empty with no callback', (t) => { + const pool = new BaseConnectionPool({ Connection }); + pool.addConnection('http://localhost:9200/'); + pool.addConnection('http://localhost:9201/'); + pool.empty(); + t.equal(pool.size, 0); + t.end(); + }); + t.test('urlToHost', (t) => { const pool = new BaseConnectionPool({ Connection }); const url = 'http://localhost:9200'; From e9676ede4afb6bb43b6c70d5ae99aa68ac9e119a Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 21:28:48 +0600 Subject: [PATCH 08/27] chore: string type Signed-off-by: Timur Bolotov --- lib/pool/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pool/index.d.ts b/lib/pool/index.d.ts index 81fe4a7e1..fd05f5e1e 100644 --- a/lib/pool/index.d.ts +++ b/lib/pool/index.d.ts @@ -121,7 +121,7 @@ declare class BaseConnectionPool { * @param {object|string} host * @returns {ConnectionPool} */ - addConnection(opts: ConnectionOptions | ConnectionOptions[]): Connection; + addConnection(opts: string | ConnectionOptions | ConnectionOptions[]): Connection; /** * Removes a new connection to the pool. * From 116c3722786e817d3a5d6314728dc80de5ebe30c Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 21:53:51 +0600 Subject: [PATCH 09/27] chore: update changelog Signed-off-by: Timur Bolotov --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81c51f83d..ef4530fdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Make fields in `BulkOperation` optional to match OpenSearch Bulk API requirements ([#378](https://github.com/opensearch-project/opensearch-js/pull/378)) - Remove guidance on using npm and switch completely to yarn in developer_guide ([#439](https://github.com/opensearch-project/opensearch-js/issues/435)) - Change coverage, compatability, integration, integration with unreleased Open Search, node ci, bundler tests not to run on documentation change ([441](https://github.com/opensearch-project/opensearch-js/pull/441)) +- Make `callback` arg in `BaseConnectionPool` optional ([#451](https://github.com/opensearch-project/opensearch-js/pull/451)) ### Deprecated From 68c130071018bee06d10ad0d37b071c19d44dcd7 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 22:00:56 +0600 Subject: [PATCH 10/27] fix: update index.d.ts empty method args Signed-off-by: Timur Bolotov --- lib/pool/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pool/index.d.ts b/lib/pool/index.d.ts index fd05f5e1e..f02899a24 100644 --- a/lib/pool/index.d.ts +++ b/lib/pool/index.d.ts @@ -134,7 +134,7 @@ declare class BaseConnectionPool { * * @returns {ConnectionPool} */ - empty(): this; + empty(callback?: () => void): this; /** * Update the ConnectionPool with new connections. * From 544e5066935afda294f2c2acf8a0ea9909e3b31b Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 22:01:31 +0600 Subject: [PATCH 11/27] chore: user guide on empty method use Signed-off-by: Timur Bolotov --- USER_GUIDE.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 579c170e3..eb2016f16 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -249,4 +249,19 @@ console.log('Deleting all PITs:'); var response = await client.deleteAllPits(); console.log(response.body); +``` + +## Empty all pool connections +```javascript +console.log('Emptying all pool connections:'); + +var pool = new ConnectionPool({ Connection }); +pool.addConnection('http://localhost:9200/'); +pool.addConnection('http://localhost:9201/'); + +pool.empty() +// OR +pool.empty(() => { + console.log('All connections are empty'); +}) ``` \ No newline at end of file From dc1d6b984f7ca9faf4aa9664f2ad4391cf131eaf Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 22:08:12 +0600 Subject: [PATCH 12/27] fix: remove optional chaining Signed-off-by: Timur Bolotov --- lib/pool/BaseConnectionPool.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index 780bae2bd..95ba15e0c 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -109,8 +109,8 @@ class BaseConnectionPool { opts = this.urlToHost(opts); } - const connectionId = opts?.id; - const connectionUrl = opts?.url?.href; + const connectionId = opts.id; + const connectionUrl = opts.url.href; if (connectionId || connectionUrl) { const connectionById = this.connections.find((c) => c.id === connectionId); From 20028501611ad2325658addbda2bbe73b1712525 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Fri, 24 Mar 2023 23:19:05 +0600 Subject: [PATCH 13/27] refactor: remove else block Signed-off-by: Timur Bolotov --- lib/pool/BaseConnectionPool.js | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index 95ba15e0c..bd36b4562 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -104,29 +104,29 @@ class BaseConnectionPool { addConnection(opts) { if (Array.isArray(opts)) { opts.forEach((o) => this.addConnection(o)); - } else { - if (typeof opts === 'string') { - opts = this.urlToHost(opts); - } + return; + } + if (typeof opts === 'string') { + opts = this.urlToHost(opts); + } - const connectionId = opts.id; - const connectionUrl = opts.url.href; + const connectionId = opts.id; + const connectionUrl = opts.url.href; - if (connectionId || connectionUrl) { - const connectionById = this.connections.find((c) => c.id === connectionId); - const connectionByUrl = this.connections.find((c) => c.id === connectionUrl); + if (connectionId || connectionUrl) { + const connectionById = this.connections.find((c) => c.id === connectionId); + const connectionByUrl = this.connections.find((c) => c.id === connectionUrl); - if (connectionById || connectionByUrl) { - throw new ConfigurationError( - `Connection with id '${connectionId || connectionUrl}' is already present` - ); - } + if (connectionById || connectionByUrl) { + throw new ConfigurationError( + `Connection with id '${connectionId || connectionUrl}' is already present` + ); } - - const connection = this.createConnection(opts); - this.update([...this.connections, connection]); - return this.connections[this.size - 1]; } + + const connection = this.createConnection(opts); + this.update([...this.connections, connection]); + return this.connections[this.size - 1]; } /** From 828271167ede101b40917cd00f93bbfc3be9dd91 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Sat, 25 Mar 2023 01:08:11 +0600 Subject: [PATCH 14/27] fix: make cb arg optional Signed-off-by: Timur Bolotov --- lib/pool/CloudConnectionPool.js | 4 ++-- lib/pool/ConnectionPool.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pool/CloudConnectionPool.js b/lib/pool/CloudConnectionPool.js index 82f2001b9..d512c6f80 100644 --- a/lib/pool/CloudConnectionPool.js +++ b/lib/pool/CloudConnectionPool.js @@ -30,6 +30,7 @@ 'use strict'; const BaseConnectionPool = require('./BaseConnectionPool'); +const noop = () => {}; class CloudConnectionPool extends BaseConnectionPool { constructor(opts) { @@ -49,9 +50,8 @@ class CloudConnectionPool extends BaseConnectionPool { /** * Empties the connection pool. * - * @returns {ConnectionPool} */ - empty(callback) { + empty(callback = noop) { super.empty(() => { this.cloudConnection = null; callback(); diff --git a/lib/pool/ConnectionPool.js b/lib/pool/ConnectionPool.js index b36abd324..21f728543 100644 --- a/lib/pool/ConnectionPool.js +++ b/lib/pool/ConnectionPool.js @@ -227,7 +227,7 @@ class ConnectionPool extends BaseConnectionPool { * * @returns {ConnectionPool} */ - empty(callback) { + empty(callback = noop) { super.empty(() => { this.dead = []; callback(); From cc57aecbc9b2da970492c0bb33a90575728b9aaa Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Sat, 25 Mar 2023 01:08:32 +0600 Subject: [PATCH 15/27] fix: remove jsdoc return type Signed-off-by: Timur Bolotov --- lib/pool/BaseConnectionPool.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index bd36b4562..2aa81c4a5 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -142,8 +142,6 @@ class BaseConnectionPool { /** * Empties the connection pool. - * - * @returns {ConnectionPool} */ empty(callback = noop) { debug('Emptying the connection pool'); From caa82c68f99b6dcc37ef7b265d0c6fda73e11002 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Sat, 25 Mar 2023 01:09:08 +0600 Subject: [PATCH 16/27] chore: update types and tests Signed-off-by: Timur Bolotov --- lib/pool/index.d.ts | 27 +++++++++++++++++++++++++-- test/types/connection-pool.test-d.ts | 4 ++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/pool/index.d.ts b/lib/pool/index.d.ts index f02899a24..3c32dbf47 100644 --- a/lib/pool/index.d.ts +++ b/lib/pool/index.d.ts @@ -134,14 +134,14 @@ declare class BaseConnectionPool { * * @returns {ConnectionPool} */ - empty(callback?: () => void): this; + empty(callback?: () => void): void; /** * Update the ConnectionPool with new connections. * * @param {array} array of connections * @returns {ConnectionPool} */ - update(connections: any[]): this; + update(connections: Connection[]): this; /** * Transforms the nodes objects to a host object. * @@ -183,12 +183,35 @@ declare class ConnectionPool extends BaseConnectionPool { opts: resurrectOptions, callback?: (isAlive: boolean | null, connection: Connection | null) => void ): void; + + /** + * Empties the connection pool. + */ + empty(callback?: () => void): void; + /** + * Update the ConnectionPool with new connections. + * + * @param {array} array of connections + * @returns {ConnectionPool} + */ + update(connections: Connection[]): this; } declare class CloudConnectionPool extends BaseConnectionPool { cloudConnection: Connection | null; constructor(opts?: BaseConnectionPoolOptions); getConnection(): Connection | null; + /** + * Empties the connection pool. + */ + empty(callback?: () => void): void; + /** + * Update the ConnectionPool with new connections. + * + * @param {array} array of connections + * @returns {ConnectionPool} + */ + update(connections: Connection[]): this; } declare function defaultNodeFilter(node: Connection): boolean; diff --git a/test/types/connection-pool.test-d.ts b/test/types/connection-pool.test-d.ts index ffb527f2f..cb333faac 100644 --- a/test/types/connection-pool.test-d.ts +++ b/test/types/connection-pool.test-d.ts @@ -62,7 +62,7 @@ import { ConnectionOptions } from '../../lib/Connection'; ); expectType(pool.addConnection({ url: new URL('url') })); expectType(pool.removeConnection(new Connection())); - expectType(pool.empty()); + expectType(pool.empty()); expectType(pool.update([])); expectType(pool.nodesToHost([], 'https')); expectType(pool.urlToHost('url')); @@ -102,7 +102,7 @@ import { ConnectionOptions } from '../../lib/Connection'; ); expectType(pool.addConnection({ url: new URL('url') })); expectAssignable(pool.removeConnection(new Connection())); - expectAssignable(pool.empty()); + expectAssignable(pool.empty()); expectAssignable(pool.update([])); expectType(pool.nodesToHost([], 'https')); expectType(pool.urlToHost('url')); From 5a82af51b8a3528ec78f9a7ec8655a8cf2d92fc9 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Sat, 25 Mar 2023 01:10:47 +0600 Subject: [PATCH 17/27] chore: update changelog Signed-off-by: Timur Bolotov --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef4530fdb..9548400e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,7 +35,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Make fields in `BulkOperation` optional to match OpenSearch Bulk API requirements ([#378](https://github.com/opensearch-project/opensearch-js/pull/378)) - Remove guidance on using npm and switch completely to yarn in developer_guide ([#439](https://github.com/opensearch-project/opensearch-js/issues/435)) - Change coverage, compatability, integration, integration with unreleased Open Search, node ci, bundler tests not to run on documentation change ([441](https://github.com/opensearch-project/opensearch-js/pull/441)) -- Make `callback` arg in `BaseConnectionPool` optional ([#451](https://github.com/opensearch-project/opensearch-js/pull/451)) +- Make `callback` arg in `BaseConnectionPool`, `CloudConnectionPool` and `ConnectionPool` optional ([#451](https://github.com/opensearch-project/opensearch-js/pull/451)) ### Deprecated From abfb2cb9a61025c819920ddcc5f15d6df9088ad4 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Sat, 25 Mar 2023 01:23:48 +0600 Subject: [PATCH 18/27] refactor: swap to expecttype Signed-off-by: Timur Bolotov --- test/types/connection-pool.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/connection-pool.test-d.ts b/test/types/connection-pool.test-d.ts index cb333faac..e2df1334f 100644 --- a/test/types/connection-pool.test-d.ts +++ b/test/types/connection-pool.test-d.ts @@ -102,7 +102,7 @@ import { ConnectionOptions } from '../../lib/Connection'; ); expectType(pool.addConnection({ url: new URL('url') })); expectAssignable(pool.removeConnection(new Connection())); - expectAssignable(pool.empty()); + expectType(pool.empty()); expectAssignable(pool.update([])); expectType(pool.nodesToHost([], 'https')); expectType(pool.urlToHost('url')); From ed1178d432dc5a50d9e5517f04a46382aab100fc Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Sat, 25 Mar 2023 01:29:31 +0600 Subject: [PATCH 19/27] feat: connection empty tests Signed-off-by: Timur Bolotov --- test/unit/cloud-connection-pool.test.js | 9 +++++++++ test/unit/connection-pool.test.js | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/test/unit/cloud-connection-pool.test.js b/test/unit/cloud-connection-pool.test.js index ea90d91e6..f251f4393 100644 --- a/test/unit/cloud-connection-pool.test.js +++ b/test/unit/cloud-connection-pool.test.js @@ -56,3 +56,12 @@ test('pool.empty should reset cloudConnection', (t) => { t.end(); }); }); + +test('empty with no callback', (t) => { + const pool = new CloudConnectionPool({ Connection }); + pool.addConnection('http://localhost:9200/'); + t.ok(pool.cloudConnection instanceof Connection); + pool.empty(); + t.equal(pool.cloudConnection, null); + t.end(); +}); diff --git a/test/unit/connection-pool.test.js b/test/unit/connection-pool.test.js index 4c638126a..a571b4ae5 100644 --- a/test/unit/connection-pool.test.js +++ b/test/unit/connection-pool.test.js @@ -325,6 +325,16 @@ test('API', (t) => { }); }); + t.test('empty with no callback', (t) => { + const pool = new ConnectionPool({ Connection }); + pool.addConnection('http://localhost:9200/'); + pool.addConnection('http://localhost:9201/'); + pool.empty(); + t.equal(pool.size, 0); + t.same(pool.dead, []); + t.end(); + }); + t.test('urlToHost', (t) => { const pool = new ConnectionPool({ Connection }); const url = 'http://localhost:9200'; From 39934c7e4194a9804471ff71f9bd6512311a7d35 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Sat, 25 Mar 2023 01:44:20 +0600 Subject: [PATCH 20/27] chore: remove log from user guide Signed-off-by: Timur Bolotov --- USER_GUIDE.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index eb2016f16..d26bee8af 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -253,8 +253,6 @@ console.log(response.body); ## Empty all pool connections ```javascript -console.log('Emptying all pool connections:'); - var pool = new ConnectionPool({ Connection }); pool.addConnection('http://localhost:9200/'); pool.addConnection('http://localhost:9201/'); @@ -262,6 +260,6 @@ pool.addConnection('http://localhost:9201/'); pool.empty() // OR pool.empty(() => { - console.log('All connections are empty'); + // Do something after emptying the pool }) ``` \ No newline at end of file From 1b480e18e45824d61a046c1ffb0c33cb5b711f74 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Sat, 25 Mar 2023 01:49:07 +0600 Subject: [PATCH 21/27] chore: format and update toc Signed-off-by: Timur Bolotov --- USER_GUIDE.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index d26bee8af..d917f93f6 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -10,6 +10,7 @@ - [Search for the Document](#search-for-the-document) - [Delete the document](#delete-the-document) - [Delete the index](#delete-the-index) + - [Empty all Pool Connections](#empty-all-pool-connections) ## Initializing a Client @@ -87,7 +88,7 @@ const { AwsSigv4Signer } = require('@opensearch-project/opensearch/aws'); const client = new Client({ ...AwsSigv4Signer({ region: 'us-east-1', - service: 'es', // 'aoss' for OpenSearch Serverless + service: 'es', // 'aoss' for OpenSearch Serverless // Must return a Promise that resolve to an AWS.Credentials object. // This function is used to acquire the credentials when the client start and // when the credentials are expired. @@ -251,15 +252,16 @@ var response = await client.deleteAllPits(); console.log(response.body); ``` -## Empty all pool connections +## Empty all Pool Connections + ```javascript var pool = new ConnectionPool({ Connection }); pool.addConnection('http://localhost:9200/'); pool.addConnection('http://localhost:9201/'); -pool.empty() +pool.empty(); // OR pool.empty(() => { // Do something after emptying the pool -}) -``` \ No newline at end of file +}); +``` From 16c454ac3e6d64f2b6839955843baa9de994c23b Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Mon, 27 Mar 2023 23:47:09 +0600 Subject: [PATCH 22/27] fix: remove passing connection instance to update Signed-off-by: Timur Bolotov --- lib/pool/BaseConnectionPool.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index 2aa81c4a5..30c5ad8df 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -65,6 +65,9 @@ class BaseConnectionPool { * Creates a new connection instance. */ createConnection(opts) { + if (opts instanceof Connection) { + throw new ConfigurationError('The argument provided is already a Connection instance.'); + } if (typeof opts === 'string') { opts = this.urlToHost(opts); } @@ -124,8 +127,7 @@ class BaseConnectionPool { } } - const connection = this.createConnection(opts); - this.update([...this.connections, connection]); + this.update([...this.connections, opts]); return this.connections[this.size - 1]; } From 94557dfc01c7673f9252f0bfd873fe4c1dde4eff Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Mon, 27 Mar 2023 23:47:51 +0600 Subject: [PATCH 23/27] chore: test for createConnection with a Connection instance Signed-off-by: Timur Bolotov --- test/unit/base-connection-pool.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/unit/base-connection-pool.test.js b/test/unit/base-connection-pool.test.js index 34b9bcf3a..a79bcd369 100644 --- a/test/unit/base-connection-pool.test.js +++ b/test/unit/base-connection-pool.test.js @@ -599,5 +599,18 @@ test('API', (t) => { } }); + t.test('Create Connection with a Connection instance', (t) => { + t.plan(1); + const pool = new BaseConnectionPool({ Connection }); + const conn = pool.createConnection('http://localhost:9200'); + pool.connections.push(conn); + try { + pool.createConnection(conn); + t.fail('Should throw'); + } catch (err) { + t.pass(); + } + }); + t.end(); }); From c5de3d1847b15aeb1477123fe8712240d56f45ee Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Mon, 27 Mar 2023 23:52:19 +0600 Subject: [PATCH 24/27] chore: remove jsdoc return Signed-off-by: Timur Bolotov --- lib/pool/index.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pool/index.d.ts b/lib/pool/index.d.ts index 3c32dbf47..97cbde41f 100644 --- a/lib/pool/index.d.ts +++ b/lib/pool/index.d.ts @@ -132,7 +132,6 @@ declare class BaseConnectionPool { /** * Empties the connection pool. * - * @returns {ConnectionPool} */ empty(callback?: () => void): void; /** From 9b38bdb2df520daebe2ecca9fa97c04f4e316a5a Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Mon, 27 Mar 2023 23:59:42 +0600 Subject: [PATCH 25/27] chore: update changelog Signed-off-by: Timur Bolotov --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 787658e11..14941415b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Added Amazon OpenSearch Serverless in user_guide ([#372](https://github.com/opensearch-project/opensearch-js/issues/372)) - Add guidelines on installing yarn, dependencies; instructions on running ESLint in developer_guide ([#439](https://github.com/opensearch-project/opensearch-js/issues/435)) - Added pull_request_template ([440](https://github.com/opensearch-project/opensearch-js/pull/440)) +- Added test for creating a connection with an instance of Connection ([#451](https://github.com/opensearch-project/opensearch-js/pull/451)) ### Dependencies From 1922e37cc2f719b71455f0f575563d4ec60a9d41 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Tue, 28 Mar 2023 20:54:29 +0600 Subject: [PATCH 26/27] chore: revert changelog Signed-off-by: Timur Bolotov --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14941415b..787658e11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,6 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Added Amazon OpenSearch Serverless in user_guide ([#372](https://github.com/opensearch-project/opensearch-js/issues/372)) - Add guidelines on installing yarn, dependencies; instructions on running ESLint in developer_guide ([#439](https://github.com/opensearch-project/opensearch-js/issues/435)) - Added pull_request_template ([440](https://github.com/opensearch-project/opensearch-js/pull/440)) -- Added test for creating a connection with an instance of Connection ([#451](https://github.com/opensearch-project/opensearch-js/pull/451)) ### Dependencies From 6c18ce15f9c573eec0ec10d94d6eeca21180b7f0 Mon Sep 17 00:00:00 2001 From: Timur Bolotov Date: Tue, 28 Mar 2023 20:54:57 +0600 Subject: [PATCH 27/27] feat: added call empty twice test Signed-off-by: Timur Bolotov --- test/unit/base-connection-pool.test.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/unit/base-connection-pool.test.js b/test/unit/base-connection-pool.test.js index a79bcd369..5b2ea5fc7 100644 --- a/test/unit/base-connection-pool.test.js +++ b/test/unit/base-connection-pool.test.js @@ -140,6 +140,22 @@ test('API', (t) => { t.end(); }); + t.test('call empty twice', (t) => { + const pool = new BaseConnectionPool({ Connection }); + pool.addConnection('http://localhost:9200/'); + pool.addConnection('http://localhost:9201/'); + try { + pool.empty(); + pool.empty(() => { + t.equal(pool.size, 0); + t.pass(); + }); + } catch (error) { + t.fail('Should not throw'); + } + t.end(); + }); + t.test('urlToHost', (t) => { const pool = new BaseConnectionPool({ Connection }); const url = 'http://localhost:9200';