Skip to content

Commit 3e60b67

Browse files
committed
Merge branch '7.x' of https://github.com/elastic/elasticsearch-js into 7.x
2 parents c1dfb01 + 18b62b4 commit 3e60b67

File tree

8 files changed

+124
-79
lines changed

8 files changed

+124
-79
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 {

lib/Helpers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ class Helpers {
104104
}
105105

106106
while (response.body.hits && response.body.hits.hits.length > 0) {
107+
// scroll id is always present in the response, but it might
108+
// change over time based on the number of shards
107109
scroll_id = response.body._scroll_id
108110
response.clear = clear
109111
addDocumentsGetter(response)
110112

111113
yield response
112114

113-
if (!scroll_id || stop === true) {
115+
if (stop === true) {
114116
break
115117
}
116118

@@ -127,6 +129,10 @@ class Helpers {
127129
throw new ResponseError(response)
128130
}
129131
}
132+
133+
if (stop === false) {
134+
await clear()
135+
}
130136
}
131137

132138
/**

test/integration/test-runner.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,26 @@ function build (opts = {}) {
279279
// eg: 'Basic ${auth}' we search the stahed value 'auth'
280280
// and the resulting value will be 'Basic valueOfAuth'
281281
if (typeof val === 'string' && val.includes('${')) {
282-
const start = val.indexOf('${')
283-
const end = val.indexOf('}', val.indexOf('${'))
284-
const stashedKey = val.slice(start + 2, end)
285-
const stashed = stash.get(stashedKey)
286-
obj[key] = val.slice(0, start) + stashed + val.slice(end + 1)
282+
while (obj[key].includes('${')) {
283+
const val = obj[key]
284+
const start = val.indexOf('${')
285+
const end = val.indexOf('}', val.indexOf('${'))
286+
const stashedKey = val.slice(start + 2, end)
287+
const stashed = stash.get(stashedKey)
288+
obj[key] = val.slice(0, start) + stashed + val.slice(end + 1)
289+
}
287290
continue
288291
}
289292
// handle json strings, eg: '{"hello":"$world"}'
290293
if (typeof val === 'string' && val.includes('"$')) {
291-
const start = val.indexOf('"$')
292-
const end = val.indexOf('"', start + 1)
293-
const stashedKey = val.slice(start + 2, end)
294-
const stashed = '"' + stash.get(stashedKey) + '"'
295-
obj[key] = val.slice(0, start) + stashed + val.slice(end + 1)
294+
while (obj[key].includes('"$')) {
295+
const val = obj[key]
296+
const start = val.indexOf('"$')
297+
const end = val.indexOf('"', start + 1)
298+
const stashedKey = val.slice(start + 2, end)
299+
const stashed = '"' + stash.get(stashedKey) + '"'
300+
obj[key] = val.slice(0, start) + stashed + val.slice(end + 1)
301+
}
296302
continue
297303
}
298304
// if the key value is a string, and the string includes '$'

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({

test/unit/helpers/scroll.test.js

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,26 @@ test('Scroll search', async t => {
2727
var count = 0
2828
const MockConnection = connection.buildMockConnection({
2929
onRequest (params) {
30-
t.strictEqual(params.querystring, 'scroll=1m')
30+
count += 1
31+
if (params.method === 'POST') {
32+
t.strictEqual(params.querystring, 'scroll=1m')
33+
}
34+
if (count === 4) {
35+
// final automated clear
36+
t.strictEqual(params.method, 'DELETE')
37+
}
3138
return {
3239
body: {
33-
_scroll_id: count === 3 ? undefined : 'id',
40+
_scroll_id: 'id',
3441
count,
3542
hits: {
36-
hits: [
37-
{ _source: { one: 'one' } },
38-
{ _source: { two: 'two' } },
39-
{ _source: { three: 'three' } }
40-
]
43+
hits: count === 3
44+
? []
45+
: [
46+
{ _source: { one: 'one' } },
47+
{ _source: { two: 'two' } },
48+
{ _source: { three: 'three' } }
49+
]
4150
}
4251
}
4352
}
@@ -56,12 +65,7 @@ test('Scroll search', async t => {
5665

5766
for await (const result of scrollSearch) {
5867
t.strictEqual(result.body.count, count)
59-
if (count < 3) {
60-
t.strictEqual(result.body._scroll_id, 'id')
61-
} else {
62-
t.strictEqual(result.body._scroll_id, undefined)
63-
}
64-
count += 1
68+
t.strictEqual(result.body._scroll_id, 'id')
6569
}
6670
})
6771

@@ -115,21 +119,27 @@ test('Scroll search (retry)', async t => {
115119
var count = 0
116120
const MockConnection = connection.buildMockConnection({
117121
onRequest (params) {
122+
count += 1
118123
if (count === 1) {
119-
count += 1
120124
return { body: {}, statusCode: 429 }
121125
}
126+
if (count === 5) {
127+
// final automated clear
128+
t.strictEqual(params.method, 'DELETE')
129+
}
122130
return {
123131
statusCode: 200,
124132
body: {
125-
_scroll_id: count === 4 ? undefined : 'id',
133+
_scroll_id: 'id',
126134
count,
127135
hits: {
128-
hits: [
129-
{ _source: { one: 'one' } },
130-
{ _source: { two: 'two' } },
131-
{ _source: { three: 'three' } }
132-
]
136+
hits: count === 4
137+
? []
138+
: [
139+
{ _source: { one: 'one' } },
140+
{ _source: { two: 'two' } },
141+
{ _source: { three: 'three' } }
142+
]
133143
}
134144
}
135145
}
@@ -151,12 +161,7 @@ test('Scroll search (retry)', async t => {
151161
for await (const result of scrollSearch) {
152162
t.strictEqual(result.body.count, count)
153163
t.notStrictEqual(result.body.count, 1)
154-
if (count < 4) {
155-
t.strictEqual(result.body._scroll_id, 'id')
156-
} else {
157-
t.strictEqual(result.body._scroll_id, undefined)
158-
}
159-
count += 1
164+
t.strictEqual(result.body._scroll_id, 'id')
160165
}
161166
})
162167

@@ -198,20 +203,20 @@ test('Scroll search (retry throws and maxRetries)', async t => {
198203

199204
test('Scroll search (retry throws later)', async t => {
200205
const maxRetries = 5
201-
const expectedAttempts = maxRetries + 1
206+
const expectedAttempts = maxRetries + 2
202207
var count = 0
203208
const MockConnection = connection.buildMockConnection({
204209
onRequest (params) {
205-
// filter_path should not be added if is not already present
210+
count += 1
211+
// filter_path should not be added if is not already present
206212
t.strictEqual(params.querystring, 'scroll=1m')
207213
if (count > 1) {
208-
count += 1
209214
return { body: {}, statusCode: 429 }
210215
}
211216
return {
212217
statusCode: 200,
213218
body: {
214-
_scroll_id: count === 4 ? undefined : 'id',
219+
_scroll_id: 'id',
215220
count,
216221
hits: {
217222
hits: [
@@ -227,7 +232,8 @@ test('Scroll search (retry throws later)', async t => {
227232

228233
const client = new Client({
229234
node: 'http://localhost:9200',
230-
Connection: MockConnection
235+
Connection: MockConnection,
236+
maxRetries
231237
})
232238

233239
const scrollSearch = client.helpers.scrollSearch({
@@ -240,7 +246,6 @@ test('Scroll search (retry throws later)', async t => {
240246
try {
241247
for await (const result of scrollSearch) { // eslint-disable-line
242248
t.strictEqual(result.body.count, count)
243-
count += 1
244249
}
245250
} catch (err) {
246251
t.true(err instanceof errors.ResponseError)
@@ -256,19 +261,23 @@ test('Scroll search documents', async t => {
256261
if (count === 0) {
257262
t.strictEqual(params.querystring, 'filter_path=hits.hits._source%2C_scroll_id&scroll=1m')
258263
} else {
259-
t.strictEqual(params.querystring, 'scroll=1m')
260-
t.strictEqual(params.body, '{"scroll_id":"id"}')
264+
if (params.method !== 'DELETE') {
265+
t.strictEqual(params.querystring, 'scroll=1m')
266+
t.strictEqual(params.body, '{"scroll_id":"id"}')
267+
}
261268
}
262269
return {
263270
body: {
264-
_scroll_id: count === 3 ? undefined : 'id',
271+
_scroll_id: 'id',
265272
count,
266273
hits: {
267-
hits: [
268-
{ _source: { val: 1 * count } },
269-
{ _source: { val: 2 * count } },
270-
{ _source: { val: 3 * count } }
271-
]
274+
hits: count === 3
275+
? []
276+
: [
277+
{ _source: { val: 1 * count } },
278+
{ _source: { val: 2 * count } },
279+
{ _source: { val: 3 * count } }
280+
]
272281
}
273282
}
274283
}
@@ -339,15 +348,19 @@ test('Fix querystring for scroll search', async t => {
339348
if (count === 0) {
340349
t.strictEqual(params.querystring, 'size=1&scroll=1m')
341350
} else {
342-
t.strictEqual(params.querystring, 'scroll=1m')
351+
if (params.method !== 'DELETE') {
352+
t.strictEqual(params.querystring, 'scroll=1m')
353+
}
343354
}
344355
return {
345356
body: {
346-
_scroll_id: count === 3 ? undefined : 'id',
357+
_scroll_id: 'id',
347358
hits: {
348-
hits: [
349-
{ _source: { val: count } }
350-
]
359+
hits: count === 3
360+
? []
361+
: [
362+
{ _source: { val: count } }
363+
]
351364
}
352365
}
353366
}

0 commit comments

Comments
 (0)