Skip to content

Commit dd8ee90

Browse files
[Backport 7.x] The agent function should take the Connection contructor options as argument (#1334)
Co-authored-by: Tomas Della Vedova <[email protected]>
1 parent 34dedb8 commit dd8ee90

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

docs/configuration.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ const client = new Client({
149149
150150
const client = new Client({
151151
node: 'http://localhost:9200',
152-
agent: () => new CustomAgent()
152+
// the function takes as parameter the option
153+
// object passed to the Connection constructor
154+
agent: (opts) => new CustomAgent()
153155
})
154156
155157
const client = new Client({

lib/Connection.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ import * as https from 'https'
2828
import * as hpagent from 'hpagent'
2929
import { ConnectionOptions as TlsConnectionOptions } from 'tls'
3030

31-
export declare type agentFn = () => any;
31+
export declare type agentFn = (opts: ConnectionOptions) => any;
3232

33-
interface ConnectionOptions {
33+
export interface ConnectionOptions {
3434
url: URL;
3535
ssl?: TlsConnectionOptions;
3636
id?: string;

lib/Connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Connection {
5353
}
5454

5555
if (typeof opts.agent === 'function') {
56-
this.agent = opts.agent()
56+
this.agent = opts.agent(opts)
5757
} else if (opts.agent === false) {
5858
this.agent = undefined
5959
} else {

test/types/connection.test-d.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,35 @@
2020
import { expectType } from 'tsd'
2121
import { URL } from 'url'
2222
import { Connection } from '../../'
23+
import { ConnectionOptions } from '../../lib/Connection'
2324

24-
const conn = new Connection({
25-
url: new URL('http://localhost:9200'),
26-
ssl: { ca: 'string' },
27-
id: 'id',
28-
headers: {},
29-
agent: { keepAlive: false },
30-
status: 'alive',
31-
roles: { master: true },
32-
auth: { username: 'username', password: 'password' }
33-
})
25+
{
26+
const conn = new Connection({
27+
url: new URL('http://localhost:9200'),
28+
ssl: { ca: 'string' },
29+
id: 'id',
30+
headers: {},
31+
agent: { keepAlive: false },
32+
status: 'alive',
33+
roles: { master: true },
34+
auth: { username: 'username', password: 'password' }
35+
})
3436

35-
expectType<Connection>(conn)
36-
expectType<URL>(conn.url)
37-
expectType<string>(conn.id)
38-
expectType<Record<string, any>>(conn.headers)
39-
expectType<number>(conn.deadCount)
40-
expectType<number>(conn.resurrectTimeout)
41-
expectType<string>(conn.status)
37+
expectType<Connection>(conn)
38+
expectType<URL>(conn.url)
39+
expectType<string>(conn.id)
40+
expectType<Record<string, any>>(conn.headers)
41+
expectType<number>(conn.deadCount)
42+
expectType<number>(conn.resurrectTimeout)
43+
expectType<string>(conn.status)
44+
}
45+
46+
{
47+
const conn = new Connection({
48+
url: new URL('http://localhost:9200'),
49+
agent (opts) {
50+
expectType<ConnectionOptions>(opts)
51+
return 'the agent'
52+
}
53+
})
54+
}

test/unit/connection.test.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ test('Basic (https with ssl agent)', t => {
152152
})
153153

154154
test('Custom http agent', t => {
155-
t.plan(5)
155+
t.plan(6)
156156

157157
function handler (req, res) {
158158
t.match(req.headers, {
@@ -172,7 +172,12 @@ test('Custom http agent', t => {
172172
agent.custom = true
173173
const connection = new Connection({
174174
url: new URL(`http://localhost:${port}`),
175-
agent: () => agent
175+
agent: opts => {
176+
t.match(opts, {
177+
url: new URL(`http://localhost:${port}`)
178+
})
179+
return agent
180+
}
176181
})
177182
t.true(connection.agent.custom)
178183
connection.request({

0 commit comments

Comments
 (0)