@nsshunt/stsappframework 3.1.236 → 3.1.237
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/stsappframework.mjs +1979 -0
- package/dist/stsappframework.mjs.map +1 -0
- package/dist/stsappframework.umd.js +1954 -0
- package/dist/stsappframework.umd.js.map +1 -0
- package/package.json +21 -7
- package/.github/dependabot.yml +0 -13
- package/.github/workflows/npm-publish.yml +0 -46
- package/.gitignore copy +0 -108
- package/build.sh +0 -36
- package/dist/index.js +0 -30
- package/eslint.config.mjs +0 -55
- package/jest/setEnvVars.js +0 -19
- package/keys/server.cert +0 -21
- package/keys/server.key +0 -28
- package/local-redis-stack.conf +0 -2
- package/run-grpc-client.sh +0 -2
- package/run-grpc-server.sh +0 -2
- package/run1.sh +0 -20
- package/run2.sh +0 -20
- package/run3.sh +0 -20
- package/runc1.sh +0 -19
- package/runc2.sh +0 -19
- package/runkafka.sh +0 -19
- package/runkafkaconsume01.sh +0 -21
- package/runkafkaconsume02.sh +0 -21
- package/runpromise.sh +0 -5
- package/runredis.sh +0 -5
- package/runredis1.sh +0 -4
- package/runredis2.sh +0 -24
- package/runredis3.sh +0 -4
- package/runtest1.sh +0 -19
- package/runtest2.sh +0 -19
- package/runtest_ipc_legacy.sh +0 -19
- package/runtest_ipcex.sh +0 -19
- package/runtest_redis.sh +0 -19
- package/runtest_ww.sh +0 -19
- package/src/commonTypes.ts +0 -374
- package/src/controller/stscontrollerbase.ts +0 -14
- package/src/controller/stslatencycontroller.ts +0 -26
- package/src/index.ts +0 -13
- package/src/logger/stsTransportLoggerWinston.ts +0 -24
- package/src/logger/stsTransportWinston.ts +0 -48
- package/src/middleware/serverNetworkMiddleware.ts +0 -243
- package/src/network.ts +0 -36
- package/src/process/masterprocessbase.ts +0 -674
- package/src/process/processbase.ts +0 -483
- package/src/process/serverprocessbase.ts +0 -455
- package/src/process/singleprocessbase.ts +0 -63
- package/src/process/workerprocessbase.ts +0 -224
- package/src/publishertransports/publishTransportUtils.ts +0 -53
- package/src/route/stslatencyroute.ts +0 -15
- package/src/route/stsrouterbase.ts +0 -21
- package/src/stsexpressserver.ts +0 -137
- package/src/validation/errors.ts +0 -6
- package/src/vitesttesting/appConfig.ts +0 -111
- package/src/vitesttesting/appSingleWSS.ts +0 -142
- package/src/vitesttesting/server.ts +0 -17
- package/src/vitesttesting/singleservertest.test.ts +0 -352
- package/src/vitesttesting/wsevents.ts +0 -44
- package/tsconfig.json +0 -42
- package/vite.config.ts +0 -19
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
/* eslint @typescript-eslint/no-explicit-any: 0, @typescript-eslint/no-unused-vars: 0 */ // --> OFF
|
|
2
|
-
import chalk from 'chalk'; // Note: Do NOT upgrade beyond 4.1.2 as the ESM version (5.0.0+) breaks this entire codebase
|
|
3
|
-
import { Request, Response, NextFunction } from 'express'
|
|
4
|
-
import { Socket } from 'node:net'
|
|
5
|
-
import { TinyEmitter } from 'tiny-emitter';
|
|
6
|
-
|
|
7
|
-
import { ISTSLogger, STSOptionsBase } from '@nsshunt/stsutils';
|
|
8
|
-
|
|
9
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
10
|
-
import onHeaders from 'on-headers';
|
|
11
|
-
|
|
12
|
-
export enum ServerNetworkMiddlewareEventName {
|
|
13
|
-
UpdateInstrument_SERVER_NET_VAL = 'UpdateInstrument_SERVER_NET_VAL' // request net stats
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ISocketRecord {
|
|
17
|
-
id: string
|
|
18
|
-
socket: Socket,
|
|
19
|
-
currentBytesRead: number
|
|
20
|
-
currentBytesWritten: number
|
|
21
|
-
lastBytesRead: number
|
|
22
|
-
lastBytesWritten: number
|
|
23
|
-
requestBytesRead: number
|
|
24
|
-
requestBytesWritten: number
|
|
25
|
-
originalUrl: string
|
|
26
|
-
eventName: string
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export type ServerNetworkMiddlewareEventFunc = (data: ISocketRecord) => void;
|
|
30
|
-
|
|
31
|
-
export interface IServerNetworkMiddleware {
|
|
32
|
-
name: string
|
|
33
|
-
outputDebug: boolean
|
|
34
|
-
logger: ISTSLogger
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export class ServerNetworkMiddleware extends STSOptionsBase
|
|
38
|
-
{
|
|
39
|
-
#tinyEmitter: TinyEmitter = new TinyEmitter();
|
|
40
|
-
#socketCollection: Record<string, ISocketRecord> = { };
|
|
41
|
-
#id: string = '';
|
|
42
|
-
|
|
43
|
-
constructor(options: IServerNetworkMiddleware) {
|
|
44
|
-
super(options);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
#LogSillyMessage(message: any) {
|
|
48
|
-
this.options?.logger.silly(message);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
#LogErrorMessage(message: any) {
|
|
52
|
-
this.options?.logger.error(message);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
on(eventName: ServerNetworkMiddlewareEventName, callBackFn: ServerNetworkMiddlewareEventFunc) {
|
|
56
|
-
this.#tinyEmitter.on(eventName, callBackFn);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
GetSocketRecord = (socket: Socket): ISocketRecord | null => {
|
|
60
|
-
for (const [, socketRecord] of Object.entries(this.#socketCollection)) {
|
|
61
|
-
if (socketRecord.socket === socket) {
|
|
62
|
-
return socketRecord;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
UpdateNetworkStats = (workingSocketRecord: ISocketRecord, eventName: string, req: Request) => {
|
|
69
|
-
workingSocketRecord.originalUrl = req.originalUrl;
|
|
70
|
-
workingSocketRecord.eventName = eventName;
|
|
71
|
-
|
|
72
|
-
workingSocketRecord.currentBytesRead = req.socket.bytesRead;
|
|
73
|
-
workingSocketRecord.currentBytesWritten = req.socket.bytesWritten;
|
|
74
|
-
|
|
75
|
-
workingSocketRecord.requestBytesRead = workingSocketRecord.currentBytesRead - workingSocketRecord.lastBytesRead;
|
|
76
|
-
workingSocketRecord.requestBytesWritten = workingSocketRecord.currentBytesWritten - workingSocketRecord.lastBytesWritten;
|
|
77
|
-
|
|
78
|
-
//this.#LogDebugMessage(chalk.gray(`totalBytesRead: [${workingSocketRecord.id}] [${workingSocketRecord.originalUrl}] [${eventName}] [${workingSocketRecord.requestBytesRead}]`));
|
|
79
|
-
//this.#LogDebugMessage(chalk.gray(`totalBytesWritten: [${workingSocketRecord.id}] [${workingSocketRecord.originalUrl}] [${eventName}] [${workingSocketRecord.requestBytesWritten}]`));
|
|
80
|
-
|
|
81
|
-
const workingSocketEventRecord = { ...workingSocketRecord };
|
|
82
|
-
delete (workingSocketEventRecord as any).socket;
|
|
83
|
-
|
|
84
|
-
this.#LogSillyMessage(chalk.gray(`Sending event: [${JSON.stringify(workingSocketEventRecord)}]`));
|
|
85
|
-
|
|
86
|
-
this.#tinyEmitter.emit(ServerNetworkMiddlewareEventName.UpdateInstrument_SERVER_NET_VAL, workingSocketEventRecord);
|
|
87
|
-
|
|
88
|
-
workingSocketRecord.lastBytesRead = workingSocketRecord.currentBytesRead;
|
|
89
|
-
workingSocketRecord.lastBytesWritten = workingSocketRecord.currentBytesWritten;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
#GetRequestContentSize = (req: Request) => {
|
|
93
|
-
try {
|
|
94
|
-
let contentLength = 0;
|
|
95
|
-
if ((req as any)['_contentLength']) {
|
|
96
|
-
contentLength = (req as any)['_contentLength'];
|
|
97
|
-
} else {
|
|
98
|
-
if (req.headers['content-length']) {
|
|
99
|
-
contentLength = parseInt(req.headers['content-length'])
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return contentLength;
|
|
103
|
-
} catch (error) {
|
|
104
|
-
this.#LogErrorMessage(chalk.red(`ServerNetworkMiddleware:#GetRequestContentSize(): Error: [${error}]`));
|
|
105
|
-
return 0;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
Middleware = (req: Request, res: Response, next: NextFunction) => {
|
|
110
|
-
let workingSocketRecord: ISocketRecord | null = null;
|
|
111
|
-
|
|
112
|
-
workingSocketRecord = this.GetSocketRecord(req.socket);
|
|
113
|
-
|
|
114
|
-
if (!workingSocketRecord) {
|
|
115
|
-
this.#id = uuidv4();
|
|
116
|
-
workingSocketRecord = {
|
|
117
|
-
id: `${(this.options as IServerNetworkMiddleware).name}_${this.#id.toString()}`,
|
|
118
|
-
socket: req.socket,
|
|
119
|
-
currentBytesRead: 0,
|
|
120
|
-
currentBytesWritten: 0,
|
|
121
|
-
lastBytesRead: 0,
|
|
122
|
-
lastBytesWritten: 0,
|
|
123
|
-
requestBytesRead: 0,
|
|
124
|
-
requestBytesWritten: 0,
|
|
125
|
-
originalUrl: req.originalUrl,
|
|
126
|
-
eventName: '',
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
this.#socketCollection[workingSocketRecord.id] = workingSocketRecord;
|
|
130
|
-
|
|
131
|
-
this.#LogSillyMessage(chalk.gray(`Adding new socket to recordset: ID: [${workingSocketRecord.id}], originalUrl: [${workingSocketRecord.originalUrl}]`));
|
|
132
|
-
|
|
133
|
-
workingSocketRecord.socket.on('data', () => {
|
|
134
|
-
const socketRecord = this.GetSocketRecord((workingSocketRecord as ISocketRecord).socket);
|
|
135
|
-
if (socketRecord) {
|
|
136
|
-
this.#LogSillyMessage(chalk.gray(`Socket data event: ID: [${socketRecord.id}], originalUrl: [${socketRecord.originalUrl}]`));
|
|
137
|
-
this.UpdateNetworkStats(socketRecord, 'socket_data', req);
|
|
138
|
-
} else {
|
|
139
|
-
this.#LogSillyMessage(chalk.magenta(`Socket data event: Could not find socket within recordset`));
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
workingSocketRecord.socket.on('close', () => {
|
|
144
|
-
const socketRecord = this.GetSocketRecord((workingSocketRecord as ISocketRecord).socket);
|
|
145
|
-
if (socketRecord) {
|
|
146
|
-
this.#LogSillyMessage(chalk.gray(`Socket close event: ID: [${socketRecord.id}], originalUrl: [${socketRecord.originalUrl}]`));
|
|
147
|
-
this.UpdateNetworkStats(socketRecord, 'socket_close', req);
|
|
148
|
-
delete this.#socketCollection[socketRecord.id];
|
|
149
|
-
this.#LogSillyMessage(chalk.gray(`Socket removed from recordset: ID: [${socketRecord.id}], originalUrl: [${socketRecord.originalUrl}]`));
|
|
150
|
-
} else {
|
|
151
|
-
this.#LogSillyMessage(chalk.magenta(`Socket close event: Could not find socket within recordset`));
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
workingSocketRecord.socket.on('end', () => {
|
|
156
|
-
const socketRecord = this.GetSocketRecord((workingSocketRecord as ISocketRecord).socket);
|
|
157
|
-
if (socketRecord) {
|
|
158
|
-
this.#LogSillyMessage(chalk.gray(`Socket end event: ID: [${socketRecord.id}], originalUrl: [${socketRecord.originalUrl}]`));
|
|
159
|
-
this.UpdateNetworkStats(socketRecord, 'socket_end', req);
|
|
160
|
-
} else {
|
|
161
|
-
this.#LogSillyMessage(chalk.magenta(`Socket end event: Could not find socket within recordset`));
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// The following gets the payload size from adding _contentLength or content-length to the header size.
|
|
167
|
-
// From brief observations, this appears to be about 80 bytes short from what is reported by the socket writtenBytes.
|
|
168
|
-
onHeaders(res, () => {
|
|
169
|
-
let contentLength = 0;
|
|
170
|
-
if ((res as any)['_contentLength']) {
|
|
171
|
-
contentLength = (res as any)['_contentLength'];
|
|
172
|
-
} else {
|
|
173
|
-
if (res.hasHeader('content-length')) {
|
|
174
|
-
contentLength = parseInt(res.getHeader('content-length') as string);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// Ensure that the client does NOT cache the response
|
|
179
|
-
res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); // HTTP 1.1
|
|
180
|
-
res.setHeader('Pragma', 'no-cache'); // HTTP 1.0
|
|
181
|
-
res.setHeader('Expires', '0'); // Proxies
|
|
182
|
-
|
|
183
|
-
const headerLength = JSON.stringify(res.getHeaders()).length;
|
|
184
|
-
const totalSize = contentLength + headerLength;
|
|
185
|
-
|
|
186
|
-
this.#LogSillyMessage(chalk.white.bgBlue(`contentLength: [${contentLength}], headerLength: [${headerLength}], total Size: [${totalSize}]`));
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
// This event is the one where bytesWritten is generally recorded
|
|
190
|
-
req.on('end', () => {
|
|
191
|
-
const socketRecord = this.GetSocketRecord(req.socket);
|
|
192
|
-
if (socketRecord) {
|
|
193
|
-
this.#LogSillyMessage(chalk.gray(`Request end event: ID: [${socketRecord.id}], originalUrl: [${socketRecord.originalUrl}]`))
|
|
194
|
-
this.UpdateNetworkStats(socketRecord, 'req_end', req);
|
|
195
|
-
} else {
|
|
196
|
-
this.#LogSillyMessage(chalk.magenta(`Request end event: Could not find socket within recordset`));
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
req.on('close', () => {
|
|
201
|
-
const socketRecord = this.GetSocketRecord(req.socket);
|
|
202
|
-
if (socketRecord) {
|
|
203
|
-
this.#LogSillyMessage(chalk.gray(`Request close event: ID: [${socketRecord.id}], originalUrl: [${socketRecord.originalUrl}]`));
|
|
204
|
-
this.UpdateNetworkStats(socketRecord, 'req_close', req);
|
|
205
|
-
} else {
|
|
206
|
-
this.#LogSillyMessage(chalk.magenta(`Request close event: Could not find socket within recordset`));
|
|
207
|
-
}
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
/* These methods are also available but from testing they provide no specific value add, i.e. the calculated numbers are always 0.
|
|
211
|
-
req.on('data', () => {
|
|
212
|
-
workingSocketRecord = this.GetSocketRecord(req.socket);
|
|
213
|
-
if (workingSocketRecord) {
|
|
214
|
-
this.#LogDebugMessage(chalk.gray(`Request close event: ID: [${workingSocketRecord.id}], originalUrl: [${workingSocketRecord.originalUrl}]`));
|
|
215
|
-
this.UpdateNetworkStats(workingSocketRecord, 'data', req);
|
|
216
|
-
} else {
|
|
217
|
-
this.#LogDebugMessage(chalk.magenta(`Request close event: Could not find socket within recordset`));
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
req.on('readable', () => {
|
|
222
|
-
workingSocketRecord = this.GetSocketRecord(req.socket);
|
|
223
|
-
if (workingSocketRecord) {
|
|
224
|
-
this.#LogDebugMessage(chalk.gray(`Request close event: ID: [${workingSocketRecord.id}], originalUrl: [${workingSocketRecord.originalUrl}]`));
|
|
225
|
-
this.UpdateNetworkStats(workingSocketRecord, 'readable', req);
|
|
226
|
-
} else {
|
|
227
|
-
this.#LogDebugMessage(chalk.magenta(`Request close event: Could not find socket within recordset`));
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
*/
|
|
231
|
-
|
|
232
|
-
const inHeadersLength = JSON.stringify(req.headers).length;
|
|
233
|
-
const inContentLength = this.#GetRequestContentSize(req);
|
|
234
|
-
const inTotal = inHeadersLength + inContentLength;
|
|
235
|
-
|
|
236
|
-
this.#LogSillyMessage(chalk.white.bgGray(`inHeadersLength: [${inHeadersLength}], inContentLength: [${inContentLength}], inTotal: [${inTotal}]`));
|
|
237
|
-
|
|
238
|
-
// This event is the one where bytesRead is generally recorded
|
|
239
|
-
this.UpdateNetworkStats(workingSocketRecord, 'middleware', req);
|
|
240
|
-
|
|
241
|
-
next();
|
|
242
|
-
}
|
|
243
|
-
}
|
package/src/network.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import os from 'os'
|
|
2
|
-
|
|
3
|
-
export type NetworkInterfaces = Record<string, string[]>
|
|
4
|
-
|
|
5
|
-
export function GetNetworkInterfaces(): NetworkInterfaces {
|
|
6
|
-
const nets = os.networkInterfaces();
|
|
7
|
-
const results: NetworkInterfaces = { };
|
|
8
|
-
for (const name of Object.keys(nets)) {
|
|
9
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
-
for (const net of nets[name] as any) {
|
|
11
|
-
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
|
|
12
|
-
if (net.family === 'IPv4' && !net.internal) {
|
|
13
|
-
if (!results[name]) {
|
|
14
|
-
results[name] = [];
|
|
15
|
-
}
|
|
16
|
-
results[name].push(net.address);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return results;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function GetFirstNetworkInterface(): string | null {
|
|
24
|
-
const nics: NetworkInterfaces = GetNetworkInterfaces();
|
|
25
|
-
let hostaddr = null;
|
|
26
|
-
for (const nic in nics)
|
|
27
|
-
{
|
|
28
|
-
const val: string[] = nics[nic];
|
|
29
|
-
if (val.length > 0)
|
|
30
|
-
{
|
|
31
|
-
hostaddr = val[0];
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return hostaddr;
|
|
36
|
-
}
|