@cloudnux/local-cloud-provider 0.18.0 → 0.20.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/dev-console-plugin/index.d.ts +76 -6
- package/dist/dev-console-plugin/index.js +994 -2037
- package/dist/dev-console-plugin/index.js.map +1 -1
- package/dist/index.d.ts +19 -4
- package/dist/index.js +162 -197
- package/dist/index.js.map +1 -1
- package/dist/queue-plugin/index.d.ts +36 -13
- package/dist/queue-plugin/index.js +342 -1431
- package/dist/queue-plugin/index.js.map +1 -1
- package/dist/schedule-plugin/index.d.ts +49 -21
- package/dist/schedule-plugin/index.js +529 -1590
- package/dist/schedule-plugin/index.js.map +1 -1
- package/dist/websocket-plugin/index.d.ts +18 -0
- package/dist/websocket-plugin/index.js +18 -1029
- package/dist/websocket-plugin/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
|
-
import { CloudProvider } from '@cloudnux/core-cloud-provider';
|
|
1
|
+
import { LoggerService, LogEntry, CloudProvider } from '@cloudnux/core-cloud-provider';
|
|
2
2
|
import { FastifyInstance } from 'fastify';
|
|
3
3
|
|
|
4
|
+
type LogContext = {
|
|
5
|
+
module?: string;
|
|
6
|
+
reqId?: string;
|
|
7
|
+
};
|
|
8
|
+
type LogListener = (entry: LogEntry) => void;
|
|
9
|
+
type RunWithLogContext = <T>(context: LogContext, fn: () => T) => T;
|
|
10
|
+
type SubscribeToLogs = (listener: LogListener) => () => void;
|
|
11
|
+
type LoggingContext = LoggerService & {
|
|
12
|
+
runWithLogContext: RunWithLogContext;
|
|
13
|
+
subscribeToLogs: SubscribeToLogs;
|
|
14
|
+
};
|
|
15
|
+
declare module "fastify" {
|
|
16
|
+
interface FastifyInstance {
|
|
17
|
+
logging: LoggingContext;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
4
21
|
type RouterInstance = FastifyInstance;
|
|
5
|
-
declare function createRouter(
|
|
6
|
-
logger?: boolean;
|
|
7
|
-
}): RouterInstance;
|
|
22
|
+
declare function createRouter(): RouterInstance;
|
|
8
23
|
|
|
9
24
|
declare const localCloudProvider: CloudProvider;
|
|
10
25
|
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import
|
|
3
|
-
import { EOL as EOL2 } from "os";
|
|
1
|
+
// src/services/event-broker.ts
|
|
2
|
+
import axios from "axios";
|
|
4
3
|
|
|
5
4
|
// ../../../node_modules/lodash-es/_freeGlobal.js
|
|
6
5
|
var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
|
|
@@ -964,190 +963,9 @@ var tokenUtils = {
|
|
|
964
963
|
verifyAccessToken: verify
|
|
965
964
|
};
|
|
966
965
|
|
|
967
|
-
// ../../utils/src/logging/types.ts
|
|
968
|
-
var logLevels = {
|
|
969
|
-
fatal: -1,
|
|
970
|
-
error: 0,
|
|
971
|
-
warn: 1,
|
|
972
|
-
info: 2,
|
|
973
|
-
debug: 3,
|
|
974
|
-
trace: 4
|
|
975
|
-
};
|
|
976
|
-
|
|
977
|
-
// ../../utils/src/logging/error-to-string.ts
|
|
978
|
-
var safeJsonStringify = (value) => {
|
|
979
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
980
|
-
return JSON.stringify(value, (key, currentValue) => {
|
|
981
|
-
if (key === "constructor") return void 0;
|
|
982
|
-
if (typeof currentValue === "object" && currentValue !== null) {
|
|
983
|
-
if (seen.has(currentValue)) return "[Circular]";
|
|
984
|
-
seen.add(currentValue);
|
|
985
|
-
}
|
|
986
|
-
return currentValue;
|
|
987
|
-
});
|
|
988
|
-
};
|
|
989
|
-
var hasOwnConstructorProp = (value) => {
|
|
990
|
-
if (typeof value !== "object" || value === null) return false;
|
|
991
|
-
return Object.prototype.hasOwnProperty.call(value, "constructor");
|
|
992
|
-
};
|
|
993
|
-
var errorToString = (error) => {
|
|
994
|
-
if (error === null) return "Null error";
|
|
995
|
-
if (error === void 0) return "Undefined error";
|
|
996
|
-
if (error instanceof Error) {
|
|
997
|
-
return [
|
|
998
|
-
`Name: ${error.name}`,
|
|
999
|
-
`Message: ${error.message}`,
|
|
1000
|
-
`Stack: ${error.stack || "No stack trace available"}`,
|
|
1001
|
-
// Handle additional properties that might exist on custom errors
|
|
1002
|
-
...Object.entries(error).filter(
|
|
1003
|
-
([key, value]) => !["name", "message", "stack"].includes(key) && !hasOwnConstructorProp(value)
|
|
1004
|
-
).map(([key, value]) => `${key}: ${safeJsonStringify(value)}`)
|
|
1005
|
-
].join("\n");
|
|
1006
|
-
}
|
|
1007
|
-
if (typeof error === "string") return error;
|
|
1008
|
-
if (typeof error === "object") {
|
|
1009
|
-
try {
|
|
1010
|
-
return JSON.stringify(
|
|
1011
|
-
error,
|
|
1012
|
-
(key, value) => {
|
|
1013
|
-
if (key === "constructor") return void 0;
|
|
1014
|
-
return value;
|
|
1015
|
-
},
|
|
1016
|
-
2
|
|
1017
|
-
);
|
|
1018
|
-
} catch {
|
|
1019
|
-
return `[Object that cannot be stringified: ${Object.prototype.toString.call(error)}]`;
|
|
1020
|
-
}
|
|
1021
|
-
}
|
|
1022
|
-
return String(error);
|
|
1023
|
-
};
|
|
1024
|
-
|
|
1025
|
-
// ../../utils/src/logging/writer.ts
|
|
1026
|
-
import { EOL } from "os";
|
|
1027
|
-
var jsonWriter = (entry) => {
|
|
1028
|
-
process.stdout.write(JSON.stringify(entry) + EOL);
|
|
1029
|
-
};
|
|
1030
|
-
var _writer = jsonWriter;
|
|
1031
|
-
function setWriter(writer) {
|
|
1032
|
-
_writer = writer;
|
|
1033
|
-
}
|
|
1034
|
-
function getWriter() {
|
|
1035
|
-
return _writer;
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
|
-
// ../../utils/src/logging/index.ts
|
|
1039
|
-
var levelValues = {
|
|
1040
|
-
fatal: 70,
|
|
1041
|
-
error: 60,
|
|
1042
|
-
warn: 50,
|
|
1043
|
-
info: 40,
|
|
1044
|
-
debug: 30,
|
|
1045
|
-
trace: 20
|
|
1046
|
-
};
|
|
1047
|
-
var currentLogLevel = 2;
|
|
1048
|
-
var _module = "default";
|
|
1049
|
-
var _requestId = "";
|
|
1050
|
-
function writeLine(level, mergeObject, msg, bindings) {
|
|
1051
|
-
getWriter()({
|
|
1052
|
-
level: levelValues[level],
|
|
1053
|
-
levelName: level,
|
|
1054
|
-
time: Date.now(),
|
|
1055
|
-
module: bindings.module || _module,
|
|
1056
|
-
reqId: bindings.reqId || _requestId,
|
|
1057
|
-
msg: level === "error" || level === "fatal" ? errorToString(msg) : msg,
|
|
1058
|
-
...mergeObject ? { meta: mergeObject } : {}
|
|
1059
|
-
});
|
|
1060
|
-
}
|
|
1061
|
-
function createLogger(bindings = {}) {
|
|
1062
|
-
const currentLogLevelName = env("LOG_LEVEL")?.toLowerCase() ?? "info";
|
|
1063
|
-
currentLogLevel = logLevels[currentLogLevelName] ?? logLevels.info;
|
|
1064
|
-
return {
|
|
1065
|
-
level: currentLogLevelName,
|
|
1066
|
-
fatal: (mergeObject, msg) => {
|
|
1067
|
-
if (currentLogLevel >= logLevels.fatal) {
|
|
1068
|
-
typeof mergeObject === "string" || mergeObject instanceof Error ? writeLine("fatal", null, mergeObject, bindings) : writeLine("fatal", mergeObject, msg ?? "", bindings);
|
|
1069
|
-
}
|
|
1070
|
-
},
|
|
1071
|
-
error: (mergeObject, msg) => {
|
|
1072
|
-
if (currentLogLevel >= logLevels.error) {
|
|
1073
|
-
typeof mergeObject === "string" || mergeObject instanceof Error ? writeLine("error", null, mergeObject, bindings) : writeLine("error", mergeObject, msg ?? "", bindings);
|
|
1074
|
-
}
|
|
1075
|
-
},
|
|
1076
|
-
warn: (mergeObject, msg) => {
|
|
1077
|
-
if (currentLogLevel >= logLevels.warn) {
|
|
1078
|
-
typeof mergeObject === "string" ? writeLine("warn", null, mergeObject, bindings) : writeLine("warn", mergeObject, msg ?? "", bindings);
|
|
1079
|
-
}
|
|
1080
|
-
},
|
|
1081
|
-
info: (mergeObject, msg) => {
|
|
1082
|
-
if (currentLogLevel >= logLevels.info) {
|
|
1083
|
-
typeof mergeObject === "string" ? writeLine("info", null, mergeObject, bindings) : writeLine("info", mergeObject, msg ?? "", bindings);
|
|
1084
|
-
}
|
|
1085
|
-
},
|
|
1086
|
-
debug: (mergeObject, msg) => {
|
|
1087
|
-
if (currentLogLevel >= logLevels.debug) {
|
|
1088
|
-
typeof mergeObject === "string" ? writeLine("debug", null, mergeObject, bindings) : writeLine("debug", mergeObject, msg ?? "", bindings);
|
|
1089
|
-
}
|
|
1090
|
-
},
|
|
1091
|
-
trace: (mergeObject, msg) => {
|
|
1092
|
-
if (currentLogLevel >= logLevels.trace) {
|
|
1093
|
-
typeof mergeObject === "string" ? writeLine("trace", null, mergeObject, bindings) : writeLine("trace", mergeObject, msg ?? "", bindings);
|
|
1094
|
-
}
|
|
1095
|
-
},
|
|
1096
|
-
silent: () => {
|
|
1097
|
-
},
|
|
1098
|
-
child: (childBindings) => createLogger({ ...bindings, ...childBindings })
|
|
1099
|
-
};
|
|
1100
|
-
}
|
|
1101
|
-
var logger = createLogger();
|
|
1102
|
-
|
|
1103
|
-
// src/logging/pretty-writer.ts
|
|
1104
|
-
var levelColor = {
|
|
1105
|
-
fatal: chalk.bgRed.white,
|
|
1106
|
-
error: chalk.red,
|
|
1107
|
-
warn: chalk.yellow,
|
|
1108
|
-
info: chalk.cyan,
|
|
1109
|
-
debug: chalk.gray,
|
|
1110
|
-
trace: chalk.white
|
|
1111
|
-
};
|
|
1112
|
-
function formatTime(ms) {
|
|
1113
|
-
return new Date(ms).toTimeString().slice(0, 8);
|
|
1114
|
-
}
|
|
1115
|
-
function formatMeta(meta) {
|
|
1116
|
-
const { req, res, ...rest } = meta;
|
|
1117
|
-
const lines = [];
|
|
1118
|
-
if (req) {
|
|
1119
|
-
const reqParts = [`${req.method} ${req.url}`];
|
|
1120
|
-
if (req.remoteAddress) reqParts.push(`from ${req.remoteAddress}:${req.remotePort ?? ""}`);
|
|
1121
|
-
if (req.headers) reqParts.push(`headers=${JSON.stringify(req.headers, null, 2)}`);
|
|
1122
|
-
if (req.body !== void 0) reqParts.push(`body=${typeof req.body === "string" ? req.body : JSON.stringify(req.body, null, 2)}`);
|
|
1123
|
-
lines.push(chalk.dim(" req: " + reqParts.join(" ")));
|
|
1124
|
-
}
|
|
1125
|
-
if (res) {
|
|
1126
|
-
const resParts = [`${res.statusCode}`];
|
|
1127
|
-
if (res.headers) resParts.push(`headers=${JSON.stringify(res.headers, null, 2)}`);
|
|
1128
|
-
if (res.body !== void 0) resParts.push(`body=${typeof res.body === "string" ? res.body : JSON.stringify(res.body, null, 2)}`);
|
|
1129
|
-
lines.push(chalk.dim(" res: " + resParts.join(" ")));
|
|
1130
|
-
}
|
|
1131
|
-
if (Object.keys(rest).length) {
|
|
1132
|
-
lines.push(chalk.dim(" " + JSON.stringify(rest, null, 2)));
|
|
1133
|
-
}
|
|
1134
|
-
return lines.length ? EOL2 + lines.join(EOL2) : "";
|
|
1135
|
-
}
|
|
1136
|
-
var prettyWriter = (entry) => {
|
|
1137
|
-
const color = levelColor[entry.levelName] ?? chalk.white;
|
|
1138
|
-
const time = chalk.dim(formatTime(entry.time));
|
|
1139
|
-
const level = color(entry.levelName.toUpperCase().padEnd(5));
|
|
1140
|
-
const module = chalk.magenta(entry.module);
|
|
1141
|
-
const reqId = entry.reqId ? chalk.dim(`[${entry.reqId}]`) + " " : "";
|
|
1142
|
-
const meta = entry.meta ? formatMeta(entry.meta) : "";
|
|
1143
|
-
process.stdout.write(`${time} ${level} ${module} ${reqId}- ${entry.msg}${meta}${EOL2}`);
|
|
1144
|
-
};
|
|
1145
|
-
setWriter(prettyWriter);
|
|
1146
|
-
|
|
1147
966
|
// src/services/event-broker.ts
|
|
1148
|
-
import axios from "axios";
|
|
1149
967
|
function createLocalEventBrokerService() {
|
|
1150
|
-
const baseURL = env("DEV_CLOUD_EVENT_BROKER_URL"
|
|
968
|
+
const baseURL = env("DEV_CLOUD_EVENT_BROKER_URL");
|
|
1151
969
|
function getQueueUrl(queueName) {
|
|
1152
970
|
if (queueName.startsWith("http://") || queueName.startsWith("https://")) {
|
|
1153
971
|
return queueName;
|
|
@@ -1243,9 +1061,11 @@ function createLocalEventBrokerService() {
|
|
|
1243
1061
|
// src/services/storage.ts
|
|
1244
1062
|
import * as fs from "fs";
|
|
1245
1063
|
import * as path from "path";
|
|
1064
|
+
import { getService } from "@cloudnux/core-cloud-provider";
|
|
1246
1065
|
var { mkdir, writeFile, readFile, unlink, access } = fs.promises;
|
|
1247
1066
|
function createLocalStorageService() {
|
|
1248
1067
|
const baseDir = env("DEV_CLOUD_STORAGE_BUCKET", path.join(process.cwd(), ".local-storage"));
|
|
1068
|
+
const logger2 = getService("logger");
|
|
1249
1069
|
if (!fs.existsSync(baseDir)) {
|
|
1250
1070
|
fs.mkdirSync(baseDir, { recursive: true });
|
|
1251
1071
|
}
|
|
@@ -1341,7 +1161,7 @@ function createLocalStorageService() {
|
|
|
1341
1161
|
await unlink(metadataPath);
|
|
1342
1162
|
} catch (error) {
|
|
1343
1163
|
if (error.code !== "ENOENT") {
|
|
1344
|
-
|
|
1164
|
+
logger2.warn({ error }, "Error deleting metadata file:");
|
|
1345
1165
|
}
|
|
1346
1166
|
}
|
|
1347
1167
|
} catch (error) {
|
|
@@ -1357,6 +1177,9 @@ function createLocalStorageService() {
|
|
|
1357
1177
|
// src/services/locations.ts
|
|
1358
1178
|
import * as fs2 from "fs";
|
|
1359
1179
|
import * as path2 from "path";
|
|
1180
|
+
import {
|
|
1181
|
+
getService as getService2
|
|
1182
|
+
} from "@cloudnux/core-cloud-provider";
|
|
1360
1183
|
var { readFile: readFile2, writeFile: writeFile2, access: access2, mkdir: mkdir2 } = fs2.promises;
|
|
1361
1184
|
var DEFAULT_MOCK_LOCATIONS = {
|
|
1362
1185
|
"place-001": {
|
|
@@ -1590,6 +1413,7 @@ function toLocationAddress(location) {
|
|
|
1590
1413
|
function createLocalLocationService() {
|
|
1591
1414
|
const baseDir = env("DEV_CLOUD_LOCATION_PATH", path2.join(process.cwd(), ".develop", ".local-location"));
|
|
1592
1415
|
const placesFilePath = path2.join(baseDir, "places.json");
|
|
1416
|
+
const logger2 = getService2("logger");
|
|
1593
1417
|
async function loadPlaces() {
|
|
1594
1418
|
try {
|
|
1595
1419
|
try {
|
|
@@ -1603,11 +1427,11 @@ function createLocalLocationService() {
|
|
|
1603
1427
|
return JSON.parse(data);
|
|
1604
1428
|
} catch {
|
|
1605
1429
|
await writeFile2(placesFilePath, JSON.stringify(DEFAULT_MOCK_LOCATIONS, null, 2), "utf8");
|
|
1606
|
-
|
|
1430
|
+
logger2.info(`Created default places file at: ${placesFilePath}`);
|
|
1607
1431
|
return DEFAULT_MOCK_LOCATIONS;
|
|
1608
1432
|
}
|
|
1609
1433
|
} catch (error) {
|
|
1610
|
-
|
|
1434
|
+
logger2.warn(`Error loading places data: ${String(error)}`);
|
|
1611
1435
|
return DEFAULT_MOCK_LOCATIONS;
|
|
1612
1436
|
}
|
|
1613
1437
|
}
|
|
@@ -1746,7 +1570,7 @@ function createLocalLocationService() {
|
|
|
1746
1570
|
Math.max(...lats)
|
|
1747
1571
|
// maxLat
|
|
1748
1572
|
];
|
|
1749
|
-
|
|
1573
|
+
logger2.info(`Calculated route: ${totalDistanceKm.toFixed(2)}km, ${totalDurationMinutes.toFixed(0)}min, ${legs.length} legs`);
|
|
1750
1574
|
return {
|
|
1751
1575
|
totalDistanceKm,
|
|
1752
1576
|
totalDurationMinutes,
|
|
@@ -1778,8 +1602,7 @@ function createLocalFunctionsService() {
|
|
|
1778
1602
|
createHttRequest(request) {
|
|
1779
1603
|
const rawBody = request.rawBody;
|
|
1780
1604
|
const isAuth = env("DEV_IDENTITY");
|
|
1781
|
-
const
|
|
1782
|
-
const authorization = index !== -1 ? request.raw.rawHeaders[index + 1] : null;
|
|
1605
|
+
const authorization = request.headers.authorization ?? null;
|
|
1783
1606
|
const token = authorization ? authorization.replace(/^bearer\s+/i, "") : null;
|
|
1784
1607
|
const httpRequest = {
|
|
1785
1608
|
body: String(rawBody),
|
|
@@ -1790,7 +1613,8 @@ function createLocalFunctionsService() {
|
|
|
1790
1613
|
params: request.params,
|
|
1791
1614
|
rawQueryString: querystring.stringify(request.query),
|
|
1792
1615
|
host: request.hostname,
|
|
1793
|
-
requestId: request.id
|
|
1616
|
+
requestId: request.id,
|
|
1617
|
+
moduleName: request.routeOptions.config?.module
|
|
1794
1618
|
};
|
|
1795
1619
|
const jwtClaims = token && tokenUtils.decodeAccessToken(token);
|
|
1796
1620
|
const httpAuth = isAuth ? {
|
|
@@ -1819,7 +1643,8 @@ function createLocalFunctionsService() {
|
|
|
1819
1643
|
const scheduleName = job.name;
|
|
1820
1644
|
const scheduleRequest = {
|
|
1821
1645
|
name: scheduleName,
|
|
1822
|
-
requestId: execution.id
|
|
1646
|
+
requestId: execution.id,
|
|
1647
|
+
moduleName: job.module
|
|
1823
1648
|
};
|
|
1824
1649
|
return [scheduleRequest];
|
|
1825
1650
|
},
|
|
@@ -1837,7 +1662,6 @@ function createLocalFunctionsService() {
|
|
|
1837
1662
|
return [eventRequest];
|
|
1838
1663
|
},
|
|
1839
1664
|
createWebSocketRequest: (connectionId, event, data, request) => {
|
|
1840
|
-
logger.info(request.params);
|
|
1841
1665
|
const wsRequest = {
|
|
1842
1666
|
connectionId,
|
|
1843
1667
|
event,
|
|
@@ -1937,16 +1761,138 @@ function createLocalInvokeService() {
|
|
|
1937
1761
|
};
|
|
1938
1762
|
}
|
|
1939
1763
|
|
|
1764
|
+
// src/logger/index.ts
|
|
1765
|
+
import chalk from "chalk";
|
|
1766
|
+
import { EOL } from "os";
|
|
1767
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
1768
|
+
var storage = new AsyncLocalStorage();
|
|
1769
|
+
function enterLogContext(context) {
|
|
1770
|
+
storage.enterWith(context);
|
|
1771
|
+
}
|
|
1772
|
+
function runWithLogContext(context, fn) {
|
|
1773
|
+
return storage.run(context, fn);
|
|
1774
|
+
}
|
|
1775
|
+
function getLogContext() {
|
|
1776
|
+
return storage.getStore() ?? {};
|
|
1777
|
+
}
|
|
1778
|
+
var listeners = /* @__PURE__ */ new Set();
|
|
1779
|
+
function subscribeToLogs(listener) {
|
|
1780
|
+
listeners.add(listener);
|
|
1781
|
+
return () => listeners.delete(listener);
|
|
1782
|
+
}
|
|
1783
|
+
function publish(entry) {
|
|
1784
|
+
for (const listener of listeners) {
|
|
1785
|
+
listener(entry);
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
var levels = ["fatal", "error", "warn", "info", "debug", "trace"];
|
|
1789
|
+
function resolveLevel() {
|
|
1790
|
+
const configured = env("LOG_LEVEL", "info").toLowerCase();
|
|
1791
|
+
return levels.includes(configured) ? configured : "info";
|
|
1792
|
+
}
|
|
1793
|
+
var cachedLevel;
|
|
1794
|
+
function currentLevel() {
|
|
1795
|
+
if (cachedLevel === void 0) {
|
|
1796
|
+
cachedLevel = resolveLevel();
|
|
1797
|
+
}
|
|
1798
|
+
return cachedLevel;
|
|
1799
|
+
}
|
|
1800
|
+
function isEnabled(level) {
|
|
1801
|
+
return levels.indexOf(level) <= levels.indexOf(currentLevel());
|
|
1802
|
+
}
|
|
1803
|
+
var levelColor = {
|
|
1804
|
+
fatal: chalk.bgRed.white,
|
|
1805
|
+
error: chalk.red,
|
|
1806
|
+
warn: chalk.yellow,
|
|
1807
|
+
info: chalk.cyan,
|
|
1808
|
+
debug: chalk.gray,
|
|
1809
|
+
trace: chalk.white
|
|
1810
|
+
};
|
|
1811
|
+
function formatTime(ms) {
|
|
1812
|
+
return new Date(ms).toTimeString().slice(0, 8);
|
|
1813
|
+
}
|
|
1814
|
+
function circularSafeReplacer() {
|
|
1815
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
1816
|
+
return (_key, value) => {
|
|
1817
|
+
if (typeof value === "object" && value !== null) {
|
|
1818
|
+
if (seen.has(value)) return "[Circular]";
|
|
1819
|
+
seen.add(value);
|
|
1820
|
+
}
|
|
1821
|
+
return value;
|
|
1822
|
+
};
|
|
1823
|
+
}
|
|
1824
|
+
function formatMeta(meta) {
|
|
1825
|
+
return EOL + chalk.dim(" " + JSON.stringify(meta, circularSafeReplacer()));
|
|
1826
|
+
}
|
|
1827
|
+
function printPretty(entry) {
|
|
1828
|
+
const color = levelColor[entry.levelName] ?? chalk.white;
|
|
1829
|
+
const time = chalk.dim(formatTime(entry.time));
|
|
1830
|
+
const level = color(entry.levelName.toUpperCase().padEnd(5));
|
|
1831
|
+
const module = chalk.magenta(entry.module);
|
|
1832
|
+
const reqId = entry.reqId ? chalk.dim(`[${entry.reqId}] `) : "";
|
|
1833
|
+
const meta = entry.meta ? formatMeta(entry.meta) : "";
|
|
1834
|
+
process.stdout.write(`${time} ${level} ${module} ${reqId}- ${entry.msg}${meta}${EOL}`);
|
|
1835
|
+
}
|
|
1836
|
+
function write(level, mergeObject, msg) {
|
|
1837
|
+
if (!isEnabled(level)) return;
|
|
1838
|
+
const context = getLogContext();
|
|
1839
|
+
let module = context.module ?? "default";
|
|
1840
|
+
let reqId = context.reqId ?? "";
|
|
1841
|
+
let text;
|
|
1842
|
+
let meta;
|
|
1843
|
+
if (typeof mergeObject === "string") {
|
|
1844
|
+
text = mergeObject;
|
|
1845
|
+
} else if (mergeObject instanceof Error) {
|
|
1846
|
+
text = mergeObject.stack ?? mergeObject.message;
|
|
1847
|
+
} else {
|
|
1848
|
+
const { module: explicitModule, reqId: explicitReqId, ...rest } = mergeObject;
|
|
1849
|
+
if (explicitModule) module = explicitModule;
|
|
1850
|
+
if (explicitReqId) reqId = explicitReqId;
|
|
1851
|
+
meta = Object.keys(rest).length ? rest : void 0;
|
|
1852
|
+
text = msg ?? "";
|
|
1853
|
+
}
|
|
1854
|
+
const entry = {
|
|
1855
|
+
level: levels.indexOf(level),
|
|
1856
|
+
levelName: level,
|
|
1857
|
+
time: Date.now(),
|
|
1858
|
+
module,
|
|
1859
|
+
reqId,
|
|
1860
|
+
msg: text,
|
|
1861
|
+
...meta ? { meta } : {}
|
|
1862
|
+
};
|
|
1863
|
+
printPretty(entry);
|
|
1864
|
+
publish(entry);
|
|
1865
|
+
}
|
|
1866
|
+
var logger = {
|
|
1867
|
+
get level() {
|
|
1868
|
+
return currentLevel();
|
|
1869
|
+
},
|
|
1870
|
+
setBindings: () => {
|
|
1871
|
+
},
|
|
1872
|
+
fatal: (mergeObject, msg) => write("fatal", mergeObject, msg),
|
|
1873
|
+
error: (mergeObject, msg) => write("error", mergeObject, msg),
|
|
1874
|
+
warn: (mergeObject, msg) => write("warn", mergeObject, msg),
|
|
1875
|
+
info: (mergeObject, msg) => write("info", mergeObject, msg),
|
|
1876
|
+
debug: (mergeObject, msg) => write("debug", mergeObject, msg),
|
|
1877
|
+
trace: (mergeObject, msg) => write("trace", mergeObject, msg)
|
|
1878
|
+
};
|
|
1879
|
+
|
|
1880
|
+
// src/services/logger.ts
|
|
1881
|
+
function createLocalLoggerService() {
|
|
1882
|
+
return logger;
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1940
1885
|
// src/router/index.ts
|
|
1941
1886
|
import Fastify from "fastify";
|
|
1942
1887
|
import fastifyRawBody from "fastify-raw-body";
|
|
1943
1888
|
import cors from "@fastify/cors";
|
|
1944
1889
|
import fastifyPrintRoutes from "fastify-print-routes";
|
|
1945
|
-
function createRouter(
|
|
1890
|
+
function createRouter() {
|
|
1946
1891
|
const fastify = Fastify({
|
|
1947
1892
|
maxParamLength: 1e3,
|
|
1948
|
-
|
|
1893
|
+
logger: false
|
|
1949
1894
|
});
|
|
1895
|
+
fastify.decorate("logging", { ...logger, runWithLogContext, subscribeToLogs });
|
|
1950
1896
|
fastify.register(fastifyRawBody, {
|
|
1951
1897
|
field: "rawBody",
|
|
1952
1898
|
encoding: "utf8"
|
|
@@ -1955,6 +1901,24 @@ function createRouter(options = {}) {
|
|
|
1955
1901
|
"methods": ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
|
|
1956
1902
|
});
|
|
1957
1903
|
fastify.register(fastifyPrintRoutes);
|
|
1904
|
+
fastify.addHook("onRequest", (request, _reply, done) => {
|
|
1905
|
+
const module = request.routeOptions.config?.module;
|
|
1906
|
+
enterLogContext({ module, reqId: request.id });
|
|
1907
|
+
done();
|
|
1908
|
+
});
|
|
1909
|
+
fastify.addHook("onResponse", (request, reply, done) => {
|
|
1910
|
+
if (!request.url.startsWith("/console")) {
|
|
1911
|
+
logger.info(
|
|
1912
|
+
{ method: request.method, url: request.url, statusCode: reply.statusCode, responseTime: reply.elapsedTime },
|
|
1913
|
+
`${request.method} ${request.url} ${reply.statusCode}`
|
|
1914
|
+
);
|
|
1915
|
+
}
|
|
1916
|
+
done();
|
|
1917
|
+
});
|
|
1918
|
+
fastify.addHook("onError", (_request, _reply, error, done) => {
|
|
1919
|
+
logger.error(error);
|
|
1920
|
+
done();
|
|
1921
|
+
});
|
|
1958
1922
|
fastify.addHook("onReady", () => {
|
|
1959
1923
|
setWebSocketManager(fastify.websockets);
|
|
1960
1924
|
setInvokeManager(fastify.invokes);
|
|
@@ -1970,7 +1934,8 @@ var localCloudProvider = {
|
|
|
1970
1934
|
createEventBrokerService: createLocalEventBrokerService,
|
|
1971
1935
|
createFunctionsService: createLocalFunctionsService,
|
|
1972
1936
|
createWebSocketService: createLocalWebSocketService,
|
|
1973
|
-
createInvokeService: createLocalInvokeService
|
|
1937
|
+
createInvokeService: createLocalInvokeService,
|
|
1938
|
+
createLoggerService: createLocalLoggerService
|
|
1974
1939
|
};
|
|
1975
1940
|
export {
|
|
1976
1941
|
createRouter,
|