UNPKG

rjweb-server

Version:

Easy and Robust Way to create a Web Server with Many Easy-to-use Features in NodeJS

47 lines (46 loc) 1.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const WsOpenContext_1 = __importDefault(require("./WsOpenContext")); class WsMessageContext extends WsOpenContext_1.default { constructor(context, rawContext, abort) { super(context, rawContext, abort, 'message'); } /** * The Websocket Message (JSON Automatically parsed if enabled) * @since 5.4.0 */ message() { const stringified = this.context.body.raw.toString(); if ((stringified.startsWith('{') && stringified.endsWith('}')) || (stringified.startsWith('[') && stringified.endsWith(']'))) { try { const json = JSON.parse(stringified); this.context.body.parsed = json; } catch { this.context.body.parsed = stringified; } } return this.context.body.parsed; } /** * The Websocket Message Type * @since 9.0.0 */ messageType() { return this.rawContext.messageType(); } /** * The Raw Websocket Message * @since 5.5.2 */ rawMessage(encoding) { return this.context.body.raw.toString(encoding); } /** * The Raw Socket Message as Buffer * @since 8.1.4 */ rawMessageBytes() { return this.context.body.raw; } } exports.default = WsMessageContext;