@minimaltech/node-infra 0.5.1 → 0.5.3-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/dist/base/loopback/@lb/core/index.d.ts +1 -0
- package/dist/base/loopback/@lb/core/index.js +1 -0
- package/dist/base/loopback/@lb/core/index.js.map +1 -1
- package/dist/base/loopback/filter/index.d.ts +2 -0
- package/dist/base/loopback/filter/index.js +18 -0
- package/dist/base/loopback/filter/index.js.map +1 -0
- package/dist/components/authenticate/controllers/oauth2.controller.js +2 -2
- package/dist/components/authenticate/controllers/oauth2.controller.js.map +1 -1
- package/dist/components/authorize/component.js +2 -2
- package/dist/components/authorize/component.js.map +1 -1
- package/dist/components/authorize/services/enforcer.service.js +2 -2
- package/dist/components/authorize/services/enforcer.service.js.map +1 -1
- package/dist/components/authorize/services/generator.service.js +3 -3
- package/dist/components/authorize/services/generator.service.js.map +1 -1
- package/dist/components/grpc/components/server.component.js +2 -2
- package/dist/components/grpc/components/server.component.js.map +1 -1
- package/dist/components/grpc/helpers/grpc-server.js +4 -4
- package/dist/components/grpc/helpers/grpc-server.js.map +1 -1
- package/dist/components/static-asset/controllers/resource.controller.js +7 -7
- package/dist/components/static-asset/controllers/resource.controller.js.map +1 -1
- package/dist/helpers/crypto/algorithms/aes.algorithm.d.ts +1 -1
- package/dist/helpers/crypto/algorithms/aes.algorithm.js +7 -7
- package/dist/helpers/crypto/algorithms/aes.algorithm.js.map +1 -1
- package/dist/helpers/logger/default-logger.js +3 -3
- package/dist/helpers/logger/default-logger.js.map +1 -1
- package/dist/helpers/network/base-tcp-client.helper.d.ts +56 -0
- package/dist/helpers/network/base-tcp-client.helper.js +134 -0
- package/dist/helpers/network/base-tcp-client.helper.js.map +1 -0
- package/dist/helpers/network/base-tcp-server.helper.d.ts +108 -0
- package/dist/helpers/network/base-tcp-server.helper.js +156 -0
- package/dist/helpers/network/base-tcp-server.helper.js.map +1 -0
- package/dist/helpers/network/index.d.ts +5 -1
- package/dist/helpers/network/index.js +5 -1
- package/dist/helpers/network/index.js.map +1 -1
- package/dist/helpers/network/network-tcp-client.helper.d.ts +5 -48
- package/dist/helpers/network/network-tcp-client.helper.js +4 -126
- package/dist/helpers/network/network-tcp-client.helper.js.map +1 -1
- package/dist/helpers/network/network-tcp-server.helper.d.ts +5 -80
- package/dist/helpers/network/network-tcp-server.helper.js +4 -143
- package/dist/helpers/network/network-tcp-server.helper.js.map +1 -1
- package/dist/helpers/network/network-tls-tcp-client.helper.d.ts +6 -0
- package/dist/helpers/network/network-tls-tcp-client.helper.js +16 -0
- package/dist/helpers/network/network-tls-tcp-client.helper.js.map +1 -0
- package/dist/helpers/network/network-tls-tcp-server.helper.d.ts +6 -0
- package/dist/helpers/network/network-tls-tcp-server.helper.js +15 -0
- package/dist/helpers/network/network-tls-tcp-server.helper.js.map +1 -0
- package/dist/helpers/queue/queue.helper.d.ts +13 -7
- package/dist/helpers/queue/queue.helper.js +19 -6
- package/dist/helpers/queue/queue.helper.js.map +1 -1
- package/dist/migrations/base-path.js +2 -2
- package/dist/migrations/base-path.js.map +1 -1
- package/package.json +9 -1
@@ -0,0 +1,134 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.BaseNetworkTcpClient = void 0;
|
7
|
+
const base_helper_1 = require("../../base/base.helper");
|
8
|
+
const isEmpty_1 = __importDefault(require("lodash/isEmpty"));
|
9
|
+
const DEFAULT_MAX_RETRY = 5;
|
10
|
+
class BaseNetworkTcpClient extends base_helper_1.BaseHelper {
|
11
|
+
constructor(opts) {
|
12
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
13
|
+
super({
|
14
|
+
scope: (_b = (_a = opts.scope) !== null && _a !== void 0 ? _a : opts.identifier) !== null && _b !== void 0 ? _b : BaseNetworkTcpClient.name,
|
15
|
+
identifier: opts.identifier,
|
16
|
+
});
|
17
|
+
this.retry = {
|
18
|
+
maxReconnect: DEFAULT_MAX_RETRY,
|
19
|
+
currentReconnect: 0,
|
20
|
+
};
|
21
|
+
this.options = opts.options;
|
22
|
+
this.retry = {
|
23
|
+
maxReconnect: (_c = opts.maxRetry) !== null && _c !== void 0 ? _c : DEFAULT_MAX_RETRY,
|
24
|
+
currentReconnect: 0,
|
25
|
+
};
|
26
|
+
this.createClientFn = opts.createClientFn;
|
27
|
+
this.onConnected = (_d = opts === null || opts === void 0 ? void 0 : opts.onConnected) !== null && _d !== void 0 ? _d : this.handleConnected;
|
28
|
+
this.onData = (_e = opts === null || opts === void 0 ? void 0 : opts.onData) !== null && _e !== void 0 ? _e : this.handleData;
|
29
|
+
this.onClosed = (_f = opts === null || opts === void 0 ? void 0 : opts.onClosed) !== null && _f !== void 0 ? _f : this.handleClosed;
|
30
|
+
this.onError = (_g = opts === null || opts === void 0 ? void 0 : opts.onError) !== null && _g !== void 0 ? _g : this.handleError;
|
31
|
+
this.reconnect = (_h = opts === null || opts === void 0 ? void 0 : opts.reconnect) !== null && _h !== void 0 ? _h : false;
|
32
|
+
this.encoding = opts === null || opts === void 0 ? void 0 : opts.encoding;
|
33
|
+
}
|
34
|
+
getClient() {
|
35
|
+
return this.client;
|
36
|
+
}
|
37
|
+
handleConnected() {
|
38
|
+
this.logger.info('[handleConnected][%s] Connected to TCP Server | Options: %j', this.identifier, this.options);
|
39
|
+
this.retry.currentReconnect = 0;
|
40
|
+
}
|
41
|
+
handleData(_opts) { }
|
42
|
+
handleClosed() {
|
43
|
+
this.logger.info('[handleClosed][%s] Closed connection TCP Server | Options: %j', this.identifier, this.options);
|
44
|
+
}
|
45
|
+
handleError(error) {
|
46
|
+
this.logger.error('[handleError][%s] Connection error | Options: %j | Error: %s', this.identifier, this.options, error);
|
47
|
+
if (!this.reconnect || this.retry.currentReconnect >= this.retry.maxReconnect) {
|
48
|
+
return;
|
49
|
+
}
|
50
|
+
const { currentReconnect, maxReconnect } = this.retry;
|
51
|
+
if (maxReconnect > -1 && currentReconnect >= maxReconnect) {
|
52
|
+
this.logger.info('[handleData] Exceeded max retry to reconnect! Max: %d | Current: %d', maxReconnect, currentReconnect);
|
53
|
+
return;
|
54
|
+
}
|
55
|
+
this.reconnectTimeout = setTimeout(() => {
|
56
|
+
var _a;
|
57
|
+
(_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy();
|
58
|
+
this.client = null;
|
59
|
+
this.logger.info('[handleClosed][%s] Retrying to establish TCP Connection | Options: %j', this.identifier, this.options);
|
60
|
+
this.connect({ resetReconnectCounter: false });
|
61
|
+
this.retry.currentReconnect++;
|
62
|
+
}, 5000);
|
63
|
+
}
|
64
|
+
connect(opts) {
|
65
|
+
var _a;
|
66
|
+
if (this.isConnected()) {
|
67
|
+
this.logger.info('[connect][%s] NetworkTcpClient is already initialized!', this.identifier);
|
68
|
+
return;
|
69
|
+
}
|
70
|
+
if ((0, isEmpty_1.default)(this.options)) {
|
71
|
+
this.logger.info('[connect][%s] Cannot init TCP Client with null options', this.identifier);
|
72
|
+
return;
|
73
|
+
}
|
74
|
+
this.logger.info('[connect][%s] New network tcp client | Options: %s', this.identifier, this.options);
|
75
|
+
if (opts === null || opts === void 0 ? void 0 : opts.resetReconnectCounter) {
|
76
|
+
this.retry.currentReconnect = 0;
|
77
|
+
}
|
78
|
+
if (this.client !== null && this.client !== undefined) {
|
79
|
+
(_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy();
|
80
|
+
this.client = null;
|
81
|
+
}
|
82
|
+
this.client = this.createClientFn(this.options, () => {
|
83
|
+
var _a;
|
84
|
+
(_a = this.onConnected) === null || _a === void 0 ? void 0 : _a.call(this);
|
85
|
+
});
|
86
|
+
if (this.encoding) {
|
87
|
+
this.client.setEncoding(this.encoding);
|
88
|
+
}
|
89
|
+
this.client.on('data', (message) => {
|
90
|
+
this.onData({ identifier: this.identifier, message });
|
91
|
+
});
|
92
|
+
this.client.on('close', () => {
|
93
|
+
var _a;
|
94
|
+
(_a = this.onClosed) === null || _a === void 0 ? void 0 : _a.call(this);
|
95
|
+
});
|
96
|
+
this.client.on('error', error => {
|
97
|
+
var _a;
|
98
|
+
(_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
99
|
+
});
|
100
|
+
}
|
101
|
+
disconnect() {
|
102
|
+
var _a;
|
103
|
+
if (!this.client) {
|
104
|
+
this.logger.info('[disconnect][%s] NetworkTcpClient is not initialized yet!', this.identifier);
|
105
|
+
return;
|
106
|
+
}
|
107
|
+
(_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy();
|
108
|
+
this.client = null;
|
109
|
+
clearTimeout(this.reconnectTimeout);
|
110
|
+
this.reconnectTimeout = null;
|
111
|
+
this.logger.info('[disconnect][%s] NetworkTcpClient is destroyed!', this.identifier);
|
112
|
+
}
|
113
|
+
forceReconnect() {
|
114
|
+
this.disconnect();
|
115
|
+
this.connect({ resetReconnectCounter: true });
|
116
|
+
}
|
117
|
+
isConnected() {
|
118
|
+
return this.client && this.client.readyState !== 'closed';
|
119
|
+
}
|
120
|
+
emit(opts) {
|
121
|
+
if (!this.client) {
|
122
|
+
this.logger.info('[emit][%s] TPC Client is not configured yet!', this.identifier);
|
123
|
+
return;
|
124
|
+
}
|
125
|
+
const { payload } = opts;
|
126
|
+
if (!(payload === null || payload === void 0 ? void 0 : payload.length)) {
|
127
|
+
this.logger.info('[emit][%s] Invalid payload to write to TCP Socket!', this.identifier);
|
128
|
+
return;
|
129
|
+
}
|
130
|
+
this.client.write(payload);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
exports.BaseNetworkTcpClient = BaseNetworkTcpClient;
|
134
|
+
//# sourceMappingURL=base-tcp-client.helper.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"base-tcp-client.helper.js","sourceRoot":"","sources":["../../../src/helpers/network/base-tcp-client.helper.ts"],"names":[],"mappings":";;;;;;AAAA,oD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,UAAU,KAAK,QAAQ,CAAC;IAC5D,CAAC;IAED,IAAI,CAAC,IAAkC;QACrC,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,CAAC;YACjB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,8CAA8C,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;YAClF,OAAO;QACT,CAAC;QAED,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC;QACzB,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAA,EAAE,CAAC;YACrB,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,oDAAoD,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;YACxF,OAAO;QACT,CAAC;QAED,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;CACF;AAnMD,oDAmMC"}
|
@@ -0,0 +1,108 @@
|
|
1
|
+
import { BaseHelper } from '../../base/base.helper';
|
2
|
+
import { ValueOrPromise } from '../../common/types';
|
3
|
+
import { dayjs } from '../../utilities/date.utility';
|
4
|
+
import { ListenOptions, ServerOpts, Server as SocketServer, Socket as SocketClient } from 'node:net';
|
5
|
+
interface ITcpSocketClient<SocketClientType> {
|
6
|
+
id: string;
|
7
|
+
socket: SocketClientType;
|
8
|
+
state: 'unauthorized' | 'authenticating' | 'authenticated';
|
9
|
+
subscriptions: Set<string>;
|
10
|
+
storage: {
|
11
|
+
connectedAt: dayjs.Dayjs;
|
12
|
+
authenticatedAt: dayjs.Dayjs | null;
|
13
|
+
[additionField: symbol | string]: any;
|
14
|
+
};
|
15
|
+
}
|
16
|
+
export interface ITcpSocketServerOptions<SocketServerOptions extends ServerOpts = ServerOpts, SocketServerType extends SocketServer = SocketServer, SocketClientType extends SocketClient = SocketClient> {
|
17
|
+
scope?: string;
|
18
|
+
identifier: string;
|
19
|
+
serverOptions: Partial<SocketServerOptions>;
|
20
|
+
listenOptions: Partial<ListenOptions>;
|
21
|
+
authenticateOptions: {
|
22
|
+
required: boolean;
|
23
|
+
duration?: number;
|
24
|
+
};
|
25
|
+
extraEvents?: Record<string, (opts: {
|
26
|
+
id: string;
|
27
|
+
socket: SocketClientType;
|
28
|
+
args: any;
|
29
|
+
}) => ValueOrPromise<void>>;
|
30
|
+
createServerFn: (options: Partial<SocketServerOptions>, connectionListener: (socket: SocketClientType) => void) => SocketServerType;
|
31
|
+
onServerReady?: (opts: {
|
32
|
+
server: SocketServerType;
|
33
|
+
}) => void;
|
34
|
+
onClientConnected?: (opts: {
|
35
|
+
id: string;
|
36
|
+
socket: SocketClientType;
|
37
|
+
}) => void;
|
38
|
+
onClientData?: (opts: {
|
39
|
+
id: string;
|
40
|
+
socket: SocketClientType;
|
41
|
+
data: Buffer | string;
|
42
|
+
}) => void;
|
43
|
+
onClientClose?: (opts: {
|
44
|
+
id: string;
|
45
|
+
socket: SocketClientType;
|
46
|
+
}) => void;
|
47
|
+
onClientError?: (opts: {
|
48
|
+
id: string;
|
49
|
+
socket: SocketClientType;
|
50
|
+
error: Error;
|
51
|
+
}) => void;
|
52
|
+
}
|
53
|
+
export declare class BaseNetworkTcpServer<SocketServerOptions extends ServerOpts = ServerOpts, SocketServerType extends SocketServer = SocketServer, SocketClientType extends SocketClient = SocketClient> extends BaseHelper {
|
54
|
+
protected serverOptions: Partial<SocketServerOptions>;
|
55
|
+
protected listenOptions: Partial<ListenOptions>;
|
56
|
+
protected authenticateOptions: {
|
57
|
+
required: boolean;
|
58
|
+
duration?: number;
|
59
|
+
};
|
60
|
+
protected clients: Record<string, ITcpSocketClient<SocketClientType>>;
|
61
|
+
protected server: SocketServerType;
|
62
|
+
protected extraEvents: Record<string, (opts: {
|
63
|
+
id: string;
|
64
|
+
socket: SocketClientType;
|
65
|
+
args: any;
|
66
|
+
}) => ValueOrPromise<void>>;
|
67
|
+
protected createServerFn: (options: Partial<SocketServerOptions>, connectionListener: (socket: SocketClientType) => void) => SocketServerType;
|
68
|
+
protected onServerReady?: (opts: {
|
69
|
+
server: SocketServerType;
|
70
|
+
}) => void;
|
71
|
+
protected onClientConnected?: (opts: {
|
72
|
+
id: string;
|
73
|
+
socket: SocketClientType;
|
74
|
+
}) => void;
|
75
|
+
protected onClientData?: (opts: {
|
76
|
+
id: string;
|
77
|
+
socket: SocketClientType;
|
78
|
+
data: Buffer | string;
|
79
|
+
}) => void;
|
80
|
+
protected onClientClose?: (opts: {
|
81
|
+
id: string;
|
82
|
+
socket: SocketClientType;
|
83
|
+
}) => void;
|
84
|
+
protected onClientError?: (opts: {
|
85
|
+
id: string;
|
86
|
+
socket: SocketClientType;
|
87
|
+
error: Error;
|
88
|
+
}) => void;
|
89
|
+
constructor(opts: ITcpSocketServerOptions<SocketServerOptions, SocketServerType, SocketClientType>);
|
90
|
+
configure(): void;
|
91
|
+
onNewConnection(opts: {
|
92
|
+
socket: SocketClientType;
|
93
|
+
}): void;
|
94
|
+
getClients(): Record<string, ITcpSocketClient<SocketClientType>>;
|
95
|
+
getClient(opts: {
|
96
|
+
id: string;
|
97
|
+
}): ITcpSocketClient<SocketClientType>;
|
98
|
+
getServer(): SocketServerType;
|
99
|
+
doAuthenticate(opts: {
|
100
|
+
id: string;
|
101
|
+
state: 'unauthorized' | 'authenticating' | 'authenticated';
|
102
|
+
}): void;
|
103
|
+
emit(opts: {
|
104
|
+
clientId: string;
|
105
|
+
payload: Buffer | string;
|
106
|
+
}): void;
|
107
|
+
}
|
108
|
+
export {};
|
@@ -0,0 +1,156 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.BaseNetworkTcpServer = void 0;
|
7
|
+
const base_helper_1 = require("../../base/base.helper");
|
8
|
+
const utilities_1 = require("../../utilities");
|
9
|
+
const date_utility_1 = require("../../utilities/date.utility");
|
10
|
+
const parse_utility_1 = require("../../utilities/parse.utility");
|
11
|
+
const omit_1 = __importDefault(require("lodash/omit"));
|
12
|
+
class BaseNetworkTcpServer extends base_helper_1.BaseHelper {
|
13
|
+
constructor(opts) {
|
14
|
+
var _a, _b, _c;
|
15
|
+
super({
|
16
|
+
scope: (_b = (_a = opts.scope) !== null && _a !== void 0 ? _a : opts.identifier) !== null && _b !== void 0 ? _b : BaseNetworkTcpServer.name,
|
17
|
+
identifier: opts.identifier,
|
18
|
+
});
|
19
|
+
this.serverOptions = {};
|
20
|
+
this.listenOptions = {};
|
21
|
+
this.clients = Object.assign({});
|
22
|
+
this.serverOptions = opts.serverOptions;
|
23
|
+
this.listenOptions = opts.listenOptions;
|
24
|
+
this.authenticateOptions = opts.authenticateOptions;
|
25
|
+
if (this.authenticateOptions.required &&
|
26
|
+
(!this.authenticateOptions.duration || this.authenticateOptions.duration < 0)) {
|
27
|
+
throw (0, utilities_1.getError)({
|
28
|
+
message: 'TCP Server | Invalid authenticate duration | Required duration for authenticateOptions',
|
29
|
+
});
|
30
|
+
}
|
31
|
+
this.extraEvents = (_c = opts.extraEvents) !== null && _c !== void 0 ? _c : {};
|
32
|
+
this.createServerFn = opts.createServerFn;
|
33
|
+
this.onServerReady = opts.onServerReady;
|
34
|
+
this.onClientConnected = opts.onClientConnected;
|
35
|
+
this.onClientData = opts.onClientData;
|
36
|
+
this.onClientClose = opts.onClientClose;
|
37
|
+
this.configure();
|
38
|
+
}
|
39
|
+
configure() {
|
40
|
+
this.server = this.createServerFn(this.serverOptions, socket => {
|
41
|
+
this.onNewConnection({ socket });
|
42
|
+
});
|
43
|
+
this.server.listen(this.listenOptions, () => {
|
44
|
+
var _a;
|
45
|
+
this.logger.info('[configure] TCP Socket Server is now listening | Options: %j', this.listenOptions);
|
46
|
+
(_a = this.onServerReady) === null || _a === void 0 ? void 0 : _a.call(this, { server: this.server });
|
47
|
+
});
|
48
|
+
}
|
49
|
+
onNewConnection(opts) {
|
50
|
+
var _a;
|
51
|
+
const { socket } = opts;
|
52
|
+
const id = (0, parse_utility_1.getUID)();
|
53
|
+
socket.on('data', (data) => {
|
54
|
+
var _a;
|
55
|
+
(_a = this.onClientData) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket, data });
|
56
|
+
});
|
57
|
+
socket.on('error', (error) => {
|
58
|
+
var _a;
|
59
|
+
this.logger.error('[onClientConnect][error] ID: %s | Error: %s', id, error);
|
60
|
+
(_a = this.onClientError) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket, error });
|
61
|
+
socket.end();
|
62
|
+
});
|
63
|
+
socket.on('close', (hasError) => {
|
64
|
+
var _a;
|
65
|
+
this.logger.info('[onClientConnect][close] ID: %s | hasError: %s', id, hasError);
|
66
|
+
(_a = this.onClientClose) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket });
|
67
|
+
this.clients = (0, omit_1.default)(this.clients, [id]);
|
68
|
+
});
|
69
|
+
for (const extraEvent in this.extraEvents) {
|
70
|
+
socket.on(extraEvent, args => {
|
71
|
+
this.extraEvents[extraEvent]({ id, socket, args });
|
72
|
+
});
|
73
|
+
}
|
74
|
+
this.clients[id] = {
|
75
|
+
id,
|
76
|
+
socket,
|
77
|
+
state: this.authenticateOptions.required ? 'unauthorized' : 'authenticated',
|
78
|
+
subscriptions: new Set([]),
|
79
|
+
storage: {
|
80
|
+
connectedAt: (0, date_utility_1.dayjs)(),
|
81
|
+
authenticatedAt: this.authenticateOptions.required ? null : (0, date_utility_1.dayjs)(),
|
82
|
+
},
|
83
|
+
};
|
84
|
+
this.logger.info('[onClientConnect] New TCP SocketClient | Client: %s | authenticateOptions: %s %s', `${id} - ${socket.remoteAddress} - ${socket.remotePort} - ${socket.remoteFamily}`, this.authenticateOptions.required, this.authenticateOptions.duration);
|
85
|
+
(_a = this.onClientConnected) === null || _a === void 0 ? void 0 : _a.call(this, { id, socket });
|
86
|
+
// Check client authentication
|
87
|
+
if (this.authenticateOptions.required &&
|
88
|
+
this.authenticateOptions.duration &&
|
89
|
+
this.authenticateOptions.duration > 0) {
|
90
|
+
setTimeout(() => {
|
91
|
+
const client = this.getClient({ id });
|
92
|
+
if (!client) {
|
93
|
+
return;
|
94
|
+
}
|
95
|
+
if (client.state === 'authenticated') {
|
96
|
+
return;
|
97
|
+
}
|
98
|
+
this.emit({ clientId: id, payload: 'Unauthorized Client' });
|
99
|
+
client.socket.end();
|
100
|
+
}, this.authenticateOptions.duration);
|
101
|
+
}
|
102
|
+
}
|
103
|
+
getClients() {
|
104
|
+
return this.clients;
|
105
|
+
}
|
106
|
+
getClient(opts) {
|
107
|
+
var _a;
|
108
|
+
return (_a = this.clients) === null || _a === void 0 ? void 0 : _a[opts.id];
|
109
|
+
}
|
110
|
+
getServer() {
|
111
|
+
return this.server;
|
112
|
+
}
|
113
|
+
doAuthenticate(opts) {
|
114
|
+
const { id, state } = opts;
|
115
|
+
const client = this.getClient({ id });
|
116
|
+
if (!client) {
|
117
|
+
this.logger.error('[authenticateClient][%s] Client %s NOT FOUND', id);
|
118
|
+
return;
|
119
|
+
}
|
120
|
+
client.state = state;
|
121
|
+
switch (state) {
|
122
|
+
case 'unauthorized':
|
123
|
+
case 'authenticating': {
|
124
|
+
client.storage.authenticatedAt = null;
|
125
|
+
break;
|
126
|
+
}
|
127
|
+
case 'authenticated': {
|
128
|
+
client.storage.authenticatedAt = (0, date_utility_1.dayjs)();
|
129
|
+
break;
|
130
|
+
}
|
131
|
+
default: {
|
132
|
+
break;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
emit(opts) {
|
137
|
+
const { clientId, payload } = opts;
|
138
|
+
const client = this.getClient({ id: clientId });
|
139
|
+
if (!client) {
|
140
|
+
this.logger.error('[emit][%s] Client %s NOT FOUND', clientId);
|
141
|
+
return;
|
142
|
+
}
|
143
|
+
const { socket } = client;
|
144
|
+
if (!socket.writable) {
|
145
|
+
this.logger.error('[emit][%s] Client %s NOT WRITABLE', clientId);
|
146
|
+
return;
|
147
|
+
}
|
148
|
+
if (!(payload === null || payload === void 0 ? void 0 : payload.length)) {
|
149
|
+
this.logger.info('[emit][%s] Client %s | Invalid payload to write to TCP Socket!', this.identifier, clientId);
|
150
|
+
return;
|
151
|
+
}
|
152
|
+
socket.write(payload);
|
153
|
+
}
|
154
|
+
}
|
155
|
+
exports.BaseNetworkTcpServer = BaseNetworkTcpServer;
|
156
|
+
//# sourceMappingURL=base-tcp-server.helper.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"base-tcp-server.helper.js","sourceRoot":"","sources":["../../../src/helpers/network/base-tcp-server.helper.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAgD;AAEhD,2CAAuC;AACvC,2DAAiD;AACjD,6DAAmD;AACnD,uDAA+B;AAkD/B,MAAa,oBAIX,SAAQ,wBAAU;IA6BlB,YACE,IAAsF;;QAEtF,KAAK,CAAC;YACJ,KAAK,EAAE,MAAA,MAAA,IAAI,CAAC,KAAK,mCAAI,IAAI,CAAC,UAAU,mCAAI,oBAAoB,CAAC,IAAI;YACjE,UAAU,EAAE,IAAI,CAAC,UAAU;SAC5B,CAAC,CAAC;QAlCK,kBAAa,GAAiC,EAAE,CAAC;QACjD,kBAAa,GAA2B,EAAE,CAAC;QAmCnD,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAEjC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC;QACxC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC;QACxC,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC,mBAAmB,CAAC;QAEpD,IACE,IAAI,CAAC,mBAAmB,CAAC,QAAQ;YACjC,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,QAAQ,IAAI,IAAI,CAAC,mBAAmB,CAAC,QAAQ,GAAG,CAAC,CAAC,EAC7E,CAAC;YACD,MAAM,IAAA,oBAAQ,EAAC;gBACb,OAAO,EACL,wFAAwF;aAC3F,CA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,QAAQ,CAAC,CAAC;YACjE,OAAO;QACT,CAAC;QAED,IAAI,CAAC,CAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,MAAM,CAAA,EAAE,CAAC;YACrB,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,gEAAgE,EAChE,IAAI,CAAC,UAAU,EACf,QAAQ,CACT,CAAC;YACF,OAAO;QACT,CAAC;QAED,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACxB,CAAC;CACF;AA5ND,oDA4NC"}
|
@@ -1,5 +1,9 @@
|
|
1
1
|
export * from './base-network-request';
|
2
|
-
export * from './
|
2
|
+
export * from './base-tcp-server.helper';
|
3
3
|
export * from './network-tcp-server.helper';
|
4
|
+
export * from './network-tls-tcp-server.helper';
|
5
|
+
export * from './base-tcp-client.helper';
|
6
|
+
export * from './network-tcp-client.helper';
|
7
|
+
export * from './network-tls-tcp-client.helper';
|
4
8
|
export * from './network-udp-client.helper';
|
5
9
|
export * from './network.helper';
|
@@ -15,8 +15,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
15
|
};
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
17
17
|
__exportStar(require("./base-network-request"), exports);
|
18
|
-
__exportStar(require("./
|
18
|
+
__exportStar(require("./base-tcp-server.helper"), exports);
|
19
19
|
__exportStar(require("./network-tcp-server.helper"), exports);
|
20
|
+
__exportStar(require("./network-tls-tcp-server.helper"), exports);
|
21
|
+
__exportStar(require("./base-tcp-client.helper"), exports);
|
22
|
+
__exportStar(require("./network-tcp-client.helper"), exports);
|
23
|
+
__exportStar(require("./network-tls-tcp-client.helper"), exports);
|
20
24
|
__exportStar(require("./network-udp-client.helper"), exports);
|
21
25
|
__exportStar(require("./network.helper"), exports);
|
22
26
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/helpers/network/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC;AAEvC,8DAA4C;AAC5C,8DAA4C;
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/helpers/network/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,yDAAuC;AAEvC,2DAAyC;AACzC,8DAA4C;AAC5C,kEAAgD;AAEhD,2DAAyC;AACzC,8DAA4C;AAC5C,kEAAgD;AAEhD,8DAA4C;AAC5C,mDAAiC"}
|
@@ -1,49 +1,6 @@
|
|
1
|
-
import { Socket
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
host: string;
|
7
|
-
port: number;
|
8
|
-
localAddress?: string;
|
9
|
-
};
|
10
|
-
reconnect?: boolean;
|
11
|
-
maxRetry?: number;
|
12
|
-
encoding?: BufferEncoding;
|
13
|
-
onConnected?: () => void;
|
14
|
-
onData?: (opts: {
|
15
|
-
identifier: string;
|
16
|
-
message: string | Buffer;
|
17
|
-
}) => void;
|
18
|
-
onClosed?: () => void;
|
19
|
-
onError?: (error: any) => void;
|
1
|
+
import { Socket, TcpSocketConnectOpts } from 'node:net';
|
2
|
+
import { BaseNetworkTcpClient, INetworkTcpClientProps } from './base-tcp-client.helper';
|
3
|
+
export declare class NetworkTcpClient extends BaseNetworkTcpClient<TcpSocketConnectOpts, Socket> {
|
4
|
+
constructor(opts: Omit<INetworkTcpClientProps<TcpSocketConnectOpts, Socket>, 'createClientFn'>);
|
5
|
+
static newInstance(opts: Omit<INetworkTcpClientProps<TcpSocketConnectOpts, Socket>, 'createClientFn'>): NetworkTcpClient;
|
20
6
|
}
|
21
|
-
export declare class NetworkTcpClient extends BaseHelper {
|
22
|
-
private client?;
|
23
|
-
private options;
|
24
|
-
private reconnect?;
|
25
|
-
private retry;
|
26
|
-
private reconnectTimeout;
|
27
|
-
private encoding?;
|
28
|
-
private onConnected;
|
29
|
-
private onData;
|
30
|
-
private onClosed?;
|
31
|
-
private onError?;
|
32
|
-
constructor(opts: INetworkTcpClientProps);
|
33
|
-
static newInstance(opts: INetworkTcpClientProps): NetworkTcpClient;
|
34
|
-
getClient(): SocketClient | null | undefined;
|
35
|
-
handleConnected(): void;
|
36
|
-
handleData(raw: any): void;
|
37
|
-
handleClosed(): void;
|
38
|
-
handleError(error: any): void;
|
39
|
-
connect(opts: {
|
40
|
-
resetReconnectCounter: boolean;
|
41
|
-
}): void;
|
42
|
-
disconnect(): void;
|
43
|
-
forceReconnect(): void;
|
44
|
-
isConnected(): boolean | null | undefined;
|
45
|
-
emit(opts: {
|
46
|
-
payload: Buffer | string;
|
47
|
-
}): void;
|
48
|
-
}
|
49
|
-
export {};
|
@@ -1,138 +1,16 @@
|
|
1
1
|
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
3
|
exports.NetworkTcpClient = void 0;
|
7
|
-
const
|
8
|
-
const
|
9
|
-
|
10
|
-
const DEFAULT_MAX_RETRY = 5;
|
11
|
-
class NetworkTcpClient extends base_helper_1.BaseHelper {
|
4
|
+
const node_net_1 = require("node:net");
|
5
|
+
const base_tcp_client_helper_1 = require("./base-tcp-client.helper");
|
6
|
+
class NetworkTcpClient extends base_tcp_client_helper_1.BaseNetworkTcpClient {
|
12
7
|
constructor(opts) {
|
13
|
-
|
14
|
-
super({ scope: NetworkTcpClient.name, identifier: opts.identifier });
|
15
|
-
this.retry = {
|
16
|
-
maxReconnect: DEFAULT_MAX_RETRY,
|
17
|
-
currentReconnect: 0,
|
18
|
-
};
|
8
|
+
super(Object.assign(Object.assign({}, opts), { scope: NetworkTcpClient.name, createClientFn: node_net_1.connect }));
|
19
9
|
this.options = opts.options;
|
20
|
-
this.retry = {
|
21
|
-
maxReconnect: (_a = opts.maxRetry) !== null && _a !== void 0 ? _a : DEFAULT_MAX_RETRY,
|
22
|
-
currentReconnect: 0,
|
23
|
-
};
|
24
|
-
this.onConnected = (_b = opts === null || opts === void 0 ? void 0 : opts.onConnected) !== null && _b !== void 0 ? _b : this.handleConnected;
|
25
|
-
this.onData = (_c = opts === null || opts === void 0 ? void 0 : opts.onData) !== null && _c !== void 0 ? _c : this.handleData;
|
26
|
-
this.onClosed = (_d = opts === null || opts === void 0 ? void 0 : opts.onClosed) !== null && _d !== void 0 ? _d : this.handleClosed;
|
27
|
-
this.onError = (_e = opts === null || opts === void 0 ? void 0 : opts.onError) !== null && _e !== void 0 ? _e : this.handleError;
|
28
|
-
this.reconnect = (_f = opts === null || opts === void 0 ? void 0 : opts.reconnect) !== null && _f !== void 0 ? _f : false;
|
29
|
-
this.encoding = opts === null || opts === void 0 ? void 0 : opts.encoding;
|
30
10
|
}
|
31
11
|
static newInstance(opts) {
|
32
12
|
return new NetworkTcpClient(opts);
|
33
13
|
}
|
34
|
-
getClient() {
|
35
|
-
return this.client;
|
36
|
-
}
|
37
|
-
handleConnected() {
|
38
|
-
this.logger.info('[handleConnected][%s] Connected to TCP Server | Options: %j', this.identifier, this.options);
|
39
|
-
this.retry.currentReconnect = 0;
|
40
|
-
}
|
41
|
-
handleData(raw) {
|
42
|
-
const { host, port } = this.options;
|
43
|
-
this.logger.info('[handleData][%s][%s:%d][<==] Raw: %s', this.identifier, host, port, raw);
|
44
|
-
}
|
45
|
-
handleClosed() {
|
46
|
-
this.logger.info('[handleClosed][%s] Closed connection TCP Server | Options: %j', this.identifier, this.options);
|
47
|
-
}
|
48
|
-
handleError(error) {
|
49
|
-
this.logger.error('[handleError][%s] Connection error | Options: %j | Error: %s', this.identifier, this.options, error);
|
50
|
-
if (!this.reconnect || this.retry.currentReconnect >= this.retry.maxReconnect) {
|
51
|
-
return;
|
52
|
-
}
|
53
|
-
const { currentReconnect, maxReconnect } = this.retry;
|
54
|
-
if (maxReconnect > -1 && currentReconnect >= maxReconnect) {
|
55
|
-
this.logger.info('[handleData] Exceeded max retry to reconnect! Max: %d | Current: %d', maxReconnect, currentReconnect);
|
56
|
-
return;
|
57
|
-
}
|
58
|
-
this.reconnectTimeout = setTimeout(() => {
|
59
|
-
var _a;
|
60
|
-
(_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy();
|
61
|
-
this.client = null;
|
62
|
-
this.logger.info('[handleClosed][%s] Retrying to establish TCP Connection | Options: %j', this.identifier, this.options);
|
63
|
-
this.connect({ resetReconnectCounter: false });
|
64
|
-
this.retry.currentReconnect++;
|
65
|
-
}, 5000);
|
66
|
-
}
|
67
|
-
connect(opts) {
|
68
|
-
var _a;
|
69
|
-
if (this.isConnected()) {
|
70
|
-
this.logger.info('[connect][%s] NetworkTcpClient is already initialized!', this.identifier);
|
71
|
-
return;
|
72
|
-
}
|
73
|
-
if ((0, isEmpty_1.default)(this.options)) {
|
74
|
-
this.logger.info('[connect][%s] Cannot init TCP Client with null options', this.identifier);
|
75
|
-
return;
|
76
|
-
}
|
77
|
-
this.logger.info('[connect][%s] New network tcp client | Options: %s', this.identifier, this.options);
|
78
|
-
if (opts === null || opts === void 0 ? void 0 : opts.resetReconnectCounter) {
|
79
|
-
this.retry.currentReconnect = 0;
|
80
|
-
}
|
81
|
-
if (this.client !== null && this.client !== undefined) {
|
82
|
-
(_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy();
|
83
|
-
this.client = null;
|
84
|
-
}
|
85
|
-
this.client = new net_1.Socket();
|
86
|
-
if (this.encoding) {
|
87
|
-
this.client.setEncoding(this.encoding);
|
88
|
-
}
|
89
|
-
this.client.connect(this.options, () => {
|
90
|
-
var _a;
|
91
|
-
(_a = this.onConnected) === null || _a === void 0 ? void 0 : _a.call(this);
|
92
|
-
});
|
93
|
-
this.client.on('data', (message) => {
|
94
|
-
this.onData({ identifier: this.identifier, message });
|
95
|
-
});
|
96
|
-
this.client.on('close', () => {
|
97
|
-
var _a;
|
98
|
-
(_a = this.onClosed) === null || _a === void 0 ? void 0 : _a.call(this);
|
99
|
-
});
|
100
|
-
this.client.on('error', error => {
|
101
|
-
var _a;
|
102
|
-
(_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, error);
|
103
|
-
});
|
104
|
-
}
|
105
|
-
disconnect() {
|
106
|
-
var _a;
|
107
|
-
if (!this.client) {
|
108
|
-
this.logger.info('[disconnect][%s] NetworkTcpClient is not initialized yet!', this.identifier);
|
109
|
-
return;
|
110
|
-
}
|
111
|
-
(_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy();
|
112
|
-
this.client = null;
|
113
|
-
clearTimeout(this.reconnectTimeout);
|
114
|
-
this.reconnectTimeout = null;
|
115
|
-
this.logger.info('[disconnect][%s] NetworkTcpClient is destroyed!', this.identifier);
|
116
|
-
}
|
117
|
-
forceReconnect() {
|
118
|
-
this.disconnect();
|
119
|
-
this.connect({ resetReconnectCounter: true });
|
120
|
-
}
|
121
|
-
isConnected() {
|
122
|
-
return this.client && this.client.readyState !== 'closed';
|
123
|
-
}
|
124
|
-
emit(opts) {
|
125
|
-
if (!this.client) {
|
126
|
-
this.logger.info('[emit][%s] TPC Client is not configured yet!', this.identifier);
|
127
|
-
return;
|
128
|
-
}
|
129
|
-
const { payload } = opts;
|
130
|
-
if (!(payload === null || payload === void 0 ? void 0 : payload.length)) {
|
131
|
-
this.logger.info('[emit][%s] Invalid payload to write to TCP Socket!', this.identifier);
|
132
|
-
return;
|
133
|
-
}
|
134
|
-
this.client.write(payload);
|
135
|
-
}
|
136
14
|
}
|
137
15
|
exports.NetworkTcpClient = NetworkTcpClient;
|
138
16
|
//# sourceMappingURL=network-tcp-client.helper.js.map
|