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`.
This commit is contained in:
Tomas Della Vedova
2019-03-19 09:50:36 +01:00
committed by GitHub
parent 3b41c555ae
commit 5c60d98180
3 changed files with 66 additions and 3 deletions

View File

@ -20,6 +20,7 @@
'use strict'
const { test } = require('tap')
const { inspect } = require('util')
const { createGzip, createDeflate } = require('zlib')
const { URL } = require('url')
const intoStream = require('into-stream')
@ -670,3 +671,45 @@ test('setRole', t => {
t.end()
})
test('Util.inspect Connection class should hide agent and ssl', t => {
t.plan(1)
const connection = new Connection({
url: new URL('http://localhost:9200'),
id: 'node-id',
headers: { foo: 'bar' }
})
// Removes spaces and new lines because
// utils.inspect is handled differently
// between major versions of Node.js
function cleanStr (str) {
return str
.replace(/\s/g, '')
.replace(/(\r\n|\n|\r)/gm, '')
}
t.strictEqual(cleanStr(inspect(connection)), cleanStr(`{ url:
URL {
href: 'http://localhost:9200/',
origin: 'http://localhost:9200',
protocol: 'http:',
username: '',
password: '',
host: 'localhost:9200',
hostname: 'localhost',
port: '9200',
pathname: '/',
search: '',
searchParams: URLSearchParams {},
hash: '' },
id: 'node-id',
headers: { foo: 'bar' },
deadCount: 0,
resurrectTimeout: 0,
_openRequests: 0,
status: 'alive',
roles: { master: true, data: true, ingest: true, ml: false } }`)
)
})