Skip to content

Commit 7ef4666

Browse files
authored
fix(http): Serverless-optimized HTTP options (#18)
* Disable multi-node support * Drop sniffing support * Drop node selector function support * Drop node filtering support * Enable compression by default
1 parent e71b2ff commit 7ef4666

File tree

10 files changed

+87
-184
lines changed

10 files changed

+87
-184
lines changed

index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
*/
1919

2020
import Client from './lib/client'
21-
import SniffingTransport from './lib/sniffingTransport'
2221

2322
export * from '@elastic/transport'
2423
export * as estypes from './lib/api/types'
2524
export * as estypesWithBody from './lib/api/typesWithBodyKey'
26-
export { Client, SniffingTransport }
25+
export { Client }
2726
export type { ClientOptions, NodeOptions } from './lib/client'

index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ const {
3535
} = require('@elastic/transport')
3636

3737
const { default: Client } = require('./lib/client')
38-
const { default: SniffingTransport } = require('./lib/sniffingTransport')
3938

4039
module.exports = {
4140
Client,
42-
SniffingTransport,
4341
Diagnostic,
4442
Transport,
4543
WeightedConnectionPool,

src/client.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import os from 'os'
2424
import {
2525
Transport,
2626
UndiciConnection,
27-
WeightedConnectionPool,
2827
CloudConnectionPool,
2928
Serializer,
3029
Diagnostic,
@@ -35,16 +34,13 @@ import {
3534
HttpAgentOptions,
3635
UndiciAgentOptions,
3736
agentFn,
38-
nodeFilterFn,
39-
nodeSelectorFn,
4037
generateRequestIdFn,
4138
BasicAuth,
4239
ApiKeyAuth,
4340
BearerAuth,
4441
Context
4542
} from '@elastic/transport/lib/types'
4643
import BaseConnection, { prepareHeaders } from '@elastic/transport/lib/connection/BaseConnection'
47-
import SniffingTransport from './sniffingTransport'
4844
import Helpers from './helpers'
4945
import API from './api'
5046

@@ -89,16 +85,10 @@ export interface ClientOptions {
8985
maxRetries?: number
9086
requestTimeout?: number
9187
pingTimeout?: number
92-
sniffInterval?: number | boolean
93-
sniffOnStart?: boolean
94-
sniffEndpoint?: string
95-
sniffOnConnectionFault?: boolean
9688
resurrectStrategy?: 'ping' | 'optimistic' | 'none'
9789
compression?: boolean
9890
tls?: TlsConnectionOptions
9991
agent?: HttpAgentOptions | UndiciAgentOptions | agentFn | false
100-
nodeFilter?: nodeFilterFn
101-
nodeSelector?: nodeSelectorFn
10292
headers?: Record<string, any>
10393
opaqueIdPrefix?: string
10494
generateRequestId?: generateRequestIdFn
@@ -120,7 +110,7 @@ export default class Client extends API {
120110
diagnostic: Diagnostic
121111
name: string | symbol
122112
connectionPool: BaseConnectionPool
123-
transport: SniffingTransport
113+
transport: Transport
124114
serializer: Serializer
125115
helpers: Helpers
126116
constructor (opts: ClientOptions) {
@@ -160,25 +150,20 @@ export default class Client extends API {
160150

161151
const options: Required<ClientOptions> = Object.assign({}, {
162152
Connection: UndiciConnection,
163-
Transport: SniffingTransport,
153+
Transport,
164154
Serializer,
165-
ConnectionPool: (opts.cloud != null) ? CloudConnectionPool : WeightedConnectionPool,
155+
ConnectionPool: CloudConnectionPool,
166156
maxRetries: 3,
167157
requestTimeout: 30000,
168158
pingTimeout: 3000,
169-
sniffInterval: false,
170-
sniffOnStart: false,
171-
sniffEndpoint: '_nodes/_all/http',
172-
sniffOnConnectionFault: false,
173159
resurrectStrategy: 'ping',
174-
compression: false,
160+
compression: true,
175161
tls: null,
176162
caFingerprint: null,
177163
agent: null,
178164
headers: {
179165
'user-agent': `elasticsearch-js/${clientVersion} Node.js ${nodeVersion}; Transport ${transportVersion}; (${os.platform()} ${os.release()} ${os.arch()})`
180166
},
181-
nodeFilter: null,
182167
generateRequestId: null,
183168
name: 'elasticsearch-js',
184169
auth: null,
@@ -232,7 +217,13 @@ export default class Client extends API {
232217
diagnostic: this.diagnostic,
233218
caFingerprint: options.caFingerprint
234219
})
235-
this.connectionPool.addConnection(options.node ?? options.nodes)
220+
221+
// serverless only supports one node. keeping array support, to simplify
222+
// for people migrating from the stack client, but only using the first
223+
// node in the list.
224+
let node = options.node ?? options.nodes
225+
if (Array.isArray(node)) node = node[0]
226+
this.connectionPool.addConnection(node)
236227
}
237228

238229
this.transport = new options.Transport({
@@ -241,14 +232,8 @@ export default class Client extends API {
241232
serializer: this.serializer,
242233
maxRetries: options.maxRetries,
243234
requestTimeout: options.requestTimeout,
244-
sniffInterval: options.sniffInterval,
245-
sniffOnStart: options.sniffOnStart,
246-
sniffOnConnectionFault: options.sniffOnConnectionFault,
247-
sniffEndpoint: options.sniffEndpoint,
248235
compression: options.compression,
249236
headers: options.headers,
250-
nodeFilter: options.nodeFilter,
251-
nodeSelector: options.nodeSelector,
252237
generateRequestId: options.generateRequestId,
253238
name: options.name,
254239
opaqueIdPrefix: options.opaqueIdPrefix,
@@ -276,7 +261,7 @@ export default class Client extends API {
276261
// Merge the new options with the initial ones
277262
// @ts-expect-error kChild symbol is for internal use only
278263
const options: ClientOptions = Object.assign({}, this[kInitialOptions], opts)
279-
// Pass to the child client the parent instances that cannot be overriden
264+
// Pass to the child client the parent instances that cannot be overridden
280265
// @ts-expect-error kInitialOptions symbol is for internal use only
281266
options[kChild] = {
282267
connectionPool: this.connectionPool,

src/sniffingTransport.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

test/unit/api.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ test('Api without body key and top level body', async t => {
3838

3939
const client = new Client({
4040
node: 'http://localhost:9200',
41-
Connection
41+
Connection,
42+
compression: false,
4243
})
4344

4445
const response = await client.search({
@@ -66,7 +67,8 @@ test('Api with body key and top level body', async t => {
6667

6768
const client = new Client({
6869
node: 'http://localhost:9200',
69-
Connection
70+
Connection,
71+
compression: false,
7072
})
7173

7274
const response = await client.search({
@@ -96,7 +98,8 @@ test('Api without body key and keyed body', async t => {
9698

9799
const client = new Client({
98100
node: 'http://localhost:9200',
99-
Connection
101+
Connection,
102+
compression: false,
100103
})
101104

102105
const response = await client.create({
@@ -124,7 +127,8 @@ test('Api with body key and keyed body', async t => {
124127

125128
const client = new Client({
126129
node: 'http://localhost:9200',
127-
Connection
130+
Connection,
131+
compression: false,
128132
})
129133

130134
const response = await client.create({
@@ -152,7 +156,8 @@ test('Using the body key should not mutate the body', async t => {
152156

153157
const client = new Client({
154158
node: 'http://localhost:9200',
155-
Connection
159+
Connection,
160+
compression: false,
156161
})
157162

158163
const body = { query: { match_all: {} } }
@@ -181,7 +186,8 @@ test('Using the body key with a string value', async t => {
181186

182187
const client = new Client({
183188
node: 'http://localhost:9200',
184-
Connection
189+
Connection,
190+
compression: false,
185191
})
186192

187193
try {

test/unit/client.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { URL } from 'url'
2222
import { connection } from '../utils'
2323
import { Client, errors } from '../..'
2424
import * as symbols from '@elastic/transport/lib/symbols'
25-
import { BaseConnectionPool, CloudConnectionPool, WeightedConnectionPool } from '@elastic/transport'
25+
import { BaseConnectionPool, CloudConnectionPool } from '@elastic/transport'
2626

2727
let clientVersion: string = require('../../package.json').version // eslint-disable-line
2828
if (clientVersion.includes('-')) {
@@ -36,15 +36,15 @@ const nodeVersion = process.versions.node
3636

3737
test('Create a client instance, single node as string', t => {
3838
const client = new Client({ node: 'http://localhost:9200' })
39-
t.ok(client.connectionPool instanceof WeightedConnectionPool)
39+
t.ok(client.connectionPool instanceof CloudConnectionPool)
4040
t.equal(client.connectionPool.size, 1)
4141
t.end()
4242
})
4343

44-
test('Create a client instance, multi node as strings', t => {
44+
test('Create a client instance, multi node as strings, serverless only uses one', t => {
4545
const client = new Client({ nodes: ['http://localhost:9200', 'http://localhost:9201'] })
46-
t.ok(client.connectionPool instanceof WeightedConnectionPool)
47-
t.equal(client.connectionPool.size, 2)
46+
t.ok(client.connectionPool instanceof CloudConnectionPool)
47+
t.equal(client.connectionPool.size, 1)
4848
t.end()
4949
})
5050

@@ -58,15 +58,15 @@ test('Create a client instance, single node as object', t => {
5858
t.end()
5959
})
6060

61-
test('Create a client instance, multi node as object', t => {
61+
test('Create a client instance, multi node as object, serverless only uses one', t => {
6262
const client = new Client({
6363
nodes: [{
6464
url: new URL('http://localhost:9200')
6565
}, {
6666
url: new URL('http://localhost:9201')
6767
}]
6868
})
69-
t.equal(client.connectionPool.size, 2)
69+
t.equal(client.connectionPool.size, 1)
7070
t.end()
7171
})
7272

0 commit comments

Comments
 (0)