Compare commits

..

5 Commits

Author SHA1 Message Date
2e90d5b55e Bumped v5.6.18 2019-05-22 12:20:41 +02:00
32836b4f6c Support for non-friendly chars in url username and password (#858)
* Support for non-friendly chars in url username and password
- Added auth option to Connection class
- Updated pool.addConnection

* Updated test
2019-05-20 17:10:49 +02:00
f8034c60bc API generation 2019-05-16 16:56:01 -04:00
c68a5ce9a2 Patch deprecated parameters (#851)
* Updated code generation

* API generation

* API generation

* Updated code generation
2019-05-16 16:55:07 -04:00
554bb1ff05 missing comma (#854) 2019-05-16 09:39:59 -04:00
10 changed files with 82 additions and 22 deletions

View File

@ -8,7 +8,7 @@ The client is designed to be easily configured as you see fit for your needs, fo
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'http://localhost:9200'
node: 'http://localhost:9200',
maxRetries: 5,
requestTimeout: 60000,
sniffOnStart: true

View File

@ -35,6 +35,7 @@ class Connection {
this.ssl = opts.ssl || null
this.id = opts.id || stripAuth(opts.url.href)
this.headers = opts.headers || null
this.auth = opts.auth || { username: null, password: null }
this.deadCount = 0
this.resurrectTimeout = 0
@ -180,6 +181,7 @@ class Connection {
buildRequestObject (params) {
const url = this.url
const { username, password } = this.auth
const request = {
protocol: url.protocol,
hostname: url.hostname[0] === '['
@ -194,8 +196,8 @@ class Connection {
// https://github.com/elastic/elasticsearch-js/issues/843
port: url.port !== '' ? url.port : undefined,
headers: this.headers,
auth: !!url.username === true || !!url.password === true
? `${url.username}:${url.password}`
auth: username != null && password != null
? `${username}:${password}`
: undefined,
agent: this.agent
}
@ -224,7 +226,7 @@ class Connection {
}
// Handles console.log and utils.inspect invocations.
// We want to hide `agent` and `ssl` since they made
// We want to hide `auth`, `agent` and `ssl` since they made
// the logs very hard to read. The user can still
// access them with `instance.agent` and `instance.ssl`.
[inspect.custom] (depth, options) {

View File

@ -222,21 +222,24 @@ class ConnectionPool {
// we can add it to them once the connection instance has been created
if (opts.url.username !== '' && opts.url.password !== '') {
this._auth = {
username: opts.url.username,
password: opts.url.password
username: decodeURIComponent(opts.url.username),
password: decodeURIComponent(opts.url.password)
}
opts.auth = this._auth
}
if (this._auth != null) {
if (opts.auth == null || (opts.auth.username == null && opts.auth.password == null)) {
opts.auth = this._auth
opts.url.username = this._auth.username
opts.url.password = this._auth.password
}
}
if (opts.ssl == null) opts.ssl = this._ssl
if (opts.agent == null) opts.agent = this._agent
const connection = new this.Connection(opts)
if (connection.url.username === '' &&
connection.url.password === '' &&
this._auth != null) {
connection.url.username = this._auth.username
connection.url.password = this._auth.password
}
debug('Adding a new connection', connection)
if (this.connections.has(connection.id)) {
throw new Error(`Connection with id '${connection.id}' is already present`)

View File

@ -4,7 +4,7 @@
"main": "index.js",
"types": "index.d.ts",
"homepage": "http://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html",
"version": "5.6.17",
"version": "5.6.18",
"keywords": [
"elasticsearch",
"elastic",

View File

@ -74,7 +74,7 @@ function start (opts) {
writeFileSync(
requestParamsOutputFile,
generateRequestTypes(allSpec),
generateRequestTypes(opts.branch || opts.tag, allSpec),
{ encoding: 'utf8' }
)
@ -118,7 +118,7 @@ function start (opts) {
const spec = require(join(apiFolder, file))
allSpec.push(spec)
const code = generate(spec, common)
const code = generate(opts.branch || opts.tag, spec, common)
const filePath = join(apiOutputFolder, `${file.slice(0, file.lastIndexOf('.'))}.js`)
writeFileSync(filePath, code, { encoding: 'utf8' })

View File

@ -20,11 +20,16 @@
'use strict'
const dedent = require('dedent')
const semver = require('semver')
const allowedMethods = {
noBody: ['GET', 'HEAD', 'DELETE'],
body: ['POST', 'PUT', 'DELETE']
}
// if a parameter is depracted in a minor release
// we should be able to support it until the next major
const deprecatedParameters = require('./patch.json')
// list of apis that does not need any kind of validation
// because of how the url is built or the `type` handling in ES7
const noPathValidation = [
@ -59,7 +64,8 @@ const ndjsonApi = [
'xpack.monitoring.bulk'
]
function generate (spec, common) {
function generate (version, spec, common) {
const release = semver.valid(version) ? semver.major(version) : version
const api = Object.keys(spec)[0]
const name = api
.replace(/\.([a-z])/g, k => k[1].toUpperCase())
@ -80,7 +86,11 @@ function generate (spec, common) {
if (params[key].required) {
required.push(key)
}
acceptedQuerystring.push(key)
if (deprecatedParameters[release] && deprecatedParameters[release][key]) {
acceptedQuerystring.push(deprecatedParameters[release][key])
}
}
for (const key in spec[api]) {

View File

@ -19,7 +19,11 @@
'use strict'
function generate (api) {
const semver = require('semver')
const deprecatedParameters = require('./patch.json')
function generate (version, api) {
const release = semver.valid(version) ? semver.major(version) : version
var types = `/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
@ -64,9 +68,20 @@ export interface Generic {
const partsArr = Object.keys(parts)
.map(k => ({ key: k, value: parts[k] }))
const deprecatedParametersToAdd = []
const paramsArr = Object.keys(params)
.filter(k => !Object.keys(parts).includes(k))
.map(k => ({ key: k, value: params[k] }))
.map(k => {
if (deprecatedParameters[release] && deprecatedParameters[release][k]) {
deprecatedParametersToAdd.push({
key: deprecatedParameters[release][k],
value: params[k]
})
}
return { key: k, value: params[k] }
})
deprecatedParametersToAdd.forEach(k => partsArr.push(k))
const genLine = e => {
const optional = e.value.required ? '' : '?'

14
scripts/utils/patch.json Normal file
View File

@ -0,0 +1,14 @@
{
"6": {
"_source_includes": "_source_include",
"_source_excludes": "_source_exclude"
},
"7": {
"_source_includes": "_source_include",
"_source_excludes": "_source_exclude"
},
"master": {
"_source_includes": "_source_include",
"_source_excludes": "_source_exclude"
}
}

View File

@ -61,8 +61,23 @@ test('API', t => {
t.deepEqual(pool._auth, { username: 'foo', password: 'bar' })
pool.addConnection('http://localhost:9201')
t.strictEqual(pool.connections.get('http://localhost:9201/').url.username, 'foo')
t.strictEqual(pool.connections.get('http://localhost:9201/').url.password, 'bar')
const conn = pool.connections.get('http://localhost:9201/')
t.strictEqual(conn.url.username, 'foo')
t.strictEqual(conn.url.password, 'bar')
t.strictEqual(conn.auth.username, 'foo')
t.strictEqual(conn.auth.password, 'bar')
t.end()
})
t.test('addConnection should handle not-friendly url parameters for user and password', t => {
const pool = new ConnectionPool({ Connection })
const href = 'http://us"er:p@assword@localhost:9200/'
pool.addConnection(href)
const conn = pool.getConnection()
t.strictEqual(conn.url.username, 'us%22er')
t.strictEqual(conn.url.password, 'p%40assword')
t.strictEqual(conn.auth.username, 'us"er')
t.strictEqual(conn.auth.password, 'p@assword')
t.end()
})

View File

@ -526,7 +526,8 @@ test('Url with auth', t => {
buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://foo:bar@localhost:${port}`)
url: new URL(`http://foo:bar@localhost:${port}`),
auth: { username: 'foo', password: 'bar' }
})
connection.request({
path: '/hello',