@sme.up/kokos-sdk-node 0.0.2 → 0.0.5
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 +1 -1
- package/lib/caller/serviceCaller.d.ts +3 -2
- package/lib/caller/serviceCaller.js +46 -12
- package/lib/configuration/configuration.d.ts +8 -0
- package/lib/configuration/configuration.js +29 -0
- package/lib/entrypoint/restapi/index.js +2 -110
- package/lib/entrypoint/restapi/restapi.d.ts +3 -0
- package/lib/entrypoint/restapi/restapi.js +145 -0
- package/lib/index.d.ts +8 -3
- package/lib/index.js +8 -3
- package/lib/logger/logger.d.ts +25 -0
- package/lib/logger/logger.js +84 -0
- package/lib/services/JS_00_01.js +42 -51
- package/lib/types/data-structures/smeupDataStructure.d.ts +28 -2
- package/lib/types/data-structures/smeupDataStructure.js +22 -1
- package/lib/types/data-structures/smeupObject.d.ts +2 -2
- package/lib/types/data-structures/smeupTree.d.ts +1 -1
- package/lib/types/general.d.ts +19 -2
- package/lib/types/general.js +7 -0
- package/lib/utils/path.js +1 -1
- package/lib/utils/regex.d.ts +9 -0
- package/lib/utils/regex.js +53 -0
- package/lib/utils/smeupDataStructureWriter.d.ts +29 -0
- package/lib/utils/smeupDataStructureWriter.js +158 -0
- package/package.json +6 -7
- package/tsconfig.json +1 -0
- package/bin/kokos.js +0 -26
- package/docker/restapi/Dockerfile +0 -14
- package/init/JS_00_01.txt +0 -64
- package/lib/types/exceptions/execution.d.ts +0 -7
- package/lib/types/exceptions/execution.js +0 -10
- package/scripts/build.js +0 -26
- package/scripts/init.js +0 -95
- package/scripts/restapi/build-image-restapi.js +0 -60
- package/scripts/restapi/dev.js +0 -30
- package/scripts/restapi/start.js +0 -14
package/README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
/// <reference types="node" />
|
|
2
2
|
import { ExecutionContext, Fun } from "../types/general.js";
|
|
3
|
+
import stream from "stream";
|
|
3
4
|
/**
|
|
4
5
|
* PROJECT FILE SYSTEM
|
|
5
6
|
*
|
|
@@ -29,4 +30,4 @@ import { ExecutionContext, Fun } from "../types/general.js";
|
|
|
29
30
|
* @returns
|
|
30
31
|
* @throws ExecutionException
|
|
31
32
|
*/
|
|
32
|
-
export declare function serviceCaller(fun: Fun, context: ExecutionContext): Promise<
|
|
33
|
+
export declare function serviceCaller(fun: Fun, context: ExecutionContext, out: stream.Writable): Promise<void>;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import url from "url";
|
|
3
3
|
import os from "os";
|
|
4
|
-
import { ExecutionException } from "../types/exceptions/execution.js";
|
|
5
4
|
import { getProjectRoot } from "../utils/path.js";
|
|
5
|
+
import { SmeupDataStructureWriter } from "../utils/smeupDataStructureWriter.js";
|
|
6
|
+
import { SMEUP_MESSAGE_GRAVITY, SMEUP_MESSAGE_MODE, } from "../types/data-structures/smeupDataStructure.js";
|
|
7
|
+
import { LOGGER, clearLoggerContext, createLoggerContext, } from "../logger/logger.js";
|
|
6
8
|
/**
|
|
7
9
|
* PROJECT FILE SYSTEM
|
|
8
10
|
*
|
|
@@ -32,9 +34,17 @@ import { getProjectRoot } from "../utils/path.js";
|
|
|
32
34
|
* @returns
|
|
33
35
|
* @throws ExecutionException
|
|
34
36
|
*/
|
|
35
|
-
export async function serviceCaller(fun, context) {
|
|
37
|
+
export async function serviceCaller(fun, context, out) {
|
|
38
|
+
// setup logger context
|
|
39
|
+
const loggerContext = {
|
|
40
|
+
fun: "TODO",
|
|
41
|
+
sessionId: context.user.sessionId,
|
|
42
|
+
user: context.user.username,
|
|
43
|
+
environment: context.user.environment,
|
|
44
|
+
};
|
|
45
|
+
createLoggerContext(loggerContext);
|
|
36
46
|
// get service path
|
|
37
|
-
let servicePath;
|
|
47
|
+
let servicePath = "";
|
|
38
48
|
if (!process.env.NODE_ENV || process.env.NODE_ENV == "production") {
|
|
39
49
|
servicePath = path.resolve(getProjectRoot(), "lib", "services", `${fun.service}.js`);
|
|
40
50
|
}
|
|
@@ -44,6 +54,8 @@ export async function serviceCaller(fun, context) {
|
|
|
44
54
|
}
|
|
45
55
|
// load service module
|
|
46
56
|
let serviceModule;
|
|
57
|
+
const writer = new SmeupDataStructureWriter(out);
|
|
58
|
+
writer.writeDataStructureInitialization(fun);
|
|
47
59
|
try {
|
|
48
60
|
// import service by its name as es module
|
|
49
61
|
if (os.platform() == "win32") {
|
|
@@ -52,15 +64,37 @@ export async function serviceCaller(fun, context) {
|
|
|
52
64
|
else {
|
|
53
65
|
serviceModule = await import(servicePath);
|
|
54
66
|
}
|
|
67
|
+
// check method
|
|
68
|
+
if (serviceModule.default.methods[fun.function]) {
|
|
69
|
+
// call method
|
|
70
|
+
try {
|
|
71
|
+
await serviceModule.default.methods[fun.function](fun, context, writer);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
LOGGER.error(error);
|
|
75
|
+
writer.writeMessage({
|
|
76
|
+
gravity: SMEUP_MESSAGE_GRAVITY.ERROR,
|
|
77
|
+
mode: SMEUP_MESSAGE_MODE.PN,
|
|
78
|
+
message: error.message,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
writer.writeMessage({
|
|
84
|
+
gravity: SMEUP_MESSAGE_GRAVITY.ERROR,
|
|
85
|
+
mode: SMEUP_MESSAGE_MODE.PN,
|
|
86
|
+
message: `Method "${fun.function ? fun.function : ""}" not found for service "${fun.service}"`,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
55
89
|
}
|
|
56
|
-
catch (
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
else {
|
|
64
|
-
throw new ExecutionException(`Method "${fun.function ? fun.function : ""}" not found for service "${fun.service}"`);
|
|
90
|
+
catch (error) {
|
|
91
|
+
LOGGER.error(error);
|
|
92
|
+
writer.writeMessage({
|
|
93
|
+
gravity: SMEUP_MESSAGE_GRAVITY.ERROR,
|
|
94
|
+
mode: SMEUP_MESSAGE_MODE.PN,
|
|
95
|
+
message: `Service "${fun.service}" not found: ${error.message}`,
|
|
96
|
+
});
|
|
65
97
|
}
|
|
98
|
+
clearLoggerContext();
|
|
99
|
+
writer.writeDataStructureFinalization(fun);
|
|
66
100
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { MicroExecutorConfiguration } from "../types/general";
|
|
2
|
+
/**
|
|
3
|
+
* Init micro executor configuration
|
|
4
|
+
* @param microExecutorId
|
|
5
|
+
* @param defatultConfigurationClass
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export declare function init<T extends MicroExecutorConfiguration>(microExecutorId: string, defatultConfigurationClass: T): T;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import os from "os";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import yaml from "js-yaml";
|
|
5
|
+
const baseDir = path.join(os.homedir(), "etc", "kokos");
|
|
6
|
+
/**
|
|
7
|
+
* Init micro executor configuration
|
|
8
|
+
* @param microExecutorId
|
|
9
|
+
* @param defatultConfigurationClass
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
export function init(microExecutorId, defatultConfigurationClass) {
|
|
13
|
+
const microExecutorBaseDir = path.join(baseDir, microExecutorId);
|
|
14
|
+
const microExecutorConfigFilePath = path.join(microExecutorBaseDir, microExecutorId + ".yaml");
|
|
15
|
+
// create dir if not exists
|
|
16
|
+
if (!fs.existsSync(microExecutorBaseDir)) {
|
|
17
|
+
fs.mkdirSync(microExecutorBaseDir, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
// create config file if not exists
|
|
20
|
+
if (!fs.existsSync(microExecutorConfigFilePath)) {
|
|
21
|
+
const yamlData = yaml.dump(defatultConfigurationClass);
|
|
22
|
+
fs.writeFileSync(microExecutorConfigFilePath, yamlData, "utf8");
|
|
23
|
+
}
|
|
24
|
+
// load configuration
|
|
25
|
+
const yamlData = fs.readFileSync(microExecutorConfigFilePath, "utf8");
|
|
26
|
+
const yamlObject = yaml.load(yamlData);
|
|
27
|
+
const configuration = { ...defatultConfigurationClass, ...yamlObject };
|
|
28
|
+
return configuration;
|
|
29
|
+
}
|
|
@@ -1,110 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { plainToInstance } from "class-transformer";
|
|
4
|
-
import { validate } from "class-validator";
|
|
5
|
-
import { ValidationException } from "../../types/exceptions/validation.js";
|
|
6
|
-
import { ExecuteFunPayload } from "../../types/general.js";
|
|
7
|
-
import { serviceCaller } from "../../caller/serviceCaller.js";
|
|
8
|
-
import { fileURLToPath } from 'url';
|
|
9
|
-
import { dirname, normalize } from 'path';
|
|
10
|
-
// constant
|
|
11
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
12
|
-
const __dirname = dirname(__filename);
|
|
13
|
-
const app = express();
|
|
14
|
-
// body parse middleware
|
|
15
|
-
app.use(bodyParser.json());
|
|
16
|
-
// swagger
|
|
17
|
-
const swaggerPath = normalize(`${__dirname}/../../../swagger`);
|
|
18
|
-
app.use("/swagger", express.static(swaggerPath));
|
|
19
|
-
// docs
|
|
20
|
-
const docsPath = normalize(`${__dirname}/../../../docs`);
|
|
21
|
-
app.use("/docs", express.static(docsPath));
|
|
22
|
-
/**
|
|
23
|
-
* REST API Entrypoint
|
|
24
|
-
*/
|
|
25
|
-
/**
|
|
26
|
-
* EXECUTION API
|
|
27
|
-
*/
|
|
28
|
-
/**
|
|
29
|
-
* /executeFun API
|
|
30
|
-
*/
|
|
31
|
-
app.use("/api/v1/executeFun", validationMiddleware(ExecuteFunPayload), async (req, res, next) => {
|
|
32
|
-
try {
|
|
33
|
-
// get body
|
|
34
|
-
const { fun, context } = req.body;
|
|
35
|
-
// call service caller
|
|
36
|
-
const smeupDataStructure = await serviceCaller(fun, context);
|
|
37
|
-
// return response
|
|
38
|
-
res.status(200).json(smeupDataStructure);
|
|
39
|
-
}
|
|
40
|
-
catch (error) {
|
|
41
|
-
next(error);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
/**
|
|
45
|
-
* MONITORING API
|
|
46
|
-
*/
|
|
47
|
-
/**
|
|
48
|
-
* /livez API
|
|
49
|
-
*/
|
|
50
|
-
app.use("/api/v1/livez", async (req, res) => {
|
|
51
|
-
res.status(200).send({
|
|
52
|
-
status: "ok",
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
/**
|
|
56
|
-
* /readyz API
|
|
57
|
-
*/
|
|
58
|
-
app.use("/api/v1/readyz", async (req, res) => {
|
|
59
|
-
res.status(200).send({
|
|
60
|
-
status: "ok",
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
// error middleware
|
|
64
|
-
app.use(errorMiddleware);
|
|
65
|
-
// start server
|
|
66
|
-
const PORT = process.env.PORT || 3000;
|
|
67
|
-
app.listen(PORT, () => {
|
|
68
|
-
console.log("App listening on the port " + PORT);
|
|
69
|
-
});
|
|
70
|
-
/**
|
|
71
|
-
* MIDDLEWARES
|
|
72
|
-
*/
|
|
73
|
-
/**
|
|
74
|
-
* Validation Middleware
|
|
75
|
-
* Validate request body
|
|
76
|
-
* @param type
|
|
77
|
-
* @param skipMissingProperties
|
|
78
|
-
* @returns
|
|
79
|
-
*/
|
|
80
|
-
function validationMiddleware(type) {
|
|
81
|
-
return (req, res, next) => {
|
|
82
|
-
validate(plainToInstance(type, req.body)).then((errors) => {
|
|
83
|
-
if (errors.length > 0) {
|
|
84
|
-
const message = errors
|
|
85
|
-
.map((error) => Object.values(error.constraints))
|
|
86
|
-
.join(", ");
|
|
87
|
-
next(new ValidationException(message));
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
next();
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Error Middleware
|
|
97
|
-
* Handle all application error
|
|
98
|
-
* @param error
|
|
99
|
-
* @param request
|
|
100
|
-
* @param response
|
|
101
|
-
*/
|
|
102
|
-
function errorMiddleware(error, _request, response, _next) {
|
|
103
|
-
const status = error.status || 500;
|
|
104
|
-
const message = error.message || "Something went wrong";
|
|
105
|
-
console.error(message);
|
|
106
|
-
response.status(status).json({
|
|
107
|
-
message: message,
|
|
108
|
-
status: status,
|
|
109
|
-
});
|
|
110
|
-
}
|
|
1
|
+
import { startServer } from "./restapi.js";
|
|
2
|
+
startServer("me-node-example");
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MicroExecutorConfiguration } from "../../types/general.js";
|
|
2
|
+
export declare function startServer(microExecutorId: string): void;
|
|
3
|
+
export declare function startServerWithCustomConfig<T extends MicroExecutorConfiguration>(microExecutorId: string, defaultConfigurationClass: T, callback?: (configuration: T) => void): void;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import express from "express";
|
|
2
|
+
import bodyParser from "body-parser";
|
|
3
|
+
import { plainToInstance } from "class-transformer";
|
|
4
|
+
import { validate } from "class-validator";
|
|
5
|
+
import { ValidationException } from "../../types/exceptions/validation.js";
|
|
6
|
+
import { ExecuteFunPayload, } from "../../types/general.js";
|
|
7
|
+
import { serviceCaller } from "../../caller/serviceCaller.js";
|
|
8
|
+
import { fileURLToPath } from "url";
|
|
9
|
+
import { dirname, normalize } from "path";
|
|
10
|
+
import stream from "stream";
|
|
11
|
+
import { SMEUP_MESSAGE_GRAVITY, SMEUP_MESSAGE_MODE, } from "../../types/data-structures/smeupDataStructure.js";
|
|
12
|
+
import { init } from "../../configuration/configuration.js";
|
|
13
|
+
import { LOGGER, configureLogger } from "../../logger/logger.js";
|
|
14
|
+
// constant
|
|
15
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
16
|
+
const __dirname = dirname(__filename);
|
|
17
|
+
export function startServer(microExecutorId) {
|
|
18
|
+
startServerWithCustomConfig(microExecutorId, {
|
|
19
|
+
server: {
|
|
20
|
+
port: 8011,
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
export function startServerWithCustomConfig(microExecutorId, defaultConfigurationClass, callback) {
|
|
25
|
+
// load config
|
|
26
|
+
const configuration = init(microExecutorId, defaultConfigurationClass);
|
|
27
|
+
if (callback) {
|
|
28
|
+
// call callback
|
|
29
|
+
callback(configuration);
|
|
30
|
+
}
|
|
31
|
+
configureLogger(configuration.logger?.level, configuration.logger?.format);
|
|
32
|
+
// create server
|
|
33
|
+
const app = express();
|
|
34
|
+
// body parse middleware
|
|
35
|
+
app.use(bodyParser.json());
|
|
36
|
+
// swagger
|
|
37
|
+
const swaggerPath = normalize(`${__dirname}/../../../swagger`);
|
|
38
|
+
app.use("/swagger", express.static(swaggerPath));
|
|
39
|
+
// docs
|
|
40
|
+
const docsPath = normalize(`${__dirname}/../../../docs`);
|
|
41
|
+
app.use("/docs", express.static(docsPath));
|
|
42
|
+
/**
|
|
43
|
+
* REST API Entrypoint
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* EXECUTION API
|
|
47
|
+
*/
|
|
48
|
+
/**
|
|
49
|
+
* /executeFun API
|
|
50
|
+
*/
|
|
51
|
+
app.use("/api/v1/executeFun", validationMiddleware(ExecuteFunPayload), (req, res, next) => {
|
|
52
|
+
// get body
|
|
53
|
+
const { fun, context } = req.body;
|
|
54
|
+
// set status
|
|
55
|
+
res.status(200);
|
|
56
|
+
res.setHeader("Content-Type", "application/json");
|
|
57
|
+
// create stream
|
|
58
|
+
const out = new stream.Writable({
|
|
59
|
+
write(chunk, encoding, callback) {
|
|
60
|
+
res.write(chunk, encoding);
|
|
61
|
+
callback();
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
// call service caller
|
|
65
|
+
serviceCaller(fun, context, out)
|
|
66
|
+
.then(() => {
|
|
67
|
+
res.end();
|
|
68
|
+
})
|
|
69
|
+
.catch((err) => {
|
|
70
|
+
next(err);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
/**
|
|
74
|
+
* MONITORING API
|
|
75
|
+
*/
|
|
76
|
+
/**
|
|
77
|
+
* /livez API
|
|
78
|
+
*/
|
|
79
|
+
app.use("/api/v1/livez", async (req, res) => {
|
|
80
|
+
res.status(200).send({
|
|
81
|
+
status: "ok",
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
/**
|
|
85
|
+
* /readyz API
|
|
86
|
+
*/
|
|
87
|
+
app.use("/api/v1/readyz", async (req, res) => {
|
|
88
|
+
res.status(200).send({
|
|
89
|
+
status: "ok",
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
// error middleware
|
|
93
|
+
app.use(errorMiddleware);
|
|
94
|
+
// start server
|
|
95
|
+
const PORT = configuration.server.port || 3000;
|
|
96
|
+
app.listen(PORT, () => {
|
|
97
|
+
LOGGER.info(`App listening on the port ${PORT}`);
|
|
98
|
+
});
|
|
99
|
+
/**
|
|
100
|
+
* MIDDLEWARES
|
|
101
|
+
*/
|
|
102
|
+
/**
|
|
103
|
+
* Validation Middleware
|
|
104
|
+
* Validate request body
|
|
105
|
+
* @param type
|
|
106
|
+
* @param skipMissingProperties
|
|
107
|
+
* @returns
|
|
108
|
+
*/
|
|
109
|
+
function validationMiddleware(type) {
|
|
110
|
+
return (req, res, next) => {
|
|
111
|
+
validate(plainToInstance(type, req.body)).then((errors) => {
|
|
112
|
+
if (errors.length > 0) {
|
|
113
|
+
const message = errors
|
|
114
|
+
.map((error) => error.constraints && Object.values(error.constraints))
|
|
115
|
+
.join(", ");
|
|
116
|
+
next(new ValidationException(message));
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
next();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Error Middleware
|
|
126
|
+
* Handle all application error
|
|
127
|
+
* @param error
|
|
128
|
+
* @param request
|
|
129
|
+
* @param response
|
|
130
|
+
*/
|
|
131
|
+
function errorMiddleware(error, _request, response, _next) {
|
|
132
|
+
const status = error.status || 500;
|
|
133
|
+
const message = error.message || "Something went wrong";
|
|
134
|
+
LOGGER.error(error);
|
|
135
|
+
response.status(status).json({
|
|
136
|
+
messages: [
|
|
137
|
+
{
|
|
138
|
+
gravity: SMEUP_MESSAGE_GRAVITY.ERROR,
|
|
139
|
+
message: message,
|
|
140
|
+
mode: SMEUP_MESSAGE_MODE.PN,
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TYPES
|
|
3
|
-
*/
|
|
1
|
+
/** TYPES */
|
|
4
2
|
export * from "./types/general.js";
|
|
5
3
|
/** DATA STRUCTURES */
|
|
6
4
|
export * from "./types/data-structures/smeupDataStructure.js";
|
|
7
5
|
export * from "./types/data-structures/smeupObject.js";
|
|
8
6
|
export * from "./types/data-structures/smeupTable.js";
|
|
9
7
|
export * from "./types/data-structures/smeupTree.js";
|
|
8
|
+
/** ENTRYPOINT */
|
|
9
|
+
export * from "./entrypoint/restapi/restapi.js";
|
|
10
|
+
/** LOGGER */
|
|
11
|
+
export * from "./logger/logger.js";
|
|
12
|
+
/** UTILS */
|
|
13
|
+
export * from "./utils/smeupDataStructureWriter.js";
|
|
14
|
+
export * from "./utils/regex.js";
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TYPES
|
|
3
|
-
*/
|
|
1
|
+
/** TYPES */
|
|
4
2
|
export * from "./types/general.js";
|
|
5
3
|
/** DATA STRUCTURES */
|
|
6
4
|
export * from "./types/data-structures/smeupDataStructure.js";
|
|
7
5
|
export * from "./types/data-structures/smeupObject.js";
|
|
8
6
|
export * from "./types/data-structures/smeupTable.js";
|
|
9
7
|
export * from "./types/data-structures/smeupTree.js";
|
|
8
|
+
/** ENTRYPOINT */
|
|
9
|
+
export * from "./entrypoint/restapi/restapi.js";
|
|
10
|
+
/** LOGGER */
|
|
11
|
+
export * from "./logger/logger.js";
|
|
12
|
+
/** UTILS */
|
|
13
|
+
export * from "./utils/smeupDataStructureWriter.js";
|
|
14
|
+
export * from "./utils/regex.js";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface LoggerContext {
|
|
2
|
+
fun: string;
|
|
3
|
+
sessionId: string;
|
|
4
|
+
user: string;
|
|
5
|
+
environment: string;
|
|
6
|
+
}
|
|
7
|
+
export declare enum Level {
|
|
8
|
+
INFO = "INFO",
|
|
9
|
+
DEBUG = "DEBUG",
|
|
10
|
+
ERROR = "ERROR"
|
|
11
|
+
}
|
|
12
|
+
export declare enum Format {
|
|
13
|
+
JSON = "JSON",
|
|
14
|
+
TXT = "TXT"
|
|
15
|
+
}
|
|
16
|
+
export declare const configureLogger: (level?: Level, format?: Format) => void;
|
|
17
|
+
export declare const createLoggerContext: (context: LoggerContext) => void;
|
|
18
|
+
export declare const clearLoggerContext: () => void;
|
|
19
|
+
declare class Logger {
|
|
20
|
+
info(message: string): void;
|
|
21
|
+
error(error: Error): void;
|
|
22
|
+
private _log;
|
|
23
|
+
}
|
|
24
|
+
export declare const LOGGER: Logger;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import async_hooks from "async_hooks";
|
|
2
|
+
export var Level;
|
|
3
|
+
(function (Level) {
|
|
4
|
+
Level["INFO"] = "INFO";
|
|
5
|
+
Level["DEBUG"] = "DEBUG";
|
|
6
|
+
Level["ERROR"] = "ERROR";
|
|
7
|
+
})(Level || (Level = {}));
|
|
8
|
+
export var Format;
|
|
9
|
+
(function (Format) {
|
|
10
|
+
Format["JSON"] = "JSON";
|
|
11
|
+
Format["TXT"] = "TXT";
|
|
12
|
+
})(Format || (Format = {}));
|
|
13
|
+
const LOGGER_SETTING = {
|
|
14
|
+
level: Level.DEBUG,
|
|
15
|
+
format: Format.TXT,
|
|
16
|
+
};
|
|
17
|
+
const LOGGER_CONTEXT_MAP = {};
|
|
18
|
+
export const configureLogger = (level, format) => {
|
|
19
|
+
LOGGER_SETTING.level = level ? level : Level.DEBUG;
|
|
20
|
+
LOGGER_SETTING.format = format ? format : Format.TXT;
|
|
21
|
+
};
|
|
22
|
+
export const createLoggerContext = (context) => {
|
|
23
|
+
// get the async execution id
|
|
24
|
+
const id = async_hooks.executionAsyncId();
|
|
25
|
+
// save to context map
|
|
26
|
+
LOGGER_CONTEXT_MAP[id] = context;
|
|
27
|
+
};
|
|
28
|
+
export const clearLoggerContext = () => {
|
|
29
|
+
// get the async execution id
|
|
30
|
+
const id = async_hooks.executionAsyncId();
|
|
31
|
+
// delete from context map
|
|
32
|
+
delete LOGGER_CONTEXT_MAP[id];
|
|
33
|
+
};
|
|
34
|
+
class Logger {
|
|
35
|
+
info(message) {
|
|
36
|
+
this._log(Level.INFO, message);
|
|
37
|
+
}
|
|
38
|
+
error(error) {
|
|
39
|
+
this._log(Level.ERROR, error.message, error);
|
|
40
|
+
}
|
|
41
|
+
_log(level, message, error) {
|
|
42
|
+
// logger level
|
|
43
|
+
if (LOGGER_SETTING.level == Level.INFO && level == Level.DEBUG) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
// get the async execution id
|
|
47
|
+
const id = async_hooks.executionAsyncId();
|
|
48
|
+
// get context
|
|
49
|
+
const context = LOGGER_CONTEXT_MAP[id];
|
|
50
|
+
// create log record
|
|
51
|
+
const record = {
|
|
52
|
+
...context,
|
|
53
|
+
timestamp: new Date().toISOString(),
|
|
54
|
+
applicationId: "",
|
|
55
|
+
message: message,
|
|
56
|
+
level: level.toString(),
|
|
57
|
+
applicationReserved: {
|
|
58
|
+
executionId: id.toString(),
|
|
59
|
+
stackTrace: error && error.stack ? error.stack : "",
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
let stringRecord = "";
|
|
63
|
+
if (LOGGER_SETTING.format == Format.JSON) {
|
|
64
|
+
stringRecord = JSON.stringify(record);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
stringRecord = `${record.timestamp} ${record.applicationReserved.executionId} ${record.level} - ${record.message} ${record.applicationReserved.stackTrace
|
|
68
|
+
? record.applicationReserved.stackTrace
|
|
69
|
+
: ""}`;
|
|
70
|
+
}
|
|
71
|
+
switch (level) {
|
|
72
|
+
case Level.INFO:
|
|
73
|
+
console.info(stringRecord);
|
|
74
|
+
break;
|
|
75
|
+
case Level.DEBUG:
|
|
76
|
+
console.debug(stringRecord);
|
|
77
|
+
break;
|
|
78
|
+
case Level.ERROR:
|
|
79
|
+
console.error(stringRecord);
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export const LOGGER = new Logger();
|
package/lib/services/JS_00_01.js
CHANGED
|
@@ -1,67 +1,58 @@
|
|
|
1
|
-
import { SmeupDataStructureType } from "../types/data-structures/smeupDataStructure.js";
|
|
2
1
|
const JS_00_01 = {
|
|
3
2
|
methods: {
|
|
4
3
|
"GET.TRE": getTree,
|
|
5
4
|
"GET.EXB": getTable,
|
|
5
|
+
"THR.ERR": throwError,
|
|
6
6
|
},
|
|
7
7
|
};
|
|
8
|
-
async function getTree(
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
async function getTree(_fun, _context, printer) {
|
|
9
|
+
printer.writeTreeNode({
|
|
10
|
+
children: [],
|
|
11
|
+
content: {
|
|
12
|
+
tipo: "CN",
|
|
13
|
+
parametro: "COL",
|
|
14
|
+
codice: "PIPPO",
|
|
15
|
+
testo: "Pippo",
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
async function getTable(_fun, _context, printer) {
|
|
20
|
+
printer.writeColumns([
|
|
21
|
+
{
|
|
22
|
+
code: "COL1",
|
|
23
|
+
text: "Column 1",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
code: "COL2",
|
|
27
|
+
text: "Column 2",
|
|
28
|
+
},
|
|
29
|
+
]);
|
|
30
|
+
printer.writeRow({
|
|
31
|
+
fields: {
|
|
32
|
+
COL1: {
|
|
33
|
+
name: "COL1",
|
|
34
|
+
tooltip: false,
|
|
35
|
+
smeupObject: {
|
|
15
36
|
tipo: "CN",
|
|
16
37
|
parametro: "COL",
|
|
17
38
|
codice: "PIPPO",
|
|
18
39
|
testo: "Pippo",
|
|
19
40
|
},
|
|
20
41
|
},
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
{
|
|
30
|
-
code: "COL1",
|
|
31
|
-
text: "Column 1",
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
code: "COL2",
|
|
35
|
-
text: "Column 2",
|
|
36
|
-
},
|
|
37
|
-
],
|
|
38
|
-
rows: [
|
|
39
|
-
{
|
|
40
|
-
fields: {
|
|
41
|
-
COL1: {
|
|
42
|
-
name: "COL1",
|
|
43
|
-
tooltip: false,
|
|
44
|
-
smeupObject: {
|
|
45
|
-
tipo: "CN",
|
|
46
|
-
parametro: "COL",
|
|
47
|
-
codice: "PIPPO",
|
|
48
|
-
testo: "Pippo",
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
COL2: {
|
|
52
|
-
name: "COL2",
|
|
53
|
-
tooltip: false,
|
|
54
|
-
smeupObject: {
|
|
55
|
-
tipo: "CN",
|
|
56
|
-
parametro: "COL",
|
|
57
|
-
codice: "PLUTO",
|
|
58
|
-
testo: "Pluto",
|
|
59
|
-
},
|
|
60
|
-
},
|
|
42
|
+
COL2: {
|
|
43
|
+
name: "COL2",
|
|
44
|
+
tooltip: false,
|
|
45
|
+
smeupObject: {
|
|
46
|
+
tipo: "CN",
|
|
47
|
+
parametro: "COL",
|
|
48
|
+
codice: "PLUTO",
|
|
49
|
+
testo: "Pluto",
|
|
61
50
|
},
|
|
62
51
|
},
|
|
63
|
-
|
|
64
|
-
};
|
|
65
|
-
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
async function throwError(_fun, _context, _printer) {
|
|
56
|
+
throw new Error("Dummy error");
|
|
66
57
|
}
|
|
67
58
|
export default JS_00_01;
|