xjs-node 1.0.7 → 2.0.1-alpha.1

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.
@@ -1,51 +1,50 @@
1
- /// <reference types="node" />
2
- import { Loggable } from "xjs-common";
3
- import { HttpResolverContext } from "./http-resolver-context";
4
- import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
5
- export interface ClientMode {
6
- id: number;
7
- cipherOrder: number[];
8
- }
9
- export interface ProxyConfig {
10
- server: string;
11
- port: number;
12
- auth?: {
13
- name: string;
14
- pass: string;
15
- };
16
- }
17
- export declare class HttpResolver implements IHttpClient {
18
- private _baseCmv;
19
- private _l;
20
- /**
21
- * @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
22
- * @param _l custom logger. default is `console`.
23
- */
24
- constructor(_baseCmv?: number, _l?: Loggable);
25
- /**
26
- * create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
27
- * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
28
- * @param op.proxy proxy configuration.
29
- * @returns a http client as new context.
30
- */
31
- newContext(op?: ClientOption): HttpResolverContext;
32
- get(url: string, op?: RequestOption & ClientOption & {
33
- redirectAsNewRequest?: boolean;
34
- responseType: "string";
35
- }): Promise<HttpResponse<string>>;
36
- get(url: string, op?: RequestOption & ClientOption & {
37
- redirectAsNewRequest?: boolean;
38
- responseType: "buffer";
39
- }): Promise<HttpResponse<Buffer>>;
40
- get(url: string, op?: RequestOption & ClientOption & {
41
- redirectAsNewRequest?: boolean;
42
- }): Promise<HttpResponse<string>>;
43
- post(url: string, payload: any, op?: RequestOption & ClientOption & {
44
- responseType: "string";
45
- }): Promise<HttpResponse<string>>;
46
- post(url: string, payload: any, op?: RequestOption & ClientOption & {
47
- responseType: "buffer";
48
- }): Promise<HttpResponse<Buffer>>;
49
- post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse<string>>;
50
- private fixCmv;
51
- }
1
+ import { Loggable } from "xjs-common";
2
+ import { HttpResolverContext } from "./http-resolver-context";
3
+ import { ClientOption, HttpResponse, IHttpClient, RequestOption } from "./i-http-client";
4
+ export interface ClientMode {
5
+ id: number;
6
+ cipherOrder: number[];
7
+ }
8
+ export interface ProxyConfig {
9
+ server: string;
10
+ port: number;
11
+ auth?: {
12
+ name: string;
13
+ pass: string;
14
+ };
15
+ }
16
+ export declare class HttpResolver implements IHttpClient {
17
+ private _baseCmv;
18
+ private _l;
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?: Loggable);
24
+ /**
25
+ * create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
26
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
27
+ * @param op.proxy proxy configuration.
28
+ * @returns a http client as new context.
29
+ */
30
+ newContext(op?: ClientOption): HttpResolverContext;
31
+ get(url: string, op?: RequestOption & ClientOption & {
32
+ redirectAsNewRequest?: boolean;
33
+ responseType: "string";
34
+ }): Promise<HttpResponse<string>>;
35
+ get(url: string, op?: RequestOption & ClientOption & {
36
+ redirectAsNewRequest?: boolean;
37
+ responseType: "buffer";
38
+ }): Promise<HttpResponse<Buffer>>;
39
+ get(url: string, op?: RequestOption & ClientOption & {
40
+ redirectAsNewRequest?: boolean;
41
+ }): Promise<HttpResponse<string>>;
42
+ post(url: string, payload: any, op?: RequestOption & ClientOption & {
43
+ responseType: "string";
44
+ }): Promise<HttpResponse<string>>;
45
+ post(url: string, payload: any, op?: RequestOption & ClientOption & {
46
+ responseType: "buffer";
47
+ }): Promise<HttpResponse<Buffer>>;
48
+ post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse<string>>;
49
+ private fixCmv;
50
+ }
@@ -1,53 +1,53 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HttpResolver = void 0;
4
- const xjs_common_1 = require("xjs-common");
5
- const http_resolver_context_1 = require("./http-resolver-context");
6
- const s_cmvRange = 5;
7
- const s_defaultCmv = 138;
8
- class HttpResolver {
9
- _baseCmv;
10
- _l;
11
- /**
12
- * @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
13
- * @param _l custom logger. default is `console`.
14
- */
15
- constructor(_baseCmv = s_defaultCmv, _l = console) {
16
- this._baseCmv = _baseCmv;
17
- this._l = _l;
18
- }
19
- /**
20
- * create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
21
- * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
22
- * @param op.proxy proxy configuration.
23
- * @returns a http client as new context.
24
- */
25
- newContext(op) {
26
- return new http_resolver_context_1.HttpResolverContext(this.fixCmv(), op, this._l);
27
- }
28
- async get(url, op) {
29
- let redirectCount = op?.redirectAsNewRequest && -1;
30
- const bindOp = () => {
31
- const option = Object.assign({}, op);
32
- if (redirectCount)
33
- Object.assign(option, { outerRedirectCount: ++redirectCount });
34
- return option;
35
- };
36
- try {
37
- return await this.newContext(op).get(url, bindOp());
38
- }
39
- catch (e) {
40
- if (!(e instanceof xjs_common_1.XjsErr) || e.code !== -1)
41
- throw e;
42
- else
43
- return await this.newContext(op).get(e.message, bindOp());
44
- }
45
- }
46
- async post(url, payload, op) {
47
- return await this.newContext(op).post(url, payload, op);
48
- }
49
- fixCmv() {
50
- return this._baseCmv - Math.floor(Math.random() * s_cmvRange);
51
- }
52
- }
53
- exports.HttpResolver = HttpResolver;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpResolver = void 0;
4
+ const xjs_common_1 = require("xjs-common");
5
+ const http_resolver_context_1 = require("./http-resolver-context");
6
+ const s_cmvRange = 5;
7
+ const s_defaultCmv = 138;
8
+ class HttpResolver {
9
+ _baseCmv;
10
+ _l;
11
+ /**
12
+ * @param _baseCmv chrome major version refered when construct a user agent, and the version will be randomized between `n` to `n-4`.
13
+ * @param _l custom logger. default is `console`.
14
+ */
15
+ constructor(_baseCmv = s_defaultCmv, _l = console) {
16
+ this._baseCmv = _baseCmv;
17
+ this._l = _l;
18
+ }
19
+ /**
20
+ * create a http client as new context that keeps some states. (browser type, cookies, ciphers order, etc...)
21
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
22
+ * @param op.proxy proxy configuration.
23
+ * @returns a http client as new context.
24
+ */
25
+ newContext(op) {
26
+ return new http_resolver_context_1.HttpResolverContext(this.fixCmv(), op, this._l);
27
+ }
28
+ async get(url, op) {
29
+ let redirectCount = op?.redirectAsNewRequest && -1;
30
+ const bindOp = () => {
31
+ const option = Object.assign({}, op);
32
+ if (redirectCount)
33
+ Object.assign(option, { outerRedirectCount: ++redirectCount });
34
+ return option;
35
+ };
36
+ try {
37
+ return await this.newContext(op).get(url, bindOp());
38
+ }
39
+ catch (e) {
40
+ if (!(e instanceof xjs_common_1.XjsErr) || e.code !== -1)
41
+ throw e;
42
+ else
43
+ return await this.newContext(op).get(e.message, bindOp());
44
+ }
45
+ }
46
+ async post(url, payload, op) {
47
+ return await this.newContext(op).post(url, payload, op);
48
+ }
49
+ fixCmv() {
50
+ return this._baseCmv - Math.floor(Math.random() * s_cmvRange);
51
+ }
52
+ }
53
+ exports.HttpResolver = HttpResolver;
@@ -1,90 +1,88 @@
1
- /// <reference types="node" />
2
- /// <reference types="node" />
3
- import { IncomingHttpHeaders, OutgoingHttpHeaders } from "http";
4
- import { ClientMode, ProxyConfig } from "./http-resolver";
5
- export interface ClientOption {
6
- /**
7
- * {@link s_clientMode} that is imitated. default is random between chrome or firefox.
8
- */
9
- mode?: ClientMode;
10
- /**
11
- * proxy configuration.
12
- */
13
- proxy?: ProxyConfig;
14
- }
15
- export interface RequestOption {
16
- headers?: OutgoingHttpHeaders;
17
- /**
18
- * if true, query part in the `url` is ignored.
19
- */
20
- ignoreQuery?: boolean;
21
- /**
22
- * destination directory or file path for download. this is only used when `Content-Disposition` header exists.
23
- * default is current directory of the process with `filename` of the disposition.
24
- */
25
- downloadPath?: string;
26
- /**
27
- * timeout milliseconds to wait for socket inactivity. default is infinity.
28
- */
29
- timeout?: number;
30
- /**
31
- * type of response payload. default is string (utf-8).
32
- */
33
- responseType?: "string" | "buffer";
34
- }
35
- export interface HttpResponse<T = string | Buffer> {
36
- /**
37
- * http headers in the response.
38
- */
39
- headers?: IncomingHttpHeaders;
40
- /**
41
- * response payload which has a type depends on {@link RequestOption.responseType}.
42
- */
43
- payload?: T;
44
- }
45
- export interface IHttpClient {
46
- /**
47
- * request GET to the url with new context.
48
- * @param url target url. (currently https only)
49
- * @param op.headers http headers.
50
- * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
51
- * @param op.proxy proxy configuration.
52
- * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
53
- * @param op.downloadPath {@link RequestOption.downloadPath}
54
- * @param op.timeout {@link RequestOption.timeout}
55
- * @param op.responseType {@link RequestOption.responseType}
56
- * @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
57
- * @returns http response. {@link HttpResponse}
58
- */
59
- get(url: string, op?: RequestOption & ClientOption & {
60
- redirectAsNewRequest?: boolean;
61
- responseType: "string";
62
- }): Promise<HttpResponse<string>>;
63
- get(url: string, op?: RequestOption & ClientOption & {
64
- redirectAsNewRequest?: boolean;
65
- responseType: "buffer";
66
- }): Promise<HttpResponse<Buffer>>;
67
- get(url: string, op?: RequestOption & ClientOption & {
68
- redirectAsNewRequest?: boolean;
69
- }): Promise<HttpResponse<string>>;
70
- /**
71
- * request POST to the url with new context.
72
- * @param url target url. (currently https only)
73
- * @param payload request payload. if this is a Stream, pipe will be used, otherwise if an object, this is treated as json.
74
- * @param op.headers http headers.
75
- * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
76
- * @param op.proxy proxy configuration.
77
- * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
78
- * @param op.downloadPath {@link RequestOption.downloadPath}
79
- * @param op.timeout {@link RequestOption.timeout}
80
- * @param op.responseType {@link RequestOption.responseType}
81
- * @returns http response. {@link HttpResponse}
82
- */
83
- post(url: string, payload: any, op?: RequestOption & ClientOption & {
84
- responseType: "string";
85
- }): Promise<HttpResponse<string>>;
86
- post(url: string, payload: any, op?: RequestOption & ClientOption & {
87
- responseType: "buffer";
88
- }): Promise<HttpResponse<Buffer>>;
89
- post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse<string>>;
90
- }
1
+ import { IncomingHttpHeaders, OutgoingHttpHeaders } from "http";
2
+ import { ClientMode, ProxyConfig } from "./http-resolver";
3
+ export interface ClientOption {
4
+ /**
5
+ * {@link s_clientMode} that is imitated. default is random between chrome or firefox.
6
+ */
7
+ mode?: ClientMode;
8
+ /**
9
+ * proxy configuration.
10
+ */
11
+ proxy?: ProxyConfig;
12
+ }
13
+ export interface RequestOption {
14
+ headers?: OutgoingHttpHeaders;
15
+ /**
16
+ * if true, query part in the `url` is ignored.
17
+ */
18
+ ignoreQuery?: boolean;
19
+ /**
20
+ * destination directory or file path for download. this is only used when `Content-Disposition` header exists.
21
+ * default is current directory of the process with `filename` of the disposition.
22
+ */
23
+ downloadPath?: string;
24
+ /**
25
+ * timeout milliseconds to wait for socket inactivity. default is infinity.
26
+ */
27
+ timeout?: number;
28
+ /**
29
+ * type of response payload. default is string (utf-8).
30
+ */
31
+ responseType?: "string" | "buffer";
32
+ }
33
+ export interface HttpResponse<T = string | Buffer> {
34
+ /**
35
+ * http headers in the response.
36
+ */
37
+ headers?: IncomingHttpHeaders;
38
+ /**
39
+ * response payload which has a type depends on {@link RequestOption.responseType}.
40
+ */
41
+ payload?: T;
42
+ }
43
+ export interface IHttpClient {
44
+ /**
45
+ * request GET to the url with new context.
46
+ * @param url target url. (currently https only)
47
+ * @param op.headers http headers.
48
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
49
+ * @param op.proxy proxy configuration.
50
+ * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
51
+ * @param op.downloadPath {@link RequestOption.downloadPath}
52
+ * @param op.timeout {@link RequestOption.timeout}
53
+ * @param op.responseType {@link RequestOption.responseType}
54
+ * @param op.redirectAsNewRequest handle redirect as new request. this may be efficient when using proxy which is implemented reverse proxy.
55
+ * @returns http response. {@link HttpResponse}
56
+ */
57
+ get(url: string, op?: RequestOption & ClientOption & {
58
+ redirectAsNewRequest?: boolean;
59
+ responseType: "string";
60
+ }): Promise<HttpResponse<string>>;
61
+ get(url: string, op?: RequestOption & ClientOption & {
62
+ redirectAsNewRequest?: boolean;
63
+ responseType: "buffer";
64
+ }): Promise<HttpResponse<Buffer>>;
65
+ get(url: string, op?: RequestOption & ClientOption & {
66
+ redirectAsNewRequest?: boolean;
67
+ }): Promise<HttpResponse<string>>;
68
+ /**
69
+ * request POST to the url with new context.
70
+ * @param url target url. (currently https only)
71
+ * @param payload request payload. if this is a Stream, pipe will be used, otherwise if an object, this is treated as json.
72
+ * @param op.headers http headers.
73
+ * @param op.mode {@link s_clientMode} that is imitated. default is random between chrome or firefox.
74
+ * @param op.proxy proxy configuration.
75
+ * @param op.ignoreQuery {@link RequestOption.ignoreQuery}
76
+ * @param op.downloadPath {@link RequestOption.downloadPath}
77
+ * @param op.timeout {@link RequestOption.timeout}
78
+ * @param op.responseType {@link RequestOption.responseType}
79
+ * @returns http response. {@link HttpResponse}
80
+ */
81
+ post(url: string, payload: any, op?: RequestOption & ClientOption & {
82
+ responseType: "string";
83
+ }): Promise<HttpResponse<string>>;
84
+ post(url: string, payload: any, op?: RequestOption & ClientOption & {
85
+ responseType: "buffer";
86
+ }): Promise<HttpResponse<Buffer>>;
87
+ post(url: string, payload: any, op?: RequestOption & ClientOption): Promise<HttpResponse<string>>;
88
+ }
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xjs-node",
3
- "version": "1.0.7",
3
+ "version": "2.0.1-alpha.1",
4
4
  "description": "library modules for nodejs + typescript that bundled general-purpose implementations.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -8,27 +8,34 @@
8
8
  },
9
9
  "keywords": [
10
10
  "nodejs",
11
+ "javascript",
11
12
  "typescript",
12
13
  "utility"
13
14
  ],
14
15
  "scripts": {
15
- "build": "tsc",
16
- "test": "node ./dist/test/test.js"
16
+ "build": "rm -rdf ./build && tsc",
17
+ "test": "tsx ./src/test/test.ts"
17
18
  },
18
19
  "author": "begyyal",
19
20
  "license": "Apache-2.0",
20
21
  "files": [
21
- "dist",
22
- "!/dist/test*"
22
+ "build",
23
+ "!/build/test*"
23
24
  ],
24
- "main": "./dist/index",
25
- "types": "./dist/index.d.ts",
25
+ "main": "./build/index",
26
+ "types": "./build/index.d.ts",
27
+ "exports": {
28
+ ".": [
29
+ "./build/index",
30
+ "./build/index.d.ts"
31
+ ]
32
+ },
26
33
  "devDependencies": {
27
- "@types/node": "^20.3.1",
28
- "ts-node": "^10.9.1",
29
- "typescript": "^4.9.5"
34
+ "@types/node": "^24.2.1",
35
+ "tsx": "^4.20.3",
36
+ "typescript": "^5.8.3"
30
37
  },
31
38
  "dependencies": {
32
- "xjs-common": "^10.1.3"
39
+ "xjs-common": "^11.0.1-alpha.3"
33
40
  }
34
41
  }