Skip to content

Commit 09d76d2

Browse files
committed
Inspect Connection (#784)
Handles `console.log` and `utils.inspect` invocations for a better debugging experience. `agent` and `ssl` are hidden since they made the logs very hard to read. The user can still access them with `instance.agent` and `instance.ssl`.
1 parent b2ddd36 commit 09d76d2

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

lib/Connection.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/// <reference types="node" />
2121

2222
import { URL } from 'url';
23+
import { inspect, InspectOptions } from 'util';
2324
import * as http from 'http';
2425
import { SecureContextOptions } from 'tls';
2526

@@ -73,6 +74,7 @@ export default class Connection {
7374
setRole(role: string, enabled: boolean): Connection;
7475
status: string;
7576
buildRequestObject(params: any): http.ClientRequestArgs;
77+
[inspect.custom](object: any, options: InspectOptions): string;
7678
}
7779

7880
export {};

lib/Connection.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'use strict'
2121

2222
const assert = require('assert')
23+
const { inspect } = require('util')
2324
const http = require('http')
2425
const https = require('https')
2526
const debug = require('debug')('elasticsearch')
@@ -54,7 +55,7 @@ class Connection {
5455
maxSockets: keepAliveFalse ? Infinity : 256,
5556
maxFreeSockets: 256
5657
}, opts.agent)
57-
this._agent = this.url.protocol === 'http:'
58+
this.agent = this.url.protocol === 'http:'
5859
? new http.Agent(agentOptions)
5960
: new https.Agent(Object.assign({}, agentOptions, this.ssl))
6061

@@ -146,7 +147,7 @@ class Connection {
146147
if (this._openRequests > 0) {
147148
setTimeout(() => this.close(callback), 1000)
148149
} else {
149-
this._agent.destroy()
150+
this.agent.destroy()
150151
callback()
151152
}
152153
}
@@ -193,7 +194,7 @@ class Connection {
193194
auth: !!url.username === true || !!url.password === true
194195
? `${url.username}:${url.password}`
195196
: undefined,
196-
agent: this._agent
197+
agent: this.agent
197198
}
198199

199200
const paramsKeys = Object.keys(params)
@@ -218,6 +219,23 @@ class Connection {
218219

219220
return request
220221
}
222+
223+
// Handles console.log and utils.inspect invocations.
224+
// We want to hide `agent` and `ssl` since they made
225+
// the logs very hard to read. The user can still
226+
// access them with `instance.agent` and `instance.ssl`.
227+
[inspect.custom] (depth, options) {
228+
return {
229+
url: this.url,
230+
id: this.id,
231+
headers: this.headers,
232+
deadCount: this.deadCount,
233+
resurrectTimeout: this.resurrectTimeout,
234+
_openRequests: this._openRequests,
235+
status: this.status,
236+
roles: this.roles
237+
}
238+
}
221239
}
222240

223241
Connection.statuses = {

test/unit/connection.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'use strict'
2121

2222
const { test } = require('tap')
23+
const { inspect } = require('util')
2324
const { createGzip, createDeflate } = require('zlib')
2425
const { URL } = require('url')
2526
const intoStream = require('into-stream')
@@ -670,3 +671,45 @@ test('setRole', t => {
670671

671672
t.end()
672673
})
674+
675+
test('Util.inspect Connection class should hide agent and ssl', t => {
676+
t.plan(1)
677+
678+
const connection = new Connection({
679+
url: new URL('http://localhost:9200'),
680+
id: 'node-id',
681+
headers: { foo: 'bar' }
682+
})
683+
684+
// Removes spaces and new lines because
685+
// utils.inspect is handled differently
686+
// between major versions of Node.js
687+
function cleanStr (str) {
688+
return str
689+
.replace(/\s/g, '')
690+
.replace(/(\r\n|\n|\r)/gm, '')
691+
}
692+
693+
t.strictEqual(cleanStr(inspect(connection)), cleanStr(`{ url:
694+
URL {
695+
href: 'http://localhost:9200/',
696+
origin: 'http://localhost:9200',
697+
protocol: 'http:',
698+
username: '',
699+
password: '',
700+
host: 'localhost:9200',
701+
hostname: 'localhost',
702+
port: '9200',
703+
pathname: '/',
704+
search: '',
705+
searchParams: URLSearchParams {},
706+
hash: '' },
707+
id: 'node-id',
708+
headers: { foo: 'bar' },
709+
deadCount: 0,
710+
resurrectTimeout: 0,
711+
_openRequests: 0,
712+
status: 'alive',
713+
roles: { master: true, data: true, ingest: true, ml: false } }`)
714+
)
715+
})

0 commit comments

Comments
 (0)