@jayfong/x-server 2.12.5 → 2.12.7
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/lib/_cjs/services/request.js +84 -19
- package/lib/services/request.d.ts +49 -9
- package/lib/services/request.js +76 -18
- package/package.json +1 -1
|
@@ -3,26 +3,51 @@
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
4
|
|
|
5
5
|
exports.__esModule = true;
|
|
6
|
-
exports.RequestService = exports.RequestFormUrlencoded = exports.RequestFormStream = exports.RequestFormData = void 0;
|
|
6
|
+
exports.RequestService = exports.RequestFormUrlencoded = exports.RequestFormStream = exports.RequestFormFile = exports.RequestFormData = void 0;
|
|
7
|
+
|
|
8
|
+
var _assert = _interopRequireDefault(require("assert"));
|
|
7
9
|
|
|
8
10
|
var _formData = _interopRequireDefault(require("form-data"));
|
|
9
11
|
|
|
10
12
|
var _formstream = _interopRequireDefault(require("formstream"));
|
|
11
13
|
|
|
14
|
+
var _fs = _interopRequireDefault(require("fs"));
|
|
15
|
+
|
|
12
16
|
var _got = _interopRequireDefault(require("got"));
|
|
13
17
|
|
|
18
|
+
var _vtils = require("vtils");
|
|
19
|
+
|
|
14
20
|
var _proxyAgent = require("proxy-agent");
|
|
15
21
|
|
|
22
|
+
var _stream = require("stream");
|
|
23
|
+
|
|
16
24
|
class RequestFormUrlencoded extends URLSearchParams {}
|
|
17
25
|
|
|
18
26
|
exports.RequestFormUrlencoded = RequestFormUrlencoded;
|
|
19
27
|
|
|
20
28
|
class RequestFormData extends _formData.default {
|
|
21
|
-
constructor(
|
|
29
|
+
constructor(cbOrObject) {
|
|
22
30
|
super();
|
|
23
31
|
|
|
24
|
-
if (
|
|
25
|
-
|
|
32
|
+
if (cbOrObject) {
|
|
33
|
+
if (typeof cbOrObject === 'function') {
|
|
34
|
+
cbOrObject(this);
|
|
35
|
+
} else {
|
|
36
|
+
Object.keys(cbOrObject).forEach(key => {
|
|
37
|
+
const value = cbOrObject[key];
|
|
38
|
+
|
|
39
|
+
if (value instanceof RequestFormFile) {
|
|
40
|
+
var _value$options, _value$options2;
|
|
41
|
+
|
|
42
|
+
this.append(key, typeof value.file === 'string' ? _fs.default.createReadStream(value.file) : value.file, {
|
|
43
|
+
filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
|
|
44
|
+
contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
|
|
45
|
+
});
|
|
46
|
+
} else {
|
|
47
|
+
this.append(key, value);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
26
51
|
}
|
|
27
52
|
}
|
|
28
53
|
|
|
@@ -31,11 +56,31 @@ class RequestFormData extends _formData.default {
|
|
|
31
56
|
exports.RequestFormData = RequestFormData;
|
|
32
57
|
|
|
33
58
|
class RequestFormStream extends _formstream.default {
|
|
34
|
-
constructor(
|
|
59
|
+
constructor(cbOrObject) {
|
|
35
60
|
super();
|
|
36
61
|
|
|
37
|
-
if (
|
|
38
|
-
|
|
62
|
+
if (cbOrObject) {
|
|
63
|
+
if (typeof cbOrObject === 'function') {
|
|
64
|
+
cbOrObject(this);
|
|
65
|
+
} else {
|
|
66
|
+
Object.keys(cbOrObject).forEach(key => {
|
|
67
|
+
const value = cbOrObject[key];
|
|
68
|
+
|
|
69
|
+
if (value instanceof RequestFormFile) {
|
|
70
|
+
if (typeof value.file === 'string') {
|
|
71
|
+
var _value$options3;
|
|
72
|
+
|
|
73
|
+
this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
|
|
74
|
+
} else if (Buffer.isBuffer(value.file)) {
|
|
75
|
+
this.buffer(key, value.file, value.options.name, value.options.type);
|
|
76
|
+
} else if (value.file instanceof _stream.Readable) {
|
|
77
|
+
this.stream(key, value.file, value.options.name, value.options.type);
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
this.field(key, value);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
39
84
|
}
|
|
40
85
|
}
|
|
41
86
|
|
|
@@ -43,16 +88,30 @@ class RequestFormStream extends _formstream.default {
|
|
|
43
88
|
|
|
44
89
|
exports.RequestFormStream = RequestFormStream;
|
|
45
90
|
|
|
46
|
-
class
|
|
47
|
-
constructor(options) {
|
|
91
|
+
class RequestFormFile {
|
|
92
|
+
constructor(file, options) {
|
|
93
|
+
this.file = file;
|
|
48
94
|
this.options = options;
|
|
49
|
-
|
|
95
|
+
|
|
96
|
+
if (Buffer.isBuffer(file) || file instanceof _stream.Readable) {
|
|
97
|
+
(0, _assert.default)(options == null ? void 0 : options.name, 'options.name is required');
|
|
98
|
+
}
|
|
50
99
|
}
|
|
51
100
|
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
exports.RequestFormFile = RequestFormFile;
|
|
104
|
+
|
|
105
|
+
class RequestService {
|
|
52
106
|
static getGotOptions(options, responseType) {
|
|
107
|
+
var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
|
|
108
|
+
|
|
53
109
|
const gotOptions = {
|
|
110
|
+
url: options.url,
|
|
54
111
|
responseType: responseType === 'stream' ? undefined : responseType,
|
|
55
|
-
|
|
112
|
+
https: {
|
|
113
|
+
rejectUnauthorized: false
|
|
114
|
+
},
|
|
56
115
|
isStream: responseType === 'stream'
|
|
57
116
|
};
|
|
58
117
|
|
|
@@ -79,16 +138,18 @@ class RequestService {
|
|
|
79
138
|
};
|
|
80
139
|
}
|
|
81
140
|
|
|
82
|
-
if (options.encoding) {
|
|
83
|
-
gotOptions.encoding = options.encoding;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
141
|
if (options.cookieJar) {
|
|
87
142
|
gotOptions.cookieJar = options.cookieJar;
|
|
143
|
+
} // 头处理
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
gotOptions.headers = (0, _vtils.mapKeys)(gotOptions.headers || {}, (v, k) => k.toLowerCase());
|
|
147
|
+
(_gotOptions$headers$_ = (_gotOptions$headers = gotOptions.headers)[_userAgent = 'user-agent']) != null ? _gotOptions$headers$_ : _gotOptions$headers[_userAgent] = (_options$userAgent = options.userAgent) != null ? _options$userAgent : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36';
|
|
148
|
+
|
|
149
|
+
if (options.autoReferer && gotOptions.headers['referer'] == null) {
|
|
150
|
+
gotOptions.headers['referer'] = options.url;
|
|
88
151
|
}
|
|
89
152
|
|
|
90
|
-
gotOptions.headers = gotOptions.headers || {};
|
|
91
|
-
gotOptions.headers['User-Agent'] = options.userAgent || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36';
|
|
92
153
|
return gotOptions;
|
|
93
154
|
}
|
|
94
155
|
|
|
@@ -104,8 +165,8 @@ class RequestService {
|
|
|
104
165
|
url = _url.toString();
|
|
105
166
|
}
|
|
106
167
|
|
|
168
|
+
options.url = url;
|
|
107
169
|
const res = (0, _got.default)({
|
|
108
|
-
url: url,
|
|
109
170
|
method: 'GET',
|
|
110
171
|
...this.getGotOptions(options, responseType || 'buffer')
|
|
111
172
|
});
|
|
@@ -129,7 +190,6 @@ class RequestService {
|
|
|
129
190
|
}
|
|
130
191
|
|
|
131
192
|
const res = (0, _got.default)({
|
|
132
|
-
url: options.url,
|
|
133
193
|
method: 'POST',
|
|
134
194
|
...(options.data == null ? {
|
|
135
195
|
body: ''
|
|
@@ -186,6 +246,11 @@ class RequestService {
|
|
|
186
246
|
return err instanceof _got.default.TimeoutError;
|
|
187
247
|
}
|
|
188
248
|
|
|
249
|
+
constructor(options) {
|
|
250
|
+
this.options = options;
|
|
251
|
+
this.serviceName = 'request';
|
|
252
|
+
}
|
|
253
|
+
|
|
189
254
|
getRaw(options, responseType) {
|
|
190
255
|
var _this$options;
|
|
191
256
|
|
|
@@ -1,26 +1,51 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
2
3
|
import FormData from 'form-data';
|
|
3
4
|
import FormStream from 'formstream';
|
|
4
5
|
import Request from 'got/dist/source/core';
|
|
5
6
|
import { BaseService } from './base';
|
|
6
7
|
import { CookieJar } from 'tough-cookie';
|
|
8
|
+
import { OmitStrict } from 'vtils/types';
|
|
9
|
+
import { Readable } from 'stream';
|
|
7
10
|
export interface RequestServiceOptions {
|
|
11
|
+
/**
|
|
12
|
+
* 请求地址
|
|
13
|
+
*/
|
|
14
|
+
url: string;
|
|
15
|
+
/**
|
|
16
|
+
* 代理地址
|
|
17
|
+
*/
|
|
8
18
|
proxyUrl?: string;
|
|
19
|
+
/**
|
|
20
|
+
* 超时毫秒数
|
|
21
|
+
*/
|
|
9
22
|
timeoutMs?: number;
|
|
23
|
+
/**
|
|
24
|
+
* 头
|
|
25
|
+
*/
|
|
10
26
|
headers?: Record<string, any>;
|
|
11
27
|
/**
|
|
28
|
+
* 是否跟随跳转
|
|
29
|
+
*
|
|
12
30
|
* @default true
|
|
13
31
|
*/
|
|
14
32
|
followRedirect?: boolean;
|
|
15
33
|
/**
|
|
16
|
-
*
|
|
34
|
+
* COOKIE 容器
|
|
17
35
|
*/
|
|
18
|
-
encoding?: BufferEncoding;
|
|
19
36
|
cookieJar?: CookieJar;
|
|
20
37
|
/**
|
|
38
|
+
* 用户代理
|
|
39
|
+
*
|
|
21
40
|
* @default "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
|
|
22
41
|
*/
|
|
23
42
|
userAgent?: string;
|
|
43
|
+
/**
|
|
44
|
+
* 是否自动设置来源地址,设为 true 后若未手动设置来源页则将把请求地址设为来源地址
|
|
45
|
+
*
|
|
46
|
+
* @default false
|
|
47
|
+
*/
|
|
48
|
+
autoReferer?: boolean;
|
|
24
49
|
}
|
|
25
50
|
export interface RequestServiceResponse<T> {
|
|
26
51
|
status: number;
|
|
@@ -28,33 +53,47 @@ export interface RequestServiceResponse<T> {
|
|
|
28
53
|
data: T;
|
|
29
54
|
}
|
|
30
55
|
export interface RequestServiceGetOptions extends RequestServiceOptions {
|
|
31
|
-
url: string;
|
|
32
56
|
data?: Record<string, any>;
|
|
33
57
|
}
|
|
34
58
|
export interface RequestServicePostOptions extends RequestServiceOptions {
|
|
35
|
-
url: string;
|
|
36
59
|
data?: Record<string, any> | FormData | URLSearchParams;
|
|
37
60
|
}
|
|
38
61
|
export interface RequestServiceGetStreamOptions extends RequestServiceOptions {
|
|
39
|
-
url: string;
|
|
40
62
|
data?: Record<string, any>;
|
|
41
63
|
}
|
|
42
64
|
export interface RequestServicePostStreamOptions extends RequestServiceOptions {
|
|
43
|
-
url: string;
|
|
44
65
|
data?: Record<string, any> | FormData | URLSearchParams | FormStream;
|
|
45
66
|
}
|
|
46
67
|
export declare class RequestFormUrlencoded extends URLSearchParams {
|
|
47
68
|
}
|
|
48
69
|
export declare class RequestFormData extends FormData {
|
|
49
|
-
constructor(
|
|
70
|
+
constructor(cbOrObject?: ((formData: FormData) => FormData) | Record<string, any>);
|
|
50
71
|
}
|
|
51
72
|
export declare class RequestFormStream extends FormStream {
|
|
52
|
-
constructor(
|
|
73
|
+
constructor(cbOrObject?: ((formStream: FormStream) => FormStream) | Record<string, any>);
|
|
74
|
+
}
|
|
75
|
+
export declare class RequestFormFile {
|
|
76
|
+
file: any;
|
|
77
|
+
options?: {
|
|
78
|
+
name?: string;
|
|
79
|
+
type?: string;
|
|
80
|
+
};
|
|
81
|
+
constructor(file: Buffer, options: {
|
|
82
|
+
name: string;
|
|
83
|
+
type?: string;
|
|
84
|
+
});
|
|
85
|
+
constructor(file: Readable, options: {
|
|
86
|
+
name: string;
|
|
87
|
+
type?: string;
|
|
88
|
+
});
|
|
89
|
+
constructor(file: string, options?: {
|
|
90
|
+
name?: string;
|
|
91
|
+
type?: string;
|
|
92
|
+
});
|
|
53
93
|
}
|
|
54
94
|
export declare class RequestService implements BaseService {
|
|
55
95
|
private options?;
|
|
56
96
|
serviceName: string;
|
|
57
|
-
constructor(options?: RequestServiceOptions);
|
|
58
97
|
private static getGotOptions;
|
|
59
98
|
static getRaw(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<Buffer>>;
|
|
60
99
|
static getRaw(options: RequestServiceGetStreamOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
|
|
@@ -73,6 +112,7 @@ export declare class RequestService implements BaseService {
|
|
|
73
112
|
static getStream(options: RequestServiceGetOptions): Request;
|
|
74
113
|
static postStream(options: RequestServicePostStreamOptions): Request;
|
|
75
114
|
static isTimeoutError(err: any): boolean;
|
|
115
|
+
constructor(options?: OmitStrict<RequestServiceOptions, 'url'>);
|
|
76
116
|
getRaw(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<Buffer>>;
|
|
77
117
|
getRaw(options: RequestServiceGetStreamOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
|
|
78
118
|
getRaw(options: RequestServiceGetStreamOptions, responseType: 'text'): Promise<RequestServiceResponse<string>>;
|
package/lib/services/request.js
CHANGED
|
@@ -1,38 +1,90 @@
|
|
|
1
|
+
import assert from 'assert';
|
|
1
2
|
import FormData from 'form-data';
|
|
2
3
|
import FormStream from 'formstream';
|
|
4
|
+
import fs from 'fs';
|
|
3
5
|
import got from 'got';
|
|
6
|
+
import { mapKeys } from 'vtils';
|
|
4
7
|
import { ProxyAgent } from 'proxy-agent';
|
|
8
|
+
import { Readable } from 'stream';
|
|
5
9
|
export class RequestFormUrlencoded extends URLSearchParams {}
|
|
6
10
|
export class RequestFormData extends FormData {
|
|
7
|
-
constructor(
|
|
11
|
+
constructor(cbOrObject) {
|
|
8
12
|
super();
|
|
9
13
|
|
|
10
|
-
if (
|
|
11
|
-
|
|
14
|
+
if (cbOrObject) {
|
|
15
|
+
if (typeof cbOrObject === 'function') {
|
|
16
|
+
cbOrObject(this);
|
|
17
|
+
} else {
|
|
18
|
+
Object.keys(cbOrObject).forEach(key => {
|
|
19
|
+
const value = cbOrObject[key];
|
|
20
|
+
|
|
21
|
+
if (value instanceof RequestFormFile) {
|
|
22
|
+
var _value$options, _value$options2;
|
|
23
|
+
|
|
24
|
+
this.append(key, typeof value.file === 'string' ? fs.createReadStream(value.file) : value.file, {
|
|
25
|
+
filename: (_value$options = value.options) == null ? void 0 : _value$options.name,
|
|
26
|
+
contentType: (_value$options2 = value.options) == null ? void 0 : _value$options2.type
|
|
27
|
+
});
|
|
28
|
+
} else {
|
|
29
|
+
this.append(key, value);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
12
33
|
}
|
|
13
34
|
}
|
|
14
35
|
|
|
15
36
|
}
|
|
16
37
|
export class RequestFormStream extends FormStream {
|
|
17
|
-
constructor(
|
|
38
|
+
constructor(cbOrObject) {
|
|
18
39
|
super();
|
|
19
40
|
|
|
20
|
-
if (
|
|
21
|
-
|
|
41
|
+
if (cbOrObject) {
|
|
42
|
+
if (typeof cbOrObject === 'function') {
|
|
43
|
+
cbOrObject(this);
|
|
44
|
+
} else {
|
|
45
|
+
Object.keys(cbOrObject).forEach(key => {
|
|
46
|
+
const value = cbOrObject[key];
|
|
47
|
+
|
|
48
|
+
if (value instanceof RequestFormFile) {
|
|
49
|
+
if (typeof value.file === 'string') {
|
|
50
|
+
var _value$options3;
|
|
51
|
+
|
|
52
|
+
this.file(key, value.file, (_value$options3 = value.options) == null ? void 0 : _value$options3.name);
|
|
53
|
+
} else if (Buffer.isBuffer(value.file)) {
|
|
54
|
+
this.buffer(key, value.file, value.options.name, value.options.type);
|
|
55
|
+
} else if (value.file instanceof Readable) {
|
|
56
|
+
this.stream(key, value.file, value.options.name, value.options.type);
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
this.field(key, value);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
22
63
|
}
|
|
23
64
|
}
|
|
24
65
|
|
|
25
66
|
}
|
|
26
|
-
export class
|
|
27
|
-
constructor(options) {
|
|
67
|
+
export class RequestFormFile {
|
|
68
|
+
constructor(file, options) {
|
|
69
|
+
this.file = file;
|
|
28
70
|
this.options = options;
|
|
29
|
-
|
|
71
|
+
|
|
72
|
+
if (Buffer.isBuffer(file) || file instanceof Readable) {
|
|
73
|
+
assert(options == null ? void 0 : options.name, 'options.name is required');
|
|
74
|
+
}
|
|
30
75
|
}
|
|
31
76
|
|
|
77
|
+
}
|
|
78
|
+
export class RequestService {
|
|
32
79
|
static getGotOptions(options, responseType) {
|
|
80
|
+
var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
|
|
81
|
+
|
|
33
82
|
const gotOptions = {
|
|
83
|
+
url: options.url,
|
|
34
84
|
responseType: responseType === 'stream' ? undefined : responseType,
|
|
35
|
-
|
|
85
|
+
https: {
|
|
86
|
+
rejectUnauthorized: false
|
|
87
|
+
},
|
|
36
88
|
isStream: responseType === 'stream'
|
|
37
89
|
};
|
|
38
90
|
|
|
@@ -59,16 +111,18 @@ export class RequestService {
|
|
|
59
111
|
};
|
|
60
112
|
}
|
|
61
113
|
|
|
62
|
-
if (options.encoding) {
|
|
63
|
-
gotOptions.encoding = options.encoding;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
114
|
if (options.cookieJar) {
|
|
67
115
|
gotOptions.cookieJar = options.cookieJar;
|
|
116
|
+
} // 头处理
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
gotOptions.headers = mapKeys(gotOptions.headers || {}, (v, k) => k.toLowerCase());
|
|
120
|
+
(_gotOptions$headers$_ = (_gotOptions$headers = gotOptions.headers)[_userAgent = 'user-agent']) != null ? _gotOptions$headers$_ : _gotOptions$headers[_userAgent] = (_options$userAgent = options.userAgent) != null ? _options$userAgent : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36';
|
|
121
|
+
|
|
122
|
+
if (options.autoReferer && gotOptions.headers['referer'] == null) {
|
|
123
|
+
gotOptions.headers['referer'] = options.url;
|
|
68
124
|
}
|
|
69
125
|
|
|
70
|
-
gotOptions.headers = gotOptions.headers || {};
|
|
71
|
-
gotOptions.headers['User-Agent'] = options.userAgent || 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36';
|
|
72
126
|
return gotOptions;
|
|
73
127
|
}
|
|
74
128
|
|
|
@@ -84,8 +138,8 @@ export class RequestService {
|
|
|
84
138
|
url = _url.toString();
|
|
85
139
|
}
|
|
86
140
|
|
|
141
|
+
options.url = url;
|
|
87
142
|
const res = got({
|
|
88
|
-
url: url,
|
|
89
143
|
method: 'GET',
|
|
90
144
|
...this.getGotOptions(options, responseType || 'buffer')
|
|
91
145
|
});
|
|
@@ -109,7 +163,6 @@ export class RequestService {
|
|
|
109
163
|
}
|
|
110
164
|
|
|
111
165
|
const res = got({
|
|
112
|
-
url: options.url,
|
|
113
166
|
method: 'POST',
|
|
114
167
|
...(options.data == null ? {
|
|
115
168
|
body: ''
|
|
@@ -166,6 +219,11 @@ export class RequestService {
|
|
|
166
219
|
return err instanceof got.TimeoutError;
|
|
167
220
|
}
|
|
168
221
|
|
|
222
|
+
constructor(options) {
|
|
223
|
+
this.options = options;
|
|
224
|
+
this.serviceName = 'request';
|
|
225
|
+
}
|
|
226
|
+
|
|
169
227
|
getRaw(options, responseType) {
|
|
170
228
|
var _this$options;
|
|
171
229
|
|