UNPKG

ws-rmi

Version:

A Remote Method Invocation implementation written in JavaScript utilising the WebSocket protocol

44 lines (43 loc) 1.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createRMIMessageValidator = exports.createRMIMessage = void 0; const JSONValidation_1 = require("../JSONValidation"); /** * Creates an object that matches the {@link RMIMessage} schema. This is generally used by sub-types to * create full message objects. * * @param id The ID of the message to be created. * @param type The type of message to be created. * @param data The data to be inserted into this message. */ const createRMIMessage = (id, type, data) => ({ rmi: { version: "1", id, type, data } }); exports.createRMIMessage = createRMIMessage; /** * Returns a function that can be used to validate a message that should be validated as an RMI message. Assuming * the message conforms to the schema of an RMI message, the given dataValidator will be used to validate the inner * `data` property. * * @param dataValidator A function to validate the `data` property of the message. */ const createRMIMessageValidator = (dataValidator) => (message) => { if (!(0, JSONValidation_1.isObject)(message)) return false; if (!(0, JSONValidation_1.hasPropertyOfType)(message, "rmi", JSONValidation_1.isObject)) return false; const { rmi } = message; if (!(0, JSONValidation_1.hasPropertyOfType)(rmi, "version", JSONValidation_1.isString)) return false; if (!(0, JSONValidation_1.hasPropertyOfType)(rmi, "id", JSONValidation_1.isString)) return false; if (!(0, JSONValidation_1.hasPropertyOfType)(rmi, "type", JSONValidation_1.isRMIMessageType)) return false; return (0, JSONValidation_1.hasPropertyOfType)(rmi, "data", dataValidator); }; exports.createRMIMessageValidator = createRMIMessageValidator;