98 lines
2.3 KiB
JavaScript
98 lines
2.3 KiB
JavaScript
const path = require("path");
|
|
require("dotenv").config({
|
|
path: path.resolve(
|
|
process.cwd(),
|
|
`.env.${process.env.NODE_ENV || "development"}`
|
|
),
|
|
});
|
|
|
|
const { io } = require("../../server");
|
|
const { admin } = require("../firebase/firebase-handler");
|
|
const CdkJobExport = require("../cdk/cdk-job-export").default;
|
|
const { isArray } = require("lodash");
|
|
|
|
io.use(function (socket, next) {
|
|
try {
|
|
if (socket.handshake.auth.token) {
|
|
admin
|
|
.auth()
|
|
.verifyIdToken(socket.handshake.auth.token)
|
|
.then((user) => {
|
|
socket.user = user;
|
|
next();
|
|
})
|
|
.catch((error) => {
|
|
next(new Error("Authentication error", JSON.stringify(error)));
|
|
});
|
|
} else {
|
|
next(new Error("Authentication error - no authorization token."));
|
|
}
|
|
} catch (error) {
|
|
console.log("Uncaught connection error:::", error);
|
|
next(new Error(`Authentication error ${error}`));
|
|
}
|
|
});
|
|
|
|
io.on("connection", (socket) => {
|
|
socket.log_level = "TRACE";
|
|
createLogEvent(socket, "DEBUG", `Connected and Authenticated.`);
|
|
|
|
socket.on("set-log-level", (level) => {
|
|
socket.log_level = level;
|
|
createLogEvent(socket, "DEBUG", `Updated log level to ${level}`);
|
|
});
|
|
|
|
socket.on("cdk-export-job", (jobid) => {
|
|
CdkJobExport(socket, jobid);
|
|
});
|
|
|
|
socket.on("disconnect", () => {
|
|
createLogEvent(socket, "DEBUG", `User disconnected.`);
|
|
});
|
|
});
|
|
|
|
function createLogEvent(socket, level, message) {
|
|
if (LogLevelHierarchy(socket.log_level) >= LogLevelHierarchy(level)) {
|
|
console.log(
|
|
`[CDK LOG EVENT] ${level} - ${new Date()} - ${socket.user.email} - ${
|
|
socket.id
|
|
} - ${message}`
|
|
);
|
|
socket.emit("log-event", {
|
|
timestamp: new Date(),
|
|
level,
|
|
message,
|
|
});
|
|
|
|
if (socket.logEvents && isArray(socket.logEvents)) {
|
|
socket.logEvents.push({
|
|
timestamp: new Date(),
|
|
level,
|
|
message,
|
|
});
|
|
}
|
|
// if (level === "ERROR") {
|
|
// throw new Error(message);
|
|
// }
|
|
}
|
|
}
|
|
|
|
function LogLevelHierarchy(level) {
|
|
switch (level) {
|
|
case "TRACE":
|
|
return 5;
|
|
case "DEBUG":
|
|
return 4;
|
|
case "INFO":
|
|
return 3;
|
|
case "WARNING":
|
|
return 2;
|
|
case "ERROR":
|
|
return 1;
|
|
default:
|
|
return 3;
|
|
}
|
|
}
|
|
|
|
exports.createLogEvent = createLogEvent;
|