xjs-node 2.1.2 → 2.2.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/build/index.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ export * from "./func/u";
|
|
|
2
2
|
export * from "./func/u-file";
|
|
3
3
|
export { HttpResolver, ClientMode } from "./prcs/http-resolver";
|
|
4
4
|
export { s_clientMode } from "./prcs/http-resolver-context";
|
|
5
|
-
export { HttpClient, HttpResponse } from "./obj/http-client";
|
|
5
|
+
export { HttpClient, HttpResponse, ClientOption, RequestOption } from "./obj/http-client";
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { IncomingHttpHeaders, OutgoingHttpHeaders } from "http";
|
|
2
2
|
import { ClientMode, ProxyConfig } from "../prcs/http-resolver";
|
|
3
|
+
import { Loggable } from "xjs-common";
|
|
4
|
+
export type LogLevel = "log" | "warn" | "error";
|
|
3
5
|
export interface ClientOption {
|
|
4
6
|
/**
|
|
5
7
|
* {@link s_clientMode} that is imitated. default is random between chrome or firefox.
|
|
@@ -9,6 +11,18 @@ export interface ClientOption {
|
|
|
9
11
|
* proxy configuration.
|
|
10
12
|
*/
|
|
11
13
|
proxy?: ProxyConfig;
|
|
14
|
+
/**
|
|
15
|
+
* chrome major version refered when construct an user agent.
|
|
16
|
+
*/
|
|
17
|
+
cmv: number;
|
|
18
|
+
/**
|
|
19
|
+
* custom logger. default is `console`.
|
|
20
|
+
*/
|
|
21
|
+
logger: Loggable;
|
|
22
|
+
/**
|
|
23
|
+
* verbosity of logging.
|
|
24
|
+
*/
|
|
25
|
+
logLevel: LogLevel;
|
|
12
26
|
}
|
|
13
27
|
export interface RequestOption {
|
|
14
28
|
headers?: OutgoingHttpHeaders;
|
|
@@ -41,6 +55,14 @@ export interface HttpResponse<T = string> {
|
|
|
41
55
|
payload?: T;
|
|
42
56
|
}
|
|
43
57
|
export interface HttpClient {
|
|
58
|
+
/**
|
|
59
|
+
* {@link s_clientMode} that is imitated. default is random between chrome or firefox.
|
|
60
|
+
*/
|
|
61
|
+
readonly mode?: ClientMode;
|
|
62
|
+
/**
|
|
63
|
+
* chrome major version refered when construct an user agent.
|
|
64
|
+
*/
|
|
65
|
+
readonly cmv: number;
|
|
44
66
|
/**
|
|
45
67
|
* request GET to the url with new context.
|
|
46
68
|
* @param url target url. (currently https only)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import { ClientMode } from "./http-resolver";
|
|
1
2
|
import { ClientOption, HttpResponse, HttpClient, RequestOption } from "../obj/http-client";
|
|
2
|
-
import { Loggable } from "xjs-common";
|
|
3
3
|
export declare const s_clientMode: {
|
|
4
4
|
nodejs: {
|
|
5
5
|
id: number;
|
|
@@ -15,16 +15,16 @@ export declare const s_clientMode: {
|
|
|
15
15
|
};
|
|
16
16
|
};
|
|
17
17
|
export declare class HttpResolverContext implements HttpClient {
|
|
18
|
+
readonly mode: ClientMode;
|
|
18
19
|
readonly cmv: number;
|
|
19
|
-
private _l;
|
|
20
20
|
private readonly _als;
|
|
21
|
-
private readonly
|
|
21
|
+
private readonly _l;
|
|
22
|
+
private readonly _logLevel;
|
|
22
23
|
private readonly _ciphers;
|
|
23
24
|
private readonly _proxyConfig?;
|
|
24
25
|
private readonly _chHeaders;
|
|
25
26
|
private _cookies?;
|
|
26
|
-
|
|
27
|
-
constructor(cmv: number, op?: ClientOption, _l?: Loggable);
|
|
27
|
+
constructor(op?: ClientOption);
|
|
28
28
|
get(url: string, op?: RequestOption & {
|
|
29
29
|
outerRedirectCount?: number;
|
|
30
30
|
responseType: "string";
|
|
@@ -42,6 +42,7 @@ const url_1 = require("url");
|
|
|
42
42
|
const https_1 = require("https");
|
|
43
43
|
const http_1 = require("http");
|
|
44
44
|
const async_hooks_1 = require("async_hooks");
|
|
45
|
+
const http_resolver_1 = require("./http-resolver");
|
|
45
46
|
const u_file_1 = require("../func/u-file");
|
|
46
47
|
const u_1 = require("../func/u");
|
|
47
48
|
const xjs_common_1 = require("xjs-common");
|
|
@@ -88,24 +89,26 @@ const s_mode2headers = new Map([
|
|
|
88
89
|
return ch;
|
|
89
90
|
}]
|
|
90
91
|
]);
|
|
92
|
+
const s_logLevelMap = new Map([["log", 30], ["warn", 20], ["error", 10]]);
|
|
91
93
|
class HttpResolverContext {
|
|
94
|
+
mode;
|
|
92
95
|
cmv;
|
|
93
|
-
_l;
|
|
94
96
|
_als = new async_hooks_1.AsyncLocalStorage();
|
|
95
|
-
|
|
97
|
+
_l;
|
|
98
|
+
_logLevel;
|
|
96
99
|
_ciphers;
|
|
97
100
|
_proxyConfig;
|
|
98
101
|
_chHeaders;
|
|
99
102
|
_cookies;
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
this.cmv = cmv;
|
|
103
|
-
this.
|
|
104
|
-
this.
|
|
105
|
-
this.
|
|
106
|
-
if (this.
|
|
107
|
-
this._ciphers = this.createCiphers(this.
|
|
108
|
-
this._chHeaders = s_mode2headers.get(this.
|
|
103
|
+
constructor(op = http_resolver_1.s_defaultClientOption) {
|
|
104
|
+
this.mode = op.mode ?? xjs_common_1.UArray.randomPick([exports.s_clientMode.chrome, exports.s_clientMode.firefox]);
|
|
105
|
+
this.cmv = op.cmv;
|
|
106
|
+
this._proxyConfig = op.proxy;
|
|
107
|
+
this._l = op.logger;
|
|
108
|
+
this._logLevel = s_logLevelMap.get(op.logLevel);
|
|
109
|
+
if (this.mode.id > 0) {
|
|
110
|
+
this._ciphers = this.createCiphers(this.mode);
|
|
111
|
+
this._chHeaders = s_mode2headers.get(this.mode)(this.cmv);
|
|
109
112
|
}
|
|
110
113
|
}
|
|
111
114
|
async get(url, op) {
|
|
@@ -178,7 +181,7 @@ class HttpResolverContext {
|
|
|
178
181
|
params.host = u.host;
|
|
179
182
|
params.path = (rc.ignoreQuery || !u.search) ? u.pathname : `${u.pathname}${u.search}`;
|
|
180
183
|
params.agent = rc.proxyAgent;
|
|
181
|
-
if (this.
|
|
184
|
+
if (this.mode.id > 0) {
|
|
182
185
|
params.ciphers = this._ciphers;
|
|
183
186
|
params.headers = params.headers ? Object.assign(params.headers, this._chHeaders) : this._chHeaders;
|
|
184
187
|
}
|
|
@@ -325,13 +328,16 @@ class HttpResolverContext {
|
|
|
325
328
|
this.log(JSON.stringify(this._cookies));
|
|
326
329
|
}
|
|
327
330
|
log(msg) {
|
|
328
|
-
|
|
331
|
+
if (s_logLevelMap.get("log") <= this._logLevel)
|
|
332
|
+
this._l.log(`[http-resolver] ${msg}`);
|
|
329
333
|
}
|
|
330
334
|
warn(msg) {
|
|
331
|
-
|
|
335
|
+
if (s_logLevelMap.get("warn") <= this._logLevel)
|
|
336
|
+
this._l.warn(`[http-resolver] ${msg}`);
|
|
332
337
|
}
|
|
333
338
|
error(msg) {
|
|
334
|
-
|
|
339
|
+
if (s_logLevelMap.get("error") <= this._logLevel)
|
|
340
|
+
this._l.error(`[http-resolver] ${msg}`);
|
|
335
341
|
}
|
|
336
342
|
}
|
|
337
343
|
exports.HttpResolverContext = HttpResolverContext;
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Loggable } from "xjs-common";
|
|
2
|
-
import { HttpResolverContext } from "./http-resolver-context";
|
|
3
1
|
import { ClientOption, HttpResponse, HttpClient, RequestOption } from "../obj/http-client";
|
|
4
2
|
export interface ClientMode {
|
|
5
3
|
id: number;
|
|
@@ -13,21 +11,20 @@ export interface ProxyConfig {
|
|
|
13
11
|
pass: string;
|
|
14
12
|
};
|
|
15
13
|
}
|
|
14
|
+
export declare const s_defaultClientOption: ClientOption;
|
|
16
15
|
export declare class HttpResolver implements HttpClient {
|
|
17
|
-
private
|
|
18
|
-
|
|
16
|
+
private readonly _op;
|
|
17
|
+
readonly mode?: ClientMode;
|
|
18
|
+
readonly cmv: number;
|
|
19
19
|
/**
|
|
20
|
-
* @param
|
|
21
|
-
* @param _l custom logger. default is `console`.
|
|
20
|
+
* @param op {@link ClientOption}
|
|
22
21
|
*/
|
|
23
|
-
constructor(
|
|
22
|
+
constructor(_op?: ClientOption);
|
|
24
23
|
/**
|
|
25
24
|
* create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
|
|
26
|
-
* @param op
|
|
27
|
-
* @param op.proxy proxy configuration.
|
|
28
|
-
* @returns a http client as new context.
|
|
25
|
+
* @param op {@link ClientOption}
|
|
29
26
|
*/
|
|
30
|
-
newContext(op?: ClientOption):
|
|
27
|
+
newContext(op?: ClientOption): HttpClient;
|
|
31
28
|
get(url: string, op?: RequestOption & ClientOption & {
|
|
32
29
|
redirectAsNewRequest?: boolean;
|
|
33
30
|
responseType: "string";
|
|
@@ -46,5 +43,4 @@ export declare class HttpResolver implements HttpClient {
|
|
|
46
43
|
responseType: "buffer";
|
|
47
44
|
}): Promise<HttpResponse<Buffer>>;
|
|
48
45
|
post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse<string>>;
|
|
49
|
-
private fixCmv;
|
|
50
46
|
}
|
|
@@ -1,29 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.HttpResolver = void 0;
|
|
3
|
+
exports.HttpResolver = exports.s_defaultClientOption = void 0;
|
|
4
4
|
const xjs_common_1 = require("xjs-common");
|
|
5
5
|
const http_resolver_context_1 = require("./http-resolver-context");
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
exports.s_defaultClientOption = {
|
|
7
|
+
cmv: 141,
|
|
8
|
+
logger: console,
|
|
9
|
+
logLevel: "warn"
|
|
10
|
+
};
|
|
8
11
|
class HttpResolver {
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
_op;
|
|
13
|
+
mode;
|
|
14
|
+
cmv;
|
|
11
15
|
/**
|
|
12
|
-
* @param
|
|
13
|
-
* @param _l custom logger. default is `console`.
|
|
16
|
+
* @param op {@link ClientOption}
|
|
14
17
|
*/
|
|
15
|
-
constructor(
|
|
16
|
-
this.
|
|
17
|
-
this.
|
|
18
|
+
constructor(_op = exports.s_defaultClientOption) {
|
|
19
|
+
this._op = _op;
|
|
20
|
+
this.mode = this._op.mode;
|
|
21
|
+
this.cmv = this._op.cmv;
|
|
18
22
|
}
|
|
19
23
|
/**
|
|
20
24
|
* create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
|
|
21
|
-
* @param op
|
|
22
|
-
* @param op.proxy proxy configuration.
|
|
23
|
-
* @returns a http client as new context.
|
|
25
|
+
* @param op {@link ClientOption}
|
|
24
26
|
*/
|
|
25
27
|
newContext(op) {
|
|
26
|
-
return new http_resolver_context_1.HttpResolverContext(
|
|
28
|
+
return new http_resolver_context_1.HttpResolverContext(Object.assign({}, this._op, op));
|
|
27
29
|
}
|
|
28
30
|
async get(url, op) {
|
|
29
31
|
let redirectCount = op?.redirectAsNewRequest && -1;
|
|
@@ -46,8 +48,5 @@ class HttpResolver {
|
|
|
46
48
|
async post(url, payload, op) {
|
|
47
49
|
return await this.newContext(op).post(url, payload, op);
|
|
48
50
|
}
|
|
49
|
-
fixCmv() {
|
|
50
|
-
return this._baseCmv - Math.floor(Math.random() * s_cmvRange);
|
|
51
|
-
}
|
|
52
51
|
}
|
|
53
52
|
exports.HttpResolver = HttpResolver;
|