xjs-common 8.4.1 → 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 +18 -15
- package/dist/func/u-file.d.ts +25 -0
- package/dist/func/u-file.js +36 -4
- package/dist/prcs/http/http-resolver-context.d.ts +10 -5
- package/dist/prcs/http/http-resolver-context.js +81 -34
- package/dist/prcs/http/http-resolver.d.ts +4 -4
- package/dist/prcs/http/http-resolver.js +2 -1
- package/dist/prcs/http/i-http-client.d.ts +28 -8
- package/package.json +1 -1
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,35 +94,38 @@ 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
|
|
|
101
101
|
(async () => {
|
|
102
|
-
const chromeMajorVersion = 134;
|
|
103
102
|
// can customize logging. (default is console.)
|
|
104
103
|
// const http = new HttpResolver(chromeMajorVersion, logger);
|
|
105
|
-
const http = new HttpResolver(
|
|
104
|
+
const http = new HttpResolver();
|
|
106
105
|
|
|
107
|
-
//
|
|
108
|
-
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;
|
|
109
109
|
|
|
110
110
|
// use proxy by passing the configuration.
|
|
111
111
|
const proxy = { server: "proxy.sample.com", port: 8080, auth: { name: "prx", pass: "****" } }
|
|
112
|
-
|
|
112
|
+
res = await http.post("https://begyyal.net", { proxy });
|
|
113
113
|
|
|
114
|
-
//
|
|
115
|
-
|
|
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 });
|
|
116
|
+
|
|
117
|
+
// download a file when [Content-Disposition: attachment] exists in the response.
|
|
118
|
+
await http.get("https://begyyal.net/a.txt", { downloadPath: "/path/to/store" });
|
|
116
119
|
|
|
117
120
|
// if you want to keep some states of requests (and suppress to randomize), it can create new context to do.
|
|
118
121
|
const context = http.newContext();
|
|
119
|
-
|
|
122
|
+
res = await context.get("https://begyyal.net/1");
|
|
120
123
|
// this request sends with cookies that is set by precedent requests.
|
|
121
124
|
// in POST, payload is treated as json if it is an object.
|
|
122
|
-
|
|
125
|
+
res = await context.post("https://begyyal.net/2", { a: "b" });
|
|
123
126
|
})();
|
|
124
127
|
```
|
|
125
|
-
|
|
128
|
+
### Mark method as transaction.
|
|
126
129
|
**NOTE**: this feature uses decorator, so requires `"experimentalDecorators": true` in tsconfig.
|
|
127
130
|
```ts
|
|
128
131
|
import { transaction, delay } from "xjs-common";
|
|
@@ -147,7 +150,7 @@ class Cls {
|
|
|
147
150
|
console.log(e);
|
|
148
151
|
});
|
|
149
152
|
```
|
|
150
|
-
|
|
153
|
+
### Validate and crop class fields.
|
|
151
154
|
**NOTE**: this feature uses decorator, so requires `"experimentalDecorators": true` in tsconfig.
|
|
152
155
|
**NOTE**: some functionalities in this feature are based on `"useDefineForClassFields": true` in tsconfig.
|
|
153
156
|
this flag is true by default at the target higher than `ES2022`, [here is for more](https://www.typescriptlang.org/tsconfig/#useDefineForClassFields).
|
package/dist/func/u-file.d.ts
CHANGED
|
@@ -1,14 +1,39 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { MaybeArray } from "../const/types";
|
|
3
|
+
interface FileStatus {
|
|
4
|
+
isFile(): boolean;
|
|
5
|
+
isDirectory(): boolean;
|
|
6
|
+
isBlockDevice(): boolean;
|
|
7
|
+
isCharacterDevice(): boolean;
|
|
8
|
+
isSymbolicLink(): boolean;
|
|
9
|
+
isFIFO(): boolean;
|
|
10
|
+
isSocket(): boolean;
|
|
11
|
+
}
|
|
3
12
|
export declare namespace UFile {
|
|
4
13
|
function mkdir(p: MaybeArray<string>): boolean;
|
|
5
14
|
function write(p: MaybeArray<string>, c: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* remove a file. no error if the file to be removed doesn't exist.
|
|
17
|
+
*/
|
|
6
18
|
function rm(p: MaybeArray<string>): void;
|
|
7
19
|
function exists(p: MaybeArray<string>): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* return a file status. if the file of the status doesn't exist, this returns `null`.
|
|
22
|
+
*/
|
|
23
|
+
function status(p: MaybeArray<string>): FileStatus;
|
|
8
24
|
function read(p: MaybeArray<string>): Buffer;
|
|
9
25
|
function read(p: MaybeArray<string>, encoding: BufferEncoding): string;
|
|
10
26
|
function cp(from: MaybeArray<string>, to: MaybeArray<string>): void;
|
|
11
27
|
function mv(from: MaybeArray<string>, to: MaybeArray<string>): void;
|
|
12
28
|
function ls(p: MaybeArray<string>): string[];
|
|
29
|
+
/**
|
|
30
|
+
* check availability to export a file with specified directory and file name.
|
|
31
|
+
* if it doesn't, retry to check after appending incremental number (e.g. `.1`) to the filename.
|
|
32
|
+
* @param dir destination directory path.
|
|
33
|
+
* @param fname file name wanna export to.
|
|
34
|
+
* @returns exportable file path.
|
|
35
|
+
*/
|
|
36
|
+
function reserveFilePath(dir: MaybeArray<string>, fname: string): string;
|
|
13
37
|
function joinPath(...p: MaybeArray<string>[]): string;
|
|
14
38
|
}
|
|
39
|
+
export {};
|
package/dist/func/u-file.js
CHANGED
|
@@ -45,14 +45,27 @@ var UFile;
|
|
|
45
45
|
fs.writeFileSync(joinPath(p), c);
|
|
46
46
|
}
|
|
47
47
|
UFile.write = write;
|
|
48
|
+
/**
|
|
49
|
+
* remove a file. no error if the file to be removed doesn't exist.
|
|
50
|
+
*/
|
|
48
51
|
function rm(p) {
|
|
49
|
-
|
|
52
|
+
const pt = joinPath(p);
|
|
53
|
+
if (fs.existsSync(pt))
|
|
54
|
+
fs.rmSync(pt, { recursive: true });
|
|
50
55
|
}
|
|
51
56
|
UFile.rm = rm;
|
|
52
57
|
function exists(p) {
|
|
53
58
|
return fs.existsSync(joinPath(p));
|
|
54
59
|
}
|
|
55
60
|
UFile.exists = exists;
|
|
61
|
+
/**
|
|
62
|
+
* return a file status. if the file of the status doesn't exist, this returns `null`.
|
|
63
|
+
*/
|
|
64
|
+
function status(p) {
|
|
65
|
+
const pt = joinPath(p);
|
|
66
|
+
return fs.existsSync(pt) ? fs.statSync(pt) : null;
|
|
67
|
+
}
|
|
68
|
+
UFile.status = status;
|
|
56
69
|
function read(p, encoding) {
|
|
57
70
|
const f = joinPath(p);
|
|
58
71
|
if (!fs.existsSync(f))
|
|
@@ -75,12 +88,31 @@ var UFile;
|
|
|
75
88
|
}
|
|
76
89
|
UFile.mv = mv;
|
|
77
90
|
function ls(p) {
|
|
78
|
-
const
|
|
79
|
-
if (!fs.statSync(
|
|
91
|
+
const pt = joinPath(p);
|
|
92
|
+
if (!pt || !fs.statSync(pt).isDirectory())
|
|
80
93
|
throw new xjs_err_1.XjsErr(s_errCode, "Specified path for ls is not directory.");
|
|
81
|
-
return fs.readdirSync(
|
|
94
|
+
return fs.readdirSync(pt);
|
|
82
95
|
}
|
|
83
96
|
UFile.ls = ls;
|
|
97
|
+
/**
|
|
98
|
+
* check availability to export a file with specified directory and file name.
|
|
99
|
+
* if it doesn't, retry to check after appending incremental number (e.g. `.1`) to the filename.
|
|
100
|
+
* @param dir destination directory path.
|
|
101
|
+
* @param fname file name wanna export to.
|
|
102
|
+
* @returns exportable file path.
|
|
103
|
+
*/
|
|
104
|
+
function reserveFilePath(dir, fname) {
|
|
105
|
+
const pt = joinPath(dir);
|
|
106
|
+
if (!pt || !fs.statSync(pt).isDirectory())
|
|
107
|
+
throw new xjs_err_1.XjsErr(s_errCode, "Specified directory path is not directory.");
|
|
108
|
+
if (!fname || fname.match(/[\\/:*?"<>|]/))
|
|
109
|
+
throw new xjs_err_1.XjsErr(s_errCode, "Specified filename is invalid due to empty or including disallowed characters.");
|
|
110
|
+
let dest = joinPath(pt, fname), i = 1;
|
|
111
|
+
while (fs.existsSync(dest))
|
|
112
|
+
dest = joinPath(pt, `${fname}.${i++}`);
|
|
113
|
+
return dest;
|
|
114
|
+
}
|
|
115
|
+
UFile.reserveFilePath = reserveFilePath;
|
|
84
116
|
function joinPath(...p) {
|
|
85
117
|
return path.join(...p.flatMap(u_type_1.UType.takeAsArray));
|
|
86
118
|
}
|
|
@@ -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 {
|
|
@@ -16,29 +16,34 @@ export declare class HttpResolverContext implements IHttpClient {
|
|
|
16
16
|
* request GET to the url.
|
|
17
17
|
* @param url target url.
|
|
18
18
|
* @param op.headers http headers.
|
|
19
|
-
* @param op.ignoreQuery
|
|
19
|
+
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
20
|
+
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
20
21
|
* @returns string encoded by utf-8 as response payload.
|
|
21
22
|
*/
|
|
22
23
|
get(url: string, op?: RequestOption & {
|
|
23
24
|
outerRedirectCount?: number;
|
|
24
|
-
}): Promise<
|
|
25
|
+
}): Promise<HttpResponse>;
|
|
25
26
|
/**
|
|
26
27
|
* request POST to the url.
|
|
27
28
|
* @param url target url.
|
|
28
29
|
* @param payload request payload. if this is an object, it is treated as json.
|
|
29
30
|
* @param op.headers http headers.
|
|
30
|
-
* @param op.ignoreQuery
|
|
31
|
+
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
32
|
+
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
31
33
|
* @returns string encoded by utf-8 as response payload.
|
|
32
34
|
*/
|
|
33
|
-
post(url: string, payload: any, op?: RequestOption): Promise<
|
|
35
|
+
post(url: string, payload: any, op?: RequestOption): Promise<HttpResponse>;
|
|
34
36
|
private createProxyAgent;
|
|
35
37
|
private getIn;
|
|
36
38
|
private postIn;
|
|
37
39
|
private reqHttps;
|
|
40
|
+
private processResponse;
|
|
41
|
+
private resolveDownloadPath;
|
|
38
42
|
private handleRedirect;
|
|
39
43
|
private createCiphers;
|
|
40
44
|
private setCookies;
|
|
41
45
|
private storeCookies;
|
|
42
46
|
private log;
|
|
43
47
|
private warn;
|
|
48
|
+
private error;
|
|
44
49
|
}
|
|
@@ -26,6 +26,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
26
26
|
exports.HttpResolverContext = exports.s_clientMode = void 0;
|
|
27
27
|
const tls = __importStar(require("tls"));
|
|
28
28
|
const zlib = __importStar(require("zlib"));
|
|
29
|
+
const fs = __importStar(require("fs"));
|
|
29
30
|
const url_1 = require("url");
|
|
30
31
|
const https_1 = require("https");
|
|
31
32
|
const http_1 = require("http");
|
|
@@ -35,6 +36,8 @@ const u_http_1 = require("../../func/u-http");
|
|
|
35
36
|
const u_array_1 = require("../../func/u-array");
|
|
36
37
|
const http_method_1 = require("../../const/http-method");
|
|
37
38
|
const u_type_1 = require("../../func/u-type");
|
|
39
|
+
const u_file_1 = require("../../func/u-file");
|
|
40
|
+
const u_string_1 = require("../../func/u-string");
|
|
38
41
|
exports.s_clientMode = {
|
|
39
42
|
nodejs: { id: 0, cipherOrder: null },
|
|
40
43
|
chrome: { id: 1, cipherOrder: [2, 0, 1] },
|
|
@@ -100,7 +103,8 @@ class HttpResolverContext {
|
|
|
100
103
|
* request GET to the url.
|
|
101
104
|
* @param url target url.
|
|
102
105
|
* @param op.headers http headers.
|
|
103
|
-
* @param op.ignoreQuery
|
|
106
|
+
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
107
|
+
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
104
108
|
* @returns string encoded by utf-8 as response payload.
|
|
105
109
|
*/
|
|
106
110
|
async get(url, op) {
|
|
@@ -115,7 +119,8 @@ class HttpResolverContext {
|
|
|
115
119
|
* @param url target url.
|
|
116
120
|
* @param payload request payload. if this is an object, it is treated as json.
|
|
117
121
|
* @param op.headers http headers.
|
|
118
|
-
* @param op.ignoreQuery
|
|
122
|
+
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
123
|
+
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
119
124
|
* @returns string encoded by utf-8 as response payload.
|
|
120
125
|
*/
|
|
121
126
|
async post(url, payload, op) {
|
|
@@ -185,38 +190,7 @@ class HttpResolverContext {
|
|
|
185
190
|
if (this._cookies)
|
|
186
191
|
this.setCookies(params.headers);
|
|
187
192
|
return new Promise((resolve, reject) => {
|
|
188
|
-
const req = (0, https_1.request)(params, (res) =>
|
|
189
|
-
if (res.headers["set-cookie"])
|
|
190
|
-
this.storeCookies(res.headers["set-cookie"]);
|
|
191
|
-
const sc = u_http_1.UHttp.statusCategoryOf(res.statusCode);
|
|
192
|
-
if (sc === 3) {
|
|
193
|
-
this.handleRedirect(res, params.host).then(resolve).catch(reject).finally(() => res.destroy());
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
const bfs = [];
|
|
197
|
-
const contentEncofing = res.headers["content-encoding"]?.toLocaleLowerCase();
|
|
198
|
-
res.on('data', chunk => bfs.push(chunk));
|
|
199
|
-
res.on('end', () => {
|
|
200
|
-
try {
|
|
201
|
-
let retBuf = Buffer.concat(bfs);
|
|
202
|
-
if (contentEncofing == "gzip")
|
|
203
|
-
retBuf = zlib.gunzipSync(retBuf);
|
|
204
|
-
else if (contentEncofing == "br")
|
|
205
|
-
retBuf = zlib.brotliDecompressSync(retBuf);
|
|
206
|
-
const data = retBuf.toString("utf8");
|
|
207
|
-
if (sc !== 2) {
|
|
208
|
-
if (data.trim())
|
|
209
|
-
this.warn(data);
|
|
210
|
-
reject(new xjs_err_1.XjsErr(s_errCode, `Https received a error status ${res.statusCode}`));
|
|
211
|
-
}
|
|
212
|
-
else
|
|
213
|
-
resolve(data);
|
|
214
|
-
}
|
|
215
|
-
catch (e) {
|
|
216
|
-
reject(e);
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
});
|
|
193
|
+
const req = (0, https_1.request)(params, (res) => this.processResponse(resolve, reject, rc, params.host, res));
|
|
220
194
|
req.on('error', reject);
|
|
221
195
|
req.on('timeout', () => {
|
|
222
196
|
req.destroy();
|
|
@@ -227,6 +201,76 @@ class HttpResolverContext {
|
|
|
227
201
|
req.end();
|
|
228
202
|
});
|
|
229
203
|
}
|
|
204
|
+
processResponse(resolve, reject, rc, host, res) {
|
|
205
|
+
if (res.headers["set-cookie"])
|
|
206
|
+
this.storeCookies(res.headers["set-cookie"]);
|
|
207
|
+
const sc = u_http_1.UHttp.statusCategoryOf(res.statusCode);
|
|
208
|
+
if (sc === 3) {
|
|
209
|
+
this.handleRedirect(res, host).then(resolve).catch(reject).finally(() => res.destroy());
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (res.headers["content-disposition"]?.trim().startsWith("attachment")) {
|
|
213
|
+
try {
|
|
214
|
+
const dest = this.resolveDownloadPath(rc.downloadPath, res.headers["content-disposition"]);
|
|
215
|
+
const stream = fs.createWriteStream(dest);
|
|
216
|
+
res.pipe(stream);
|
|
217
|
+
stream.on("finish", () => stream.close());
|
|
218
|
+
resolve({ headers: res.headers });
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
this.error(e);
|
|
222
|
+
reject(new xjs_err_1.XjsErr(s_errCode, "Failed to download a file."));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const bfs = [];
|
|
226
|
+
const contentEncofing = res.headers["content-encoding"]?.toLocaleLowerCase();
|
|
227
|
+
res.on('data', chunk => bfs.push(chunk));
|
|
228
|
+
res.on('end', () => {
|
|
229
|
+
try {
|
|
230
|
+
let retBuf = Buffer.concat(bfs);
|
|
231
|
+
if (contentEncofing == "gzip")
|
|
232
|
+
retBuf = zlib.gunzipSync(retBuf);
|
|
233
|
+
else if (contentEncofing == "br")
|
|
234
|
+
retBuf = zlib.brotliDecompressSync(retBuf);
|
|
235
|
+
const data = retBuf.toString("utf8");
|
|
236
|
+
if (sc !== 2) {
|
|
237
|
+
if (data.trim())
|
|
238
|
+
this.warn(data);
|
|
239
|
+
reject(new xjs_err_1.XjsErr(s_errCode, `Https received a error status ${res.statusCode}`));
|
|
240
|
+
}
|
|
241
|
+
else
|
|
242
|
+
resolve({ payload: data, headers: res.headers });
|
|
243
|
+
}
|
|
244
|
+
catch (e) {
|
|
245
|
+
reject(e);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
resolveDownloadPath(opPath, disposition) {
|
|
250
|
+
const appendFname = (d) => {
|
|
251
|
+
const fname = disposition.split(";")
|
|
252
|
+
.find(f => f.trim().startsWith("filename"))
|
|
253
|
+
?.replace(/^\s+filename\s+=/, "").trim()
|
|
254
|
+
?? u_file_1.UFile.reserveFilePath(d, `xjs-download_${u_string_1.UString.simpleTime()}`);
|
|
255
|
+
return u_file_1.UFile.joinPath(d, fname);
|
|
256
|
+
};
|
|
257
|
+
if (opPath) {
|
|
258
|
+
const st = u_file_1.UFile.status(opPath);
|
|
259
|
+
if (!st || st.isFile()) {
|
|
260
|
+
const pathArray = opPath.split("/");
|
|
261
|
+
pathArray.pop();
|
|
262
|
+
if (!u_file_1.UFile.exists(pathArray))
|
|
263
|
+
throw new xjs_err_1.XjsErr(s_errCode, "Directory of the download path was not found.");
|
|
264
|
+
return opPath;
|
|
265
|
+
}
|
|
266
|
+
if (st.isDirectory()) {
|
|
267
|
+
if (!u_file_1.UFile.exists(opPath))
|
|
268
|
+
throw new xjs_err_1.XjsErr(s_errCode, "Directory of the download path was not found.");
|
|
269
|
+
return appendFname(opPath);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return appendFname("./");
|
|
273
|
+
}
|
|
230
274
|
async handleRedirect(res, host) {
|
|
231
275
|
const rc = this._als.getStore();
|
|
232
276
|
if (!res.headers.location)
|
|
@@ -283,5 +327,8 @@ class HttpResolverContext {
|
|
|
283
327
|
warn(msg) {
|
|
284
328
|
this._l.warn(`[http-resolver] ${msg}`);
|
|
285
329
|
}
|
|
330
|
+
error(msg) {
|
|
331
|
+
this._l.error(`[http-resolver] ${msg}`);
|
|
332
|
+
}
|
|
286
333
|
}
|
|
287
334
|
exports.HttpResolverContext = HttpResolverContext;
|
|
@@ -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[];
|
|
@@ -20,7 +20,7 @@ export declare class HttpResolver implements IHttpClient {
|
|
|
20
20
|
* @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
|
|
21
21
|
* @param _l custom logger. default is `console`.
|
|
22
22
|
*/
|
|
23
|
-
constructor(_baseCmv
|
|
23
|
+
constructor(_baseCmv?: number, _l?: Loggable);
|
|
24
24
|
/**
|
|
25
25
|
* create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
|
|
26
26
|
* @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
|
|
@@ -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
|
}
|
|
@@ -4,6 +4,7 @@ exports.HttpResolver = void 0;
|
|
|
4
4
|
const xjs_err_1 = require("../../obj/xjs-err");
|
|
5
5
|
const http_resolver_context_1 = require("./http-resolver-context");
|
|
6
6
|
const s_cmvRange = 5;
|
|
7
|
+
const s_defaultCmv = 137;
|
|
7
8
|
class HttpResolver {
|
|
8
9
|
_baseCmv;
|
|
9
10
|
_l;
|
|
@@ -11,7 +12,7 @@ class HttpResolver {
|
|
|
11
12
|
* @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
|
|
12
13
|
* @param _l custom logger. default is `console`.
|
|
13
14
|
*/
|
|
14
|
-
constructor(_baseCmv, _l = console) {
|
|
15
|
+
constructor(_baseCmv = s_defaultCmv, _l = console) {
|
|
15
16
|
this._baseCmv = _baseCmv;
|
|
16
17
|
this._l = _l;
|
|
17
18
|
}
|
|
@@ -1,13 +1,31 @@
|
|
|
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;
|
|
6
6
|
proxy?: ProxyConfig;
|
|
7
7
|
}
|
|
8
8
|
export interface RequestOption {
|
|
9
|
-
ignoreQuery?: boolean;
|
|
10
9
|
headers?: OutgoingHttpHeaders;
|
|
10
|
+
/**
|
|
11
|
+
* if true, query part in the `url` is ignored.
|
|
12
|
+
*/
|
|
13
|
+
ignoreQuery?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* destination directory or file path for download. this is only used when `Content-Disposition` header exists.
|
|
16
|
+
* default is current directory of the process with `filename` of the disposition.
|
|
17
|
+
*/
|
|
18
|
+
downloadPath?: string;
|
|
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;
|
|
11
29
|
}
|
|
12
30
|
export interface IHttpClient {
|
|
13
31
|
/**
|
|
@@ -16,13 +34,14 @@ export interface IHttpClient {
|
|
|
16
34
|
* @param op.headers http headers.
|
|
17
35
|
* @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
|
|
18
36
|
* @param op.proxy proxy configuration.
|
|
19
|
-
* @param op.ignoreQuery
|
|
37
|
+
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
38
|
+
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
20
39
|
* @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
|
|
21
|
-
* @returns
|
|
40
|
+
* @returns http response. {@link HttpResponse}
|
|
22
41
|
*/
|
|
23
42
|
get(url: string, op?: RequestOption & ClientOption & {
|
|
24
43
|
redirectAsNewRequest?: boolean;
|
|
25
|
-
}): Promise<
|
|
44
|
+
}): Promise<HttpResponse>;
|
|
26
45
|
/**
|
|
27
46
|
* request POST to the url with new context.
|
|
28
47
|
* @param url target url.
|
|
@@ -30,8 +49,9 @@ export interface IHttpClient {
|
|
|
30
49
|
* @param op.headers http headers.
|
|
31
50
|
* @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
|
|
32
51
|
* @param op.proxy proxy configuration.
|
|
33
|
-
* @param op.ignoreQuery
|
|
34
|
-
* @
|
|
52
|
+
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
53
|
+
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
54
|
+
* @returns http response. {@link HttpResponse}
|
|
35
55
|
*/
|
|
36
|
-
post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<
|
|
56
|
+
post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse>;
|
|
37
57
|
}
|