From 8cfa2f1c54b82283206faa45155e607d08fa9c15 Mon Sep 17 00:00:00 2001 From: Volker Schukai <volker.schukai@schukai.com> Date: Sat, 7 Jan 2023 18:56:24 +0100 Subject: [PATCH] feat: new Webconnect and Message --- application/source/net/webconnect/message.mjs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 application/source/net/webconnect/message.mjs diff --git a/application/source/net/webconnect/message.mjs b/application/source/net/webconnect/message.mjs new file mode 100644 index 000000000..2df1bd5e6 --- /dev/null +++ b/application/source/net/webconnect/message.mjs @@ -0,0 +1,55 @@ +/** + * Copyright schukai GmbH and contributors 2022. All Rights Reserved. + * Node module: @schukai/monster + * This file is licensed under the AGPLv3 License. + * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html + */ + +import {Base} from "../../types/base.mjs"; +import {validateObject, validateString} from "../../types/validate.mjs"; + +export {Message} + +const dataSymbol = Symbol("@@data"); + +/** + * This class represents a WebSocket message. + */ +class Message extends Base { + + /** + * @param {Object} data + * @throws {TypeError} value is not a object + */ + constructor(data) { + super(); + this[dataSymbol] = validateObject(data); + } + + /** + * Returns the raw message. + * + * @returns {object} + */ + getData() { + return this[dataSymbol]; + } + + /** + * @returns {*} + */ + toJSON() { + return this[dataSymbol]; + } + + /** + * @param {string} json + * @returns {Message} + * @throws {TypeError} value is not a string + */ + static fromJSON(json) { + validateString(json); + return new Message(JSON.parse(json)); + } + +} \ No newline at end of file -- GitLab