xjs-common 9.1.6 → 10.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 +7 -44
- package/dist/func/u.d.ts +1 -3
- package/dist/func/u.js +1 -39
- package/dist/index.d.ts +0 -4
- package/dist/index.js +1 -6
- package/dist/prcs/http/http-resolver-context.d.ts +14 -2
- package/dist/prcs/http/http-resolver-context.js +4 -3
- package/package.json +4 -5
package/README.md
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
[![npm][npm-badge]][npm-url] [![CI][ci-badge]][ci-url]
|
|
2
2
|
|
|
3
3
|
# Overview
|
|
4
|
-
Library modules for
|
|
4
|
+
Library modules for typescript that bundled general-purpose implementations.
|
|
5
5
|
This module is very simple, therefore it has no dependencies.
|
|
6
6
|
|
|
7
7
|
# Install
|
|
8
8
|
```
|
|
9
9
|
npm i xjs-common
|
|
10
10
|
```
|
|
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
11
|
|
|
13
12
|
# Code example (only part)
|
|
14
13
|
### Miscellaneous utilities.
|
|
@@ -22,8 +21,12 @@ import { checkPortAvailability, delay, int2array, UFile, UHttp, retry } from "xj
|
|
|
22
21
|
// [ 0, 1, 2, 3, 4 ]
|
|
23
22
|
console.log(int2array(5));
|
|
24
23
|
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
// runs callback with customizable retry.
|
|
25
|
+
retry(async () => { }, { count: 2 });
|
|
26
|
+
|
|
27
|
+
// utility types
|
|
28
|
+
let maybeArray: MaybeArray<number> = 0; // also number array is applicable.
|
|
29
|
+
let logger: Loggable = console; // object implements log/warn/error is applicable.
|
|
27
30
|
|
|
28
31
|
// true
|
|
29
32
|
console.log(UHttp.isHttpSuccess(204));
|
|
@@ -32,13 +35,6 @@ import { checkPortAvailability, delay, int2array, UFile, UHttp, retry } from "xj
|
|
|
32
35
|
console.log(UHttp.concatParamsWithEncoding("https://aaa.com", { p1: "a", p2: ["1", "2"] }));
|
|
33
36
|
// p1=a&p2=1&p2=2
|
|
34
37
|
console.log(UHttp.concatParamsWithEncoding(null, { p1: "a", p2: ["1", "2"] }));
|
|
35
|
-
|
|
36
|
-
// runs callback with customizable retry.
|
|
37
|
-
retry(async () => { }, { count: 2 });
|
|
38
|
-
|
|
39
|
-
// concatenate file path and make directory if doesn't already exist.
|
|
40
|
-
UFile.mkdir("path/to/dir");
|
|
41
|
-
UFile.mkdir(["path", "to", "dir"]);
|
|
42
38
|
})();
|
|
43
39
|
```
|
|
44
40
|
### Array utilities.
|
|
@@ -94,37 +90,6 @@ import { UString } from "xjs-common";
|
|
|
94
90
|
console.log(UString.simpleTime({ date: getJSTDate(), unit: TimeUnit.Day }));
|
|
95
91
|
})();
|
|
96
92
|
```
|
|
97
|
-
### Enhanced http client.
|
|
98
|
-
```ts
|
|
99
|
-
import { HttpResolver, s_clientMode } from "xjs-common";
|
|
100
|
-
|
|
101
|
-
(async () => {
|
|
102
|
-
// can customize logging. (default is console.)
|
|
103
|
-
// const http = new HttpResolver(chromeMajorVersion, logger);
|
|
104
|
-
const http = new HttpResolver();
|
|
105
|
-
|
|
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
|
-
|
|
110
|
-
// use proxy by passing the configuration.
|
|
111
|
-
const proxy = { server: "proxy.sample.com", port: 8080, auth: { name: "prx", pass: "****" } }
|
|
112
|
-
res = await http.post("https://begyyal.net", { proxy });
|
|
113
|
-
|
|
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" });
|
|
119
|
-
|
|
120
|
-
// if you want to keep some states of requests (and suppress to randomize), it can create new context to do.
|
|
121
|
-
const context = http.newContext();
|
|
122
|
-
res = await context.get("https://begyyal.net/1");
|
|
123
|
-
// this request sends with cookies that is set by precedent requests.
|
|
124
|
-
// in POST, payload is treated as json if it is an object.
|
|
125
|
-
res = await context.post("https://begyyal.net/2", { a: "b" });
|
|
126
|
-
})();
|
|
127
|
-
```
|
|
128
93
|
### Mark method as transaction.
|
|
129
94
|
**NOTE**: this feature uses decorator, so requires `"experimentalDecorators": true` in tsconfig.
|
|
130
95
|
```ts
|
|
@@ -216,9 +181,7 @@ XJS throws error with `code` property which has one of the following numbers.
|
|
|
216
181
|
|10|`func/u`|
|
|
217
182
|
|20|`func/u-string`|
|
|
218
183
|
|30|`func/u-type` (include `func/decorator/d-type`) |
|
|
219
|
-
|40|`func/u-file` |
|
|
220
184
|
|100|`func/decorator/transaction`|
|
|
221
|
-
|200|`prcs/http/http-resolver`|
|
|
222
185
|
|
|
223
186
|
# License
|
|
224
187
|
[Apache-License](./LICENSE)
|
package/dist/func/u.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { Loggable
|
|
1
|
+
import { Loggable } from "../const/types";
|
|
2
2
|
export declare function getJSTDate(d?: Date): Date;
|
|
3
3
|
export declare function delay(sec: number): Promise<void>;
|
|
4
4
|
export declare function int2array(size: number): number[];
|
|
5
5
|
export declare function array2map<K, T>(array: T[], keyGen: (e: T) => K): Map<K, T[]>;
|
|
6
6
|
export declare function bitor(...bit: number[]): number;
|
|
7
|
-
export declare function checkPortAvailability(port: number): Promise<boolean>;
|
|
8
7
|
export interface RetryOption<T = void | Promise<void>> {
|
|
9
8
|
count?: number;
|
|
10
9
|
logger?: Loggable;
|
|
@@ -23,4 +22,3 @@ export declare function retry<T>(cb: () => T, op?: RetryOption<void>): T;
|
|
|
23
22
|
export declare function retry<T>(cb: () => T, op?: RetryOption<Promise<void>>): Promise<T>;
|
|
24
23
|
export declare function retry<T>(cb: () => Promise<T>, op?: RetryOption<void>): Promise<T>;
|
|
25
24
|
export declare function retry<T>(cb: () => Promise<T>, op?: RetryOption<Promise<void>>): Promise<T>;
|
|
26
|
-
export declare function joinPath(...p: MaybeArray<string>[]): string;
|
package/dist/func/u.js
CHANGED
|
@@ -1,32 +1,7 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
27
|
-
const path = __importStar(require("path"));
|
|
3
|
+
exports.retry = exports.bitor = exports.array2map = exports.int2array = exports.delay = exports.getJSTDate = void 0;
|
|
28
4
|
const xjs_err_1 = require("../obj/xjs-err");
|
|
29
|
-
const u_type_1 = require("./u-type");
|
|
30
5
|
const s_errCode = 10;
|
|
31
6
|
function getJSTDate(d) {
|
|
32
7
|
return new Date((d ? d.getTime() : Date.now()) + 9 * 60 * 60 * 1000);
|
|
@@ -59,15 +34,6 @@ function bitor(...bit) {
|
|
|
59
34
|
return bit.reduce((a, b) => a | b);
|
|
60
35
|
}
|
|
61
36
|
exports.bitor = bitor;
|
|
62
|
-
function checkPortAvailability(port) {
|
|
63
|
-
return new Promise(resolve => {
|
|
64
|
-
const server = require('net').createServer();
|
|
65
|
-
server.once('error', () => resolve(false))
|
|
66
|
-
.once('listening', () => { server.close(); resolve(true); })
|
|
67
|
-
.listen(port);
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
exports.checkPortAvailability = checkPortAvailability;
|
|
71
37
|
;
|
|
72
38
|
function retry(cb, op) {
|
|
73
39
|
const l = op?.logger ?? console;
|
|
@@ -108,7 +74,3 @@ function retry(cb, op) {
|
|
|
108
74
|
return prcs(initialCount);
|
|
109
75
|
}
|
|
110
76
|
exports.retry = retry;
|
|
111
|
-
function joinPath(...p) {
|
|
112
|
-
return path.join(...p.flatMap(u_type_1.UType.takeAsArray));
|
|
113
|
-
}
|
|
114
|
-
exports.joinPath = joinPath;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,12 +7,8 @@ export * from "./func/u";
|
|
|
7
7
|
export * from "./func/u-obj";
|
|
8
8
|
export * from "./func/u-string";
|
|
9
9
|
export * from "./func/u-array";
|
|
10
|
-
export * from "./func/u-file";
|
|
11
10
|
export * from "./func/u-http";
|
|
12
11
|
export * from "./func/u-type";
|
|
13
12
|
export * from "./func/decorator/transaction";
|
|
14
13
|
export { DType } from "./func/decorator/d-type";
|
|
15
14
|
export * from "./obj/xjs-err";
|
|
16
|
-
export { HttpResolver } from "./prcs/http/http-resolver";
|
|
17
|
-
export { s_clientMode } from "./prcs/http/http-resolver-context";
|
|
18
|
-
export { IHttpClient } from "./prcs/http/i-http-client";
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.
|
|
17
|
+
exports.DType = void 0;
|
|
18
18
|
__exportStar(require("./const/types"), exports);
|
|
19
19
|
__exportStar(require("./const/time-unit"), exports);
|
|
20
20
|
__exportStar(require("./const/gender"), exports);
|
|
@@ -24,14 +24,9 @@ __exportStar(require("./func/u"), exports);
|
|
|
24
24
|
__exportStar(require("./func/u-obj"), exports);
|
|
25
25
|
__exportStar(require("./func/u-string"), exports);
|
|
26
26
|
__exportStar(require("./func/u-array"), exports);
|
|
27
|
-
__exportStar(require("./func/u-file"), exports);
|
|
28
27
|
__exportStar(require("./func/u-http"), exports);
|
|
29
28
|
__exportStar(require("./func/u-type"), exports);
|
|
30
29
|
__exportStar(require("./func/decorator/transaction"), exports);
|
|
31
30
|
var d_type_1 = require("./func/decorator/d-type");
|
|
32
31
|
Object.defineProperty(exports, "DType", { enumerable: true, get: function () { return d_type_1.DType; } });
|
|
33
32
|
__exportStar(require("./obj/xjs-err"), exports);
|
|
34
|
-
var http_resolver_1 = require("./prcs/http/http-resolver");
|
|
35
|
-
Object.defineProperty(exports, "HttpResolver", { enumerable: true, get: function () { return http_resolver_1.HttpResolver; } });
|
|
36
|
-
var http_resolver_context_1 = require("./prcs/http/http-resolver-context");
|
|
37
|
-
Object.defineProperty(exports, "s_clientMode", { enumerable: true, get: function () { return http_resolver_context_1.s_clientMode; } });
|
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
import { ClientMode } from "./http-resolver";
|
|
2
1
|
import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
|
|
3
2
|
import { Loggable } from "../../const/types";
|
|
4
|
-
export declare const s_clientMode:
|
|
3
|
+
export declare const s_clientMode: {
|
|
4
|
+
nodejs: {
|
|
5
|
+
id: number;
|
|
6
|
+
cipherOrder: any;
|
|
7
|
+
};
|
|
8
|
+
chrome: {
|
|
9
|
+
id: number;
|
|
10
|
+
cipherOrder: number[];
|
|
11
|
+
};
|
|
12
|
+
firefox: {
|
|
13
|
+
id: number;
|
|
14
|
+
cipherOrder: number[];
|
|
15
|
+
};
|
|
16
|
+
};
|
|
5
17
|
export declare class HttpResolverContext implements IHttpClient {
|
|
6
18
|
readonly cmv: number;
|
|
7
19
|
private _l;
|
|
@@ -96,10 +96,11 @@ class HttpResolverContext {
|
|
|
96
96
|
this.cmv = cmv;
|
|
97
97
|
this._l = _l;
|
|
98
98
|
this._mode = op?.mode ?? u_array_1.UArray.randomPick([exports.s_clientMode.chrome, exports.s_clientMode.firefox]);
|
|
99
|
-
if (this._mode.id > 0)
|
|
100
|
-
this._ciphers = this.createCiphers(this._mode);
|
|
101
99
|
this._proxyConfig = op?.proxy;
|
|
102
|
-
|
|
100
|
+
if (this._mode.id > 0) {
|
|
101
|
+
this._ciphers = this.createCiphers(this._mode);
|
|
102
|
+
this._chHeaders = s_mode2headers.get(this._mode)(this.cmv);
|
|
103
|
+
}
|
|
103
104
|
}
|
|
104
105
|
/**
|
|
105
106
|
* request GET to the url.
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "xjs-common",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "library modules for
|
|
3
|
+
"version": "10.0.0",
|
|
4
|
+
"description": "library modules for typescript that bundled general-purpose implementations.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/begyyal/xjs_commons"
|
|
8
8
|
},
|
|
9
9
|
"keywords": [
|
|
10
|
-
"
|
|
10
|
+
"javascript",
|
|
11
11
|
"typescript",
|
|
12
12
|
"utility"
|
|
13
13
|
],
|
|
@@ -24,8 +24,7 @@
|
|
|
24
24
|
"main": "./dist/index",
|
|
25
25
|
"types": "./dist/index.d.ts",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/node": "^20.3.1",
|
|
28
27
|
"ts-node": "^10.9.1",
|
|
29
28
|
"typescript": "^4.9.5"
|
|
30
29
|
}
|
|
31
|
-
}
|
|
30
|
+
}
|