ismx-nexo-node-app 0.4.162 → 0.4.166
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/js/api/Service.js +6 -0
- package/dist/js/business/BusinessLogger.js +65 -2
- package/dist/js/business/BusinessServer.js +5 -1
- package/dist/types/api/Service.d.ts +2 -0
- package/dist/types/business/BusinessLogger.d.ts +9 -0
- package/package.json +1 -1
- package/src/main/node/api/Service.ts +8 -1
- package/src/main/node/business/BusinessLogger.ts +52 -2
- package/src/main/node/business/BusinessServer.ts +4 -1
package/dist/js/api/Service.js
CHANGED
|
@@ -26,6 +26,12 @@ class HttpResponse {
|
|
|
26
26
|
response.httpCode = httpCode;
|
|
27
27
|
return response;
|
|
28
28
|
}
|
|
29
|
+
static do(httpCode, content) {
|
|
30
|
+
let response = new HttpResponse();
|
|
31
|
+
response.content = content;
|
|
32
|
+
response.httpCode = httpCode;
|
|
33
|
+
return response;
|
|
34
|
+
}
|
|
29
35
|
}
|
|
30
36
|
exports.HttpResponse = HttpResponse;
|
|
31
37
|
class Service {
|
|
@@ -1,9 +1,72 @@
|
|
|
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
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
const fs = __importStar(require("fs"));
|
|
27
|
+
const path = __importStar(require("path"));
|
|
3
28
|
class BusinessLogger {
|
|
29
|
+
constructor() {
|
|
30
|
+
this.logStream = null;
|
|
31
|
+
this.currentLogFile = null;
|
|
32
|
+
}
|
|
33
|
+
file() {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
writeToFile(message) {
|
|
37
|
+
const file = this.file();
|
|
38
|
+
if (!file)
|
|
39
|
+
return;
|
|
40
|
+
if (this.currentLogFile !== file) {
|
|
41
|
+
if (this.logStream) {
|
|
42
|
+
this.logStream.end();
|
|
43
|
+
this.logStream = null;
|
|
44
|
+
}
|
|
45
|
+
const dir = path.dirname(file);
|
|
46
|
+
if (!fs.existsSync(dir))
|
|
47
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
48
|
+
this.logStream = fs.createWriteStream(file, { flags: 'a', encoding: 'utf8' });
|
|
49
|
+
this.currentLogFile = file;
|
|
50
|
+
}
|
|
51
|
+
if (this.logStream)
|
|
52
|
+
this.logStream.write(message + '\n');
|
|
53
|
+
}
|
|
54
|
+
log(level, message, writeToFile = false) {
|
|
55
|
+
const logMessage = `[${level}] ${new Date().toISOString()} -- ${message}`;
|
|
56
|
+
console.log(logMessage);
|
|
57
|
+
this.writeToFile(logMessage);
|
|
58
|
+
}
|
|
4
59
|
i(message) {
|
|
5
|
-
|
|
60
|
+
this.log("INFO", message);
|
|
61
|
+
}
|
|
62
|
+
w(message) {
|
|
63
|
+
this.log("WARN", message);
|
|
64
|
+
}
|
|
65
|
+
e(message) {
|
|
66
|
+
this.log("ERROR", message);
|
|
67
|
+
}
|
|
68
|
+
d(message) {
|
|
69
|
+
this.log("DEBUG", message);
|
|
6
70
|
}
|
|
7
|
-
;
|
|
8
71
|
}
|
|
9
72
|
exports.default = BusinessLogger;
|
|
@@ -37,6 +37,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
38
|
const Service_1 = require("../api/Service");
|
|
39
39
|
const Business_1 = __importDefault(require("./Business"));
|
|
40
|
+
const node_stream_1 = require("node:stream");
|
|
40
41
|
class BusinessServer extends Business_1.default {
|
|
41
42
|
constructor() {
|
|
42
43
|
super();
|
|
@@ -147,7 +148,10 @@ class BusinessServer extends Business_1.default {
|
|
|
147
148
|
Object.entries((_h = response.headers) !== null && _h !== void 0 ? _h : {}).forEach(([header, value]) => {
|
|
148
149
|
socket.set(header, value);
|
|
149
150
|
});
|
|
150
|
-
|
|
151
|
+
if (response.pipe)
|
|
152
|
+
node_stream_1.Readable.fromWeb(response.pipe).pipe(socket);
|
|
153
|
+
else
|
|
154
|
+
socket.send(response === null || response === void 0 ? void 0 : response.content);
|
|
151
155
|
try {
|
|
152
156
|
(_j = this.onEnd) === null || _j === void 0 ? void 0 : _j.call(this, request, response);
|
|
153
157
|
}
|
|
@@ -21,8 +21,10 @@ export declare class HttpResponse<T = any> {
|
|
|
21
21
|
headers?: {
|
|
22
22
|
[key: string]: string;
|
|
23
23
|
};
|
|
24
|
+
pipe?: ReadableStream;
|
|
24
25
|
static ok<T = any>(content: T): HttpResponse<any>;
|
|
25
26
|
static ko<T = any>(httpCode: number, content: T): HttpResponse<any>;
|
|
27
|
+
static do<T = any>(httpCode: number, content: T): HttpResponse<any>;
|
|
26
28
|
}
|
|
27
29
|
export type ServiceType = "json" | "xml" | 'stream' | 'image' | 'audio' | 'video' | '*';
|
|
28
30
|
export default abstract class Service<Req, Res> {
|
|
@@ -1,3 +1,12 @@
|
|
|
1
1
|
export default class BusinessLogger {
|
|
2
|
+
private logStream;
|
|
3
|
+
private currentLogFile;
|
|
4
|
+
constructor();
|
|
5
|
+
protected file(): string | null;
|
|
6
|
+
private writeToFile;
|
|
7
|
+
private log;
|
|
2
8
|
i(message: string): void;
|
|
9
|
+
w(message: string): void;
|
|
10
|
+
e(message: string): void;
|
|
11
|
+
d(message: string): void;
|
|
3
12
|
}
|
package/package.json
CHANGED
|
@@ -14,6 +14,7 @@ export class HttpResponse<T = any> {
|
|
|
14
14
|
content: T = null!;
|
|
15
15
|
httpCode?: number;
|
|
16
16
|
headers?: { [key: string]: string };
|
|
17
|
+
pipe?: ReadableStream;
|
|
17
18
|
|
|
18
19
|
static ok<T=any>(content: T) {
|
|
19
20
|
let response = new HttpResponse();
|
|
@@ -25,7 +26,13 @@ export class HttpResponse<T = any> {
|
|
|
25
26
|
let response = new HttpResponse();
|
|
26
27
|
response.content = content;
|
|
27
28
|
response.httpCode = httpCode;
|
|
28
|
-
return response
|
|
29
|
+
return response;
|
|
30
|
+
}
|
|
31
|
+
static do<T=any>(httpCode: number, content: T) {
|
|
32
|
+
let response = new HttpResponse();
|
|
33
|
+
response.content = content;
|
|
34
|
+
response.httpCode = httpCode;
|
|
35
|
+
return response;
|
|
29
36
|
}
|
|
30
37
|
}
|
|
31
38
|
|
|
@@ -1,6 +1,56 @@
|
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
|
|
1
4
|
export default class BusinessLogger
|
|
2
5
|
{
|
|
6
|
+
private logStream: fs.WriteStream | null = null;
|
|
7
|
+
private currentLogFile: string | null = null;
|
|
8
|
+
|
|
9
|
+
constructor() {
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
protected file(): string | null {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
private writeToFile(message: string) {
|
|
18
|
+
const file = this.file();
|
|
19
|
+
if (!file) return;
|
|
20
|
+
|
|
21
|
+
if (this.currentLogFile !== file) {
|
|
22
|
+
if (this.logStream) {
|
|
23
|
+
this.logStream.end();
|
|
24
|
+
this.logStream = null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const dir = path.dirname(file);
|
|
28
|
+
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
|
|
29
|
+
this.logStream = fs.createWriteStream(file, { flags: 'a', encoding: 'utf8' });
|
|
30
|
+
this.currentLogFile = file;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (this.logStream) this.logStream.write(message + '\n');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
private log(level: string, message: string, writeToFile: boolean = false) {
|
|
37
|
+
const logMessage = `[${level}] ${new Date().toISOString()} -- ${message}`;
|
|
38
|
+
console.log(logMessage); this.writeToFile(logMessage);
|
|
39
|
+
}
|
|
40
|
+
|
|
3
41
|
i(message: string) {
|
|
4
|
-
|
|
5
|
-
}
|
|
42
|
+
this.log("INFO", message);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
w(message: string) {
|
|
46
|
+
this.log("WARN", message);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
e(message: string) {
|
|
50
|
+
this.log("ERROR", message);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
d(message: string) {
|
|
54
|
+
this.log("DEBUG", message);
|
|
55
|
+
}
|
|
6
56
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Service, {HttpRequest, HttpResponse} from "../api/Service";
|
|
2
2
|
import Business from "./Business";
|
|
3
|
+
import {Readable} from "node:stream";
|
|
3
4
|
|
|
4
5
|
export default class BusinessServer extends Business
|
|
5
6
|
{
|
|
@@ -109,7 +110,9 @@ export default class BusinessServer extends Business
|
|
|
109
110
|
Object.entries(response.headers ?? {}).forEach(([header, value]) => {
|
|
110
111
|
socket.set(header, value);
|
|
111
112
|
});
|
|
112
|
-
|
|
113
|
+
if (response.pipe) (Readable as any).fromWeb(response.pipe).pipe(socket);
|
|
114
|
+
else socket.send(response?.content);
|
|
115
|
+
|
|
113
116
|
try { this.onEnd?.(request, response); } catch(e) { }
|
|
114
117
|
}
|
|
115
118
|
}}
|