@colyseus/monitor
Version:
Web Monitoring Panel for Colyseus
101 lines (100 loc) • 3.88 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var api_exports = {};
__export(api_exports, {
getAPI: () => getAPI
});
module.exports = __toCommonJS(api_exports);
var import_core = require("@colyseus/core");
var import_express = __toESM(require("express"));
var import_node_os_utils = __toESM(require("node-os-utils"));
const UNAVAILABLE_ROOM_ERROR = "@colyseus/monitor: room $roomId is not available anymore.";
function getAPI(opts) {
const api = import_express.default.Router();
api.get("/", async (req, res) => {
try {
const rooms = await import_core.matchMaker.query({});
const columns = opts.columns || ["roomId", "name", "clients", "maxClients", "locked", "elapsedTime"];
if (!opts.columns && rooms[0] && rooms[0].publicAddress !== void 0) {
columns.push("publicAddress");
}
let connections = 0;
res.json({
columns,
rooms: rooms.map((room) => {
const data = JSON.parse(JSON.stringify(room));
connections += room.clients;
data.locked = room.locked || false;
data.private = room.private;
data.maxClients = `${room.maxClients}`;
data.elapsedTime = Date.now() - new Date(room.createdAt).getTime();
return data;
}),
connections,
cpu: await import_node_os_utils.default.cpu.usage(),
memory: await import_node_os_utils.default.mem.used()
});
} catch (e) {
const message = e.message;
console.error(message);
res.status(500);
res.json({ message });
}
});
api.get("/room", async (req, res) => {
const roomId = req.query.roomId;
try {
const inspectData = await import_core.matchMaker.remoteRoomCall(roomId, "getInspectData");
res.json(inspectData);
} catch (e) {
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
console.error(message);
res.status(500);
res.json({ message });
}
});
api.get("/room/call", async (req, res) => {
const roomId = req.query.roomId;
const method = req.query.method;
const args = JSON.parse(req.query.args);
try {
const data = await import_core.matchMaker.remoteRoomCall(roomId, method, args);
res.json(data);
} catch (e) {
const message = UNAVAILABLE_ROOM_ERROR.replace("$roomId", roomId);
console.error(message);
res.status(500);
res.json({ message });
}
});
return api;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
getAPI
});