-
-
Notifications
You must be signed in to change notification settings - Fork 959
/
Copy pathfetch.ts
286 lines (273 loc) · 9.81 KB
/
fetch.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
'use strict'
import decompressResponse from 'decompress-response'
import { http, https } from 'follow-redirects'
import createHttpProxyAgent, { HttpProxyAgent } from 'http-proxy-agent'
import createHttpsProxyAgent, { HttpsProxyAgent } from 'https-proxy-agent'
import { ParsedUrlQueryInput, stringify } from 'querystring'
import { Readable } from 'stream'
import { URL } from 'url'
import { CancellationToken } from '../util/protocol'
import { createLogger } from '../logger'
import { CancellationError } from '../util/errors'
import { objectLiteral } from '../util/is'
import { fs } from '../util/node'
import workspace from '../workspace'
import { toText } from '../util/string'
import { getConditionValue } from '../util'
const logger = createLogger('model-fetch')
export const timeout = getConditionValue(500, 50)
export type ResponseResult = string | Buffer | { [name: string]: any }
export interface ProxyOptions {
proxy: string
proxyStrictSSL?: boolean
proxyAuthorization?: string | null
proxyCA?: string | null
}
export interface FetchOptions {
/**
* Default to 'GET'
*/
method?: string
/**
* Default no timeout
*/
timeout?: number
/**
* Always return buffer instead of parsed response.
*/
buffer?: boolean
/**
* - 'string' for text response content
* - 'object' for json response content
* - 'buffer' for response not text or json
*/
data?: string | { [key: string]: any } | Buffer
/**
* Plain object added as query of url
*/
query?: ParsedUrlQueryInput
headers?: any
/**
* User for http basic auth, should use with password
*/
user?: string
/**
* Password for http basic auth, should use with user
*/
password?: string
}
export function getRequestModule(url: URL): typeof http | typeof https {
return url.protocol === 'https:' ? https : http
}
export function getText(data: any): string | Buffer {
if (typeof data === 'string' || Buffer.isBuffer(data)) return data
return JSON.stringify(data)
}
export function toURL(urlInput: string | URL): URL {
if (urlInput instanceof URL) return urlInput
let url = new URL(urlInput)
if (!['https:', 'http:'].includes(url.protocol)) throw new Error(`Not valid protocol with ${urlInput}, should be http: or https:`)
return url
}
export function toPort(port: number | string | undefined, protocol: string): number {
if (port) {
port = typeof port === 'number' ? port : parseInt(port, 10)
if (!isNaN(port)) return port
}
return protocol.startsWith('https') ? 443 : 80
}
export function getDataType(data: any): string {
if (data === null) return 'null'
if (data === undefined) return 'undefined'
if (typeof data == 'string') return 'string'
if (Buffer.isBuffer(data)) return 'buffer'
if (Array.isArray(data) || objectLiteral(data)) return 'object'
return 'unknown'
}
export function getSystemProxyURI(endpoint: URL, env = process.env): string | null {
let noProxy = env.NO_PROXY ?? env.no_proxy
if (noProxy === '*') {
return null
}
if (noProxy) {
// canonicalize the hostname, so that 'oogle.com' won't match 'google.com'
const hostname = endpoint.hostname.replace(/^\.*/, '.').toLowerCase()
const port = toPort(endpoint.port, endpoint.protocol).toString()
const noProxyList = noProxy.split(',')
for (let i = 0, len = noProxyList.length; i < len; i++) {
let noProxyItem = noProxyList[i].trim().toLowerCase()
// no_proxy can be granular at the port level, which complicates things a bit.
if (noProxyItem.includes(':')) {
let noProxyItemParts = noProxyItem.split(':', 2)
let noProxyHost = noProxyItemParts[0].replace(/^\.*/, '.')
let noProxyPort = noProxyItemParts[1]
if (port == noProxyPort && hostname.endsWith(noProxyHost)) {
return null
}
} else {
noProxyItem = noProxyItem.replace(/^\.*/, '.')
if (hostname.endsWith(noProxyItem)) {
return null
}
}
}
}
let proxyUri: string | null
if (endpoint.protocol === 'http:') {
proxyUri = env.HTTP_PROXY || env.http_proxy || null
} else {
proxyUri = env.HTTPS_PROXY || env.https_proxy || env.HTTP_PROXY || env.http_proxy || null
}
return proxyUri
}
export function getAgent(endpoint: URL, options: ProxyOptions): HttpsProxyAgent | HttpProxyAgent {
let proxy = options.proxy || getSystemProxyURI(endpoint)
if (proxy) {
let proxyURL: URL
try {
proxyURL = new URL(proxy)
if (!/^https?:$/.test(proxyURL.protocol)) return null
} catch (e) {
return null
}
let opts = {
host: proxyURL.hostname,
port: toPort(proxyURL.port, proxyURL.protocol),
auth: proxyURL.username ? `${proxyURL.username}:${toText(proxyURL.password)}` : undefined,
rejectUnauthorized: typeof options.proxyStrictSSL === 'boolean' ? options.proxyStrictSSL : true
}
logger.info(`Using proxy ${proxy} from ${options.proxy ? 'configuration' : 'system environment'} for ${endpoint.hostname}:`)
return endpoint.protocol === 'http:' ? createHttpProxyAgent(opts) : createHttpsProxyAgent(opts)
}
return null
}
export function resolveRequestOptions(url: URL, options: FetchOptions): any {
let config = workspace.getConfiguration('http', null)
let dataType = getDataType(options.data)
let proxyOptions: ProxyOptions = {
proxy: config.get<string>('proxy', ''),
proxyStrictSSL: config.get<boolean>('proxyStrictSSL', true),
proxyAuthorization: config.get<string | null>('proxyAuthorization', null),
proxyCA: config.get<string | null>('proxyCA', null)
}
if (options.query && !url.search) {
url.search = `?${stringify(options.query)}`
}
let agent = getAgent(url, proxyOptions)
let opts: any = {
method: options.method ?? 'GET',
hostname: url.hostname,
port: toPort(url.port, url.protocol),
path: url.pathname + url.search,
agent,
rejectUnauthorized: proxyOptions.proxyStrictSSL,
maxRedirects: 3,
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64)',
'Accept-Encoding': 'gzip, deflate',
...(options.headers ?? {})
}
}
if (dataType == 'object') {
opts.headers['Content-Type'] = 'application/json'
} else if (dataType == 'string') {
opts.headers['Content-Type'] = 'text/plain'
}
if (proxyOptions.proxyAuthorization) opts.headers['Proxy-Authorization'] = proxyOptions.proxyAuthorization
if (proxyOptions.proxyCA) opts.ca = fs.readFileSync(proxyOptions.proxyCA)
if (options.user) opts.auth = options.user + ':' + (toText(options.password))
if (url.username) opts.auth = url.username + ':' + (toText(url.password))
if (options.timeout) opts.timeout = options.timeout
if (options.buffer) opts.buffer = true
return opts
}
export function request(url: URL, data: any, opts: any, token?: CancellationToken): Promise<ResponseResult> {
let mod = getRequestModule(url)
return new Promise<ResponseResult>((resolve, reject) => {
if (token) {
let disposable = token.onCancellationRequested(() => {
disposable.dispose()
req.destroy(new CancellationError())
})
}
let timer: NodeJS.Timeout
const req = mod.request(opts, res => {
let readable: Readable = res
if ((res.statusCode >= 200 && res.statusCode < 300) || res.statusCode === 1223) {
let headers = res.headers
let chunks: Buffer[] = []
let contentType: string = toText(headers['content-type'])
readable = decompressResponse(res)
readable.on('data', chunk => {
chunks.push(chunk)
})
readable.on('end', () => {
clearTimeout(timer)
let buf = Buffer.concat(chunks)
if (!opts.buffer && (contentType.startsWith('application/json') || contentType.startsWith('text/'))) {
let ms = contentType.match(/charset=(\S+)/)
let encoding = ms ? ms[1] : 'utf8'
let rawData = buf.toString(encoding as BufferEncoding)
if (!contentType.includes('application/json')) {
resolve(rawData)
} else {
try {
const parsedData = JSON.parse(rawData)
resolve(parsedData)
} catch (e) {
reject(new Error(`Parse response error: ${e}`))
}
}
} else {
resolve(buf)
}
})
readable.on('error', err => {
reject(new Error(`Connection error to ${url}: ${err.message}`))
})
} else {
reject(new Error(`Bad response from ${url}: ${res.statusCode}`))
}
})
req.on('error', e => {
// Possible succeed proxy request with ECONNRESET error on node > 14
if (opts.agent && e['code'] == 'ECONNRESET') {
timer = setTimeout(() => {
reject(e)
}, timeout)
} else {
reject(e)
}
})
req.on('timeout', () => {
req.destroy(new Error(`Request timeout after ${opts.timeout}ms`))
})
if (data) req.write(getText(data))
if (opts.timeout) req.setTimeout(opts.timeout)
req.end()
})
}
/**
* Send request to server for response, supports:
*
* - Send json data and parse json response.
* - Throw error for failed response statusCode.
* - Timeout support (no timeout by default).
* - Send buffer (as data) and receive data (as response).
* - Proxy support from user configuration & environment.
* - Redirect support, limited to 3.
* - Support of gzip & deflate response content.
*/
export default function fetch(urlInput: string | URL, options: FetchOptions = {}, token?: CancellationToken): Promise<ResponseResult> {
let url = toURL(urlInput)
let opts = resolveRequestOptions(url, options)
return request(url, options.data, opts, token).catch(err => {
logger.error(`Fetch error for ${url}:`, opts, err)
if (opts.agent && opts.agent.proxy) {
let { proxy } = opts.agent
throw new Error(`Request failed using proxy ${proxy.host}: ${err.message}`)
} else {
throw err
}
})
}