urllib 2.37.3 → 3.0.0-alpha.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 +117 -212
- package/package.json +56 -54
- package/src/HttpClient.ts +274 -0
- package/src/Request.ts +116 -0
- package/src/Response.ts +20 -0
- package/src/cjs/HttpClient.d.ts +31 -0
- package/src/cjs/HttpClient.js +270 -0
- package/src/cjs/HttpClient.js.map +1 -0
- package/src/cjs/Request.d.ts +117 -0
- package/src/cjs/Request.js +3 -0
- package/src/cjs/Request.js.map +1 -0
- package/src/cjs/Response.d.ts +20 -0
- package/src/cjs/Response.js +3 -0
- package/src/cjs/Response.js.map +1 -0
- package/src/cjs/index.d.ts +27 -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/esm/HttpClient.d.ts +31 -0
- package/src/esm/HttpClient.js +266 -0
- package/src/esm/HttpClient.js.map +1 -0
- package/src/esm/Request.d.ts +117 -0
- package/src/esm/Request.js +2 -0
- package/src/esm/Request.js.map +1 -0
- package/src/esm/Response.d.ts +20 -0
- package/src/esm/Response.js +2 -0
- package/src/esm/Response.js.map +1 -0
- package/src/esm/index.d.ts +27 -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/index.ts +17 -0
- package/History.md +0 -776
- 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 -267
- package/lib/index.js +0 -21
- package/lib/urllib.js +0 -1305
@@ -0,0 +1,266 @@
|
|
1
|
+
var _BlobFromStream_stream, _BlobFromStream_type;
|
2
|
+
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
3
|
+
import { EventEmitter } from 'events';
|
4
|
+
import { debuglog } from 'util';
|
5
|
+
import { Readable, isReadable } from 'stream';
|
6
|
+
import { Blob } from 'buffer';
|
7
|
+
import { createReadStream } from 'fs';
|
8
|
+
import { basename } from 'path';
|
9
|
+
import { fetch, Headers, FormData, } from 'undici';
|
10
|
+
import createUserAgent from 'default-user-agent';
|
11
|
+
import mime from 'mime-types';
|
12
|
+
const debug = debuglog('urllib');
|
13
|
+
// https://github.com/octet-stream/form-data
|
14
|
+
class BlobFromStream {
|
15
|
+
constructor(stream, type) {
|
16
|
+
_BlobFromStream_stream.set(this, void 0);
|
17
|
+
_BlobFromStream_type.set(this, void 0);
|
18
|
+
__classPrivateFieldSet(this, _BlobFromStream_stream, stream, "f");
|
19
|
+
__classPrivateFieldSet(this, _BlobFromStream_type, type, "f");
|
20
|
+
}
|
21
|
+
stream() {
|
22
|
+
return __classPrivateFieldGet(this, _BlobFromStream_stream, "f");
|
23
|
+
}
|
24
|
+
get type() {
|
25
|
+
return __classPrivateFieldGet(this, _BlobFromStream_type, "f");
|
26
|
+
}
|
27
|
+
get [(_BlobFromStream_stream = new WeakMap(), _BlobFromStream_type = new WeakMap(), Symbol.toStringTag)]() {
|
28
|
+
return 'Blob';
|
29
|
+
}
|
30
|
+
}
|
31
|
+
class HttpClientRequestTimeoutError extends Error {
|
32
|
+
constructor(timeout, options) {
|
33
|
+
const message = `Request timeout for ${timeout} ms`;
|
34
|
+
super(message, options);
|
35
|
+
this.name = this.constructor.name;
|
36
|
+
Error.captureStackTrace(this, this.constructor);
|
37
|
+
}
|
38
|
+
}
|
39
|
+
const HEADER_USER_AGENT = createUserAgent('node-urllib', '3.0.0');
|
40
|
+
function getFileName(stream) {
|
41
|
+
const filePath = stream.path;
|
42
|
+
if (filePath) {
|
43
|
+
return basename(filePath);
|
44
|
+
}
|
45
|
+
return '';
|
46
|
+
}
|
47
|
+
export class HttpClient extends EventEmitter {
|
48
|
+
constructor(clientOptions) {
|
49
|
+
super();
|
50
|
+
this.defaultArgs = clientOptions === null || clientOptions === void 0 ? void 0 : clientOptions.defaultArgs;
|
51
|
+
}
|
52
|
+
async request(url, options) {
|
53
|
+
var _a, _b, _c, _d;
|
54
|
+
const requestUrl = typeof url === 'string' ? new URL(url) : url;
|
55
|
+
const args = {
|
56
|
+
...this.defaultArgs,
|
57
|
+
...options,
|
58
|
+
emitter: this,
|
59
|
+
};
|
60
|
+
const requestStartTime = Date.now();
|
61
|
+
// keep urllib createCallbackResponse style
|
62
|
+
const resHeaders = {};
|
63
|
+
const res = {
|
64
|
+
status: -1,
|
65
|
+
statusCode: -1,
|
66
|
+
statusMessage: '',
|
67
|
+
headers: resHeaders,
|
68
|
+
size: 0,
|
69
|
+
aborted: false,
|
70
|
+
rt: 0,
|
71
|
+
keepAliveSocket: true,
|
72
|
+
requestUrls: [url.toString()],
|
73
|
+
timing: {
|
74
|
+
contentDownload: 0,
|
75
|
+
},
|
76
|
+
// remoteAddress: remoteAddress,
|
77
|
+
// remotePort: remotePort,
|
78
|
+
// socketHandledRequests: socketHandledRequests,
|
79
|
+
// socketHandledResponses: socketHandledResponses,
|
80
|
+
};
|
81
|
+
let requestTimeout = 5000;
|
82
|
+
if (args.timeout) {
|
83
|
+
if (Array.isArray(args.timeout)) {
|
84
|
+
requestTimeout = (_a = args.timeout[args.timeout.length - 1]) !== null && _a !== void 0 ? _a : requestTimeout;
|
85
|
+
}
|
86
|
+
else {
|
87
|
+
requestTimeout = args.timeout;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
const requestTimeoutController = new AbortController();
|
91
|
+
const requestTimerId = setTimeout(() => requestTimeoutController.abort(), requestTimeout);
|
92
|
+
const method = ((_b = args.method) !== null && _b !== void 0 ? _b : 'GET').toUpperCase();
|
93
|
+
try {
|
94
|
+
const headers = new Headers((_c = args.headers) !== null && _c !== void 0 ? _c : {});
|
95
|
+
// don't set user-agent
|
96
|
+
const disableUserAgent = args.headers &&
|
97
|
+
(args.headers['User-Agent'] === null || args.headers['user-agent'] === null);
|
98
|
+
if (!disableUserAgent && !headers.has('user-agent')) {
|
99
|
+
// need to set user-agent
|
100
|
+
headers.set('user-agent', HEADER_USER_AGENT);
|
101
|
+
}
|
102
|
+
if (args.dataType === 'json' && !headers.has('accept')) {
|
103
|
+
headers.set('accept', 'application/json');
|
104
|
+
}
|
105
|
+
const requestOptions = {
|
106
|
+
method,
|
107
|
+
keepalive: true,
|
108
|
+
signal: requestTimeoutController.signal,
|
109
|
+
};
|
110
|
+
if (args.followRedirect === false) {
|
111
|
+
requestOptions.redirect = 'manual';
|
112
|
+
}
|
113
|
+
const isGETOrHEAD = requestOptions.method === 'GET' || requestOptions.method === 'HEAD';
|
114
|
+
if (args.files) {
|
115
|
+
if (isGETOrHEAD) {
|
116
|
+
requestOptions.method = 'POST';
|
117
|
+
}
|
118
|
+
const formData = new FormData();
|
119
|
+
const uploadFiles = [];
|
120
|
+
if (Array.isArray(args.files)) {
|
121
|
+
for (const [index, file] of args.files.entries()) {
|
122
|
+
const field = index === 0 ? 'file' : `file${index}`;
|
123
|
+
uploadFiles.push([field, file]);
|
124
|
+
}
|
125
|
+
}
|
126
|
+
else if (args.files instanceof Readable || isReadable(args.files)) {
|
127
|
+
uploadFiles.push(['file', args.files]);
|
128
|
+
}
|
129
|
+
else if (typeof args.files === 'string' || Buffer.isBuffer(args.files)) {
|
130
|
+
uploadFiles.push(['file', args.files]);
|
131
|
+
}
|
132
|
+
else if (typeof args.files === 'object') {
|
133
|
+
for (const field in args.files) {
|
134
|
+
uploadFiles.push([field, args.files[field]]);
|
135
|
+
}
|
136
|
+
}
|
137
|
+
// set normal fields first
|
138
|
+
if (args.data) {
|
139
|
+
for (const field in args.data) {
|
140
|
+
formData.append(field, args.data[field]);
|
141
|
+
}
|
142
|
+
}
|
143
|
+
for (const [index, [field, file]] of uploadFiles.entries()) {
|
144
|
+
if (typeof file === 'string') {
|
145
|
+
// FIXME: support non-ascii filename
|
146
|
+
// const fileName = encodeURIComponent(basename(file));
|
147
|
+
// formData.append(field, await fileFromPath(file, `utf-8''${fileName}`, { type: mime.lookup(fileName) || '' }));
|
148
|
+
const fileName = basename(file);
|
149
|
+
const fileReader = createReadStream(file);
|
150
|
+
formData.append(field, new BlobFromStream(fileReader, mime.lookup(fileName) || ''), fileName);
|
151
|
+
}
|
152
|
+
else if (Buffer.isBuffer(file)) {
|
153
|
+
formData.append(field, new Blob([file]), `bufferfile${index}`);
|
154
|
+
}
|
155
|
+
else if (file instanceof Readable || isReadable(file)) {
|
156
|
+
const fileName = getFileName(file) || `streamfile${index}`;
|
157
|
+
formData.append(field, new BlobFromStream(file, mime.lookup(fileName) || ''), fileName);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
requestOptions.body = formData;
|
161
|
+
}
|
162
|
+
else if (args.content) {
|
163
|
+
if (!isGETOrHEAD) {
|
164
|
+
if (isReadable(args.content)) {
|
165
|
+
// disable keepalive
|
166
|
+
requestOptions.keepalive = false;
|
167
|
+
}
|
168
|
+
// handle content
|
169
|
+
requestOptions.body = args.content;
|
170
|
+
if (args.contentType) {
|
171
|
+
headers.set('content-type', args.contentType);
|
172
|
+
}
|
173
|
+
}
|
174
|
+
}
|
175
|
+
else if (args.data) {
|
176
|
+
const isStringOrBufferOrReadable = typeof args.data === 'string'
|
177
|
+
|| Buffer.isBuffer(args.data)
|
178
|
+
|| isReadable(args.data);
|
179
|
+
if (isGETOrHEAD) {
|
180
|
+
if (!isStringOrBufferOrReadable) {
|
181
|
+
for (const field in args.data) {
|
182
|
+
requestUrl.searchParams.append(field, args.data[field]);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
}
|
186
|
+
else {
|
187
|
+
if (isStringOrBufferOrReadable) {
|
188
|
+
if (isReadable(args.data)) {
|
189
|
+
// disable keepalive
|
190
|
+
requestOptions.keepalive = false;
|
191
|
+
}
|
192
|
+
requestOptions.body = args.data;
|
193
|
+
}
|
194
|
+
else {
|
195
|
+
if (args.contentType === 'json'
|
196
|
+
|| args.contentType === 'application/json'
|
197
|
+
|| ((_d = headers.get('content-type')) === null || _d === void 0 ? void 0 : _d.startsWith('application/json'))) {
|
198
|
+
requestOptions.body = JSON.stringify(args.data);
|
199
|
+
if (!headers.has('content-type')) {
|
200
|
+
headers.set('content-type', 'application/json');
|
201
|
+
}
|
202
|
+
}
|
203
|
+
else {
|
204
|
+
requestOptions.body = new URLSearchParams(args.data);
|
205
|
+
}
|
206
|
+
}
|
207
|
+
}
|
208
|
+
}
|
209
|
+
debug('%s %s, headers: %j, timeout: %s', requestOptions.method, url, headers, requestTimeout);
|
210
|
+
requestOptions.headers = headers;
|
211
|
+
const response = await fetch(requestUrl, requestOptions);
|
212
|
+
for (const [name, value] of response.headers) {
|
213
|
+
res.headers[name] = value;
|
214
|
+
}
|
215
|
+
res.status = res.statusCode = response.status;
|
216
|
+
res.statusMessage = response.statusText;
|
217
|
+
if (response.redirected) {
|
218
|
+
res.requestUrls.push(response.url);
|
219
|
+
}
|
220
|
+
if (res.headers['content-length']) {
|
221
|
+
res.size = parseInt(res.headers['content-length']);
|
222
|
+
}
|
223
|
+
let data;
|
224
|
+
if (args.streaming || args.dataType === 'stream') {
|
225
|
+
data = response.body;
|
226
|
+
}
|
227
|
+
else if (args.dataType === 'text') {
|
228
|
+
data = await response.text();
|
229
|
+
}
|
230
|
+
else if (args.dataType === 'json') {
|
231
|
+
if (requestOptions.method === 'HEAD') {
|
232
|
+
data = {};
|
233
|
+
}
|
234
|
+
else {
|
235
|
+
data = await response.json();
|
236
|
+
}
|
237
|
+
}
|
238
|
+
else {
|
239
|
+
// buffer
|
240
|
+
data = Buffer.from(await response.arrayBuffer());
|
241
|
+
}
|
242
|
+
res.rt = res.timing.contentDownload = Date.now() - requestStartTime;
|
243
|
+
return {
|
244
|
+
status: res.status,
|
245
|
+
data,
|
246
|
+
headers: res.headers,
|
247
|
+
url: response.url,
|
248
|
+
redirected: response.redirected,
|
249
|
+
res,
|
250
|
+
};
|
251
|
+
}
|
252
|
+
catch (e) {
|
253
|
+
let err = e;
|
254
|
+
if (requestTimeoutController.signal.aborted) {
|
255
|
+
err = new HttpClientRequestTimeoutError(requestTimeout, { cause: e });
|
256
|
+
}
|
257
|
+
err.res = res;
|
258
|
+
// console.error(err);
|
259
|
+
throw err;
|
260
|
+
}
|
261
|
+
finally {
|
262
|
+
clearTimeout(requestTimerId);
|
263
|
+
}
|
264
|
+
}
|
265
|
+
}
|
266
|
+
//# sourceMappingURL=HttpClient.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"HttpClient.js","sourceRoot":"","sources":["../HttpClient.ts"],"names":[],"mappings":";;AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,IAAI,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AAChC,OAAO,EACL,KAAK,EAAe,OAAO,EAAE,QAAQ,GACtC,MAAM,QAAQ,CAAC;AAChB,OAAO,eAAe,MAAM,oBAAoB,CAAC;AACjD,OAAO,IAAI,MAAM,YAAY,CAAC;AAG9B,MAAM,KAAK,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;AAMjC,4CAA4C;AAC5C,MAAM,cAAc;IAGlB,YAAY,MAAgB,EAAE,IAAY;QAF1C,yCAAQ;QACR,uCAAM;QAEJ,uBAAA,IAAI,0BAAW,MAAM,MAAA,CAAC;QACtB,uBAAA,IAAI,wBAAS,IAAI,MAAA,CAAC;IACpB,CAAC;IAED,MAAM;QACJ,OAAO,uBAAA,IAAI,8BAAQ,CAAC;IACtB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,uBAAA,IAAI,4BAAM,CAAC;IACpB,CAAC;IAED,IAAI,gFAAC,MAAM,CAAC,WAAW,EAAC;QACtB,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAED,M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,OAAO;gBACpB,GAAG,EAAE,QAAQ,CAAC,GAAG;gBACjB,UAAU,EAAE,QAAQ,CAAC,UAAU;gBAC/B,GAAG;aACJ,CAAC;SACH;QAAC,OAAO,CAAM,EAAE;YACf,IAAI,GAAG,GAAG,CAAC,CAAC;YACZ,IAAI,wBAAwB,CAAC,MAAM,CAAC,OAAO,EAAE;gBAC3C,GAAG,GAAG,IAAI,6BAA6B,CAAC,cAAc,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;aACvE;YACD,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC;YACd,sBAAsB;YACtB,MAAM,GAAG,CAAC;SACX;gBAAS;YACR,YAAY,CAAC,cAAc,CAAC,CAAC;SAC9B;IACH,CAAC;CACF"}
|
@@ -0,0 +1,117 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
/// <reference types="node" />
|
3
|
+
/// <reference types="node" />
|
4
|
+
/// <reference types="node" />
|
5
|
+
import { Readable, Writable } from 'stream';
|
6
|
+
import { LookupFunction } from 'net';
|
7
|
+
export declare type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'PUT' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'TRACE' | 'CONNECT';
|
8
|
+
export declare type RequestURL = string | URL;
|
9
|
+
export declare type RequestOptions = {
|
10
|
+
/** Request method, defaults to GET. Could be GET, POST, DELETE or PUT. Alias 'type'. */
|
11
|
+
method?: HttpMethod | Lowercase<HttpMethod>;
|
12
|
+
/** Data to be sent. Will be stringify automatically. */
|
13
|
+
data?: any;
|
14
|
+
/** Force convert data to query string. */
|
15
|
+
dataAsQueryString?: boolean;
|
16
|
+
/** Manually set the content of payload. If set, data will be ignored. */
|
17
|
+
content?: string | Buffer | Readable;
|
18
|
+
/** Stream to be pipe to the remote. If set, data and content will be ignored. */
|
19
|
+
stream?: Readable;
|
20
|
+
/**
|
21
|
+
* A writable stream to be piped by the response stream.
|
22
|
+
* Responding data will be write to this stream and callback
|
23
|
+
* will be called with data set null after finished writing.
|
24
|
+
*/
|
25
|
+
writeStream?: Writable;
|
26
|
+
/** consume the writeStream, invoke the callback after writeStream close. */
|
27
|
+
consumeWriteStream?: boolean;
|
28
|
+
/**
|
29
|
+
* The files will send with multipart/form-data format, base on formstream.
|
30
|
+
* If method not set, will use POST method by default.
|
31
|
+
*/
|
32
|
+
files?: Array<Readable | Buffer | string> | Record<string, Readable | Buffer | string> | Readable | Buffer | string;
|
33
|
+
/** Type of request data, could be 'json'. If it's 'json', will auto set Content-Type: 'application/json' header. */
|
34
|
+
contentType?: string;
|
35
|
+
/**
|
36
|
+
* Type of response data. Could be text or json.
|
37
|
+
* If it's text, the callbacked data would be a String.
|
38
|
+
* If it's json, the data of callback would be a parsed JSON Object
|
39
|
+
* and will auto set Accept: 'application/json' header.
|
40
|
+
* Default is buffer.
|
41
|
+
*/
|
42
|
+
dataType?: 'text' | 'json' | 'buffer' | 'stream';
|
43
|
+
/**
|
44
|
+
* Let you get the res object when request connected, default false.
|
45
|
+
* If set to true, `data` will be response readable stream.
|
46
|
+
* Equal to `dataType = 'stream'`
|
47
|
+
*/
|
48
|
+
streaming?: boolean;
|
49
|
+
/** Fix the control characters (U+0000 through U+001F) before JSON parse response. Default is false. */
|
50
|
+
fixJSONCtlChars?: boolean;
|
51
|
+
/** Request headers. */
|
52
|
+
headers?: Record<string, string>;
|
53
|
+
/**
|
54
|
+
* Request timeout in milliseconds for connecting phase and response receiving phase.
|
55
|
+
* Defaults to exports.
|
56
|
+
* TIMEOUT, both are 5s. You can use timeout: 5000 to tell urllib use same timeout on two phase or set them seperately such as
|
57
|
+
* timeout: [3000, 5000], which will set connecting timeout to 3s and response 5s.
|
58
|
+
*/
|
59
|
+
timeout?: number | number[];
|
60
|
+
/** username:password used in HTTP Basic Authorization. */
|
61
|
+
auth?: string;
|
62
|
+
/** username:password used in HTTP Digest Authorization. */
|
63
|
+
digestAuth?: string;
|
64
|
+
/**
|
65
|
+
* An array of strings or Buffers of trusted certificates.
|
66
|
+
* If this is omitted several well known "root" CAs will be used, like VeriSign.
|
67
|
+
* These are used to authorize connections.
|
68
|
+
* Notes: This is necessary only if the server uses the self - signed certificate
|
69
|
+
*/
|
70
|
+
ca?: string | Buffer | string[] | Buffer[];
|
71
|
+
/**
|
72
|
+
* If true, the server certificate is verified against the list of supplied CAs.
|
73
|
+
* An 'error' event is emitted if verification fails.Default: true.
|
74
|
+
*/
|
75
|
+
rejectUnauthorized?: boolean;
|
76
|
+
/** A string or Buffer containing the private key, certificate and CA certs of the server in PFX or PKCS12 format. */
|
77
|
+
pfx?: string | Buffer;
|
78
|
+
/**
|
79
|
+
* A string or Buffer containing the private key of the client in PEM format.
|
80
|
+
* Notes: This is necessary only if using the client certificate authentication
|
81
|
+
*/
|
82
|
+
key?: string | Buffer;
|
83
|
+
/**
|
84
|
+
* A string or Buffer containing the certificate key of the client in PEM format.
|
85
|
+
* Notes: This is necessary only if using the client certificate authentication
|
86
|
+
*/
|
87
|
+
cert?: string | Buffer;
|
88
|
+
/** A string of passphrase for the private key or pfx. */
|
89
|
+
passphrase?: string;
|
90
|
+
/** A string describing the ciphers to use or exclude. */
|
91
|
+
ciphers?: string;
|
92
|
+
/** The SSL method to use, e.g.SSLv3_method to force SSL version 3. */
|
93
|
+
secureProtocol?: string;
|
94
|
+
/** follow HTTP 3xx responses as redirects. defaults to false. */
|
95
|
+
followRedirect?: boolean;
|
96
|
+
/** The maximum number of redirects to follow, defaults to 10. */
|
97
|
+
maxRedirects?: number;
|
98
|
+
/** Format the redirect url by your self. Default is url.resolve(from, to). */
|
99
|
+
formatRedirectUrl?: (a: any, b: any) => void;
|
100
|
+
/** Before request hook, you can change every thing here. */
|
101
|
+
beforeRequest?: (...args: any[]) => void;
|
102
|
+
/** Accept gzip response content and auto decode it, default is false. */
|
103
|
+
gzip?: boolean;
|
104
|
+
/** Enable timing or not, default is false. */
|
105
|
+
timing?: boolean;
|
106
|
+
/**
|
107
|
+
* Custom DNS lookup function, default is dns.lookup.
|
108
|
+
* Require node >= 4.0.0(for http protocol) and node >=8(for https protocol)
|
109
|
+
*/
|
110
|
+
lookup?: LookupFunction;
|
111
|
+
/**
|
112
|
+
* check request address to protect from SSRF and similar attacks.
|
113
|
+
* It receive two arguments(ip and family) and should return true or false to identified the address is legal or not.
|
114
|
+
* It rely on lookup and have the same version requirement.
|
115
|
+
*/
|
116
|
+
checkAddress?: (ip: string, family: number | string) => boolean;
|
117
|
+
};
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Request.js","sourceRoot":"","sources":["../Request.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
/// <reference types="node" />
|
2
|
+
import { OutgoingHttpHeaders, IncomingMessage } from 'http';
|
3
|
+
export interface HttpClientResponse<T> {
|
4
|
+
data: T;
|
5
|
+
status: number;
|
6
|
+
headers: OutgoingHttpHeaders;
|
7
|
+
res: IncomingMessage & {
|
8
|
+
/**
|
9
|
+
* https://eggjs.org/en/core/httpclient.html#timing-boolean
|
10
|
+
*/
|
11
|
+
timing?: {
|
12
|
+
queuing: number;
|
13
|
+
dnslookup: number;
|
14
|
+
connected: number;
|
15
|
+
requestSent: number;
|
16
|
+
waiting: number;
|
17
|
+
contentDownload: number;
|
18
|
+
};
|
19
|
+
};
|
20
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"Response.js","sourceRoot":"","sources":["../Response.ts"],"names":[],"mappings":""}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { RequestOptions, RequestURL } from './Request';
|
2
|
+
export declare function request(url: RequestURL, options?: RequestOptions): Promise<{
|
3
|
+
status: number;
|
4
|
+
data: any;
|
5
|
+
headers: Record<string, string>;
|
6
|
+
url: string;
|
7
|
+
redirected: boolean;
|
8
|
+
res: {
|
9
|
+
status: number;
|
10
|
+
statusCode: number;
|
11
|
+
statusMessage: string;
|
12
|
+
headers: Record<string, string>;
|
13
|
+
size: number;
|
14
|
+
aborted: boolean;
|
15
|
+
rt: number;
|
16
|
+
keepAliveSocket: boolean;
|
17
|
+
requestUrls: string[];
|
18
|
+
timing: {
|
19
|
+
contentDownload: number;
|
20
|
+
};
|
21
|
+
};
|
22
|
+
}>;
|
23
|
+
export { HttpClient } from './HttpClient';
|
24
|
+
declare const _default: {
|
25
|
+
request: typeof request;
|
26
|
+
};
|
27
|
+
export default _default;
|
package/src/esm/index.js
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
import { HttpClient } from './HttpClient.js';
|
2
|
+
let httpclient;
|
3
|
+
export async function request(url, options) {
|
4
|
+
if (!httpclient) {
|
5
|
+
httpclient = new HttpClient();
|
6
|
+
}
|
7
|
+
return await httpclient.request(url, options);
|
8
|
+
}
|
9
|
+
export { HttpClient } from './HttpClient.js';
|
10
|
+
export default {
|
11
|
+
request,
|
12
|
+
};
|
13
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG1C,IAAI,UAAsB,CAAC;AAC3B,MAAM,CAAC,KAAK,UAAU,OAAO,CAAC,GAAe,EAAE,OAAwB;IACrE,IAAI,CAAC,UAAU,EAAE;QACf,UAAU,GAAG,IAAI,UAAU,EAAE,CAAC;KAC/B;IACD,OAAO,MAAM,UAAU,CAAC,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AAChD,CAAC;AAED,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAE1C,eAAe;IACb,OAAO;CACR,CAAC"}
|
package/src/index.ts
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
import { HttpClient } from './HttpClient';
|
2
|
+
import { RequestOptions, RequestURL } from './Request';
|
3
|
+
|
4
|
+
let httpclient: HttpClient;
|
5
|
+
export async function request(url: RequestURL, options?: RequestOptions) {
|
6
|
+
if (!httpclient) {
|
7
|
+
httpclient = new HttpClient();
|
8
|
+
}
|
9
|
+
return await httpclient.request(url, options);
|
10
|
+
}
|
11
|
+
|
12
|
+
export { HttpClient } from './HttpClient';
|
13
|
+
|
14
|
+
export default {
|
15
|
+
request,
|
16
|
+
};
|
17
|
+
|