Skip to content

Commit 3bf1bd0

Browse files
authored
fix: Add Elastic-Api-Version, fix MIME types (#28)
* Add elastic-api-version header to all requests * Use standard MIME types for content-type and accept headers * Github workflows don't support YAML anchors
1 parent d2d11c4 commit 3bf1bd0

File tree

5 files changed

+54
-44
lines changed

5 files changed

+54
-44
lines changed

.github/workflows/tests.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name: Unit tests
33
on:
44
push:
5-
paths-ignore: &ignore
5+
paths-ignore:
66
- '**/*.md'
77
- '**/*.asciidoc'
88
- '**/*.txt'
@@ -12,7 +12,15 @@ on:
1212
- 'scripts/**'
1313
- 'catalog-info.yaml'
1414
pull_request:
15-
paths-ignore: *ignore
15+
paths-ignore:
16+
- '**/*.md'
17+
- '**/*.asciidoc'
18+
- '**/*.txt'
19+
- 'docs/**'
20+
- '.ci/**'
21+
- '.buildkite/**'
22+
- 'scripts/**'
23+
- 'catalog-info.yaml'
1624

1725
jobs:
1826
test:

src/client.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ if (clientVersion.includes('-')) {
5252
// clean prerelease
5353
clientVersion = clientVersion.slice(0, clientVersion.indexOf('-')) + 'p'
5454
}
55-
const clientVersionNoMeta = clientVersion.split('+')[0]
55+
const [clientVersionNoMeta, apiVersion] = clientVersion.split('+')
5656
let transportVersion: string = require('@elastic/transport/package.json').version // eslint-disable-line
5757
/* istanbul ignore next */
5858
if (transportVersion.includes('-')) {
@@ -162,7 +162,8 @@ export default class Client extends API {
162162
caFingerprint: null,
163163
agent: null,
164164
headers: {
165-
'user-agent': `elasticsearch-js/${clientVersion} Node.js ${nodeVersion}; Transport ${transportVersion}; (${os.platform()} ${os.release()} ${os.arch()})`
165+
'user-agent': `elasticsearch-js/${clientVersion} Node.js ${nodeVersion}; Transport ${transportVersion}; (${os.platform()} ${os.release()} ${os.arch()})`,
166+
'elastic-api-version': `${apiVersion.slice(0, 4)}-${apiVersion.slice(4, 6)}-${apiVersion.slice(6, 8)}`
166167
},
167168
generateRequestId: null,
168169
name: 'elasticsearch-js',
@@ -240,12 +241,7 @@ export default class Client extends API {
240241
context: options.context,
241242
productCheck: 'Elasticsearch',
242243
maxResponseSize: options.maxResponseSize,
243-
maxCompressedResponseSize: options.maxCompressedResponseSize,
244-
vendoredHeaders: {
245-
jsonContentType: 'application/vnd.elasticsearch+json; compatible-with=8',
246-
ndjsonContentType: 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
247-
accept: 'application/vnd.elasticsearch+json; compatible-with=8,text/plain'
248-
}
244+
maxCompressedResponseSize: options.maxCompressedResponseSize
249245
})
250246

251247
this.helpers = new Helpers({

test/integration/test-runner.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,6 @@ function build (opts = {}) {
288288
process.exit(1)
289289
}
290290

291-
if (action.headers) {
292-
switch (action.headers['Content-Type'] || action.headers['content-type']) {
293-
case 'application/json':
294-
delete action.headers['Content-Type']
295-
delete action.headers['content-type']
296-
action.headers['Content-Type'] = `application/vnd.elasticsearch+json; compatible-with=${packageJson.version.split('.')[0]}`
297-
break
298-
case 'application/x-ndjson':
299-
delete action.headers['Content-Type']
300-
delete action.headers['content-type']
301-
action.headers['Content-Type'] = `application/vnd.elasticsearch+x-ndjson; compatible-with=${packageJson.version.split('.')[0]}`
302-
break
303-
}
304-
}
305-
306291
const options = { ignore: cmd.params.ignore, headers: action.headers, meta: true }
307292
if (!Array.isArray(options.ignore)) options.ignore = [options.ignore]
308293
if (cmd.params.ignore) delete cmd.params.ignore

test/unit/client.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,27 @@ test('Meta header disabled', async t => {
404404
await client.transport.request({ method: 'GET', path: '/' })
405405
})
406406

407+
test('elastic-api-version header exists on all requests', async t => {
408+
t.plan(1)
409+
410+
const Connection = connection.buildMockConnection({
411+
onRequest (opts) {
412+
t.equal(opts.headers?.['elastic-api-version'], '2023-10-31')
413+
return {
414+
statusCode: 200,
415+
body: { hello: 'world' }
416+
}
417+
}
418+
})
419+
420+
const client = new Client({
421+
node: 'http://localhost:9200',
422+
Connection,
423+
})
424+
425+
await client.transport.request({ method: 'GET', path: '/' })
426+
})
427+
407428
test('caFingerprint', t => {
408429
const client = new Client({
409430
node: 'https://localhost:9200',

test/unit/helpers/bulk.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ test('bulk index', t => {
5656
onRequest (params) {
5757
t.equal(params.path, '/_bulk')
5858
t.match(params.headers, {
59-
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
59+
'content-type': 'application/x-ndjson',
6060
'x-elastic-client-meta': `esv=${clientVersionNoMeta},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion},h=bp`
6161
})
6262
// @ts-expect-error
@@ -103,7 +103,7 @@ test('bulk index', t => {
103103
const MockConnection = connection.buildMockConnection({
104104
onRequest (params) {
105105
t.equal(params.path, '/_bulk')
106-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
106+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
107107
t.notMatch(params.headers, {
108108
'x-elastic-client-meta': `esv=${clientVersionNoMeta},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion},h=bp`
109109
})
@@ -150,7 +150,7 @@ test('bulk index', t => {
150150
const MockConnection = connection.buildMockConnection({
151151
onRequest (params) {
152152
t.equal(params.path, '/_bulk')
153-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
153+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
154154
// @ts-expect-error
155155
t.equal(params.body.split('\n').filter(Boolean).length, 6)
156156
return { body: { errors: false, items: new Array(3).fill({}) } }
@@ -192,7 +192,7 @@ test('bulk index', t => {
192192
const MockConnection = connection.buildMockConnection({
193193
onRequest (params) {
194194
t.equal(params.path, '/_bulk')
195-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
195+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
196196
// @ts-expect-error
197197
const [action, payload] = params.body.split('\n')
198198
t.same(JSON.parse(action), { index: { _index: 'test', _id: count } })
@@ -238,7 +238,7 @@ test('bulk index', t => {
238238
t.test('Should perform a bulk request (retry)', async t => {
239239
async function handler (req: http.IncomingMessage, res: http.ServerResponse) {
240240
t.equal(req.url, '/_bulk')
241-
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
241+
t.match(req.headers, { 'content-type': 'application/x-ndjson' })
242242

243243
let body = ''
244244
req.setEncoding('utf8')
@@ -359,7 +359,7 @@ test('bulk index', t => {
359359
t.test('Should perform a bulk request (failure)', async t => {
360360
async function handler (req: http.IncomingMessage, res: http.ServerResponse) {
361361
t.equal(req.url, '/_bulk')
362-
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
362+
t.match(req.headers, { 'content-type': 'application/x-ndjson' })
363363

364364
let body = ''
365365
req.setEncoding('utf8')
@@ -503,7 +503,7 @@ test('bulk index', t => {
503503
t.test('Should abort a bulk request', async t => {
504504
async function handler (req: http.IncomingMessage, res: http.ServerResponse) {
505505
t.equal(req.url, '/_bulk')
506-
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
506+
t.match(req.headers, { 'content-type': 'application/x-ndjson' })
507507

508508
let body = ''
509509
req.setEncoding('utf8')
@@ -606,7 +606,7 @@ test('bulk index', t => {
606606
const MockConnection = connection.buildMockConnection({
607607
onRequest (params) {
608608
t.equal(params.path, '/_bulk')
609-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
609+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
610610
// @ts-expect-error
611611
const [action, payload] = params.body.split('\n')
612612
t.same(JSON.parse(action), { index: { _index: 'test', _id: count } })
@@ -660,7 +660,7 @@ test('bulk index', t => {
660660
const MockConnection = connection.buildMockConnection({
661661
onRequest (params) {
662662
t.equal(params.path, '/_bulk')
663-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
663+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
664664
// @ts-expect-error
665665
const [action, payload] = params.body.split('\n')
666666
t.same(JSON.parse(action), { index: { _index: 'test' } })
@@ -718,7 +718,7 @@ test('bulk create', t => {
718718
const MockConnection = connection.buildMockConnection({
719719
onRequest (params) {
720720
t.equal(params.path, '/_bulk')
721-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
721+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
722722
// @ts-expect-error
723723
const [action, payload] = params.body.split('\n')
724724
t.same(JSON.parse(action), { create: { _index: 'test', _id: count } })
@@ -769,7 +769,7 @@ test('bulk update', t => {
769769
const MockConnection = connection.buildMockConnection({
770770
onRequest (params) {
771771
t.equal(params.path, '/_bulk')
772-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
772+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
773773
// @ts-expect-error
774774
const [action, payload] = params.body.split('\n')
775775
t.same(JSON.parse(action), { update: { _index: 'test', _id: count } })
@@ -819,7 +819,7 @@ test('bulk update', t => {
819819
const MockConnection = connection.buildMockConnection({
820820
onRequest (params) {
821821
t.equal(params.path, '/_bulk')
822-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
822+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
823823
// @ts-expect-error
824824
const [action, payload] = params.body.split('\n')
825825
t.same(JSON.parse(action), { update: { _index: 'test', _id: count } })
@@ -867,7 +867,7 @@ test('bulk update', t => {
867867
const MockConnection = connection.buildMockConnection({
868868
onRequest (params) {
869869
t.equal(params.path, '/_bulk')
870-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
870+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
871871
// @ts-expect-error
872872
const [action, payload] = params.body.split('\n')
873873
t.same(JSON.parse(action), { update: { _index: 'test', _id: count } })
@@ -922,7 +922,7 @@ test('bulk delete', t => {
922922
const MockConnection = connection.buildMockConnection({
923923
onRequest (params) {
924924
t.equal(params.path, '/_bulk')
925-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
925+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
926926
// @ts-expect-error
927927
t.same(JSON.parse(params.body), { delete: { _index: 'test', _id: count++ } })
928928
return { body: { errors: false, items: [{}] } }
@@ -966,7 +966,7 @@ test('bulk delete', t => {
966966
t.test('Should perform a bulk request (failure)', async t => {
967967
async function handler (req: http.IncomingMessage, res: http.ServerResponse) {
968968
t.equal(req.url, '/_bulk')
969-
t.match(req.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
969+
t.match(req.headers, { 'content-type': 'application/x-ndjson' })
970970

971971
let body = ''
972972
req.setEncoding('utf8')
@@ -1115,7 +1115,7 @@ test('transport options', t => {
11151115

11161116
if (params.path === '/_bulk') {
11171117
t.match(params.headers, {
1118-
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
1118+
'content-type': 'application/x-ndjson',
11191119
foo: 'bar'
11201120
})
11211121
return { body: { errors: false, items: [{}] } }
@@ -1231,7 +1231,7 @@ test('Flush interval', t => {
12311231
const MockConnection = connection.buildMockConnection({
12321232
onRequest (params) {
12331233
t.equal(params.path, '/_bulk')
1234-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1234+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
12351235
// @ts-expect-error
12361236
const [action, payload] = params.body.split('\n')
12371237
t.same(JSON.parse(action), { index: { _index: 'test' } })
@@ -1285,7 +1285,7 @@ test('Flush interval', t => {
12851285
onRequest (params) {
12861286
t.ok(count < 2)
12871287
t.equal(params.path, '/_bulk')
1288-
t.match(params.headers, { 'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8' })
1288+
t.match(params.headers, { 'content-type': 'application/x-ndjson' })
12891289
// @ts-expect-error
12901290
const [action, payload] = params.body.split('\n')
12911291
t.same(JSON.parse(action), { index: { _index: 'test' } })
@@ -1345,7 +1345,7 @@ test('Flush interval', t => {
13451345
onRequest (params) {
13461346
t.equal(params.path, '/_bulk')
13471347
t.match(params.headers, {
1348-
'content-type': 'application/vnd.elasticsearch+x-ndjson; compatible-with=8',
1348+
'content-type': 'application/x-ndjson',
13491349
'x-elastic-client-meta': `esv=${clientVersionNoMeta},js=${nodeVersion},t=${transportVersion},hc=${nodeVersion},h=bp`
13501350
})
13511351
// @ts-expect-error

0 commit comments

Comments
 (0)