Skip to content

Commit bb59d10

Browse files
committed
Ensure elasticsearch-js user agent header is set
Fixes #1737
1 parent b2b54f1 commit bb59d10

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import { ConnectionOptions as TlsConnectionOptions } from 'tls'
2121
import { URL } from 'url'
2222
import buffer from 'buffer'
23+
import os from 'os'
2324
import {
2425
Transport,
2526
UndiciConnection,
@@ -173,7 +174,9 @@ export default class Client extends API {
173174
tls: null,
174175
caFingerprint: null,
175176
agent: null,
176-
headers: {},
177+
headers: {
178+
'user-agent': `elasticsearch-js/${clientVersion} Node.js ${nodeVersion}; Transport ${transportVersion}; (${os.platform()} ${os.release()} ${os.arch()})`
179+
},
177180
nodeFilter: null,
178181
generateRequestId: null,
179182
name: 'elasticsearch-js',

test/unit/client.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,12 @@ test('caFingerprint can\'t be configured over http / 2', t => {
432432
)
433433
t.end()
434434
})
435+
436+
test('user agent is in the correct format', t => {
437+
const client = new Client({ node: 'http://localhost:9200' })
438+
const agentRaw = client.transport[symbols.kHeaders]['user-agent'] || ''
439+
const agentSplit = agentRaw.split(/\s+/)
440+
t.equal(agentSplit[0].split('/')[0], 'elasticsearch-js')
441+
t.ok(/^\d+\.\d+\.\d+/.test(agentSplit[0].split('/')[1]))
442+
t.end()
443+
})

0 commit comments

Comments
 (0)