@webex/webex-core
Version:
Plugin handling for Cisco Webex
301 lines (276 loc) • 8.29 kB
JavaScript
;
var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property");
var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");
_Object$defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs2/helpers/createClass"));
var _url = _interopRequireDefault(require("url"));
var _constants = require("../constants");
/**
* The parameter transfer object for {@link ServiceHost#constructor}.
*
* @typedef {Object} ServiceHostConstructorPTO
* @property {string} ServiceHostConstructorPTO.catalog - The host's catalog.
* @property {string} ServiceHostConstructorPTO.defaultUri - The host's default.
* @property {string} ServiceHostConstructorPTO.hostGroup - The host's group.
* @property {string} ServiceHostConstructorPTO.id - The host's clusterId.
* @property {number} ServiceHostConstructorPTO.priority - The host's priority.
* @property {string} ServiceHostConstructorPTO.uri - The host's uri.
*/
/**
* The parameter transfer object for {@link ServiceHost#polyGenerate}.
*
* @typedef {Object} ServiceHostPolyGeneratePTO
* @property {string} ServiceHostPolyGeneratePTO.catalog - The target catalog.
* @property {string} ServiceHostPolyGeneratePTO.name - The service name.
* @property {string} ServiceHostPolyGeneratePTO.url - The service url.
*/
/**
* @class
* @classdesc - Manages a single service host and its associated data.
*/
var ServiceHost = exports.default = /*#__PURE__*/function () {
/**
* Generate a new {@link ServiceHost}.
*
* @public
* @constructor
* @memberof ServiceHost
* @param {ServiceHostConstructorPTO} pto
*/
function ServiceHost(pto) {
(0, _classCallCheck2.default)(this, ServiceHost);
// Validate the parameter transfer object.
ServiceHost.validate(pto);
// Map the parameter transfer object to the class object.
/**
* The catalog name that the {@link ServiceHost} is associated with.
*
* @instance
* @type {string}
* @public
* @memberof ServiceHost
*/
this.catalog = pto.catalog;
/**
* The default URI for the {@link ServiceHost}.
*
* @instance
* @type {string}
* @public
* @memberof ServiceHost
*/
this.default = pto.defaultUri;
/**
* The host group that the {@link ServiceHost} is associated with.
*
* @instance
* @type {string}
* @public
* @memberof ServiceHost
*/
this.hostGroup = pto.hostGroup;
/**
* The cluster ID of the {@link ServiceHost}.
*
* @instance
* @type {string}
* @public
* @memberof ServiceHost
*/
this.id = pto.id;
/**
* The priority value of the {@link ServiceHost}. The lower the number, the
* higher the priority.
*
* @instance
* @type {number}
* @public
* @memberof ServiceHost
*/
this.priority = pto.priority;
/**
* The host uri of the {@link ServiceHost}.
*
* @instance
* @type {string}
* @public
* @memberof ServiceHost
*/
this.uri = pto.uri;
// Generate flags.
/**
* If the {@link ServiceHost} is marked as failed.
*
* @instance
* @type {boolean}
* @protected
* @memberof ServiceHost
*/
this.failed = false;
/**
* If the {@link ServiceHost} is marked as replaced.
*
* @instance
* @type {boolean}
* @protected
* @memberof ServiceHost
*/
this.replaced = false;
}
/**
* If the {@link ServiceHost} is in an active state.
*
* @public
* @memberof ServiceHost
* @type {boolean} - `true` if the service is active and usable.
*/
(0, _createClass2.default)(ServiceHost, [{
key: "active",
get: function get() {
// Validate that the `ServiceHost` was not marked as failed or replaced.
return !this.failed && !this.replaced;
}
/**
* If the host is local to the user's cluster.
*
* @public
* @memberof ServiceHost
* @type {boolean} - If the host is local.
*/
}, {
key: "local",
get: function get() {
return this.default.includes(this.hostGroup);
}
/**
* The service value.
*
* @public
* @memberof ServiceHost
* @type {string} - The service value.
*/
}, {
key: "service",
get: function get() {
return this.id.split(':')[3];
}
/**
* The formatted url for the host.
*
* @public
* @memberof ServiceHost
* @type {string} - The service url.
*/
}, {
key: "url",
get: function get() {
// Generate a url object from the default url.
var urlObj = _url.default.parse(this.default);
// Format the host of the generated url object.
urlObj.host = "".concat(this.uri).concat(urlObj.port ? ":".concat(urlObj.port) : '');
// Assign the formatted url to this.
return _url.default.format(urlObj);
}
/**
* Set one or more of the status properties of the class object.
*
* @public
* @memberof ServiceHost
* @param {Object} pto - The parameter transfer object.
* @property {boolean} [pto.failed] - The failed status to set.
* @property {boolean} [pto.replaced] - the replaced status to set.
* @returns {this}
*/
}, {
key: "setStatus",
value: function setStatus(_ref) {
var failed = _ref.failed,
replaced = _ref.replaced;
if (failed !== undefined) {
this.failed = failed;
}
if (replaced !== undefined) {
this.replaced = replaced;
}
return this;
}
/**
* Generate a service host using only a catalog, name, and URL.
*
* @public
* @static
* @memberof ServiceHost
* @param {ServiceHostPolyGeneratePTO} pto
* @returns {ServiceHost} - The generated service host.
*/
}], [{
key: "polyGenerate",
value: function polyGenerate(_ref2) {
var catalog = _ref2.catalog,
name = _ref2.name,
url = _ref2.url;
return new ServiceHost({
catalog: catalog,
defaultUri: url,
hostGroup: _url.default.parse(url).host,
id: name ? "poly-head:poly-group:poly-cluster:".concat(name) : undefined,
priority: 1,
uri: _url.default.parse(url).host
});
}
/**
* Validate that a constructor parameter transfer object is valid.
*
* @public
* @static
* @memberof ServiceHost
* @param {ServiceHostConstructorPTO} pto
* @throws - If the parameter transfer object is not valid.
* @returns {undefined}
*/
}, {
key: "validate",
value: function validate(_ref3) {
var catalog = _ref3.catalog,
defaultUri = _ref3.defaultUri,
hostGroup = _ref3.hostGroup,
id = _ref3.id,
priority = _ref3.priority,
uri = _ref3.uri;
// Generate error-throwing method.
var throwError = function throwError(msg) {
throw new Error("service-host: invalid constructor parameters, ".concat(msg));
};
// Validate the catalog property.
if (!_constants.SERVICE_CATALOGS.includes(catalog)) {
throwError("'catalog' must be a string");
}
// Validate the `defaultUri` property.
if (typeof defaultUri !== 'string') {
throwError("'defaultUri' must be a string");
}
// Validate the `hostGroup` property.
if (typeof hostGroup !== 'string') {
throwError("'hostGroup' must be a string");
}
// Validate the `id` property.
if (typeof id !== 'string' || id.split(':').length !== 4) {
throwError("'id' must be a string that contains 3 ':' characters");
}
// Validate the `priority` property.
if (typeof priority !== 'number') {
throwError("'priority' must be a number");
}
// Validate the `uri` property.
if (typeof uri !== 'string') {
throwError("'uri' must be a string");
}
}
}]);
return ServiceHost;
}();
//# sourceMappingURL=service-host.js.map