urllib 2.38.1 → 3.0.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.
- package/README.md +75 -264
- package/package.json +62 -59
- package/src/HttpAgent.ts +72 -0
- package/src/HttpClient.ts +514 -0
- package/src/Request.ts +118 -0
- package/src/Response.ts +41 -0
- package/src/cjs/HttpAgent.d.ts +16 -0
- package/src/cjs/HttpAgent.js +62 -0
- package/src/cjs/HttpAgent.js.map +1 -0
- package/src/cjs/HttpClient.d.ts +39 -0
- package/src/cjs/HttpClient.js +466 -0
- package/src/cjs/HttpClient.js.map +1 -0
- package/src/cjs/Request.d.ts +114 -0
- package/src/cjs/Request.js +3 -0
- package/src/cjs/Request.js.map +1 -0
- package/src/cjs/Response.d.ts +36 -0
- package/src/cjs/Response.js +3 -0
- package/src/cjs/Response.js.map +1 -0
- package/src/cjs/index.d.ts +7 -0
- package/src/cjs/index.js +18 -0
- package/src/cjs/index.js.map +1 -0
- package/src/cjs/package.json +3 -0
- package/src/cjs/utils.d.ts +3 -0
- package/src/cjs/utils.js +56 -0
- package/src/cjs/utils.js.map +1 -0
- package/src/esm/HttpAgent.d.ts +16 -0
- package/src/esm/HttpAgent.js +58 -0
- package/src/esm/HttpAgent.js.map +1 -0
- package/src/esm/HttpClient.d.ts +39 -0
- package/src/esm/HttpClient.js +462 -0
- package/src/esm/HttpClient.js.map +1 -0
- package/src/esm/Request.d.ts +114 -0
- package/src/esm/Request.js +2 -0
- package/src/esm/Request.js.map +1 -0
- package/src/esm/Response.d.ts +36 -0
- package/src/esm/Response.js +2 -0
- package/src/esm/Response.js.map +1 -0
- package/src/esm/index.d.ts +7 -0
- package/src/esm/index.js +13 -0
- package/src/esm/index.js.map +1 -0
- package/src/esm/package.json +3 -0
- package/src/esm/utils.d.ts +3 -0
- package/src/esm/utils.js +51 -0
- package/src/esm/utils.js.map +1 -0
- package/src/index.ts +16 -0
- package/src/utils.ts +53 -0
- package/History.md +0 -804
- package/lib/detect_proxy_agent.js +0 -31
- package/lib/get_proxy_from_uri.js +0 -81
- package/lib/httpclient.js +0 -61
- package/lib/httpclient2.js +0 -83
- package/lib/index.d.ts +0 -279
- package/lib/index.js +0 -21
- package/lib/index.test-d.ts +0 -19
- package/lib/urllib.js +0 -1317
package/src/HttpAgent.ts
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
import dns from 'dns';
|
2
|
+
import { LookupFunction, isIP } from 'net';
|
3
|
+
import {
|
4
|
+
Agent,
|
5
|
+
} from 'undici';
|
6
|
+
import { DispatchHandlers } from 'undici/types/dispatcher';
|
7
|
+
import { BuildOptions } from 'undici/types/connector';
|
8
|
+
|
9
|
+
export type CheckAddressFunction = (ip: string, family: number | string) => boolean;
|
10
|
+
|
11
|
+
export type HttpAgentOptions = {
|
12
|
+
lookup?: LookupFunction;
|
13
|
+
checkAddress?: CheckAddressFunction;
|
14
|
+
connect?: BuildOptions,
|
15
|
+
};
|
16
|
+
|
17
|
+
class IllegalAddressError extends Error {
|
18
|
+
hostname: string;
|
19
|
+
ip: string;
|
20
|
+
family: number;
|
21
|
+
|
22
|
+
constructor(hostname: string, ip: string, family: number) {
|
23
|
+
const message = 'illegal address';
|
24
|
+
super(message);
|
25
|
+
this.name = this.constructor.name;
|
26
|
+
this.hostname = hostname;
|
27
|
+
this.ip = ip;
|
28
|
+
this.family = family;
|
29
|
+
Error.captureStackTrace(this, this.constructor);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
export class HttpAgent extends Agent {
|
34
|
+
#checkAddress?: CheckAddressFunction;
|
35
|
+
|
36
|
+
constructor(options: HttpAgentOptions) {
|
37
|
+
/* eslint node/prefer-promises/dns: off*/
|
38
|
+
const _lookup = options.lookup ?? dns.lookup;
|
39
|
+
const lookup: LookupFunction = (hostname, dnsOptions, callback) => {
|
40
|
+
_lookup(hostname, dnsOptions, (err, address, family) => {
|
41
|
+
if (err) return callback(err, address, family);
|
42
|
+
if (options.checkAddress && !options.checkAddress(address, family)) {
|
43
|
+
err = new IllegalAddressError(hostname, address, family);
|
44
|
+
}
|
45
|
+
callback(err, address, family);
|
46
|
+
});
|
47
|
+
};
|
48
|
+
super({
|
49
|
+
connect: { ...options.connect, lookup },
|
50
|
+
});
|
51
|
+
this.#checkAddress = options.checkAddress;
|
52
|
+
}
|
53
|
+
|
54
|
+
dispatch(options: Agent.DispatchOptions, handler: DispatchHandlers): boolean {
|
55
|
+
if (this.#checkAddress && options.origin) {
|
56
|
+
const originUrl = typeof options.origin === 'string' ? new URL(options.origin) : options.origin;
|
57
|
+
let hostname = originUrl.hostname;
|
58
|
+
// [2001:db8:2de::e13] => 2001:db8:2de::e13
|
59
|
+
if (hostname.startsWith('[') && hostname.endsWith(']')) {
|
60
|
+
hostname = hostname.substring(1, hostname.length - 1);
|
61
|
+
}
|
62
|
+
const family = isIP(hostname);
|
63
|
+
if (family === 4 || family === 6) {
|
64
|
+
// if request hostname is ip, custom lookup won't excute
|
65
|
+
if (!this.#checkAddress(hostname, family)) {
|
66
|
+
throw new IllegalAddressError(hostname, hostname, family);
|
67
|
+
}
|
68
|
+
}
|
69
|
+
}
|
70
|
+
return super.dispatch(options, handler);
|
71
|
+
}
|
72
|
+
}
|
@@ -0,0 +1,514 @@
|
|
1
|
+
import { EventEmitter } from 'events';
|
2
|
+
import { LookupFunction } from 'net';
|
3
|
+
import { debuglog } from 'util';
|
4
|
+
import {
|
5
|
+
createGunzip,
|
6
|
+
createBrotliDecompress,
|
7
|
+
gunzipSync,
|
8
|
+
brotliDecompressSync,
|
9
|
+
} from 'zlib';
|
10
|
+
import { Blob } from 'buffer';
|
11
|
+
import { Readable, pipeline } from 'stream';
|
12
|
+
import stream from 'stream';
|
13
|
+
import { basename } from 'path';
|
14
|
+
import { createReadStream } from 'fs';
|
15
|
+
import { IncomingHttpHeaders } from 'http';
|
16
|
+
import { performance } from 'perf_hooks';
|
17
|
+
import {
|
18
|
+
FormData as FormDataNative,
|
19
|
+
request as undiciRequest,
|
20
|
+
Dispatcher,
|
21
|
+
} from 'undici';
|
22
|
+
import { FormData as FormDataNode } from 'formdata-node';
|
23
|
+
import { FormDataEncoder } from 'form-data-encoder';
|
24
|
+
import createUserAgent from 'default-user-agent';
|
25
|
+
import mime from 'mime-types';
|
26
|
+
import pump from 'pump';
|
27
|
+
import { HttpAgent, CheckAddressFunction } from './HttpAgent';
|
28
|
+
import { RequestURL, RequestOptions, HttpMethod } from './Request';
|
29
|
+
import { HttpClientResponseMeta, HttpClientResponse, ReadableWithMeta } from './Response';
|
30
|
+
import { parseJSON, sleep } from './utils';
|
31
|
+
|
32
|
+
const FormData = FormDataNative ?? FormDataNode;
|
33
|
+
// impl isReadable on Node.js 14
|
34
|
+
const isReadable = stream.isReadable ?? function isReadable(stream: any) {
|
35
|
+
return stream && typeof stream.read === 'function';
|
36
|
+
};
|
37
|
+
// impl promise pipeline on Node.js 14
|
38
|
+
const pipelinePromise = stream.promises?.pipeline ?? function pipeline(...args: any[]) {
|
39
|
+
return new Promise<void>((resolve, reject) => {
|
40
|
+
pump(...args, (err?: Error) => {
|
41
|
+
if (err) return reject(err);
|
42
|
+
resolve();
|
43
|
+
});
|
44
|
+
});
|
45
|
+
};
|
46
|
+
|
47
|
+
function noop() {
|
48
|
+
// noop
|
49
|
+
}
|
50
|
+
|
51
|
+
const MAX_REQURE_ID_VALUE = Math.pow(2, 31) - 10;
|
52
|
+
let globalRequestId = 0;
|
53
|
+
|
54
|
+
const debug = debuglog('urllib');
|
55
|
+
|
56
|
+
export type ClientOptions = {
|
57
|
+
defaultArgs?: RequestOptions;
|
58
|
+
/**
|
59
|
+
* Custom DNS lookup function, default is `dns.lookup`.
|
60
|
+
*/
|
61
|
+
lookup?: LookupFunction;
|
62
|
+
/**
|
63
|
+
* check request address to protect from SSRF and similar attacks.
|
64
|
+
* It receive two arguments(ip and family) and should return true or false to identified the address is legal or not.
|
65
|
+
* It rely on lookup and have the same version requirement.
|
66
|
+
*/
|
67
|
+
checkAddress?: CheckAddressFunction;
|
68
|
+
connect?: {
|
69
|
+
key?: string | Buffer;
|
70
|
+
/**
|
71
|
+
* A string or Buffer containing the certificate key of the client in PEM format.
|
72
|
+
* Notes: This is necessary only if using the client certificate authentication
|
73
|
+
*/
|
74
|
+
cert?: string | Buffer;
|
75
|
+
/**
|
76
|
+
* If true, the server certificate is verified against the list of supplied CAs.
|
77
|
+
* An 'error' event is emitted if verification fails.Default: true.
|
78
|
+
*/
|
79
|
+
rejectUnauthorized?: boolean;
|
80
|
+
},
|
81
|
+
};
|
82
|
+
|
83
|
+
type UndiciRquestOptions = { dispatcher?: Dispatcher } & Omit<Dispatcher.RequestOptions, 'origin' | 'path' | 'method'> & Partial<Pick<Dispatcher.RequestOptions, 'method'>>;
|
84
|
+
|
85
|
+
// https://github.com/octet-stream/form-data
|
86
|
+
class BlobFromStream {
|
87
|
+
#stream;
|
88
|
+
#type;
|
89
|
+
constructor(stream: Readable, type: string) {
|
90
|
+
this.#stream = stream;
|
91
|
+
this.#type = type;
|
92
|
+
}
|
93
|
+
|
94
|
+
stream() {
|
95
|
+
return this.#stream;
|
96
|
+
}
|
97
|
+
|
98
|
+
get type(): string {
|
99
|
+
return this.#type;
|
100
|
+
}
|
101
|
+
|
102
|
+
get [Symbol.toStringTag]() {
|
103
|
+
return 'Blob';
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
class HttpClientRequestTimeoutError extends Error {
|
108
|
+
constructor(timeout: number, options: ErrorOptions) {
|
109
|
+
const message = `Request timeout for ${timeout} ms`;
|
110
|
+
super(message, options);
|
111
|
+
this.name = this.constructor.name;
|
112
|
+
Error.captureStackTrace(this, this.constructor);
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
const HEADER_USER_AGENT = createUserAgent('node-urllib', '3.0.0');
|
117
|
+
|
118
|
+
function getFileName(stream: Readable) {
|
119
|
+
const filePath: string = (stream as any).path;
|
120
|
+
if (filePath) {
|
121
|
+
return basename(filePath);
|
122
|
+
}
|
123
|
+
return '';
|
124
|
+
}
|
125
|
+
|
126
|
+
function defaultIsRetry(response: HttpClientResponse) {
|
127
|
+
return response.status >= 500;
|
128
|
+
}
|
129
|
+
|
130
|
+
function performanceTime(startTime: number) {
|
131
|
+
return Math.floor((performance.now() - startTime) * 1000) / 1000;
|
132
|
+
}
|
133
|
+
|
134
|
+
type RequestContext = {
|
135
|
+
retries: number;
|
136
|
+
};
|
137
|
+
|
138
|
+
export class HttpClient extends EventEmitter {
|
139
|
+
#defaultArgs?: RequestOptions;
|
140
|
+
#dispatcher?: Dispatcher;
|
141
|
+
|
142
|
+
constructor(clientOptions?: ClientOptions) {
|
143
|
+
super();
|
144
|
+
this.#defaultArgs = clientOptions?.defaultArgs;
|
145
|
+
if (clientOptions?.lookup || clientOptions?.checkAddress || clientOptions?.connect) {
|
146
|
+
this.#dispatcher = new HttpAgent({
|
147
|
+
lookup: clientOptions.lookup,
|
148
|
+
checkAddress: clientOptions.checkAddress,
|
149
|
+
connect: clientOptions.connect,
|
150
|
+
});
|
151
|
+
}
|
152
|
+
}
|
153
|
+
|
154
|
+
async request(url: RequestURL, options?: RequestOptions) {
|
155
|
+
return await this.#requestInternal(url, options);
|
156
|
+
}
|
157
|
+
|
158
|
+
async #requestInternal(url: RequestURL, options?: RequestOptions, requestContext?: RequestContext): Promise<HttpClientResponse> {
|
159
|
+
if (globalRequestId >= MAX_REQURE_ID_VALUE) {
|
160
|
+
globalRequestId = 0;
|
161
|
+
}
|
162
|
+
const requestId = ++globalRequestId;
|
163
|
+
|
164
|
+
const requestUrl = typeof url === 'string' ? new URL(url) : url;
|
165
|
+
const args = {
|
166
|
+
retry: 0,
|
167
|
+
...this.#defaultArgs,
|
168
|
+
...options,
|
169
|
+
};
|
170
|
+
requestContext = {
|
171
|
+
retries: 0,
|
172
|
+
...requestContext,
|
173
|
+
};
|
174
|
+
const requestStartTime = performance.now();
|
175
|
+
|
176
|
+
const reqMeta = {
|
177
|
+
requestId,
|
178
|
+
url: requestUrl.href,
|
179
|
+
args,
|
180
|
+
ctx: args.ctx,
|
181
|
+
};
|
182
|
+
// keep urllib createCallbackResponse style
|
183
|
+
const resHeaders: IncomingHttpHeaders = {};
|
184
|
+
const res: HttpClientResponseMeta = {
|
185
|
+
status: -1,
|
186
|
+
statusCode: -1,
|
187
|
+
headers: resHeaders,
|
188
|
+
size: 0,
|
189
|
+
aborted: false,
|
190
|
+
rt: 0,
|
191
|
+
keepAliveSocket: true,
|
192
|
+
requestUrls: [],
|
193
|
+
timing: {
|
194
|
+
waiting: 0,
|
195
|
+
contentDownload: 0,
|
196
|
+
},
|
197
|
+
};
|
198
|
+
|
199
|
+
let headersTimeout = 5000;
|
200
|
+
let bodyTimeout = 5000;
|
201
|
+
if (args.timeout) {
|
202
|
+
if (Array.isArray(args.timeout)) {
|
203
|
+
headersTimeout = args.timeout[0] ?? headersTimeout;
|
204
|
+
bodyTimeout = args.timeout[1] ?? bodyTimeout;
|
205
|
+
} else {
|
206
|
+
headersTimeout = bodyTimeout = args.timeout;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
const method = (args.method ?? 'GET').toUpperCase() as HttpMethod;
|
211
|
+
const headers: IncomingHttpHeaders = {};
|
212
|
+
if (args.headers) {
|
213
|
+
// convert headers to lower-case
|
214
|
+
for (const name in args.headers) {
|
215
|
+
headers[name.toLowerCase()] = args.headers[name];
|
216
|
+
}
|
217
|
+
}
|
218
|
+
// hidden user-agent
|
219
|
+
const hiddenUserAgent = 'user-agent' in headers && !headers['user-agent'];
|
220
|
+
if (hiddenUserAgent) {
|
221
|
+
delete headers['user-agent'];
|
222
|
+
} else if (!headers['user-agent']) {
|
223
|
+
// need to set user-agent
|
224
|
+
headers['user-agent'] = HEADER_USER_AGENT;
|
225
|
+
}
|
226
|
+
// Alias to dataType = 'stream'
|
227
|
+
if (args.streaming || args.customResponse) {
|
228
|
+
args.dataType = 'stream';
|
229
|
+
}
|
230
|
+
if (args.dataType === 'json' && !headers.accept) {
|
231
|
+
headers.accept = 'application/json';
|
232
|
+
}
|
233
|
+
// gzip alias to compressed
|
234
|
+
if (args.gzip && args.compressed !== false) {
|
235
|
+
args.compressed = true;
|
236
|
+
}
|
237
|
+
if (args.compressed && !headers['accept-encoding']) {
|
238
|
+
headers['accept-encoding'] = 'gzip, br';
|
239
|
+
}
|
240
|
+
if (requestContext.retries > 0) {
|
241
|
+
headers['x-urllib-retry'] = `${requestContext.retries}/${args.retry}`;
|
242
|
+
}
|
243
|
+
if (args.auth && !headers.authorization) {
|
244
|
+
headers.authorization = `Basic ${Buffer.from(args.auth).toString('base64')}`;
|
245
|
+
}
|
246
|
+
|
247
|
+
let opaque = args.opaque;
|
248
|
+
try {
|
249
|
+
const requestOptions: UndiciRquestOptions = {
|
250
|
+
method,
|
251
|
+
keepalive: true,
|
252
|
+
maxRedirections: args.maxRedirects ?? 10,
|
253
|
+
headersTimeout,
|
254
|
+
bodyTimeout,
|
255
|
+
opaque,
|
256
|
+
dispatcher: this.#dispatcher,
|
257
|
+
};
|
258
|
+
if (args.followRedirect === false) {
|
259
|
+
requestOptions.maxRedirections = 0;
|
260
|
+
}
|
261
|
+
|
262
|
+
const isGETOrHEAD = requestOptions.method === 'GET' || requestOptions.method === 'HEAD';
|
263
|
+
// alias to args.content
|
264
|
+
if (args.stream && !args.content) {
|
265
|
+
args.content = args.stream;
|
266
|
+
}
|
267
|
+
|
268
|
+
if (args.files) {
|
269
|
+
if (isGETOrHEAD) {
|
270
|
+
requestOptions.method = 'POST';
|
271
|
+
}
|
272
|
+
const formData = new FormData();
|
273
|
+
const uploadFiles: [string, string | Readable | Buffer][] = [];
|
274
|
+
if (Array.isArray(args.files)) {
|
275
|
+
for (const [ index, file ] of args.files.entries()) {
|
276
|
+
const field = index === 0 ? 'file' : `file${index}`;
|
277
|
+
uploadFiles.push([ field, file ]);
|
278
|
+
}
|
279
|
+
} else if (args.files instanceof Readable || isReadable(args.files as any)) {
|
280
|
+
uploadFiles.push([ 'file', args.files as Readable ]);
|
281
|
+
} else if (typeof args.files === 'string' || Buffer.isBuffer(args.files)) {
|
282
|
+
uploadFiles.push([ 'file', args.files ]);
|
283
|
+
} else if (typeof args.files === 'object') {
|
284
|
+
for (const field in args.files) {
|
285
|
+
uploadFiles.push([ field, args.files[field] ]);
|
286
|
+
}
|
287
|
+
}
|
288
|
+
// set normal fields first
|
289
|
+
if (args.data) {
|
290
|
+
for (const field in args.data) {
|
291
|
+
formData.append(field, args.data[field]);
|
292
|
+
}
|
293
|
+
}
|
294
|
+
for (const [ index, [ field, file ]] of uploadFiles.entries()) {
|
295
|
+
if (typeof file === 'string') {
|
296
|
+
// FIXME: support non-ascii filename
|
297
|
+
// const fileName = encodeURIComponent(basename(file));
|
298
|
+
// formData.append(field, await fileFromPath(file, `utf-8''${fileName}`, { type: mime.lookup(fileName) || '' }));
|
299
|
+
const fileName = basename(file);
|
300
|
+
const fileReadable = createReadStream(file);
|
301
|
+
formData.append(field, new BlobFromStream(fileReadable, mime.lookup(fileName) || ''), fileName);
|
302
|
+
} else if (Buffer.isBuffer(file)) {
|
303
|
+
formData.append(field, new Blob([ file ]), `bufferfile${index}`);
|
304
|
+
} else if (file instanceof Readable || isReadable(file as any)) {
|
305
|
+
const fileName = getFileName(file) || `streamfile${index}`;
|
306
|
+
formData.append(field, new BlobFromStream(file, mime.lookup(fileName) || ''), fileName);
|
307
|
+
}
|
308
|
+
}
|
309
|
+
|
310
|
+
if (FormDataNative) {
|
311
|
+
requestOptions.body = formData;
|
312
|
+
} else {
|
313
|
+
// Node.js 14 does not support spec-compliant FormData
|
314
|
+
// https://github.com/octet-stream/form-data#usage
|
315
|
+
const encoder = new FormDataEncoder(formData as any);
|
316
|
+
Object.assign(headers, encoder.headers);
|
317
|
+
// fix "Content-Length":"NaN"
|
318
|
+
delete headers['Content-Length'];
|
319
|
+
requestOptions.body = Readable.from(encoder);
|
320
|
+
}
|
321
|
+
} else if (args.content) {
|
322
|
+
if (!isGETOrHEAD) {
|
323
|
+
// handle content
|
324
|
+
requestOptions.body = args.content;
|
325
|
+
if (args.contentType) {
|
326
|
+
headers['content-type'] = args.contentType;
|
327
|
+
}
|
328
|
+
if (typeof args.content === 'string' && !headers['content-type']) {
|
329
|
+
headers['content-type'] = 'text/plain;charset=UTF-8';
|
330
|
+
}
|
331
|
+
}
|
332
|
+
} else if (args.data) {
|
333
|
+
const isStringOrBufferOrReadable = typeof args.data === 'string'
|
334
|
+
|| Buffer.isBuffer(args.data)
|
335
|
+
|| isReadable(args.data);
|
336
|
+
if (isGETOrHEAD) {
|
337
|
+
if (!isStringOrBufferOrReadable) {
|
338
|
+
for (const field in args.data) {
|
339
|
+
requestUrl.searchParams.append(field, args.data[field]);
|
340
|
+
}
|
341
|
+
}
|
342
|
+
} else {
|
343
|
+
if (isStringOrBufferOrReadable) {
|
344
|
+
requestOptions.body = args.data;
|
345
|
+
} else {
|
346
|
+
if (args.contentType === 'json'
|
347
|
+
|| args.contentType === 'application/json'
|
348
|
+
|| headers['content-type']?.startsWith('application/json')) {
|
349
|
+
requestOptions.body = JSON.stringify(args.data);
|
350
|
+
if (!headers['content-type']) {
|
351
|
+
headers['content-type'] = 'application/json';
|
352
|
+
}
|
353
|
+
} else {
|
354
|
+
headers['content-type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
|
355
|
+
requestOptions.body = new URLSearchParams(args.data).toString();
|
356
|
+
}
|
357
|
+
}
|
358
|
+
}
|
359
|
+
}
|
360
|
+
|
361
|
+
debug('Request#%d %s %s, headers: %j, headersTimeout: %s, bodyTimeout: %s',
|
362
|
+
requestId, requestOptions.method, requestUrl.href, headers, headersTimeout, bodyTimeout);
|
363
|
+
requestOptions.headers = headers;
|
364
|
+
if (this.listenerCount('request') > 0) {
|
365
|
+
this.emit('request', reqMeta);
|
366
|
+
}
|
367
|
+
|
368
|
+
const response = await undiciRequest(requestUrl, requestOptions);
|
369
|
+
opaque = response.opaque;
|
370
|
+
if (args.timing) {
|
371
|
+
res.timing.waiting = performanceTime(requestStartTime);
|
372
|
+
}
|
373
|
+
|
374
|
+
const context = response.context as { history: URL[] };
|
375
|
+
let lastUrl = '';
|
376
|
+
if (context?.history) {
|
377
|
+
for (const urlObject of context?.history) {
|
378
|
+
res.requestUrls.push(urlObject.href);
|
379
|
+
lastUrl = urlObject.href;
|
380
|
+
}
|
381
|
+
} else {
|
382
|
+
res.requestUrls.push(requestUrl.href);
|
383
|
+
lastUrl = requestUrl.href;
|
384
|
+
}
|
385
|
+
const contentEncoding = response.headers['content-encoding'];
|
386
|
+
const isCompressedContent = contentEncoding === 'gzip' || contentEncoding === 'br';
|
387
|
+
|
388
|
+
res.headers = response.headers;
|
389
|
+
res.status = res.statusCode = response.statusCode;
|
390
|
+
if (res.headers['content-length']) {
|
391
|
+
res.size = parseInt(res.headers['content-length']);
|
392
|
+
}
|
393
|
+
|
394
|
+
let data: any = null;
|
395
|
+
let responseBodyStream: ReadableWithMeta | undefined;
|
396
|
+
if (args.dataType === 'stream') {
|
397
|
+
// streaming mode will disable retry
|
398
|
+
args.retry = 0;
|
399
|
+
const meta = {
|
400
|
+
status: res.status,
|
401
|
+
statusCode: res.statusCode,
|
402
|
+
headers: res.headers,
|
403
|
+
};
|
404
|
+
if (isCompressedContent) {
|
405
|
+
// gzip or br
|
406
|
+
const decoder = contentEncoding === 'gzip' ? createGunzip() : createBrotliDecompress();
|
407
|
+
responseBodyStream = Object.assign(pipeline(response.body, decoder, noop), meta);
|
408
|
+
} else {
|
409
|
+
responseBodyStream = Object.assign(response.body, meta);
|
410
|
+
}
|
411
|
+
} else if (args.writeStream) {
|
412
|
+
// streaming mode will disable retry
|
413
|
+
args.retry = 0;
|
414
|
+
if (isCompressedContent) {
|
415
|
+
const decoder = contentEncoding === 'gzip' ? createGunzip() : createBrotliDecompress();
|
416
|
+
await pipelinePromise(response.body, decoder, args.writeStream);
|
417
|
+
} else {
|
418
|
+
await pipelinePromise(response.body, args.writeStream);
|
419
|
+
}
|
420
|
+
} else {
|
421
|
+
// buffer
|
422
|
+
data = Buffer.from(await response.body.arrayBuffer());
|
423
|
+
if (isCompressedContent) {
|
424
|
+
try {
|
425
|
+
data = contentEncoding === 'gzip' ? gunzipSync(data) : brotliDecompressSync(data);
|
426
|
+
} catch (err: any) {
|
427
|
+
if (err.name === 'Error') {
|
428
|
+
err.name = 'UnzipError';
|
429
|
+
}
|
430
|
+
throw err;
|
431
|
+
}
|
432
|
+
}
|
433
|
+
if (args.dataType === 'text') {
|
434
|
+
data = data.toString();
|
435
|
+
} else if (args.dataType === 'json') {
|
436
|
+
if (data.length === 0) {
|
437
|
+
data = null;
|
438
|
+
} else {
|
439
|
+
data = parseJSON(data.toString(), args.fixJSONCtlChars);
|
440
|
+
}
|
441
|
+
}
|
442
|
+
}
|
443
|
+
res.rt = performanceTime(requestStartTime);
|
444
|
+
if (args.timing) {
|
445
|
+
res.timing.contentDownload = res.rt;
|
446
|
+
}
|
447
|
+
|
448
|
+
const clientResponse: HttpClientResponse = {
|
449
|
+
opaque,
|
450
|
+
data,
|
451
|
+
status: res.status,
|
452
|
+
headers: res.headers,
|
453
|
+
url: lastUrl,
|
454
|
+
redirected: res.requestUrls.length > 1,
|
455
|
+
requestUrls: res.requestUrls,
|
456
|
+
res: responseBodyStream ?? res,
|
457
|
+
};
|
458
|
+
|
459
|
+
if (args.retry > 0 && requestContext.retries < args.retry) {
|
460
|
+
const isRetry = args.isRetry ?? defaultIsRetry;
|
461
|
+
if (isRetry(clientResponse)) {
|
462
|
+
if (args.retryDelay) {
|
463
|
+
await sleep(args.retryDelay);
|
464
|
+
}
|
465
|
+
requestContext.retries++;
|
466
|
+
return await this.#requestInternal(url, options, requestContext);
|
467
|
+
}
|
468
|
+
}
|
469
|
+
|
470
|
+
if (this.listenerCount('response') > 0) {
|
471
|
+
this.emit('response', {
|
472
|
+
requestId,
|
473
|
+
error: null,
|
474
|
+
ctx: args.ctx,
|
475
|
+
req: reqMeta,
|
476
|
+
res,
|
477
|
+
});
|
478
|
+
}
|
479
|
+
|
480
|
+
return clientResponse;
|
481
|
+
} catch (e: any) {
|
482
|
+
debug('Request#%d throw error: %s', requestId, e);
|
483
|
+
let err = e;
|
484
|
+
if (err.name === 'HeadersTimeoutError') {
|
485
|
+
err = new HttpClientRequestTimeoutError(headersTimeout, { cause: e });
|
486
|
+
} else if (err.name === 'BodyTimeoutError') {
|
487
|
+
err = new HttpClientRequestTimeoutError(bodyTimeout, { cause: e });
|
488
|
+
}
|
489
|
+
err.opaque = opaque;
|
490
|
+
err.status = res.status;
|
491
|
+
err.headers = res.headers;
|
492
|
+
err.res = res;
|
493
|
+
// make sure requestUrls not empty
|
494
|
+
if (res.requestUrls.length === 0) {
|
495
|
+
res.requestUrls.push(requestUrl.href);
|
496
|
+
}
|
497
|
+
res.rt = performanceTime(requestStartTime);
|
498
|
+
if (args.timing) {
|
499
|
+
res.timing.contentDownload = res.rt;
|
500
|
+
}
|
501
|
+
|
502
|
+
if (this.listenerCount('response') > 0) {
|
503
|
+
this.emit('response', {
|
504
|
+
requestId,
|
505
|
+
error: err,
|
506
|
+
ctx: args.ctx,
|
507
|
+
req: reqMeta,
|
508
|
+
res,
|
509
|
+
});
|
510
|
+
}
|
511
|
+
throw err;
|
512
|
+
}
|
513
|
+
}
|
514
|
+
}
|
package/src/Request.ts
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
import { Readable, Writable } from 'stream';
|
2
|
+
import { IncomingHttpHeaders } from 'http';
|
3
|
+
import type {
|
4
|
+
HttpMethod as UndiciHttpMethod,
|
5
|
+
} from 'undici/types/dispatcher';
|
6
|
+
import type {
|
7
|
+
HttpClientResponse,
|
8
|
+
} from './Response';
|
9
|
+
|
10
|
+
export type HttpMethod = UndiciHttpMethod;
|
11
|
+
|
12
|
+
export type RequestURL = string | URL;
|
13
|
+
|
14
|
+
export type FixJSONCtlCharsHandler = (data: string) => string;
|
15
|
+
export type FixJSONCtlChars = boolean | FixJSONCtlCharsHandler;
|
16
|
+
|
17
|
+
export type RequestOptions = {
|
18
|
+
/** Request method, defaults to GET. Could be GET, POST, DELETE or PUT. Alias 'type'. */
|
19
|
+
method?: HttpMethod | Lowercase<HttpMethod>;
|
20
|
+
/** Data to be sent. Will be stringify automatically. */
|
21
|
+
data?: any;
|
22
|
+
/** Manually set the content of payload. If set, data will be ignored. */
|
23
|
+
content?: string | Buffer | Readable;
|
24
|
+
/**
|
25
|
+
* @deprecated
|
26
|
+
* Stream to be pipe to the remote. If set, data and content will be ignored.
|
27
|
+
* Alias to `content = Readable`
|
28
|
+
*/
|
29
|
+
stream?: Readable;
|
30
|
+
/**
|
31
|
+
* A writable stream to be piped by the response stream.
|
32
|
+
* Responding data will be write to this stream and callback
|
33
|
+
* will be called with data set null after finished writing.
|
34
|
+
*/
|
35
|
+
writeStream?: Writable;
|
36
|
+
/**
|
37
|
+
* The files will send with multipart/form-data format, base on formstream.
|
38
|
+
* If method not set, will use POST method by default.
|
39
|
+
*/
|
40
|
+
files?: Array<Readable | Buffer | string> | Record<string, Readable | Buffer | string> | Readable | Buffer | string;
|
41
|
+
/** Type of request data, could be 'json'. If it's 'json', will auto set Content-Type: 'application/json' header. */
|
42
|
+
contentType?: string;
|
43
|
+
/**
|
44
|
+
* Type of response data. Could be text or json.
|
45
|
+
* If it's text, the callbacked data would be a String.
|
46
|
+
* If it's json, the data of callback would be a parsed JSON Object
|
47
|
+
* and will auto set Accept: 'application/json' header.
|
48
|
+
* Default is 'buffer'.
|
49
|
+
*/
|
50
|
+
dataType?: 'text' | 'json' | 'buffer' | 'stream';
|
51
|
+
/**
|
52
|
+
* @deprecated
|
53
|
+
* Let you get the res object when request connected, default false.
|
54
|
+
* If set to true, `data` will be response readable stream.
|
55
|
+
* Alias to `dataType = 'stream'`
|
56
|
+
*/
|
57
|
+
streaming?: boolean;
|
58
|
+
/**
|
59
|
+
* @deprecated
|
60
|
+
* Alias to `dataType = 'stream'`
|
61
|
+
*/
|
62
|
+
customResponse?: boolean;
|
63
|
+
/** Fix the control characters (U+0000 through U+001F) before JSON parse response. Default is false. */
|
64
|
+
fixJSONCtlChars?: FixJSONCtlChars;
|
65
|
+
/** Request headers. */
|
66
|
+
headers?: IncomingHttpHeaders;
|
67
|
+
/**
|
68
|
+
* Request timeout in milliseconds for connecting phase and response receiving phase.
|
69
|
+
* Defaults to exports.
|
70
|
+
* TIMEOUT, both are 5s. You can use timeout: 5000 to tell urllib use same timeout on two phase or set them seperately such as
|
71
|
+
* timeout: [3000, 5000], which will set connecting timeout to 3s and response 5s.
|
72
|
+
*/
|
73
|
+
timeout?: number | number[];
|
74
|
+
/**
|
75
|
+
* username:password used in HTTP Basic Authorization.
|
76
|
+
* Alias to `headers.authorization = xxx`
|
77
|
+
**/
|
78
|
+
auth?: string;
|
79
|
+
/** follow HTTP 3xx responses as redirects. defaults to true. */
|
80
|
+
followRedirect?: boolean;
|
81
|
+
/** The maximum number of redirects to follow, defaults to 10. */
|
82
|
+
maxRedirects?: number;
|
83
|
+
/** Format the redirect url by your self. Default is url.resolve(from, to). */
|
84
|
+
formatRedirectUrl?: (a: any, b: any) => void;
|
85
|
+
/** Before request hook, you can change every thing here. */
|
86
|
+
beforeRequest?: (...args: any[]) => void;
|
87
|
+
/** Accept `gzip, br` response content and auto decode it, default is false. */
|
88
|
+
compressed?: boolean;
|
89
|
+
/**
|
90
|
+
* @deprecated
|
91
|
+
* Alias to compressed
|
92
|
+
* */
|
93
|
+
gzip?: boolean;
|
94
|
+
/**
|
95
|
+
* @deprecated
|
96
|
+
* Enable timing or not, default is false.
|
97
|
+
* */
|
98
|
+
timing?: boolean;
|
99
|
+
/**
|
100
|
+
* Auto retry times on 5xx response, default is 0. Don't work on streaming request
|
101
|
+
* It's not supported by using retry and writeStream, because the retry request can't stop the stream which is consuming.
|
102
|
+
**/
|
103
|
+
retry?: number;
|
104
|
+
/** Wait a delay(ms) between retries */
|
105
|
+
retryDelay?: number;
|
106
|
+
/**
|
107
|
+
* Determine whether retry, a response object as the first argument.
|
108
|
+
* It will retry when status >= 500 by default. Request error is not included.
|
109
|
+
*/
|
110
|
+
isRetry?: (response: HttpClientResponse) => boolean;
|
111
|
+
/** Default: `null` */
|
112
|
+
opaque?: unknown;
|
113
|
+
/**
|
114
|
+
* @deprecated
|
115
|
+
* Maybe you should use opaque instead
|
116
|
+
*/
|
117
|
+
ctx?: unknown;
|
118
|
+
};
|