xjs-common 8.5.0 → 9.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
CHANGED
|
@@ -11,7 +11,7 @@ npm i xjs-common
|
|
|
11
11
|
**NOTE**: The versions <= `v6.2.0` was unpublished. If you has been used these versions, please update to the version >= `v7.0.0`.
|
|
12
12
|
|
|
13
13
|
# Code example (only part)
|
|
14
|
-
|
|
14
|
+
### Miscellaneous utilities.
|
|
15
15
|
```ts
|
|
16
16
|
import { checkPortAvailability, delay, int2array, UFile, UHttp, retry } from "xjs-common";
|
|
17
17
|
|
|
@@ -41,7 +41,7 @@ import { checkPortAvailability, delay, int2array, UFile, UHttp, retry } from "xj
|
|
|
41
41
|
UFile.mkdir(["path", "to", "dir"]);
|
|
42
42
|
})();
|
|
43
43
|
```
|
|
44
|
-
|
|
44
|
+
### Array utilities.
|
|
45
45
|
```ts
|
|
46
46
|
import { UArray } from "xjs-common";
|
|
47
47
|
|
|
@@ -70,7 +70,7 @@ import { UArray } from "xjs-common";
|
|
|
70
70
|
console.log(UArray.randomPick(ary3));
|
|
71
71
|
})();
|
|
72
72
|
```
|
|
73
|
-
|
|
73
|
+
### String utilities.
|
|
74
74
|
```ts
|
|
75
75
|
import { UString } from "xjs-common";
|
|
76
76
|
|
|
@@ -94,7 +94,7 @@ import { UString } from "xjs-common";
|
|
|
94
94
|
console.log(UString.simpleTime({ date: getJSTDate(), unit: TimeUnit.Day }));
|
|
95
95
|
})();
|
|
96
96
|
```
|
|
97
|
-
|
|
97
|
+
### Enhanced http client.
|
|
98
98
|
```ts
|
|
99
99
|
import { HttpResolver, s_clientMode } from "xjs-common";
|
|
100
100
|
|
|
@@ -103,28 +103,29 @@ import { HttpResolver, s_clientMode } from "xjs-common";
|
|
|
103
103
|
// const http = new HttpResolver(chromeMajorVersion, logger);
|
|
104
104
|
const http = new HttpResolver();
|
|
105
105
|
|
|
106
|
-
//
|
|
107
|
-
let
|
|
106
|
+
// implicitly corresponds to cookies and redirect, and do randomization.
|
|
107
|
+
let res = await http.get("https://begyyal.net");
|
|
108
|
+
const { payload, headers } = res;
|
|
108
109
|
|
|
109
110
|
// use proxy by passing the configuration.
|
|
110
111
|
const proxy = { server: "proxy.sample.com", port: 8080, auth: { name: "prx", pass: "****" } }
|
|
111
|
-
|
|
112
|
+
res = await http.post("https://begyyal.net", { proxy });
|
|
112
113
|
|
|
113
|
-
//
|
|
114
|
-
|
|
114
|
+
// switch tls ciphers order pattern by passing clientMode. (default is random between chrome or firefox.)
|
|
115
|
+
res = await http.get("https://begyyal.net", { mode: s_clientMode.chrome });
|
|
115
116
|
|
|
116
117
|
// download a file when [Content-Disposition: attachment] exists in the response.
|
|
117
118
|
await http.get("https://begyyal.net/a.txt", { downloadPath: "/path/to/store" });
|
|
118
119
|
|
|
119
120
|
// if you want to keep some states of requests (and suppress to randomize), it can create new context to do.
|
|
120
121
|
const context = http.newContext();
|
|
121
|
-
|
|
122
|
+
res = await context.get("https://begyyal.net/1");
|
|
122
123
|
// this request sends with cookies that is set by precedent requests.
|
|
123
124
|
// in POST, payload is treated as json if it is an object.
|
|
124
|
-
|
|
125
|
+
res = await context.post("https://begyyal.net/2", { a: "b" });
|
|
125
126
|
})();
|
|
126
127
|
```
|
|
127
|
-
|
|
128
|
+
### Mark method as transaction.
|
|
128
129
|
**NOTE**: this feature uses decorator, so requires `"experimentalDecorators": true` in tsconfig.
|
|
129
130
|
```ts
|
|
130
131
|
import { transaction, delay } from "xjs-common";
|
|
@@ -149,7 +150,7 @@ class Cls {
|
|
|
149
150
|
console.log(e);
|
|
150
151
|
});
|
|
151
152
|
```
|
|
152
|
-
|
|
153
|
+
### Validate and crop class fields.
|
|
153
154
|
**NOTE**: this feature uses decorator, so requires `"experimentalDecorators": true` in tsconfig.
|
|
154
155
|
**NOTE**: some functionalities in this feature are based on `"useDefineForClassFields": true` in tsconfig.
|
|
155
156
|
this flag is true by default at the target higher than `ES2022`, [here is for more](https://www.typescriptlang.org/tsconfig/#useDefineForClassFields).
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ClientMode } from "./http-resolver";
|
|
2
|
-
import { ClientOption, IHttpClient, RequestOption } from "./i-http-client";
|
|
2
|
+
import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
|
|
3
3
|
import { Loggable } from "../../const/types";
|
|
4
4
|
export declare const s_clientMode: Record<string, ClientMode>;
|
|
5
5
|
export declare class HttpResolverContext implements IHttpClient {
|
|
@@ -22,7 +22,7 @@ export declare class HttpResolverContext implements IHttpClient {
|
|
|
22
22
|
*/
|
|
23
23
|
get(url: string, op?: RequestOption & {
|
|
24
24
|
outerRedirectCount?: number;
|
|
25
|
-
}): Promise<
|
|
25
|
+
}): Promise<HttpResponse>;
|
|
26
26
|
/**
|
|
27
27
|
* request POST to the url.
|
|
28
28
|
* @param url target url.
|
|
@@ -32,7 +32,7 @@ export declare class HttpResolverContext implements IHttpClient {
|
|
|
32
32
|
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
33
33
|
* @returns string encoded by utf-8 as response payload.
|
|
34
34
|
*/
|
|
35
|
-
post(url: string, payload: any, op?: RequestOption): Promise<
|
|
35
|
+
post(url: string, payload: any, op?: RequestOption): Promise<HttpResponse>;
|
|
36
36
|
private createProxyAgent;
|
|
37
37
|
private getIn;
|
|
38
38
|
private postIn;
|
|
@@ -215,7 +215,7 @@ class HttpResolverContext {
|
|
|
215
215
|
const stream = fs.createWriteStream(dest);
|
|
216
216
|
res.pipe(stream);
|
|
217
217
|
stream.on("finish", () => stream.close());
|
|
218
|
-
resolve({});
|
|
218
|
+
resolve({ headers: res.headers });
|
|
219
219
|
}
|
|
220
220
|
catch (e) {
|
|
221
221
|
this.error(e);
|
|
@@ -239,7 +239,7 @@ class HttpResolverContext {
|
|
|
239
239
|
reject(new xjs_err_1.XjsErr(s_errCode, `Https received a error status ${res.statusCode}`));
|
|
240
240
|
}
|
|
241
241
|
else
|
|
242
|
-
resolve(data);
|
|
242
|
+
resolve({ payload: data, headers: res.headers });
|
|
243
243
|
}
|
|
244
244
|
catch (e) {
|
|
245
245
|
reject(e);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Loggable } from "../../const/types";
|
|
2
2
|
import { HttpResolverContext } from "./http-resolver-context";
|
|
3
|
-
import { ClientOption, IHttpClient, RequestOption } from "./i-http-client";
|
|
3
|
+
import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
|
|
4
4
|
export interface ClientMode {
|
|
5
5
|
id: number;
|
|
6
6
|
cipherOrder: number[];
|
|
@@ -30,7 +30,7 @@ export declare class HttpResolver implements IHttpClient {
|
|
|
30
30
|
newContext(op?: ClientOption): HttpResolverContext;
|
|
31
31
|
get(url: string, op?: RequestOption & ClientOption & {
|
|
32
32
|
redirectAsNewRequest?: boolean;
|
|
33
|
-
}): Promise<
|
|
34
|
-
post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<
|
|
33
|
+
}): Promise<HttpResponse>;
|
|
34
|
+
post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse>;
|
|
35
35
|
private fixCmv;
|
|
36
36
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { OutgoingHttpHeaders } from "http";
|
|
2
|
+
import { IncomingHttpHeaders, OutgoingHttpHeaders } from "http";
|
|
3
3
|
import { ClientMode, ProxyConfig } from "./http-resolver";
|
|
4
4
|
export interface ClientOption {
|
|
5
5
|
mode?: ClientMode;
|
|
@@ -17,6 +17,16 @@ export interface RequestOption {
|
|
|
17
17
|
*/
|
|
18
18
|
downloadPath?: string;
|
|
19
19
|
}
|
|
20
|
+
export interface HttpResponse {
|
|
21
|
+
/**
|
|
22
|
+
* http headers in the response.
|
|
23
|
+
*/
|
|
24
|
+
headers?: IncomingHttpHeaders;
|
|
25
|
+
/**
|
|
26
|
+
* string encoded by utf-8 as response payload.
|
|
27
|
+
*/
|
|
28
|
+
payload?: string;
|
|
29
|
+
}
|
|
20
30
|
export interface IHttpClient {
|
|
21
31
|
/**
|
|
22
32
|
* request GET to the url with new context.
|
|
@@ -27,11 +37,11 @@ export interface IHttpClient {
|
|
|
27
37
|
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
28
38
|
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
29
39
|
* @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
|
|
30
|
-
* @returns
|
|
40
|
+
* @returns http response. {@link HttpResponse}
|
|
31
41
|
*/
|
|
32
42
|
get(url: string, op?: RequestOption & ClientOption & {
|
|
33
43
|
redirectAsNewRequest?: boolean;
|
|
34
|
-
}): Promise<
|
|
44
|
+
}): Promise<HttpResponse>;
|
|
35
45
|
/**
|
|
36
46
|
* request POST to the url with new context.
|
|
37
47
|
* @param url target url.
|
|
@@ -41,7 +51,7 @@ export interface IHttpClient {
|
|
|
41
51
|
* @param op.proxy proxy configuration.
|
|
42
52
|
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
43
53
|
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
44
|
-
* @returns
|
|
54
|
+
* @returns http response. {@link HttpResponse}
|
|
45
55
|
*/
|
|
46
|
-
post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<
|
|
56
|
+
post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse>;
|
|
47
57
|
}
|