xjs-common 7.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.
Files changed (43) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +216 -0
  3. package/dist/const/gender.d.ts +4 -0
  4. package/dist/const/gender.js +8 -0
  5. package/dist/const/http-method.d.ts +8 -0
  6. package/dist/const/http-method.js +12 -0
  7. package/dist/const/types.d.ts +12 -0
  8. package/dist/const/types.js +14 -0
  9. package/dist/func/decorator/d-type.d.ts +24 -0
  10. package/dist/func/decorator/d-type.js +71 -0
  11. package/dist/func/decorator/d-validate.d.ts +20 -0
  12. package/dist/func/decorator/d-validate.js +64 -0
  13. package/dist/func/decorator/transaction.d.ts +7 -0
  14. package/dist/func/decorator/transaction.js +36 -0
  15. package/dist/func/u-array.d.ts +40 -0
  16. package/dist/func/u-array.js +67 -0
  17. package/dist/func/u-file.d.ts +10 -0
  18. package/dist/func/u-file.js +69 -0
  19. package/dist/func/u-http.d.ts +7 -0
  20. package/dist/func/u-http.js +25 -0
  21. package/dist/func/u-obj.d.ts +18 -0
  22. package/dist/func/u-obj.js +44 -0
  23. package/dist/func/u-string.d.ts +9 -0
  24. package/dist/func/u-string.js +56 -0
  25. package/dist/func/u-type.d.ts +21 -0
  26. package/dist/func/u-type.js +53 -0
  27. package/dist/func/u.d.ts +6 -0
  28. package/dist/func/u.js +45 -0
  29. package/dist/index.d.ts +16 -0
  30. package/dist/index.js +35 -0
  31. package/dist/obj/type-exp.d.ts +8 -0
  32. package/dist/obj/type-exp.js +23 -0
  33. package/dist/obj/xjs-err.d.ts +4 -0
  34. package/dist/obj/xjs-err.js +11 -0
  35. package/dist/prcs/http/http-resolver-context.d.ts +46 -0
  36. package/dist/prcs/http/http-resolver-context.js +287 -0
  37. package/dist/prcs/http/http-resolver.d.ts +38 -0
  38. package/dist/prcs/http/http-resolver.js +52 -0
  39. package/dist/prcs/http/i-http-client.d.ts +37 -0
  40. package/dist/prcs/http/i-http-client.js +2 -0
  41. package/dist/prcs/http-resolver.d.ts +53 -0
  42. package/dist/prcs/http-resolver.js +255 -0
  43. package/package.json +30 -0
@@ -0,0 +1,46 @@
1
+ import { ClientMode } from "./http-resolver";
2
+ import { ClientOption, IHttpClient, RequestOption } from "./i-http-client";
3
+ export declare const s_clientMode: Record<string, ClientMode>;
4
+ export declare class HttpResolverContext implements IHttpClient {
5
+ private readonly _cmv;
6
+ private _l;
7
+ private readonly _als;
8
+ private readonly _mode;
9
+ private readonly _ciphers;
10
+ private readonly _proxyConfig?;
11
+ private readonly _chHeaders;
12
+ private _cookies?;
13
+ constructor(_cmv: number, op?: ClientOption, _l?: {
14
+ log: (msg: any) => void;
15
+ warn: (msg: any) => void;
16
+ });
17
+ /**
18
+ * request GET to the url.
19
+ * @param url target url.
20
+ * @param op.headers http headers.
21
+ * @param op.ignoreQuery if true, query part in the `url` is ignored.
22
+ * @returns string encoded by utf-8 as response payload.
23
+ */
24
+ get(url: string, op?: RequestOption & {
25
+ outerRedirectCount?: number;
26
+ }): Promise<any>;
27
+ /**
28
+ * request POST to the url.
29
+ * @param url target url.
30
+ * @param payload request payload. if this is an object, it is treated as json.
31
+ * @param op.headers http headers.
32
+ * @param op.ignoreQuery if true, query part in the `url` is ignored.
33
+ * @returns string encoded by utf-8 as response payload.
34
+ */
35
+ post(url: string, payload: any, op?: RequestOption): Promise<any>;
36
+ private createProxyAgent;
37
+ private getIn;
38
+ private postIn;
39
+ private reqHttps;
40
+ private handleRedirect;
41
+ private createCiphers;
42
+ private setCookies;
43
+ private storeCookies;
44
+ private log;
45
+ private warn;
46
+ }
@@ -0,0 +1,287 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.HttpResolverContext = exports.s_clientMode = void 0;
27
+ const tls = __importStar(require("tls"));
28
+ const zlib = __importStar(require("zlib"));
29
+ const url_1 = require("url");
30
+ const https_1 = require("https");
31
+ const http_1 = require("http");
32
+ const async_hooks_1 = require("async_hooks");
33
+ const xjs_err_1 = require("../../obj/xjs-err");
34
+ const u_http_1 = require("../../func/u-http");
35
+ const u_array_1 = require("../../func/u-array");
36
+ const http_method_1 = require("../../const/http-method");
37
+ const u_type_1 = require("../../func/u-type");
38
+ exports.s_clientMode = {
39
+ nodejs: { id: 0, cipherOrder: null },
40
+ chrome: { id: 1, cipherOrder: [2, 0, 1] },
41
+ firefox: { id: 2, cipherOrder: [2, 1, 0] }
42
+ };
43
+ const s_timeout = 1000 * 20;
44
+ const s_errCode = 200;
45
+ const s_redirectLimit = 5;
46
+ const s_mode2headers = new Map([
47
+ [exports.s_clientMode.firefox, (cmv) => ({
48
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
49
+ "Accept-Encoding": "gzip, deflate, br",
50
+ "Accept-Language": "en-US,en;q=0.5",
51
+ "Sec-Fetch-Dest": "document",
52
+ "Sec-Fetch-Mode": "navigate",
53
+ "Sec-Fetch-Site": "none",
54
+ "Sec-Fetch-User": "?1",
55
+ "Upgrade-Insecure-Requests": "1",
56
+ "User-Agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:${cmv}.0) Gecko/20100101 Firefox/${cmv}.0`
57
+ })],
58
+ [exports.s_clientMode.chrome, (cmv) => {
59
+ const uad = cmv < 130
60
+ ? `"Not/A)Brand";v="8", "Chromium";v="${cmv}", "Google Chrome";v="${cmv}"`
61
+ : `"Chromium";v="${cmv}", "Not:A-Brand";v="24", "Google Chrome";v="${cmv}"`;
62
+ const ch = {
63
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
64
+ "Accept-Encoding": "gzip, deflate, br, zstd",
65
+ "Accept-Language": "en-US,en;q=0.9",
66
+ "Sec-Ch-Ua": uad,
67
+ "Sec-Ch-Ua-Mobile": "?0",
68
+ "Sec-Ch-Ua-Platform": "Windows",
69
+ "Sec-Fetch-Dest": "document",
70
+ "Sec-Fetch-Mode": "navigate",
71
+ "Sec-Fetch-Site": "none",
72
+ "Sec-Fetch-User": "?1",
73
+ "Upgrade-Insecure-Requests": "1",
74
+ "User-Agent": `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${cmv}.0.0.0 Safari/537.36`
75
+ };
76
+ if (cmv >= 124)
77
+ ch["Priority"] = "u=0, i";
78
+ return ch;
79
+ }]
80
+ ]);
81
+ class HttpResolverContext {
82
+ _cmv;
83
+ _l;
84
+ _als = new async_hooks_1.AsyncLocalStorage();
85
+ _mode;
86
+ _ciphers;
87
+ _proxyConfig;
88
+ _chHeaders;
89
+ _cookies;
90
+ constructor(_cmv, op, _l = console) {
91
+ this._cmv = _cmv;
92
+ this._l = _l;
93
+ this._mode = op?.mode ?? u_array_1.UArray.randomPick([exports.s_clientMode.chrome, exports.s_clientMode.firefox]);
94
+ this._ciphers = this.createCiphers(this._mode);
95
+ this._proxyConfig = op?.proxy;
96
+ this._chHeaders = s_mode2headers.get(this._mode)(this._cmv);
97
+ this._l.log(`Context launched with ${Object.keys(exports.s_clientMode).find((_, i) => i === this._mode.id)}:${this._cmv}.`);
98
+ }
99
+ /**
100
+ * request GET to the url.
101
+ * @param url target url.
102
+ * @param op.headers http headers.
103
+ * @param op.ignoreQuery if true, query part in the `url` is ignored.
104
+ * @returns string encoded by utf-8 as response payload.
105
+ */
106
+ async get(url, op) {
107
+ const u = new url_1.URL(url);
108
+ const proxyAgent = this._proxyConfig && await this.createProxyAgent(u);
109
+ const rc = { redirectCount: op?.outerRedirectCount ?? 0, proxyAgent };
110
+ Object.assign(rc, op);
111
+ return await this._als.run(rc, this.getIn, u).finally(() => proxyAgent?.destroy());
112
+ }
113
+ /**
114
+ * request POST to the url.
115
+ * @param url target url.
116
+ * @param payload request payload. if this is an object, it is treated as json.
117
+ * @param op.headers http headers.
118
+ * @param op.ignoreQuery if true, query part in the `url` is ignored.
119
+ * @returns string encoded by utf-8 as response payload.
120
+ */
121
+ async post(url, payload, op) {
122
+ const u = new url_1.URL(url);
123
+ const proxyAgent = this._proxyConfig && await this.createProxyAgent(u);
124
+ const rc = { redirectCount: 0, proxyAgent };
125
+ Object.assign(rc, op);
126
+ return await this._als.run(rc, this.postIn, u, payload).finally(() => proxyAgent?.destroy());
127
+ }
128
+ createProxyAgent(u) {
129
+ const conf = this._proxyConfig;
130
+ return new Promise((resolve, reject) => {
131
+ const headers = {};
132
+ if (conf.auth)
133
+ headers['Proxy-Authorization'] = `Basic ${Buffer.from(conf.auth.name + ':' + conf.auth.pass).toString('base64')}`;
134
+ const req = (0, http_1.request)({
135
+ host: conf.server,
136
+ port: conf.port,
137
+ method: http_method_1.HttpMethod.Connect,
138
+ path: `${u.hostname}:443`,
139
+ headers
140
+ }).on('connect', (res, socket) => {
141
+ if (res.statusCode === 200)
142
+ resolve(new https_1.Agent({ socket, keepAlive: true }));
143
+ else
144
+ reject(new xjs_err_1.XjsErr(s_errCode, "Could not connect to proxy."));
145
+ });
146
+ req.on('error', reject);
147
+ req.on('timeout', () => {
148
+ req.destroy();
149
+ reject(new xjs_err_1.XjsErr(s_errCode, "The http request timeout, maybe server did not respond."));
150
+ });
151
+ req.end();
152
+ });
153
+ }
154
+ getIn = async (u) => {
155
+ const params = {};
156
+ const rc = this._als.getStore();
157
+ params.method = http_method_1.HttpMethod.Get;
158
+ params.headers = rc.headers ?? {};
159
+ return await this.reqHttps(u, params);
160
+ };
161
+ postIn = async (u, payload) => {
162
+ const params = {};
163
+ const rc = this._als.getStore();
164
+ params.method = http_method_1.HttpMethod.Post;
165
+ params.headers = rc.headers ?? {};
166
+ let p = payload;
167
+ if (u_type_1.UType.isObject(payload)) {
168
+ p = JSON.stringify(payload);
169
+ params.headers["Content-Length"] = p.length;
170
+ params.headers["Content-Type"] = "application/json";
171
+ }
172
+ return await this.reqHttps(u, params, p);
173
+ };
174
+ reqHttps(u, params, payload) {
175
+ const rc = this._als.getStore();
176
+ params.timeout = s_timeout;
177
+ params.protocol = u.protocol;
178
+ params.host = u.host;
179
+ params.path = (rc.ignoreQuery || !u.search) ? u.pathname : `${u.pathname}${u.search}`;
180
+ params.agent = rc.proxyAgent;
181
+ if (this._mode.id > 0) {
182
+ params.ciphers = this._ciphers;
183
+ params.headers = params.headers ? Object.assign(params.headers, this._chHeaders) : this._chHeaders;
184
+ }
185
+ if (this._cookies)
186
+ this.setCookies(params.headers);
187
+ 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
+ });
220
+ req.on('error', reject);
221
+ req.on('timeout', () => {
222
+ req.destroy();
223
+ reject(new xjs_err_1.XjsErr(s_errCode, "The http request timeout, maybe server did not respond."));
224
+ });
225
+ if (payload)
226
+ req.write(payload);
227
+ req.end();
228
+ });
229
+ }
230
+ async handleRedirect(res, host) {
231
+ const rc = this._als.getStore();
232
+ if (!res.headers.location)
233
+ throw new xjs_err_1.XjsErr(s_errCode, "Received http redirection, but no location header found.");
234
+ if (rc.redirectCount++ > s_redirectLimit)
235
+ throw new xjs_err_1.XjsErr(s_errCode, "Count of http redirection exceeds limit.");
236
+ this.log(`Redirect to ${res.headers.location}. (count is ${rc.redirectCount})`);
237
+ const dest = res.headers.location.startsWith("http") ? res.headers.location : `https://${host}${res.headers.location}`;
238
+ if (rc.outerRedirectCount)
239
+ throw new xjs_err_1.XjsErr(-1, dest);
240
+ const u = new url_1.URL(dest);
241
+ // consider for proxy which implements reverse proxy.
242
+ if (rc.proxyAgent) {
243
+ rc.proxyAgent?.destroy();
244
+ rc.proxyAgent = await this.createProxyAgent(u);
245
+ }
246
+ return await this.getIn(u);
247
+ }
248
+ createCiphers(mode) {
249
+ const defaultCiphers = tls.DEFAULT_CIPHERS.split(':');
250
+ return [
251
+ defaultCiphers[mode.cipherOrder[0]],
252
+ defaultCiphers[mode.cipherOrder[1]],
253
+ defaultCiphers[mode.cipherOrder[2]],
254
+ ...u_array_1.UArray.shuffle(defaultCiphers.slice(3))
255
+ ].join(':');
256
+ }
257
+ setCookies(headers) {
258
+ const exp = this._cookies["expires"];
259
+ if (exp && new Date(exp).getTime() <= Date.now()) {
260
+ this._cookies = null;
261
+ this.log("Cookies was cleared due to an expiraion.");
262
+ }
263
+ else
264
+ headers.cookie = Object.keys(this._cookies)
265
+ .filter(ckk => !["expires", "max-age"].includes(ckk))
266
+ .map(ckk => `${ckk}=${this._cookies[ckk]};`).join(" ");
267
+ }
268
+ storeCookies(cookies) {
269
+ this._cookies ??= {};
270
+ cookies.filter(c => c).flatMap(c => c.split(";"))
271
+ .map(c => {
272
+ const idx = c.indexOf("=");
273
+ return idx !== -1 && [c.substring(0, idx).toLowerCase().trim(), c.substring(idx + 1)];
274
+ })
275
+ .filter(cp => cp && cp[0] && !["secure", "path", "domain", "samesite"].includes(cp[0]))
276
+ .forEach(cp => this._cookies[cp[0]] = cp[1]);
277
+ this.log("Store cookies from set-cookie headers.");
278
+ this.log(JSON.stringify(this._cookies));
279
+ }
280
+ log(msg) {
281
+ this._l.log(`[http-resolver] ${msg}`);
282
+ }
283
+ warn(msg) {
284
+ this._l.warn(`[http-resolver] ${msg}`);
285
+ }
286
+ }
287
+ exports.HttpResolverContext = HttpResolverContext;
@@ -0,0 +1,38 @@
1
+ import { HttpResolverContext } from "./http-resolver-context";
2
+ import { ClientOption, IHttpClient, RequestOption } from "./i-http-client";
3
+ export interface ClientMode {
4
+ id: number;
5
+ cipherOrder: number[];
6
+ }
7
+ export interface ProxyConfig {
8
+ server: string;
9
+ port: number;
10
+ auth?: {
11
+ name: string;
12
+ pass: string;
13
+ };
14
+ }
15
+ export declare class HttpResolver implements IHttpClient {
16
+ private _baseCmv;
17
+ private _l;
18
+ /**
19
+ * @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
20
+ * @param _l custom logger. default is `console`.
21
+ */
22
+ constructor(_baseCmv: number, _l?: {
23
+ log: (msg: any) => void;
24
+ warn: (msg: any) => void;
25
+ });
26
+ /**
27
+ * create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
28
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
29
+ * @param op.proxy proxy configuration.
30
+ * @returns a http client as new context.
31
+ */
32
+ newContext(op?: ClientOption): HttpResolverContext;
33
+ get(url: string, op?: RequestOption & ClientOption & {
34
+ redirectAsNewRequest?: boolean;
35
+ }): Promise<any>;
36
+ post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<any>;
37
+ private fixCmv;
38
+ }
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpResolver = void 0;
4
+ const xjs_err_1 = require("../../obj/xjs-err");
5
+ const http_resolver_context_1 = require("./http-resolver-context");
6
+ const s_cmvRange = 5;
7
+ class HttpResolver {
8
+ _baseCmv;
9
+ _l;
10
+ /**
11
+ * @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
12
+ * @param _l custom logger. default is `console`.
13
+ */
14
+ constructor(_baseCmv, _l = console) {
15
+ this._baseCmv = _baseCmv;
16
+ this._l = _l;
17
+ }
18
+ /**
19
+ * create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
20
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
21
+ * @param op.proxy proxy configuration.
22
+ * @returns a http client as new context.
23
+ */
24
+ newContext(op) {
25
+ return new http_resolver_context_1.HttpResolverContext(this.fixCmv(), op, this._l);
26
+ }
27
+ async get(url, op) {
28
+ let redirectCount = op?.redirectAsNewRequest && -1;
29
+ const bindOp = () => {
30
+ const option = Object.assign({}, op);
31
+ if (redirectCount)
32
+ Object.assign(option, { outerRedirectCount: ++redirectCount });
33
+ return option;
34
+ };
35
+ try {
36
+ return await this.newContext(op).get(url, bindOp());
37
+ }
38
+ catch (e) {
39
+ if (!(e instanceof xjs_err_1.XjsErr) || e.code !== -1)
40
+ throw e;
41
+ else
42
+ return await this.newContext(op).get(e.message, bindOp());
43
+ }
44
+ }
45
+ async post(url, payload, op) {
46
+ return await this.newContext(op).post(url, payload, op);
47
+ }
48
+ fixCmv() {
49
+ return this._baseCmv - Math.floor(Math.random() * s_cmvRange);
50
+ }
51
+ }
52
+ exports.HttpResolver = HttpResolver;
@@ -0,0 +1,37 @@
1
+ /// <reference types="node" />
2
+ import { OutgoingHttpHeaders } from "http";
3
+ import { ClientMode, ProxyConfig } from "./http-resolver";
4
+ export interface ClientOption {
5
+ mode?: ClientMode;
6
+ proxy?: ProxyConfig;
7
+ }
8
+ export interface RequestOption {
9
+ ignoreQuery?: boolean;
10
+ headers?: OutgoingHttpHeaders;
11
+ }
12
+ export interface IHttpClient {
13
+ /**
14
+ * request GET to the url with new context.
15
+ * @param url target url.
16
+ * @param op.headers http headers.
17
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
18
+ * @param op.proxy proxy configuration.
19
+ * @param op.ignoreQuery if true, query part in the `url` is ignored.
20
+ * @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
21
+ * @returns string encoded by utf-8 as response payload.
22
+ */
23
+ get(url: string, op?: RequestOption & ClientOption & {
24
+ redirectAsNewRequest?: boolean;
25
+ }): Promise<any>;
26
+ /**
27
+ * request POST to the url with new context.
28
+ * @param url target url.
29
+ * @param payload request payload. if this is an object, it is treated as json.
30
+ * @param op.headers http headers.
31
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
32
+ * @param op.proxy proxy configuration.
33
+ * @param op.ignoreQuery if true, query part in the `url` is ignored.
34
+ * @returns string encoded by utf-8 as response payload.
35
+ */
36
+ post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<any>;
37
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,53 @@
1
+ export declare const s_clientMode: Record<string, ClientMode>;
2
+ interface ClientMode {
3
+ id: number;
4
+ cipherOrder: number[];
5
+ }
6
+ interface ProxyConfig {
7
+ server: string;
8
+ port: number;
9
+ auth?: {
10
+ name: string;
11
+ pass: string;
12
+ };
13
+ }
14
+ export declare class HttpResolver {
15
+ private _baseCmv;
16
+ private _l;
17
+ private readonly _als;
18
+ private readonly _mode2headers;
19
+ /**
20
+ * @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
21
+ * @param _l custom logger. default is `console`.
22
+ */
23
+ constructor(_baseCmv: number, _l?: {
24
+ log: (msg: any) => void;
25
+ warn: (msg: any) => void;
26
+ });
27
+ /**
28
+ * request to the url with GET.
29
+ * @param url target url.
30
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
31
+ * @param op.proxy proxy configuration.
32
+ * @param op.ignoreQuery if true, query part in the `url` is ignored.
33
+ * @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
34
+ * @returns string encoded by utf-8 as response payload.
35
+ */
36
+ get(url: string, op?: {
37
+ mode?: ClientMode;
38
+ ignoreQuery?: boolean;
39
+ proxy?: ProxyConfig;
40
+ redirectAsNewRequest?: boolean;
41
+ }): Promise<any>;
42
+ private fixCmv;
43
+ private createProxyAgent;
44
+ private getIn;
45
+ private reqHttps;
46
+ private handleRedirect;
47
+ private createCiphers;
48
+ private setCookies;
49
+ private storeCookies;
50
+ private log;
51
+ private warn;
52
+ }
53
+ export {};