Skip to content

Serverless-optimized HTTP options #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
*/

import Client from './lib/client'
import SniffingTransport from './lib/sniffingTransport'

export * from '@elastic/transport'
export * as estypes from './lib/api/types'
export * as estypesWithBody from './lib/api/typesWithBodyKey'
export { Client, SniffingTransport }
export { Client }
export type { ClientOptions, NodeOptions } from './lib/client'
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ const {
} = require('@elastic/transport')

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

module.exports = {
Client,
SniffingTransport,
Diagnostic,
Transport,
WeightedConnectionPool,
Expand Down
39 changes: 12 additions & 27 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import os from 'os'
import {
Transport,
UndiciConnection,
WeightedConnectionPool,
CloudConnectionPool,
Serializer,
Diagnostic,
Expand All @@ -35,16 +34,13 @@ import {
HttpAgentOptions,
UndiciAgentOptions,
agentFn,
nodeFilterFn,
nodeSelectorFn,
generateRequestIdFn,
BasicAuth,
ApiKeyAuth,
BearerAuth,
Context
} from '@elastic/transport/lib/types'
import BaseConnection, { prepareHeaders } from '@elastic/transport/lib/connection/BaseConnection'
import SniffingTransport from './sniffingTransport'
import Helpers from './helpers'
import API from './api'

Expand Down Expand Up @@ -89,16 +85,10 @@ export interface ClientOptions {
maxRetries?: number
requestTimeout?: number
pingTimeout?: number
sniffInterval?: number | boolean
sniffOnStart?: boolean
sniffEndpoint?: string
sniffOnConnectionFault?: boolean
resurrectStrategy?: 'ping' | 'optimistic' | 'none'
compression?: boolean
tls?: TlsConnectionOptions
agent?: HttpAgentOptions | UndiciAgentOptions | agentFn | false
nodeFilter?: nodeFilterFn
nodeSelector?: nodeSelectorFn
headers?: Record<string, any>
opaqueIdPrefix?: string
generateRequestId?: generateRequestIdFn
Expand All @@ -120,7 +110,7 @@ export default class Client extends API {
diagnostic: Diagnostic
name: string | symbol
connectionPool: BaseConnectionPool
transport: SniffingTransport
transport: Transport
serializer: Serializer
helpers: Helpers
constructor (opts: ClientOptions) {
Expand Down Expand Up @@ -160,25 +150,20 @@ export default class Client extends API {

const options: Required<ClientOptions> = Object.assign({}, {
Connection: UndiciConnection,
Transport: SniffingTransport,
Transport,
Serializer,
ConnectionPool: (opts.cloud != null) ? CloudConnectionPool : WeightedConnectionPool,
ConnectionPool: CloudConnectionPool,
maxRetries: 3,
requestTimeout: 30000,
pingTimeout: 3000,
sniffInterval: false,
sniffOnStart: false,
sniffEndpoint: '_nodes/_all/http',
sniffOnConnectionFault: false,
resurrectStrategy: 'ping',
compression: false,
compression: true,
tls: null,
caFingerprint: null,
agent: null,
headers: {
'user-agent': `elasticsearch-js/${clientVersion} Node.js ${nodeVersion}; Transport ${transportVersion}; (${os.platform()} ${os.release()} ${os.arch()})`
},
nodeFilter: null,
generateRequestId: null,
name: 'elasticsearch-js',
auth: null,
Expand Down Expand Up @@ -232,7 +217,13 @@ export default class Client extends API {
diagnostic: this.diagnostic,
caFingerprint: options.caFingerprint
})
this.connectionPool.addConnection(options.node ?? options.nodes)

// serverless only supports one node. keeping array support, to simplify
// for people migrating from the stack client, but only using the first
// node in the list.
let node = options.node ?? options.nodes
if (Array.isArray(node)) node = node[0]
this.connectionPool.addConnection(node)
}

this.transport = new options.Transport({
Expand All @@ -241,14 +232,8 @@ export default class Client extends API {
serializer: this.serializer,
maxRetries: options.maxRetries,
requestTimeout: options.requestTimeout,
sniffInterval: options.sniffInterval,
sniffOnStart: options.sniffOnStart,
sniffOnConnectionFault: options.sniffOnConnectionFault,
sniffEndpoint: options.sniffEndpoint,
compression: options.compression,
headers: options.headers,
nodeFilter: options.nodeFilter,
nodeSelector: options.nodeSelector,
generateRequestId: options.generateRequestId,
name: options.name,
opaqueIdPrefix: options.opaqueIdPrefix,
Expand Down Expand Up @@ -276,7 +261,7 @@ export default class Client extends API {
// Merge the new options with the initial ones
// @ts-expect-error kChild symbol is for internal use only
const options: ClientOptions = Object.assign({}, this[kInitialOptions], opts)
// Pass to the child client the parent instances that cannot be overriden
// Pass to the child client the parent instances that cannot be overridden
// @ts-expect-error kInitialOptions symbol is for internal use only
options[kChild] = {
connectionPool: this.connectionPool,
Expand Down
54 changes: 0 additions & 54 deletions src/sniffingTransport.ts

This file was deleted.

18 changes: 12 additions & 6 deletions test/unit/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ test('Api without body key and top level body', async t => {

const client = new Client({
node: 'http://localhost:9200',
Connection
Connection,
compression: false,
})

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

const client = new Client({
node: 'http://localhost:9200',
Connection
Connection,
compression: false,
})

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

const client = new Client({
node: 'http://localhost:9200',
Connection
Connection,
compression: false,
})

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

const client = new Client({
node: 'http://localhost:9200',
Connection
Connection,
compression: false,
})

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

const client = new Client({
node: 'http://localhost:9200',
Connection
Connection,
compression: false,
})

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

const client = new Client({
node: 'http://localhost:9200',
Connection
Connection,
compression: false,
})

try {
Expand Down
14 changes: 7 additions & 7 deletions test/unit/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { URL } from 'url'
import { connection } from '../utils'
import { Client, errors } from '../..'
import * as symbols from '@elastic/transport/lib/symbols'
import { BaseConnectionPool, CloudConnectionPool, WeightedConnectionPool } from '@elastic/transport'
import { BaseConnectionPool, CloudConnectionPool } from '@elastic/transport'

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

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

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

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

test('Create a client instance, multi node as object', t => {
test('Create a client instance, multi node as object, serverless only uses one', t => {
const client = new Client({
nodes: [{
url: new URL('http://localhost:9200')
}, {
url: new URL('http://localhost:9201')
}]
})
t.equal(client.connectionPool.size, 2)
t.equal(client.connectionPool.size, 1)
t.end()
})

Expand Down
Loading