diff --git a/application/source/net/webconnect/message.mjs b/application/source/net/webconnect/message.mjs new file mode 100644 index 0000000000000000000000000000000000000000..2df1bd5e6791b88f1169ee104fbd103f7a4cfd67 --- /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