-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathUndiciConnection.ts
266 lines (242 loc) · 10.1 KB
/
UndiciConnection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import { EventEmitter } from 'events'
import Debug from 'debug'
import buffer from 'buffer'
import { TLSSocket } from 'tls'
import { Socket } from 'net'
import BaseConnection, {
ConnectionOptions,
ConnectionRequestParams,
ConnectionRequestOptions,
ConnectionRequestOptionsAsStream,
ConnectionRequestResponse,
ConnectionRequestResponseAsStream,
getIssuerCertificate
} from './BaseConnection'
import { Pool, buildConnector, Dispatcher } from 'undici'
import {
ConfigurationError,
RequestAbortedError,
ConnectionError,
TimeoutError
} from '../errors'
import { UndiciAgentOptions } from '../types'
import { kCaFingerprint, kEmitter } from '../symbols'
const debug = Debug('elasticsearch')
const INVALID_PATH_REGEX = /[^\u0021-\u00ff]/
const MAX_BUFFER_LENGTH = buffer.constants.MAX_LENGTH
const MAX_STRING_LENGTH = buffer.constants.MAX_STRING_LENGTH
export default class Connection extends BaseConnection {
pool: Pool
[kEmitter]: EventEmitter
constructor (opts: ConnectionOptions) {
super(opts)
if (opts.proxy != null) {
throw new ConfigurationError('Undici connection can\'t work with proxies')
}
if (typeof opts.agent === 'function' || typeof opts.agent === 'boolean') {
throw new ConfigurationError('Undici connection agent options can\'t be a function or a boolean')
}
if (opts.agent != null && !isUndiciAgentOptions(opts.agent)) {
throw new ConfigurationError('Bad agent configuration for Undici agent')
}
this[kEmitter] = new EventEmitter()
this[kEmitter].setMaxListeners(this.maxEventListeners)
const undiciOptions: Pool.Options = {
keepAliveTimeout: 600e3,
keepAliveMaxTimeout: 600e3,
keepAliveTimeoutThreshold: 1000,
pipelining: 1,
maxHeaderSize: 16384,
connections: 256,
headersTimeout: this.timeout,
bodyTimeout: this.timeout,
...opts.agent
}
if (this[kCaFingerprint] !== null) {
const caFingerprint = this[kCaFingerprint]
const connector = buildConnector((this.tls ?? {}) as buildConnector.BuildOptions)
undiciOptions.connect = function (opts: buildConnector.Options, cb: buildConnector.Callback) {
connector(opts, (err, socket) => {
if (err != null) {
return cb(err, null)
}
if (caFingerprint !== null && isTlsSocket(opts, socket)) {
const issuerCertificate = getIssuerCertificate(socket)
/* istanbul ignore next */
if (issuerCertificate == null) {
socket.destroy()
return cb(new Error('Invalid or malformed certificate'), null)
}
// Check if fingerprint matches
/* istanbul ignore else */
if (caFingerprint !== issuerCertificate.fingerprint256) {
socket.destroy()
return cb(new Error('Server certificate CA fingerprint does not match the value configured in caFingerprint'), null)
}
}
return cb(null, socket)
})
}
} else if (this.tls !== null) {
undiciOptions.connect = this.tls as buildConnector.BuildOptions
}
this.pool = new Pool(this.url.toString(), undiciOptions)
}
async request (params: ConnectionRequestParams, options: ConnectionRequestOptions): Promise<ConnectionRequestResponse>
async request (params: ConnectionRequestParams, options: ConnectionRequestOptionsAsStream): Promise<ConnectionRequestResponseAsStream>
async request (params: ConnectionRequestParams, options: any): Promise<any> {
const maxResponseSize = options.maxResponseSize ?? MAX_STRING_LENGTH
const maxCompressedResponseSize = options.maxCompressedResponseSize ?? MAX_BUFFER_LENGTH
const requestParams = {
method: params.method,
path: params.path + (params.querystring == null || params.querystring === '' ? '' : `?${params.querystring}`),
headers: Object.assign({}, this.headers, params.headers),
body: params.body,
signal: options.signal ?? this[kEmitter]
}
if (requestParams.path[0] !== '/') {
requestParams.path = `/${requestParams.path}`
}
// undici does not support per-request timeouts,
// to address this issue, we default to the constructor
// timeout (which is handled by undici) and create a local
// setTimeout callback if the request-specific timeout
// is different from the constructor timeout.
let timedout = false
let timeoutId
if (options.timeout != null && options.timeout !== this.timeout) {
timeoutId = setTimeout(() => {
timedout = true
if (options.signal != null) {
options.signal.dispatchEvent(new Event('abort'))
} else {
this[kEmitter].emit('abort')
}
}, options.timeout)
}
// https://github.com/nodejs/node/commit/b961d9fd83
if (INVALID_PATH_REGEX.test(requestParams.path)) {
throw new TypeError(`ERR_UNESCAPED_CHARACTERS: ${requestParams.path}`)
}
debug('Starting a new request', params)
let response
try {
// @ts-expect-error method it's fine as string
response = (await this.pool.request(requestParams)) as Dispatcher.ResponseData
if (timeoutId != null) clearTimeout(timeoutId)
} catch (err: any) {
if (timeoutId != null) clearTimeout(timeoutId)
switch (err.code) {
case 'UND_ERR_ABORTED':
case DOMException.ABORT_ERR:
throw (timedout ? new TimeoutError('Request timed out') : new RequestAbortedError('Request aborted'))
case 'UND_ERR_HEADERS_TIMEOUT':
throw new TimeoutError('Request timed out')
case 'UND_ERR_SOCKET':
throw new ConnectionError(`${err.message} - Local: ${err.socket?.localAddress ?? 'unknown'}:${err.socket?.localPort ?? 'unknown'}, Remote: ${err.socket?.remoteAddress ?? 'unknown'}:${err.socket?.remotePort ?? 'unknown'}`) // eslint-disable-line
default:
throw new ConnectionError(err.message)
}
}
if (options.asStream === true) {
return {
statusCode: response.statusCode,
headers: response.headers,
body: response.body
}
}
// @ts-expect-error Assume header is not string[] for now.
const contentEncoding = (response.headers['content-encoding'] ?? '').toLowerCase()
const isCompressed = contentEncoding.includes('gzip') || contentEncoding.includes('deflate') // eslint-disable-line
const isVectorTile = (response.headers['content-type'] ?? '').includes('application/vnd.mapbox-vector-tile')
/* istanbul ignore else */
if (response.headers['content-length'] !== undefined) {
const contentLength = Number(response.headers['content-length'])
if (isCompressed && contentLength > maxCompressedResponseSize) { // eslint-disable-line
response.body.destroy()
throw new RequestAbortedError(`The content length (${contentLength}) is bigger than the maximum allowed buffer (${maxCompressedResponseSize})`)
} else if (contentLength > maxResponseSize) {
response.body.destroy()
throw new RequestAbortedError(`The content length (${contentLength}) is bigger than the maximum allowed string (${maxResponseSize})`)
}
}
this.diagnostic.emit('deserialization', null, options)
try {
if (isCompressed || isVectorTile) { // eslint-disable-line
let currentLength = 0
const payload: Buffer[] = []
for await (const chunk of response.body) {
currentLength += Buffer.byteLength(chunk)
if (currentLength > maxCompressedResponseSize) {
response.body.destroy()
throw new RequestAbortedError(`The content length (${currentLength}) is bigger than the maximum allowed buffer (${maxCompressedResponseSize})`)
}
payload.push(chunk)
}
return {
statusCode: response.statusCode,
headers: response.headers,
body: Buffer.concat(payload)
}
} else {
let payload = ''
let currentLength = 0
response.body.setEncoding('utf8')
for await (const chunk of response.body) {
currentLength += Buffer.byteLength(chunk)
if (currentLength > maxResponseSize) {
response.body.destroy()
throw new RequestAbortedError(`The content length (${currentLength}) is bigger than the maximum allowed string (${maxResponseSize})`)
}
payload += chunk as string
}
return {
statusCode: response.statusCode,
headers: response.headers,
body: payload
}
}
} catch (err: any) {
if (err.name === 'RequestAbortedError') {
throw err
}
throw new ConnectionError(err.message)
}
}
async close (): Promise<void> {
debug('Closing connection', this.id)
await this.pool.close()
}
}
/* istanbul ignore next */
function isUndiciAgentOptions (opts: Record<string, any>): opts is UndiciAgentOptions {
if (opts.keepAlive != null) return false
if (opts.keepAliveMsecs != null) return false
if (opts.maxSockets != null) return false
if (opts.maxFreeSockets != null) return false
if (opts.scheduling != null) return false
if (opts.proxy != null) return false
return true
}
function isTlsSocket (opts: buildConnector.Options, socket: Socket | TLSSocket | null): socket is TLSSocket {
return socket !== null && opts.protocol === 'https:'
}