The agent function should take the Connection contructor options as argument (#1332)

This commit is contained in:
Tomas Della Vedova
2020-10-12 10:47:10 +02:00
committed by GitHub
parent ea009da3b6
commit 8ec40547fb
5 changed files with 43 additions and 23 deletions

View File

@ -149,7 +149,9 @@ const client = new Client({
const client = new Client({ const client = new Client({
node: 'http://localhost:9200', node: 'http://localhost:9200',
agent: () => new CustomAgent() // the function takes as parameter the option
// object passed to the Connection constructor
agent: (opts) => new CustomAgent()
}) })
const client = new Client({ const client = new Client({

4
lib/Connection.d.ts vendored
View File

@ -28,9 +28,9 @@ import * as https from 'https'
import * as hpagent from 'hpagent' import * as hpagent from 'hpagent'
import { ConnectionOptions as TlsConnectionOptions } from 'tls' import { ConnectionOptions as TlsConnectionOptions } from 'tls'
export declare type agentFn = () => any; export declare type agentFn = (opts: ConnectionOptions) => any;
interface ConnectionOptions { export interface ConnectionOptions {
url: URL; url: URL;
ssl?: TlsConnectionOptions; ssl?: TlsConnectionOptions;
id?: string; id?: string;

View File

@ -53,7 +53,7 @@ class Connection {
} }
if (typeof opts.agent === 'function') { if (typeof opts.agent === 'function') {
this.agent = opts.agent() this.agent = opts.agent(opts)
} else if (opts.agent === false) { } else if (opts.agent === false) {
this.agent = undefined this.agent = undefined
} else { } else {

View File

@ -20,7 +20,9 @@
import { expectType } from 'tsd' import { expectType } from 'tsd'
import { URL } from 'url' import { URL } from 'url'
import { Connection } from '../../' import { Connection } from '../../'
import { ConnectionOptions } from '../../lib/Connection'
{
const conn = new Connection({ const conn = new Connection({
url: new URL('http://localhost:9200'), url: new URL('http://localhost:9200'),
ssl: { ca: 'string' }, ssl: { ca: 'string' },
@ -39,3 +41,14 @@ expectType<Record<string, any>>(conn.headers)
expectType<number>(conn.deadCount) expectType<number>(conn.deadCount)
expectType<number>(conn.resurrectTimeout) expectType<number>(conn.resurrectTimeout)
expectType<string>(conn.status) expectType<string>(conn.status)
}
{
const conn = new Connection({
url: new URL('http://localhost:9200'),
agent (opts) {
expectType<ConnectionOptions>(opts)
return 'the agent'
}
})
}

View File

@ -152,7 +152,7 @@ test('Basic (https with ssl agent)', t => {
}) })
test('Custom http agent', t => { test('Custom http agent', t => {
t.plan(5) t.plan(6)
function handler (req, res) { function handler (req, res) {
t.match(req.headers, { t.match(req.headers, {
@ -172,7 +172,12 @@ test('Custom http agent', t => {
agent.custom = true agent.custom = true
const connection = new Connection({ const connection = new Connection({
url: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
agent: () => agent agent: opts => {
t.match(opts, {
url: new URL(`http://localhost:${port}`)
})
return agent
}
}) })
t.true(connection.agent.custom) t.true(connection.agent.custom)
connection.request({ connection.request({