UNPKG

diffusion

Version:

Diffusion JavaScript client

336 lines (335 loc) 13.3 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.OptionsImpl = exports.MIN_MAX_MESSAGE_SIZE = exports.DEFAULT_RETRY_STRATEGY = void 0; var errors_1 = require("./../../errors/errors"); var math_1 = require("./../util/math"); var require_non_null_1 = require("./../util/require-non-null"); // tslint:disable-next-line:strict-type-predicates var DEFAULT_HOST = (typeof window !== 'undefined' && window.location.hostname) ? window.location.hostname : 'localhost'; // tslint:disable-next-line:strict-type-predicates var DEFAULT_PORT = (typeof window !== 'undefined' && window.location.protocol === 'http:' && window.location.port) ? parseInt(window.location.port, 10) : 80; // tslint:disable-next-line:strict-type-predicates var DEFAULT_SECURE_PORT = (typeof window !== 'undefined' && window.location.protocol === 'https:' && window.location.port) ? parseInt(window.location.port, 10) : 443; // tslint:disable-next-line:strict-type-predicates var DEFAULT_SECURE = typeof window === 'undefined' || window.location.protocol !== 'http:'; var DEFAULT_PATH = '/diffusion'; /** * The amount of time waiting for a connection to occur. */ /* istanbul ignore next */ var DEFAULT_CONNECT_TIMEOUT = (_a = global.DIFFUSION_CONNECT_TIMEOUT) !== null && _a !== void 0 ? _a : 10000; /** * Default initial connection retry strategy */ exports.DEFAULT_RETRY_STRATEGY = { attempts: 0, interval: 0 }; var DEFAULT_RECONNECT_TIMEOUT = 60000; var DEFAULT_RECONNECT_STRATEGY = function (start) { setTimeout(start, 5000); }; var DEFAULT_ABORT_STRATEGY = function (start, abort) { abort(); }; var DEFAULT_PRINCIPAL = ''; var DEFAULT_PASSWORD = ''; var DEFAULT_ACTIVITY_MONITOR = true; // tslint:disable-next-line:strict-type-predicates var DEFAULT_TRANSPORTS = typeof window === 'undefined' ? ['WEBSOCKET'] : ['WEBSOCKET', 'XHR']; var DEFAULT_MAX_MESSAGE_SIZE = 2147483647; /** * The minimum value that the maximum message size can be set to in bytes. */ exports.MIN_MAX_MESSAGE_SIZE = 1024; /** * Implementation of the {@link Options} interface. * * @inheritdoc */ var OptionsImpl = /** @class */ (function () { /** * Create a new OptionsImpl instance * * The constructor takes a partially defined Options object and sets all * undefined properties with their default values. * * @param options the options to create the instance from */ // eslint-disable-next-line complexity function OptionsImpl(options) { if (options === void 0) { options = {}; } var _a; // override options.host and options.port if supplied together // in options.host. if (options.host === undefined) { options.host = DEFAULT_HOST; } else if ((options.host.match(/:/g) || []).length === 1 || options.host.match(/^\[[^\]]*\]:\d+$/)) { var portSep = options.host.lastIndexOf(':'); if (options.port === undefined) { options.port = parseInt(options.host.substr(portSep + 1), 10); } options.host = options.host.substr(0, portSep); } // wrap IPv6 host in brackets if (options.host.indexOf(':') > -1 && !options.host.match(/^\[/)) { options.host = "[" + options.host + "]"; } if (options.path === undefined) { options.path = DEFAULT_PATH; } else { if (options.path[0] !== '/') { options.path = '/' + options.path; } // assert that the path ends with '/diffusion' if (options.path.substring(options.path.length - DEFAULT_PATH.length) !== DEFAULT_PATH) { if (options.path[options.path.length - 1] === '/') { options.path = options.path.substring(0, options.path.length - 1); } options.path = options.path + DEFAULT_PATH; } } if (options.port !== undefined) { if (typeof options.port === 'string') { if (isNaN(parseInt(options.port, 10))) { // set to undefined in order to let us derive 'secure' option correctly options.port = undefined; } else { options.port = parseInt(options.port, 10); } } } if (options.secure === undefined) { if (options.port === undefined) { // default to secure on secure port. options.secure = DEFAULT_SECURE; } else { // if specified port 80, default to insecure else secure. options.secure = options.port === DEFAULT_SECURE_PORT ? true : false; } } if (options.port === undefined) { // security specified but not port, choose 443 or 80? options.port = options.secure ? DEFAULT_SECURE_PORT : DEFAULT_PORT; } this.host = options.host; this.port = options.port; this.path = options.path; this.secure = options.secure; this.connectionTimeout = (_a = options.connectionTimeout) !== null && _a !== void 0 ? _a : DEFAULT_CONNECT_TIMEOUT; if (options.retry) { if (options.retry.attempts === null || options.retry.attempts === undefined) { if (options.retry.interval === null || options.retry.interval === undefined) { this.retry = __assign({}, exports.DEFAULT_RETRY_STRATEGY); } else { this.retry = { attempts: math_1.MAX_SAFE_INTEGER, interval: options.retry.interval }; } } else { require_non_null_1.requireNonNull(options.retry.interval, 'options.retry.interval'); this.retry = options.retry; } } else { this.retry = __assign({}, exports.DEFAULT_RETRY_STRATEGY); } if (options.reconnect === undefined || (typeof options.reconnect === 'boolean') && options.reconnect) { this.reconnect = { timeout: DEFAULT_RECONNECT_TIMEOUT, strategy: DEFAULT_RECONNECT_STRATEGY }; } else if (typeof options.reconnect === 'number') { this.reconnect = { timeout: options.reconnect, strategy: DEFAULT_RECONNECT_STRATEGY }; } else if (typeof options.reconnect === 'function') { this.reconnect = { timeout: DEFAULT_RECONNECT_TIMEOUT, strategy: options.reconnect }; } else if (typeof options.reconnect === 'object') { this.reconnect = { // tslint:disable-next-line:strict-type-predicates timeout: options.reconnect.timeout === undefined ? DEFAULT_RECONNECT_TIMEOUT : options.reconnect.timeout, strategy: options.reconnect.strategy || DEFAULT_RECONNECT_STRATEGY }; } else { this.reconnect = { timeout: 0, strategy: DEFAULT_ABORT_STRATEGY }; } if (options.principal !== undefined) { this.principal = options.principal || DEFAULT_PRINCIPAL; if (typeof options.credentials === 'string') { this.credentials = options.credentials; } else if (isTypedArray(options.credentials)) { this.credentials = new Uint8Array(options.credentials.buffer, options.credentials.byteOffset, options.credentials.byteLength); } else if (Array.isArray(options.credentials)) { ensureOctetArray(options.credentials); this.credentials = Uint8Array.from(options.credentials); } else { this.credentials = DEFAULT_PASSWORD; } } if (typeof options.transports === 'string') { this.transports = [options.transports]; } else if (typeof options.transports === 'object' && options.transports instanceof Array && options.transports.length > 0) { this.transports = options.transports.slice(); } else { this.transports = DEFAULT_TRANSPORTS.slice(); } this.transports = this.transports.slice().map(function (t) { return t.toUpperCase(); }); var mms = (options.maxMessageSize && options.maxMessageSize > exports.MIN_MAX_MESSAGE_SIZE) ? options.maxMessageSize : DEFAULT_MAX_MESSAGE_SIZE; this.maxMessageSize = mms; // tslint:disable-next-line:strict-type-predicates this.activityMonitor = (options.activityMonitor !== undefined) ? options.activityMonitor : DEFAULT_ACTIVITY_MONITOR; if (options.properties !== undefined) { var properties_1 = {}; Object.getOwnPropertyNames(options.properties).forEach(function (key) { properties_1[key] = options.properties[key].toString(); }); this.properties = properties_1; } this.httpProxyAgent = options.httpProxyAgent; if (options.tlsOptions !== undefined) { var tlsOptions_1 = options.tlsOptions; this.tlsOptions = ['ca', 'cert', 'ciphers', 'clientCertEngine', 'crl', 'dhparam', 'ecdhCurve', 'honorCipherOrder', 'key', 'passphrase', 'pfx', 'secureOptions', 'secureProtocol', 'sessionIdContext', 'rejectUnauthorized'] .filter(function (key) { return key in tlsOptions_1; }) .reduce(function (obj, key) { obj[key] = tlsOptions_1[key]; return obj; }, {}); } } /** * Create a new Options object containing the union of this with additional * options. * * @param options options to merge * @returns the new options */ OptionsImpl.prototype.with = function (options) { var e_1, _a, e_2, _b; var o = {}; var k; try { for (var _c = __values(Object.getOwnPropertyNames(this)), _d = _c.next(); !_d.done; _d = _c.next()) { k = _d.value; o[k] = this[k]; } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_d && !_d.done && (_a = _c.return)) _a.call(_c); } finally { if (e_1) throw e_1.error; } } try { for (var _e = __values(Object.getOwnPropertyNames(options)), _f = _e.next(); !_f.done; _f = _e.next()) { k = _f.value; o[k] = options[k]; } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_f && !_f.done && (_b = _e.return)) _b.call(_e); } finally { if (e_2) throw e_2.error; } } return new OptionsImpl(o); }; return OptionsImpl; }()); exports.OptionsImpl = OptionsImpl; /** * Ensure that the array contains only octet numbers * * @param value the array to check * @throws an error if any entry in the array is not a number or out of * range */ function ensureOctetArray(value) { value.forEach(function (element) { // tslint:disable-next-line:strict-type-predicates if (typeof element !== 'number' || element > 127 || element < -128) { throw new errors_1.IllegalArgumentError('Custom credentials invalid. Element must be octet.'); } }); } /** * Check if the argument is a typed array * * @param obj the object to check * @return `true` if the object is an instance of a typed array */ function isTypedArray(obj) { return (obj instanceof Int8Array || obj instanceof Int16Array || obj instanceof Int32Array || obj instanceof Uint8Array || obj instanceof Uint8ClampedArray || obj instanceof Uint16Array || obj instanceof Uint32Array || obj instanceof Float32Array || obj instanceof Float64Array); }