@mepkg/maxios 1.1.0 → 2.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/package.json +2 -2
- package/src/maxios.js +13 -8
- package/src/tls-client.js +20 -9
package/package.json
CHANGED
package/src/maxios.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {HttpProxyAgent, HttpsProxyAgent} from "hpagent";
|
|
2
|
-
import {
|
|
3
|
-
import axios from "axios";
|
|
2
|
+
import {cookiesFromResponse, cookiesToHeader} from "melperjs";
|
|
4
3
|
import {isNil, omitBy} from "lodash-es";
|
|
4
|
+
import axios from "axios";
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
export class Maxios {
|
|
@@ -10,13 +10,18 @@ export class Maxios {
|
|
|
10
10
|
this.cookies = cookies;
|
|
11
11
|
this.config = config || {};
|
|
12
12
|
|
|
13
|
-
this.config.responseType = "text";
|
|
13
|
+
this.config.responseType = config.responseType || "text";
|
|
14
14
|
this.config.timeout = config.timeout || 10000;
|
|
15
15
|
|
|
16
16
|
if (this.proxy) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
try {
|
|
18
|
+
const proxyTimeout = this.config.timeout * 0.75 || undefined;
|
|
19
|
+
this.config.httpsAgent = new HttpsProxyAgent({proxy, timeout: proxyTimeout});
|
|
20
|
+
this.config.httpAgent = new HttpProxyAgent({proxy, timeout: proxyTimeout});
|
|
21
|
+
} catch (e) {
|
|
22
|
+
e.message = "Proxy Error | " + e.message;
|
|
23
|
+
throw e;
|
|
24
|
+
}
|
|
20
25
|
}
|
|
21
26
|
this.client = axios.create(this.config);
|
|
22
27
|
|
|
@@ -98,7 +103,7 @@ export class Maxios {
|
|
|
98
103
|
|
|
99
104
|
#responseCookies = (response) => {
|
|
100
105
|
if (!this.cookies) return;
|
|
101
|
-
const parsed =
|
|
106
|
+
const parsed = cookiesFromResponse(response);
|
|
102
107
|
Object.keys(parsed).forEach(key => {
|
|
103
108
|
if (!parsed[key]) {
|
|
104
109
|
delete parsed[key];
|
|
@@ -111,7 +116,7 @@ export class Maxios {
|
|
|
111
116
|
if (!this.cookies) return;
|
|
112
117
|
request.headers = request.headers || {};
|
|
113
118
|
if (!request.headers.cookie && !request.headers.Cookie) {
|
|
114
|
-
const header =
|
|
119
|
+
const header = cookiesToHeader(this.cookies);
|
|
115
120
|
header && (request.headers.cookie = header);
|
|
116
121
|
}
|
|
117
122
|
}
|
package/src/tls-client.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {cookiesFromResponse, cookiesToHeader, Exception} from "melperjs";
|
|
2
2
|
import {isNil, omitBy} from "lodash-es";
|
|
3
3
|
import {maxios} from "./maxios.js";
|
|
4
4
|
|
|
@@ -16,11 +16,21 @@ export class TlsClient {
|
|
|
16
16
|
impersonate = "chrome_windows",
|
|
17
17
|
visit = null,
|
|
18
18
|
maxRedirects = 0,
|
|
19
|
-
|
|
19
|
+
requestBodyFormat = "text",
|
|
20
|
+
responseBodyFormat = "text",
|
|
20
21
|
validateStatus = (status) => (status >= 200 && status <= 299) || (maxRedirects && status >= 300 && status <= 399)
|
|
21
22
|
} = {}, cookies = {}) {
|
|
22
23
|
this.proxy = proxy;
|
|
23
|
-
this.config = {
|
|
24
|
+
this.config = {
|
|
25
|
+
timeout,
|
|
26
|
+
httpVersion,
|
|
27
|
+
impersonate,
|
|
28
|
+
visit,
|
|
29
|
+
maxRedirects,
|
|
30
|
+
requestBodyFormat,
|
|
31
|
+
responseBodyFormat,
|
|
32
|
+
validateStatus
|
|
33
|
+
};
|
|
24
34
|
this.cookies = cookies;
|
|
25
35
|
this.tlsServer = tlsServer;
|
|
26
36
|
if (!this.tlsServer.url) {
|
|
@@ -40,7 +50,8 @@ export class TlsClient {
|
|
|
40
50
|
impersonate,
|
|
41
51
|
visit,
|
|
42
52
|
maxRedirects,
|
|
43
|
-
|
|
53
|
+
requestBodyFormat,
|
|
54
|
+
responseBodyFormat,
|
|
44
55
|
validateStatus
|
|
45
56
|
} = {}) {
|
|
46
57
|
let tlsResponse;
|
|
@@ -67,7 +78,6 @@ export class TlsClient {
|
|
|
67
78
|
}
|
|
68
79
|
url = parsedUrl.toString();
|
|
69
80
|
}
|
|
70
|
-
maxRedirects = maxRedirects ?? this.config.maxRedirects;
|
|
71
81
|
const request = {
|
|
72
82
|
"method": method || "GET",
|
|
73
83
|
"url": url,
|
|
@@ -78,8 +88,9 @@ export class TlsClient {
|
|
|
78
88
|
"proxy": proxy || this.proxy || undefined,
|
|
79
89
|
"impersonate": impersonate || this.config.impersonate,
|
|
80
90
|
"visit": visit || this.config.visit,
|
|
81
|
-
"maxRedirects": maxRedirects,
|
|
82
|
-
"
|
|
91
|
+
"maxRedirects": maxRedirects ?? this.config.maxRedirects,
|
|
92
|
+
"requestBodyFormat": requestBodyFormat || this.config.requestBodyFormat,
|
|
93
|
+
"responseBodyFormat": responseBodyFormat || this.config.responseBodyFormat,
|
|
83
94
|
"key": this.tlsServer.key
|
|
84
95
|
};
|
|
85
96
|
this.#requestCookies(request);
|
|
@@ -171,7 +182,7 @@ export class TlsClient {
|
|
|
171
182
|
|
|
172
183
|
#responseCookies = (response) => {
|
|
173
184
|
if (!this.cookies) return;
|
|
174
|
-
const parsed =
|
|
185
|
+
const parsed = cookiesFromResponse(response);
|
|
175
186
|
Object.keys(parsed).forEach(key => {
|
|
176
187
|
if (!parsed[key]) {
|
|
177
188
|
delete parsed[key];
|
|
@@ -184,7 +195,7 @@ export class TlsClient {
|
|
|
184
195
|
if (!this.cookies) return;
|
|
185
196
|
request.headers = request.headers || {};
|
|
186
197
|
if (!request.headers.cookie && !request.headers.Cookie) {
|
|
187
|
-
const header =
|
|
198
|
+
const header = cookiesToHeader(this.cookies);
|
|
188
199
|
header && (request.headers.cookie = header);
|
|
189
200
|
}
|
|
190
201
|
}
|