@jayfong/x-server 2.12.5 → 2.12.6

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.
@@ -11,6 +11,8 @@ var _formstream = _interopRequireDefault(require("formstream"));
11
11
 
12
12
  var _got = _interopRequireDefault(require("got"));
13
13
 
14
+ var _vtils = require("vtils");
15
+
14
16
  var _proxyAgent = require("proxy-agent");
15
17
 
16
18
  class RequestFormUrlencoded extends URLSearchParams {}
@@ -44,13 +46,11 @@ class RequestFormStream extends _formstream.default {
44
46
  exports.RequestFormStream = RequestFormStream;
45
47
 
46
48
  class RequestService {
47
- constructor(options) {
48
- this.options = options;
49
- this.serviceName = 'request';
50
- }
51
-
52
49
  static getGotOptions(options, responseType) {
50
+ var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
51
+
53
52
  const gotOptions = {
53
+ url: options.url,
54
54
  responseType: responseType === 'stream' ? undefined : responseType,
55
55
  rejectUnauthorized: false,
56
56
  isStream: responseType === 'stream'
@@ -79,16 +79,18 @@ class RequestService {
79
79
  };
80
80
  }
81
81
 
82
- if (options.encoding) {
83
- gotOptions.encoding = options.encoding;
84
- }
85
-
86
82
  if (options.cookieJar) {
87
83
  gotOptions.cookieJar = options.cookieJar;
84
+ } // 头处理
85
+
86
+
87
+ gotOptions.headers = (0, _vtils.mapKeys)(gotOptions.headers || {}, (v, k) => k.toLowerCase());
88
+ (_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';
89
+
90
+ if (options.autoReferer && gotOptions.headers['referer'] == null) {
91
+ gotOptions.headers['referer'] = options.url;
88
92
  }
89
93
 
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
94
  return gotOptions;
93
95
  }
94
96
 
@@ -104,8 +106,8 @@ class RequestService {
104
106
  url = _url.toString();
105
107
  }
106
108
 
109
+ options.url = url;
107
110
  const res = (0, _got.default)({
108
- url: url,
109
111
  method: 'GET',
110
112
  ...this.getGotOptions(options, responseType || 'buffer')
111
113
  });
@@ -129,7 +131,6 @@ class RequestService {
129
131
  }
130
132
 
131
133
  const res = (0, _got.default)({
132
- url: options.url,
133
134
  method: 'POST',
134
135
  ...(options.data == null ? {
135
136
  body: ''
@@ -186,6 +187,11 @@ class RequestService {
186
187
  return err instanceof _got.default.TimeoutError;
187
188
  }
188
189
 
190
+ constructor(options) {
191
+ this.options = options;
192
+ this.serviceName = 'request';
193
+ }
194
+
189
195
  getRaw(options, responseType) {
190
196
  var _this$options;
191
197
 
@@ -4,23 +4,46 @@ import FormStream from 'formstream';
4
4
  import Request from 'got/dist/source/core';
5
5
  import { BaseService } from './base';
6
6
  import { CookieJar } from 'tough-cookie';
7
+ import { OmitStrict } from 'vtils/types';
7
8
  export interface RequestServiceOptions {
9
+ /**
10
+ * 请求地址
11
+ */
12
+ url: string;
13
+ /**
14
+ * 代理地址
15
+ */
8
16
  proxyUrl?: string;
17
+ /**
18
+ * 超时毫秒数
19
+ */
9
20
  timeoutMs?: number;
21
+ /**
22
+ * 头
23
+ */
10
24
  headers?: Record<string, any>;
11
25
  /**
26
+ * 是否跟随跳转
27
+ *
12
28
  * @default true
13
29
  */
14
30
  followRedirect?: boolean;
15
31
  /**
16
- * @default "utf-8"
32
+ * COOKIE 容器
17
33
  */
18
- encoding?: BufferEncoding;
19
34
  cookieJar?: CookieJar;
20
35
  /**
36
+ * 用户代理
37
+ *
21
38
  * @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
39
  */
23
40
  userAgent?: string;
41
+ /**
42
+ * 是否自动设置来源地址,设为 true 后若未手动设置来源页则将把请求地址设为来源地址
43
+ *
44
+ * @default false
45
+ */
46
+ autoReferer?: boolean;
24
47
  }
25
48
  export interface RequestServiceResponse<T> {
26
49
  status: number;
@@ -28,19 +51,15 @@ export interface RequestServiceResponse<T> {
28
51
  data: T;
29
52
  }
30
53
  export interface RequestServiceGetOptions extends RequestServiceOptions {
31
- url: string;
32
54
  data?: Record<string, any>;
33
55
  }
34
56
  export interface RequestServicePostOptions extends RequestServiceOptions {
35
- url: string;
36
57
  data?: Record<string, any> | FormData | URLSearchParams;
37
58
  }
38
59
  export interface RequestServiceGetStreamOptions extends RequestServiceOptions {
39
- url: string;
40
60
  data?: Record<string, any>;
41
61
  }
42
62
  export interface RequestServicePostStreamOptions extends RequestServiceOptions {
43
- url: string;
44
63
  data?: Record<string, any> | FormData | URLSearchParams | FormStream;
45
64
  }
46
65
  export declare class RequestFormUrlencoded extends URLSearchParams {
@@ -54,7 +73,6 @@ export declare class RequestFormStream extends FormStream {
54
73
  export declare class RequestService implements BaseService {
55
74
  private options?;
56
75
  serviceName: string;
57
- constructor(options?: RequestServiceOptions);
58
76
  private static getGotOptions;
59
77
  static getRaw(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<Buffer>>;
60
78
  static getRaw(options: RequestServiceGetStreamOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
@@ -73,6 +91,7 @@ export declare class RequestService implements BaseService {
73
91
  static getStream(options: RequestServiceGetOptions): Request;
74
92
  static postStream(options: RequestServicePostStreamOptions): Request;
75
93
  static isTimeoutError(err: any): boolean;
94
+ constructor(options?: OmitStrict<RequestServiceOptions, 'url'>);
76
95
  getRaw(options: RequestServiceGetStreamOptions): Promise<RequestServiceResponse<Buffer>>;
77
96
  getRaw(options: RequestServiceGetStreamOptions, responseType: 'buffer'): Promise<RequestServiceResponse<Buffer>>;
78
97
  getRaw(options: RequestServiceGetStreamOptions, responseType: 'text'): Promise<RequestServiceResponse<string>>;
@@ -1,6 +1,7 @@
1
1
  import FormData from 'form-data';
2
2
  import FormStream from 'formstream';
3
3
  import got from 'got';
4
+ import { mapKeys } from 'vtils';
4
5
  import { ProxyAgent } from 'proxy-agent';
5
6
  export class RequestFormUrlencoded extends URLSearchParams {}
6
7
  export class RequestFormData extends FormData {
@@ -24,13 +25,11 @@ export class RequestFormStream extends FormStream {
24
25
 
25
26
  }
26
27
  export class RequestService {
27
- constructor(options) {
28
- this.options = options;
29
- this.serviceName = 'request';
30
- }
31
-
32
28
  static getGotOptions(options, responseType) {
29
+ var _gotOptions$headers, _userAgent, _gotOptions$headers$_, _options$userAgent;
30
+
33
31
  const gotOptions = {
32
+ url: options.url,
34
33
  responseType: responseType === 'stream' ? undefined : responseType,
35
34
  rejectUnauthorized: false,
36
35
  isStream: responseType === 'stream'
@@ -59,16 +58,18 @@ export class RequestService {
59
58
  };
60
59
  }
61
60
 
62
- if (options.encoding) {
63
- gotOptions.encoding = options.encoding;
64
- }
65
-
66
61
  if (options.cookieJar) {
67
62
  gotOptions.cookieJar = options.cookieJar;
63
+ } // 头处理
64
+
65
+
66
+ gotOptions.headers = mapKeys(gotOptions.headers || {}, (v, k) => k.toLowerCase());
67
+ (_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';
68
+
69
+ if (options.autoReferer && gotOptions.headers['referer'] == null) {
70
+ gotOptions.headers['referer'] = options.url;
68
71
  }
69
72
 
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
73
  return gotOptions;
73
74
  }
74
75
 
@@ -84,8 +85,8 @@ export class RequestService {
84
85
  url = _url.toString();
85
86
  }
86
87
 
88
+ options.url = url;
87
89
  const res = got({
88
- url: url,
89
90
  method: 'GET',
90
91
  ...this.getGotOptions(options, responseType || 'buffer')
91
92
  });
@@ -109,7 +110,6 @@ export class RequestService {
109
110
  }
110
111
 
111
112
  const res = got({
112
- url: options.url,
113
113
  method: 'POST',
114
114
  ...(options.data == null ? {
115
115
  body: ''
@@ -166,6 +166,11 @@ export class RequestService {
166
166
  return err instanceof got.TimeoutError;
167
167
  }
168
168
 
169
+ constructor(options) {
170
+ this.options = options;
171
+ this.serviceName = 'request';
172
+ }
173
+
169
174
  getRaw(options, responseType) {
170
175
  var _this$options;
171
176
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayfong/x-server",
3
- "version": "2.12.5",
3
+ "version": "2.12.6",
4
4
  "license": "ISC",
5
5
  "sideEffects": false,
6
6
  "main": "lib/_cjs/index.js",