@ekhein/http-request 1.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 ADDED
@@ -0,0 +1,57 @@
1
+ # Sekiro Node.js Client
2
+
3
+ A Node.js client for the Sekiro WebSocket API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install sekiro-node-client
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```javascript
14
+ import SekiroClient from 'sekiro-node-client';
15
+
16
+ // Create client instance
17
+ const client = new SekiroClient('ws://your-sekiro-server:port', {
18
+ reconnectInterval: 2000,
19
+ maxReconnectAttempts: 10
20
+ });
21
+
22
+ // Register action handlers
23
+ client.registerAction('test', (request, success, failure) => {
24
+ try {
25
+ // Process request
26
+ success({ result: 'ok', data: request });
27
+ } catch (error) {
28
+ failure(error.message);
29
+ }
30
+ });
31
+
32
+ // Graceful shutdown
33
+ process.on('SIGINT', () => {
34
+ client.close();
35
+ process.exit();
36
+ });
37
+ ```
38
+
39
+ ## API
40
+
41
+ ### `new SekiroClient(wsURL, [options])`
42
+ Creates a new SekiroClient instance.
43
+
44
+ **Parameters:**
45
+ - `wsURL`: WebSocket server URL (required)
46
+ - `options`: Configuration options (optional)
47
+ - `reconnectInterval`: Base reconnect interval in ms (default: 2000)
48
+ - `maxReconnectAttempts`: Maximum reconnect attempts (default: 10)
49
+ - `wsOptions`: Additional WebSocket options
50
+
51
+ ### Methods:
52
+ - `registerAction(action, handler)`: Register a handler for an action
53
+ - `close()`: Close the WebSocket connection
54
+
55
+ ## License
56
+
57
+ MIT
@@ -0,0 +1,19 @@
1
+ import { CookieAgentOptions, HttpCookieAgent, HttpsCookieAgent } from 'http-cookie-agent/http';
2
+ import { LiuGuanProxy, Phone4GProxy } from '../proxy';
3
+ export declare const HttpCookieWithPhone4GProxyAgent: new (rest_0: {
4
+ clientIp: string;
5
+ }, rest_1: CookieAgentOptions) => Phone4GProxy;
6
+ export declare const HttpsCookieWithPhone4GProxyAgent: new (rest_0: {
7
+ clientIp: string;
8
+ }, rest_1: CookieAgentOptions) => Phone4GProxy;
9
+ export declare const HttpCookieWithLiuGuanProxyAgent: new (rest_0: {
10
+ adcode?: string;
11
+ visitorId?: string;
12
+ unbindTime?: string;
13
+ }, rest_1: CookieAgentOptions) => LiuGuanProxy;
14
+ export declare const HttpsCookieWithLiuGuanProxyAgent: new (rest_0: {
15
+ adcode?: string;
16
+ visitorId?: string;
17
+ unbindTime?: string;
18
+ }, rest_1: CookieAgentOptions) => LiuGuanProxy;
19
+ export { HttpCookieAgent, HttpsCookieAgent };
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HttpsCookieAgent = exports.HttpCookieAgent = exports.HttpsCookieWithLiuGuanProxyAgent = exports.HttpCookieWithLiuGuanProxyAgent = exports.HttpsCookieWithPhone4GProxyAgent = exports.HttpCookieWithPhone4GProxyAgent = void 0;
4
+ const http_1 = require("http-cookie-agent/http");
5
+ Object.defineProperty(exports, "HttpCookieAgent", { enumerable: true, get: function () { return http_1.HttpCookieAgent; } });
6
+ Object.defineProperty(exports, "HttpsCookieAgent", { enumerable: true, get: function () { return http_1.HttpsCookieAgent; } });
7
+ const proxy_1 = require("../proxy");
8
+ exports.HttpCookieWithPhone4GProxyAgent = (0, http_1.createCookieAgent)(proxy_1.Phone4GProxy);
9
+ exports.HttpsCookieWithPhone4GProxyAgent = (0, http_1.createCookieAgent)(proxy_1.Phone4GProxy);
10
+ exports.HttpCookieWithLiuGuanProxyAgent = (0, http_1.createCookieAgent)(proxy_1.LiuGuanProxy);
11
+ exports.HttpsCookieWithLiuGuanProxyAgent = (0, http_1.createCookieAgent)(proxy_1.LiuGuanProxy);
@@ -0,0 +1,4 @@
1
+ import { AxiosError, InternalAxiosRequestConfig } from "axios";
2
+ export declare class AxiosRetryLimitError extends AxiosError {
3
+ constructor(config: InternalAxiosRequestConfig);
4
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AxiosRetryLimitError = void 0;
4
+ const axios_1 = require("axios");
5
+ class AxiosRetryLimitError extends axios_1.AxiosError {
6
+ constructor(config) {
7
+ const code = String("AXIOS_RETRY_LIMIT_ERROR");
8
+ const message = String("Axios retry limit error");
9
+ super(message, code, config);
10
+ }
11
+ }
12
+ exports.AxiosRetryLimitError = AxiosRetryLimitError;
@@ -0,0 +1,4 @@
1
+ import { AxiosError, InternalAxiosRequestConfig } from "axios";
2
+ export declare class AxiosRetryableError extends AxiosError {
3
+ constructor(config: InternalAxiosRequestConfig);
4
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AxiosRetryableError = void 0;
4
+ const axios_1 = require("axios");
5
+ class AxiosRetryableError extends axios_1.AxiosError {
6
+ constructor(config) {
7
+ const code = String("AXIOS_RETRYABLE_ERROR");
8
+ const message = String("Axios retryable error");
9
+ super(message, code, config);
10
+ }
11
+ }
12
+ exports.AxiosRetryableError = AxiosRetryableError;
@@ -0,0 +1,2 @@
1
+ export { AxiosRetryableError } from './axiosRetryableError';
2
+ export { AxiosRetryLimitError } from './axiosRetryLimitError';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AxiosRetryLimitError = exports.AxiosRetryableError = void 0;
4
+ var axiosRetryableError_1 = require("./axiosRetryableError");
5
+ Object.defineProperty(exports, "AxiosRetryableError", { enumerable: true, get: function () { return axiosRetryableError_1.AxiosRetryableError; } });
6
+ var axiosRetryLimitError_1 = require("./axiosRetryLimitError");
7
+ Object.defineProperty(exports, "AxiosRetryLimitError", { enumerable: true, get: function () { return axiosRetryLimitError_1.AxiosRetryLimitError; } });
package/dist/main.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { AxiosInstance, AxiosRequestConfig } from "axios";
2
+ export declare class HttpRequest {
3
+ readonly instance: AxiosInstance;
4
+ constructor(options: AxiosRequestConfig);
5
+ execute<R = any>(options: AxiosRequestConfig): Promise<R>;
6
+ post<R = any>(url: string, data?: any): Promise<R>;
7
+ call<R = any>(path: string, data: object): Promise<R>;
8
+ }
package/dist/main.js ADDED
@@ -0,0 +1,216 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.HttpRequest = void 0;
37
+ const axios_1 = __importStar(require("axios"));
38
+ const jsdom_1 = require("jsdom");
39
+ const lodash_1 = require("lodash");
40
+ const tough_cookie_1 = require("tough-cookie");
41
+ const cookie_1 = require("cookie");
42
+ const tldts_1 = require("tldts");
43
+ const common_1 = require("@nestjs/common");
44
+ const http_error_by_code_util_1 = require("@nestjs/common/utils/http-error-by-code.util");
45
+ const agent_1 = require("./agent");
46
+ const agent_2 = require("./agent");
47
+ const agent_3 = require("./agent");
48
+ const exceptions_1 = require("./exceptions");
49
+ class HttpRequest {
50
+ instance;
51
+ constructor(options) {
52
+ /* Axios */
53
+ this.instance = axios_1.default.create({
54
+ ...Object.assign({
55
+ ["headers"]: {},
56
+ ["retryLimit"]: 3,
57
+ }, options, {
58
+ ["proxy"]: false,
59
+ ["retryCount"]: 0,
60
+ })
61
+ });
62
+ /* Set Http Agent */
63
+ this.instance.interceptors.request.use(async (request) => {
64
+ const jar = Object(request.jar);
65
+ const cookies = Object({ jar });
66
+ if (request.useProxy) {
67
+ if (typeof request.useProxy === "boolean") {
68
+ request.httpAgent = new agent_3.HttpCookieWithLiuGuanProxyAgent({}, { cookies });
69
+ request.httpsAgent = new agent_3.HttpsCookieWithLiuGuanProxyAgent({}, { cookies });
70
+ }
71
+ if (typeof request.useProxy === "object") {
72
+ if ("clientIp" in request.useProxy) {
73
+ request.httpAgent = new agent_2.HttpCookieWithPhone4GProxyAgent(request.useProxy, { cookies });
74
+ request.httpsAgent = new agent_2.HttpsCookieWithPhone4GProxyAgent(request.useProxy, { cookies });
75
+ }
76
+ else {
77
+ request.httpAgent = new agent_3.HttpCookieWithLiuGuanProxyAgent(request.useProxy, { cookies });
78
+ request.httpsAgent = new agent_3.HttpsCookieWithLiuGuanProxyAgent(request.useProxy, { cookies });
79
+ }
80
+ }
81
+ }
82
+ else {
83
+ request.httpAgent = new agent_1.HttpCookieAgent({ cookies });
84
+ request.httpsAgent = new agent_1.HttpsCookieAgent({ cookies });
85
+ }
86
+ return request;
87
+ });
88
+ /* Set Cookie */
89
+ this.instance.interceptors.request.use(async (request) => {
90
+ if (request.headers.cookie) {
91
+ const api = new URL(request.url, request.baseURL);
92
+ const domain = (0, tldts_1.getDomain)(api.href); /* 一级域名 */
93
+ const cookies = (0, cookie_1.parse)(request.headers.cookie);
94
+ const website = Array(api.protocol, domain).join("//");
95
+ const entries = Object.entries(cookies);
96
+ for (const [key, value] of entries) {
97
+ const cookie = new tough_cookie_1.Cookie({ key, value, domain });
98
+ request.jar.setCookieSync(cookie, website);
99
+ }
100
+ }
101
+ return request;
102
+ });
103
+ /* Set Cookie Jar */
104
+ this.instance.interceptors.request.use(async (request) => {
105
+ if (!request.jar)
106
+ request.jar = new tough_cookie_1.CookieJar();
107
+ return request;
108
+ });
109
+ /* Headers Format */
110
+ this.instance.interceptors.request.use(async (request) => {
111
+ if (request.headers) {
112
+ const headers = Object.entries(request.headers);
113
+ for (const [key, value] of headers) {
114
+ if (/^[a-zA-Z]+(?:[A-Z][a-z]*)*$/.test(key)) {
115
+ const lowerKey = String(key).replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
116
+ request.headers.delete(key);
117
+ request.headers.set(lowerKey, value);
118
+ }
119
+ }
120
+ }
121
+ return request;
122
+ });
123
+ /* Set Body */
124
+ this.instance.interceptors.request.use(async (request) => {
125
+ if (request.body) {
126
+ request.headers.setContentType("application/json");
127
+ request.data = request.body;
128
+ }
129
+ if (request.form) {
130
+ request.headers.setContentType("application/x-www-form-urlencoded");
131
+ request.data = request.form;
132
+ }
133
+ return request;
134
+ });
135
+ /* Transform */
136
+ this.instance.interceptors.request.use(async (request) => {
137
+ if (request.serviceType == 1) { }
138
+ if (request.serviceType == 2) {
139
+ const group = String(request.path).split("/").at(-2);
140
+ const action = String(request.path).split("/").at(-1);
141
+ const invoke_timeout = Number(request.timeout - 5e3);
142
+ request.url = String("/business-demo/invoke");
143
+ request.params = Object({ group, action, invoke_timeout });
144
+ }
145
+ return request;
146
+ });
147
+ /* Call Custom Hook */
148
+ this.instance.interceptors.request.use(options.interceptorHooks?.requestInterceptor, options.interceptorHooks?.requestInterceptorCatch);
149
+ /* Save Origin Config */
150
+ this.instance.interceptors.request.use(async (request) => {
151
+ request.__origin__ = (0, lodash_1.cloneDeep)(request);
152
+ return (request);
153
+ });
154
+ /* Transform */
155
+ this.instance.interceptors.response.use(async (response) => {
156
+ if (response.config.serviceType == 1) {
157
+ if (http_error_by_code_util_1.HttpErrorByCode[response.data.err])
158
+ throw new http_error_by_code_util_1.HttpErrorByCode[response.data.err](response.data.msg);
159
+ return response.data.res;
160
+ }
161
+ if (response.config.serviceType == 2) {
162
+ if (response.data.status)
163
+ throw new common_1.ServiceUnavailableException(response.data.message);
164
+ return response.data.data;
165
+ }
166
+ if (response.config.resolveBodyOnly != false) {
167
+ if (response.config.html != true)
168
+ return response.data;
169
+ return new jsdom_1.JSDOM(response.data);
170
+ }
171
+ return response;
172
+ });
173
+ /* Call Custom Hook */
174
+ this.instance.interceptors.response.use(options.interceptorHooks?.respondInterceptor, options.interceptorHooks?.respondInterceptorCatch);
175
+ /* Handler Timeout */
176
+ this.instance.interceptors.response.use(async (response) => response, async (error) => {
177
+ if (error instanceof axios_1.AxiosError) {
178
+ if (!error.code)
179
+ throw new exceptions_1.AxiosRetryableError(error.config);
180
+ switch (error.code) {
181
+ case axios_1.AxiosError.ETIMEDOUT:
182
+ case axios_1.AxiosError.ERR_NETWORK:
183
+ case axios_1.AxiosError.ECONNABORTED:
184
+ throw new exceptions_1.AxiosRetryableError(error.config);
185
+ }
186
+ }
187
+ throw error;
188
+ });
189
+ /* Auto Retry */
190
+ this.instance.interceptors.response.use(async (response) => response, async (error) => {
191
+ if (error instanceof exceptions_1.AxiosRetryableError) {
192
+ const retryCount = error.config.__origin__.retryCount++;
193
+ const retryLimit = error.config.__origin__.retryLimit;
194
+ if (retryCount < retryLimit)
195
+ return this.instance.request(error.config.__origin__);
196
+ throw new exceptions_1.AxiosRetryLimitError(error.config);
197
+ }
198
+ else {
199
+ throw error;
200
+ }
201
+ });
202
+ }
203
+ async execute(options) {
204
+ return this.instance
205
+ .request(options);
206
+ }
207
+ async post(url, data) {
208
+ return this.instance
209
+ .request({ method: "POST", serviceType: 1, url, data });
210
+ }
211
+ async call(path, data) {
212
+ return this.instance
213
+ .request({ method: "POST", serviceType: 2, path, data });
214
+ }
215
+ }
216
+ exports.HttpRequest = HttpRequest;
@@ -0,0 +1,2 @@
1
+ export { LiuGuanProxy } from './liuguan.proxy';
2
+ export { Phone4GProxy } from './phone4g.proxy';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Phone4GProxy = exports.LiuGuanProxy = void 0;
4
+ var liuguan_proxy_1 = require("./liuguan.proxy");
5
+ Object.defineProperty(exports, "LiuGuanProxy", { enumerable: true, get: function () { return liuguan_proxy_1.LiuGuanProxy; } });
6
+ var phone4g_proxy_1 = require("./phone4g.proxy");
7
+ Object.defineProperty(exports, "Phone4GProxy", { enumerable: true, get: function () { return phone4g_proxy_1.Phone4GProxy; } });