xjs-common 8.4.0 → 8.5.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 +4 -2
- package/dist/func/u-file.d.ts +26 -0
- package/dist/func/u-file.js +40 -1
- package/dist/prcs/http/http-resolver-context.d.ts +7 -2
- package/dist/prcs/http/http-resolver-context.js +81 -34
- package/dist/prcs/http/http-resolver.d.ts +1 -1
- package/dist/prcs/http/http-resolver.js +2 -1
- package/dist/prcs/http/i-http-client.d.ts +13 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,10 +99,9 @@ import { UString } from "xjs-common";
|
|
|
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
106
|
// switch tls ciphers order pattern by passing clientMode. (default is random between chrome or firefox.)
|
|
108
107
|
let body = await http.get("https://begyyal.net", { mode: s_clientMode.chrome });
|
|
@@ -114,6 +113,9 @@ import { HttpResolver, s_clientMode } from "xjs-common";
|
|
|
114
113
|
// implicitly corresponds to cookies and redirect, and do randomization.
|
|
115
114
|
body = await http.get("https://begyyal.net");
|
|
116
115
|
|
|
116
|
+
// download a file when [Content-Disposition: attachment] exists in the response.
|
|
117
|
+
await http.get("https://begyyal.net/a.txt", { downloadPath: "/path/to/store" });
|
|
118
|
+
|
|
117
119
|
// if you want to keep some states of requests (and suppress to randomize), it can create new context to do.
|
|
118
120
|
const context = http.newContext();
|
|
119
121
|
body = await context.get("https://begyyal.net/1");
|
package/dist/func/u-file.d.ts
CHANGED
|
@@ -1,13 +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;
|
|
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;
|
|
12
37
|
function joinPath(...p: MaybeArray<string>[]): string;
|
|
13
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))
|
|
@@ -74,6 +87,32 @@ var UFile;
|
|
|
74
87
|
fs.renameSync(f, t);
|
|
75
88
|
}
|
|
76
89
|
UFile.mv = mv;
|
|
90
|
+
function ls(p) {
|
|
91
|
+
const pt = joinPath(p);
|
|
92
|
+
if (!pt || !fs.statSync(pt).isDirectory())
|
|
93
|
+
throw new xjs_err_1.XjsErr(s_errCode, "Specified path for ls is not directory.");
|
|
94
|
+
return fs.readdirSync(pt);
|
|
95
|
+
}
|
|
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;
|
|
77
116
|
function joinPath(...p) {
|
|
78
117
|
return path.join(...p.flatMap(u_type_1.UType.takeAsArray));
|
|
79
118
|
}
|
|
@@ -16,7 +16,8 @@ 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 & {
|
|
@@ -27,7 +28,8 @@ export declare class HttpResolverContext implements IHttpClient {
|
|
|
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
35
|
post(url: string, payload: any, op?: RequestOption): Promise<any>;
|
|
@@ -35,10 +37,13 @@ export declare class HttpResolverContext implements IHttpClient {
|
|
|
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({});
|
|
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(data);
|
|
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;
|
|
@@ -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.
|
|
@@ -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
|
}
|
|
@@ -6,8 +6,16 @@ export interface ClientOption {
|
|
|
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;
|
|
11
19
|
}
|
|
12
20
|
export interface IHttpClient {
|
|
13
21
|
/**
|
|
@@ -16,7 +24,8 @@ export interface IHttpClient {
|
|
|
16
24
|
* @param op.headers http headers.
|
|
17
25
|
* @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
|
|
18
26
|
* @param op.proxy proxy configuration.
|
|
19
|
-
* @param op.ignoreQuery
|
|
27
|
+
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
28
|
+
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
20
29
|
* @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
|
|
21
30
|
* @returns string encoded by utf-8 as response payload.
|
|
22
31
|
*/
|
|
@@ -30,7 +39,8 @@ export interface IHttpClient {
|
|
|
30
39
|
* @param op.headers http headers.
|
|
31
40
|
* @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
|
|
32
41
|
* @param op.proxy proxy configuration.
|
|
33
|
-
* @param op.ignoreQuery
|
|
42
|
+
* @param op.ignoreQuery {@link RequestOption.ignoreQuery}
|
|
43
|
+
* @param op.downloadPath {@link RequestOption.downloadPath}
|
|
34
44
|
* @returns string encoded by utf-8 as response payload.
|
|
35
45
|
*/
|
|
36
46
|
post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<any>;
|