brainlet/api/v2/gateway/messageparser.js

18 lines
No EOL
805 B
JavaScript

const config = require("../../../config");
const opcodes = {
0: "HELLO"
};
const parseMessage = (message) => {
if (typeof message !== "string") throw new Error("GatewayMessageParser: Message is not a string");
if (message.length < 1) throw new Error("GatewayMessageParser: Message has less than 1 character");
if (message.length > config.gatewayMaxStringPayloadLength) throw new Error(`GatewayMessageParser: Message has more than ${config.gatewayMaxStringPayloadLength} characters`);
const op = parseInt(message[0]);
if (!op || isNaN(op)) throw new Error("GatewayMessageParser: Message has invalid opcode");
const opcodeName = opcodes[op];
if (!opcodeName) throw new Error("GatewayMessageParser: Message has unknown opcode");
};
module.exports = { parseMessage };