@elastic/elasticsearch 7.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/.dockerignore +5 -0
  2. package/LICENSE +202 -0
  3. package/README.md +232 -0
  4. package/api/api/async_search.js +141 -0
  5. package/api/api/autoscaling.js +147 -0
  6. package/api/api/bulk.js +70 -0
  7. package/api/api/cat.js +648 -0
  8. package/api/api/ccr.js +403 -0
  9. package/api/api/clear_scroll.js +55 -0
  10. package/api/api/close_point_in_time.js +50 -0
  11. package/api/api/cluster.js +420 -0
  12. package/api/api/count.js +64 -0
  13. package/api/api/create.js +69 -0
  14. package/api/api/dangling_indices.js +115 -0
  15. package/api/api/delete.js +65 -0
  16. package/api/api/delete_by_query.js +71 -0
  17. package/api/api/delete_by_query_rethrottle.js +60 -0
  18. package/api/api/delete_script.js +56 -0
  19. package/api/api/enrich.js +173 -0
  20. package/api/api/eql.js +150 -0
  21. package/api/api/exists.js +65 -0
  22. package/api/api/exists_source.js +74 -0
  23. package/api/api/explain.js +65 -0
  24. package/api/api/features.js +81 -0
  25. package/api/api/field_caps.js +55 -0
  26. package/api/api/fleet.js +65 -0
  27. package/api/api/get.js +65 -0
  28. package/api/api/get_script.js +56 -0
  29. package/api/api/get_script_context.js +50 -0
  30. package/api/api/get_script_languages.js +50 -0
  31. package/api/api/get_source.js +65 -0
  32. package/api/api/graph.js +72 -0
  33. package/api/api/ilm.js +317 -0
  34. package/api/api/index.js +71 -0
  35. package/api/api/indices.js +1753 -0
  36. package/api/api/info.js +50 -0
  37. package/api/api/ingest.js +200 -0
  38. package/api/api/license.js +188 -0
  39. package/api/api/logstash.js +125 -0
  40. package/api/api/mget.js +70 -0
  41. package/api/api/migration.js +60 -0
  42. package/api/api/ml.js +2010 -0
  43. package/api/api/monitoring.js +66 -0
  44. package/api/api/msearch.js +70 -0
  45. package/api/api/msearch_template.js +70 -0
  46. package/api/api/mtermvectors.js +64 -0
  47. package/api/api/nodes.js +268 -0
  48. package/api/api/open_point_in_time.js +56 -0
  49. package/api/api/ping.js +50 -0
  50. package/api/api/put_script.js +71 -0
  51. package/api/api/rank_eval.js +61 -0
  52. package/api/api/reindex.js +56 -0
  53. package/api/api/reindex_rethrottle.js +60 -0
  54. package/api/api/render_search_template.js +55 -0
  55. package/api/api/rollup.js +319 -0
  56. package/api/api/scripts_painless_execute.js +50 -0
  57. package/api/api/scroll.js +55 -0
  58. package/api/api/search.js +64 -0
  59. package/api/api/search_mvt.js +87 -0
  60. package/api/api/search_shards.js +55 -0
  61. package/api/api/search_template.js +70 -0
  62. package/api/api/searchable_snapshots.js +186 -0
  63. package/api/api/security.js +1261 -0
  64. package/api/api/shutdown.js +124 -0
  65. package/api/api/slm.js +256 -0
  66. package/api/api/snapshot.js +439 -0
  67. package/api/api/sql.js +203 -0
  68. package/api/api/ssl.js +55 -0
  69. package/api/api/tasks.js +108 -0
  70. package/api/api/terms_enum.js +56 -0
  71. package/api/api/termvectors.js +67 -0
  72. package/api/api/text_structure.js +65 -0
  73. package/api/api/transform.js +268 -0
  74. package/api/api/update.js +69 -0
  75. package/api/api/update_by_query.js +67 -0
  76. package/api/api/update_by_query_rethrottle.js +60 -0
  77. package/api/api/watcher.js +333 -0
  78. package/api/api/xpack.js +76 -0
  79. package/api/index.js +508 -0
  80. package/api/new.d.ts +1585 -0
  81. package/api/requestParams.d.ts +2920 -0
  82. package/api/types.d.ts +15420 -0
  83. package/api/utils.js +58 -0
  84. package/codecov.yml +14 -0
  85. package/index.d.ts +2991 -0
  86. package/index.js +349 -0
  87. package/index.mjs +29 -0
  88. package/lib/Connection.d.ts +99 -0
  89. package/lib/Connection.js +392 -0
  90. package/lib/Helpers.d.ts +124 -0
  91. package/lib/Helpers.js +770 -0
  92. package/lib/Serializer.d.ts +30 -0
  93. package/lib/Serializer.js +94 -0
  94. package/lib/Transport.d.ts +162 -0
  95. package/lib/Transport.js +689 -0
  96. package/lib/errors.d.ts +90 -0
  97. package/lib/errors.js +159 -0
  98. package/lib/pool/BaseConnectionPool.js +262 -0
  99. package/lib/pool/CloudConnectionPool.js +64 -0
  100. package/lib/pool/ConnectionPool.js +246 -0
  101. package/lib/pool/index.d.ts +220 -0
  102. package/lib/pool/index.js +30 -0
  103. package/package.json +106 -0
package/index.js ADDED
@@ -0,0 +1,349 @@
1
+ /*
2
+ * Licensed to Elasticsearch B.V. under one or more contributor
3
+ * license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright
5
+ * ownership. Elasticsearch B.V. licenses this file to you under
6
+ * the Apache License, Version 2.0 (the "License"); you may
7
+ * not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ 'use strict'
21
+
22
+ const { EventEmitter } = require('events')
23
+ const { URL } = require('url')
24
+ const debug = require('debug')('elasticsearch')
25
+ const Transport = require('./lib/Transport')
26
+ const Connection = require('./lib/Connection')
27
+ const { ConnectionPool, CloudConnectionPool } = require('./lib/pool')
28
+ const Helpers = require('./lib/Helpers')
29
+ const Serializer = require('./lib/Serializer')
30
+ const errors = require('./lib/errors')
31
+ const { ConfigurationError } = errors
32
+ const { prepareHeaders } = Connection.internals
33
+ let clientVersion = require('./package.json').version
34
+ /* istanbul ignore next */
35
+ if (clientVersion.includes('-')) {
36
+ // clean prerelease
37
+ clientVersion = clientVersion.slice(0, clientVersion.indexOf('-')) + 'p'
38
+ }
39
+ const nodeVersion = process.versions.node
40
+
41
+ const kInitialOptions = Symbol('elasticsearchjs-initial-options')
42
+ const kChild = Symbol('elasticsearchjs-child')
43
+ const kExtensions = Symbol('elasticsearchjs-extensions')
44
+ const kEventEmitter = Symbol('elasticsearchjs-event-emitter')
45
+
46
+ const ESAPI = require('./api')
47
+
48
+ class Client extends ESAPI {
49
+ constructor (opts = {}) {
50
+ super({ ConfigurationError })
51
+ if (opts.cloud && opts[kChild] === undefined) {
52
+ const { id, username, password } = opts.cloud
53
+ // the cloud id is `cluster-name:base64encodedurl`
54
+ // the url is a string divided by two '$', the first is the cloud url
55
+ // the second the elasticsearch instance, the third the kibana instance
56
+ const cloudUrls = Buffer.from(id.split(':')[1], 'base64').toString().split('$')
57
+
58
+ // TODO: remove username and password here in 8
59
+ if (username && password) {
60
+ opts.auth = Object.assign({}, opts.auth, { username, password })
61
+ }
62
+ opts.node = `https://${cloudUrls[1]}.${cloudUrls[0]}`
63
+
64
+ // Cloud has better performances with compression enabled
65
+ // see https://github.com/elastic/elasticsearch-py/pull/704.
66
+ // So unless the user specifies otherwise, we enable compression.
67
+ if (opts.compression == null) opts.compression = 'gzip'
68
+ if (opts.suggestCompression == null) opts.suggestCompression = true
69
+ if (opts.ssl == null ||
70
+ (opts.ssl && opts.ssl.secureProtocol == null)) {
71
+ opts.ssl = opts.ssl || {}
72
+ opts.ssl.secureProtocol = 'TLSv1_2_method'
73
+ }
74
+ }
75
+
76
+ if (!opts.node && !opts.nodes) {
77
+ throw new ConfigurationError('Missing node(s) option')
78
+ }
79
+
80
+ if (opts[kChild] === undefined) {
81
+ const checkAuth = getAuth(opts.node || opts.nodes)
82
+ if (checkAuth && checkAuth.username && checkAuth.password) {
83
+ opts.auth = Object.assign({}, opts.auth, { username: checkAuth.username, password: checkAuth.password })
84
+ }
85
+ }
86
+
87
+ const options = opts[kChild] !== undefined
88
+ ? opts[kChild].initialOptions
89
+ : Object.assign({}, {
90
+ Connection,
91
+ Transport,
92
+ Serializer,
93
+ ConnectionPool: opts.cloud ? CloudConnectionPool : ConnectionPool,
94
+ maxRetries: 3,
95
+ requestTimeout: 30000,
96
+ pingTimeout: 3000,
97
+ sniffInterval: false,
98
+ sniffOnStart: false,
99
+ sniffEndpoint: '_nodes/_all/http',
100
+ sniffOnConnectionFault: false,
101
+ resurrectStrategy: 'ping',
102
+ suggestCompression: false,
103
+ compression: false,
104
+ ssl: null,
105
+ caFingerprint: null,
106
+ agent: null,
107
+ headers: {},
108
+ nodeFilter: null,
109
+ nodeSelector: 'round-robin',
110
+ generateRequestId: null,
111
+ name: 'elasticsearch-js',
112
+ auth: null,
113
+ opaqueIdPrefix: null,
114
+ context: null,
115
+ proxy: null,
116
+ enableMetaHeader: true,
117
+ disablePrototypePoisoningProtection: false
118
+ }, opts)
119
+
120
+ if (options.caFingerprint !== null && isHttpConnection(opts.node || opts.nodes)) {
121
+ throw new ConfigurationError('You can\'t configure the caFingerprint with a http connection')
122
+ }
123
+
124
+ if (process.env.ELASTIC_CLIENT_APIVERSIONING === 'true') {
125
+ options.headers = Object.assign({ accept: 'application/vnd.elasticsearch+json; compatible-with=7' }, options.headers)
126
+ }
127
+
128
+ this[kInitialOptions] = options
129
+ this[kExtensions] = []
130
+ this.name = options.name
131
+
132
+ if (options.enableMetaHeader) {
133
+ options.headers['x-elastic-client-meta'] = `es=${clientVersion},js=${nodeVersion},t=${clientVersion},hc=${nodeVersion}`
134
+ }
135
+
136
+ if (opts[kChild] !== undefined) {
137
+ this.serializer = options[kChild].serializer
138
+ this.connectionPool = options[kChild].connectionPool
139
+ this[kEventEmitter] = options[kChild].eventEmitter
140
+ } else {
141
+ this[kEventEmitter] = new EventEmitter()
142
+ this.serializer = new options.Serializer({
143
+ disablePrototypePoisoningProtection: options.disablePrototypePoisoningProtection
144
+ })
145
+ this.connectionPool = new options.ConnectionPool({
146
+ pingTimeout: options.pingTimeout,
147
+ resurrectStrategy: options.resurrectStrategy,
148
+ ssl: options.ssl,
149
+ agent: options.agent,
150
+ proxy: options.proxy,
151
+ Connection: options.Connection,
152
+ auth: options.auth,
153
+ emit: this[kEventEmitter].emit.bind(this[kEventEmitter]),
154
+ caFingerprint: options.caFingerprint,
155
+ sniffEnabled: options.sniffInterval !== false ||
156
+ options.sniffOnStart !== false ||
157
+ options.sniffOnConnectionFault !== false
158
+ })
159
+ // Add the connections before initialize the Transport
160
+ this.connectionPool.addConnection(options.node || options.nodes)
161
+ }
162
+
163
+ this.transport = new options.Transport({
164
+ emit: this[kEventEmitter].emit.bind(this[kEventEmitter]),
165
+ connectionPool: this.connectionPool,
166
+ serializer: this.serializer,
167
+ maxRetries: options.maxRetries,
168
+ requestTimeout: options.requestTimeout,
169
+ sniffInterval: options.sniffInterval,
170
+ sniffOnStart: options.sniffOnStart,
171
+ sniffOnConnectionFault: options.sniffOnConnectionFault,
172
+ sniffEndpoint: options.sniffEndpoint,
173
+ suggestCompression: options.suggestCompression,
174
+ compression: options.compression,
175
+ headers: options.headers,
176
+ nodeFilter: options.nodeFilter,
177
+ nodeSelector: options.nodeSelector,
178
+ generateRequestId: options.generateRequestId,
179
+ name: options.name,
180
+ opaqueIdPrefix: options.opaqueIdPrefix,
181
+ context: options.context
182
+ })
183
+
184
+ this.helpers = new Helpers({
185
+ client: this,
186
+ maxRetries: options.maxRetries,
187
+ metaHeader: options.enableMetaHeader
188
+ ? `es=${clientVersion},js=${nodeVersion},t=${clientVersion},hc=${nodeVersion}`
189
+ : null
190
+ })
191
+ }
192
+
193
+ get emit () {
194
+ return this[kEventEmitter].emit.bind(this[kEventEmitter])
195
+ }
196
+
197
+ get on () {
198
+ return this[kEventEmitter].on.bind(this[kEventEmitter])
199
+ }
200
+
201
+ get once () {
202
+ return this[kEventEmitter].once.bind(this[kEventEmitter])
203
+ }
204
+
205
+ get off () {
206
+ return this[kEventEmitter].off.bind(this[kEventEmitter])
207
+ }
208
+
209
+ extend (name, opts, fn) {
210
+ if (typeof opts === 'function') {
211
+ fn = opts
212
+ opts = {}
213
+ }
214
+
215
+ let [namespace, method] = name.split('.')
216
+ if (method == null) {
217
+ method = namespace
218
+ namespace = null
219
+ }
220
+
221
+ if (namespace != null) {
222
+ if (this[namespace] != null && this[namespace][method] != null && opts.force !== true) {
223
+ throw new Error(`The method "${method}" already exists on namespace "${namespace}"`)
224
+ }
225
+
226
+ if (this[namespace] == null) this[namespace] = {}
227
+ this[namespace][method] = fn({
228
+ makeRequest: this.transport.request.bind(this.transport),
229
+ result: { body: null, statusCode: null, headers: null, warnings: null },
230
+ ConfigurationError
231
+ })
232
+ } else {
233
+ if (this[method] != null && opts.force !== true) {
234
+ throw new Error(`The method "${method}" already exists`)
235
+ }
236
+
237
+ this[method] = fn({
238
+ makeRequest: this.transport.request.bind(this.transport),
239
+ result: { body: null, statusCode: null, headers: null, warnings: null },
240
+ ConfigurationError
241
+ })
242
+ }
243
+
244
+ this[kExtensions].push({ name, opts, fn })
245
+ }
246
+
247
+ child (opts) {
248
+ // Merge the new options with the initial ones
249
+ const options = Object.assign({}, this[kInitialOptions], opts)
250
+ // Pass to the child client the parent instances that cannot be overriden
251
+ options[kChild] = {
252
+ connectionPool: this.connectionPool,
253
+ serializer: this.serializer,
254
+ eventEmitter: this[kEventEmitter],
255
+ initialOptions: options
256
+ }
257
+
258
+ /* istanbul ignore else */
259
+ if (options.auth !== undefined) {
260
+ options.headers = prepareHeaders(options.headers, options.auth)
261
+ }
262
+
263
+ const client = new Client(options)
264
+ // sync product check
265
+ const tSymbol = Object.getOwnPropertySymbols(this.transport)
266
+ .filter(symbol => symbol.description === 'product check')[0]
267
+ client.transport[tSymbol] = this.transport[tSymbol]
268
+ // Add parent extensions
269
+ if (this[kExtensions].length > 0) {
270
+ this[kExtensions].forEach(({ name, opts, fn }) => {
271
+ client.extend(name, opts, fn)
272
+ })
273
+ }
274
+ return client
275
+ }
276
+
277
+ close (callback) {
278
+ if (callback == null) {
279
+ return new Promise((resolve, reject) => {
280
+ this.close(resolve)
281
+ })
282
+ }
283
+ debug('Closing the client')
284
+ this.connectionPool.empty(callback)
285
+ }
286
+ }
287
+
288
+ function getAuth (node) {
289
+ if (Array.isArray(node)) {
290
+ for (const url of node) {
291
+ const auth = getUsernameAndPassword(url)
292
+ if (auth.username !== '' && auth.password !== '') {
293
+ return auth
294
+ }
295
+ }
296
+
297
+ return null
298
+ }
299
+
300
+ const auth = getUsernameAndPassword(node)
301
+ if (auth.username !== '' && auth.password !== '') {
302
+ return auth
303
+ }
304
+
305
+ return null
306
+
307
+ function getUsernameAndPassword (node) {
308
+ /* istanbul ignore else */
309
+ if (typeof node === 'string') {
310
+ const { username, password } = new URL(node)
311
+ return {
312
+ username: decodeURIComponent(username),
313
+ password: decodeURIComponent(password)
314
+ }
315
+ } else if (node.url instanceof URL) {
316
+ return {
317
+ username: decodeURIComponent(node.url.username),
318
+ password: decodeURIComponent(node.url.password)
319
+ }
320
+ }
321
+ }
322
+ }
323
+
324
+ function isHttpConnection (node) {
325
+ if (Array.isArray(node)) {
326
+ return node.some((n) => (typeof n === 'string' ? new URL(n).protocol : n.url.protocol) === 'http:')
327
+ } else {
328
+ return (typeof node === 'string' ? new URL(node).protocol : node.url.protocol) === 'http:'
329
+ }
330
+ }
331
+
332
+ const events = {
333
+ RESPONSE: 'response',
334
+ REQUEST: 'request',
335
+ SNIFF: 'sniff',
336
+ RESURRECT: 'resurrect',
337
+ SERIALIZATION: 'serialization',
338
+ DESERIALIZATION: 'deserialization'
339
+ }
340
+
341
+ module.exports = {
342
+ Client,
343
+ Transport,
344
+ ConnectionPool,
345
+ Connection,
346
+ Serializer,
347
+ events,
348
+ errors
349
+ }
package/index.mjs ADDED
@@ -0,0 +1,29 @@
1
+ /*
2
+ * Licensed to Elasticsearch B.V. under one or more contributor
3
+ * license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright
5
+ * ownership. Elasticsearch B.V. licenses this file to you under
6
+ * the Apache License, Version 2.0 (the "License"); you may
7
+ * not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ import mod from './index.js'
21
+
22
+ export default mod
23
+ export const Client = mod.Client
24
+ export const Transport = mod.Transport
25
+ export const ConnectionPool = mod.ConnectionPool
26
+ export const Connection = mod.Connection
27
+ export const Serializer = mod.Serializer
28
+ export const events = mod.events
29
+ export const errors = mod.errors
@@ -0,0 +1,99 @@
1
+ /*
2
+ * Licensed to Elasticsearch B.V. under one or more contributor
3
+ * license agreements. See the NOTICE file distributed with
4
+ * this work for additional information regarding copyright
5
+ * ownership. Elasticsearch B.V. licenses this file to you under
6
+ * the Apache License, Version 2.0 (the "License"); you may
7
+ * not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ /// <reference types="node" />
21
+
22
+ import { URL } from 'url';
23
+ import { inspect, InspectOptions } from 'util'
24
+ import { Readable as ReadableStream } from 'stream';
25
+ import { ApiKeyAuth, BasicAuth } from './pool'
26
+ import * as http from 'http'
27
+ import * as https from 'https'
28
+ import * as hpagent from 'hpagent'
29
+ import { ConnectionOptions as TlsConnectionOptions } from 'tls'
30
+
31
+ export declare type agentFn = (opts: ConnectionOptions) => any;
32
+
33
+ export interface ConnectionOptions {
34
+ url: URL;
35
+ ssl?: TlsConnectionOptions;
36
+ id?: string;
37
+ headers?: Record<string, any>;
38
+ agent?: AgentOptions | agentFn;
39
+ status?: string;
40
+ roles?: ConnectionRoles;
41
+ auth?: BasicAuth | ApiKeyAuth;
42
+ proxy?: string | URL;
43
+ caFingerprint?: string;
44
+ }
45
+
46
+ interface ConnectionRoles {
47
+ master?: boolean
48
+ data?: boolean
49
+ ingest?: boolean
50
+ ml?: boolean
51
+ }
52
+
53
+ interface RequestOptions extends http.ClientRequestArgs {
54
+ asStream?: boolean;
55
+ body?: string | Buffer | ReadableStream | null;
56
+ querystring?: string;
57
+ }
58
+
59
+ export interface AgentOptions {
60
+ keepAlive?: boolean;
61
+ keepAliveMsecs?: number;
62
+ maxSockets?: number;
63
+ maxFreeSockets?: number;
64
+ }
65
+
66
+ export default class Connection {
67
+ static statuses: {
68
+ ALIVE: string;
69
+ DEAD: string;
70
+ };
71
+ static roles: {
72
+ MASTER: string;
73
+ DATA: string;
74
+ INGEST: string;
75
+ ML: string;
76
+ };
77
+ url: URL
78
+ ssl: TlsConnectionOptions | null
79
+ id: string
80
+ headers: Record<string, any>
81
+ status: string
82
+ roles: ConnectionRoles
83
+ deadCount: number
84
+ resurrectTimeout: number
85
+ makeRequest: any
86
+ _openRequests: number
87
+ _status: string
88
+ _agent: http.Agent | https.Agent | hpagent.HttpProxyAgent | hpagent.HttpsProxyAgent
89
+ constructor(opts?: ConnectionOptions)
90
+ request(params: RequestOptions, callback: (err: Error | null, response: http.IncomingMessage | null) => void): http.ClientRequest
91
+ close(): Connection
92
+ setRole(role: string, enabled: boolean): Connection
93
+ buildRequestObject(params: any): http.ClientRequestArgs
94
+ // @ts-ignore
95
+ [inspect.custom](object: any, options: InspectOptions): string
96
+ toJSON(): any
97
+ }
98
+
99
+ export {};