UNPKG

diffusion

Version:

Diffusion JavaScript client

349 lines (348 loc) 15.1 kB
"use strict"; /** * @module diffusion */ Object.defineProperty(exports, "__esModule", { value: true }); exports.buffer = exports.MetricType = exports.StatusLevel = exports.LogLevel = exports.TopicCreationResult = exports.Type = exports.Prefix = exports.TopicNotificationType = exports.SessionLockScope = exports.ValidationType = exports.PathPermission = exports.GlobalPermission = exports.RemoteServerType = exports.ConnectionState = exports.ConnectionOption = exports.SessionEventType = exports.CloseReasonEnum = exports.ClientCloseReason = exports.locks = exports.clients = exports.errors = exports.topicUpdate = exports.topics = exports.datatypes = exports.newSessionEventParametersBuilder = exports.newRemoteServerBuilder = exports.selectors = exports.newTopicMetricCollectorBuilder = exports.newSessionMetricCollectorBuilder = exports.newBranchMappingTableBuilder = exports.updateConstraints = exports.rolesToString = exports.stringToRoles = exports.escape = exports.connectShared = exports.connect = exports.log = exports.build = exports.version = void 0; require("core-js/features/array"); require("core-js/features/map"); require("core-js/features/number"); require("core-js/features/object/values"); require("core-js/features/promise"); require("core-js/features/set"); var InternalSessionFactory = require("./internal/client/internal-session-factory"); var datatypes_1 = require("./internal/data/datatypes"); var DiffusionGlobals = require("./internal/diffusion/diffusion-globals"); var session_impl_1 = require("./internal/session/session-impl"); var update_constraint_factory_1 = require("./internal/topic-update/update-constraint-factory"); var logger = require("./internal/util/logger"); var error_reason_1 = require("./errors/error-reason"); var client_control_options_1 = require("./features/client-control-options"); var session_lock_options_1 = require("./features/session-lock-options"); var topic_selectors_1 = require("./selectors/topic-selectors"); var topic_update_1 = require("./topic-update/topic-update"); // eslint-disable-next-line @typescript-eslint/no-unused-vars var topics_1 = require("./topics/topics"); logger.setLevel('warn'); /** * The top-level Diffusion API. * * Provides access to Session connections and global namespaces. */ // export namespace diffusion { /** * The version of this client library in the form major.minor.patch */ exports.version = '6.11.6'; /** * The build version of this client library */ exports.build = '57dbfed4'; /** * Set the level of logging used by Diffusion. This will default to silent. * Log levels are strings that represent different degrees of information to * be logged. Available options are: * * * silent * * error * * warn * * info * * debug * * trace * * @param level the log level to use */ function log(level) { // keyof typeof works only because LogLevel maps the keys onto themselves logger.setLevel(level); } exports.log = log; /** * Connect to a specified Diffusion server. This will return a {@link * Result} that will complete successfully if a session can be connected, or * fail if an error was encountered. * * If the result is successful, the fulfilled handler will be called with a * {@link Session} instance. This session will be in a connected state and * may be used for subsequent API calls. * * If the result fails, the rejected handler will be called with an error * reason. * * If `sessionName` and `workerJs` is supplied, then the call will create a * shared session inside a shared WebWorker. If a shared session with that name * already exists, this function will return an instance of the existing * session. Shared sessions can only be created when running in a browser * environment that supports the SharedWorker. For more information regarding * shared sessions, see {@link connectShared}. * * **Example:** * ``` * diffusion.connect('example.server.com').then((session) => { * // Connected with a session * console.log('Connected!', session); * }).catch((error) => { * // Connection failed * console.log('Failed to connect', error); * }); * ``` * * @param options the options to construct the session with. If a string is * supplied, it will be interpreted as the `host` option. * @param sessionName the name of the shared session * @param workerJs the location of the diffusion worker script * @returns a {@link Result} for this operation * * @throws a {@link NullValueError} if only one of `sessionName` or `workerJs` is `undefined` * or if `options` is `undefined` * @throws an {@link IllegalArgumentError} if the options are invalid. */ function connect(options, sessionName, workerJs) { return session_impl_1.SessionImpl.create(InternalSessionFactory.create(sessionName, workerJs), options); } exports.connect = connect; /** * Connect to a specified Diffusion server using a shared WebWorker session. * This will return a {@link Result} that will complete successfully if a * session can be connected, or fail if an error was encountered. Shared * sessions can only be created when running in a browser environment that * supports the SharedWorker. * * Shared sessions are identified by a name. If a shared session with that name * already exists, this function will return an instance of the existing * session. Otherwise the call will fail. Sessions can only be shared across * scripts originating from a single domain. Otherwise the browser will create * independent shared workers resulting in one shared session for each domain. * * The shared session will stay alive as long as there is an open browser tab * that initiated a connection through the shared session. When the last tab is * closed the shared worker holding the shared session will be terminated. The * shared session is also expected to be terminated when the only tab holding a * reference to the session is reloaded or experiences a page navigation. The * exact behavior may be browser dependent. * * The `workerJs` argument must be set to the URL of the `diffusion-worker.js` * supplied with the JavaScript distribution. The same-origin policy of * the shared worker requires the calling script and the `diffusion-worker.js` * to reside on the same domain. * * If the result is successful, the fulfilled handler will be called with a * {@link Session} instance. This session will be in a connected state and * may be used for subsequent API calls. * * If the result fails, the rejected handler will be called with an error * reason. * * **Example:** * ``` * const options = { * host : 'localhost', * port : '8080', * principal : 'admin', * credentials : 'password' * }; * * diffusion.connect(options, 'some-session', 'diffusion-worker.js').then((session) => { * console.log('Connected creating new shared session', session); * }).catch((error) => { * console.log('Failed to connect', error); * }); * * diffusion.connectShared('some-session', 'diffusion-worker.js').then((session) => { * console.log('Connected to existing shared session', session); * }.catch((error) => { * console.log('Failed to connect shared', error); * }); * ``` * * @param sessionName the name of the shared session * @param workerJs the location of the diffusion worker script * @returns a {@link Result} for this operation * @throws an {@link IllegalArgumentError} if the options are invalid. */ function connectShared(sessionName, workerJs) { return session_impl_1.SessionImpl.create(InternalSessionFactory.createShared(sessionName, workerJs), undefined, true); } exports.connectShared = connectShared; /** * Escapes special characters in a string that is to be used within a topic * property or a session filter. * <P> * This is a convenience method which inserts an escape character '\' before * any of the special characters ' " or \. * * @param s the string to be escaped * @returns the string value with escape characters inserted as appropriate * * @since 6.1 */ function escape(s) { return DiffusionGlobals.escape(s); } exports.escape = escape; /** * Utility method which converts a string of the format required by the * {@link PropertyKeys.ROLES $Roles} session property into a mutable set of * strings. * * @param s the string with quoted roles separated by whitespace or * commas * * @return set of roles * @throws an error if the argument is `null` or `undefined` * @throws an {@link IllegalArgumentError} if the input is not correctly quoted * * @since 6.2 */ function stringToRoles(s) { return DiffusionGlobals.stringToRoles(s); } exports.stringToRoles = stringToRoles; /** * Utility method which converts a set of authorisation roles to the string * format required by the {@link PropertyKeys.ROLES $Roles} session property. * * @param roles a set of roles * @return a string representing the supplied roles, formatted as * required by the {@link PropertyKeys.ROLES $Roles} session * property * * @since 6.2 */ function rolesToString(roles) { return DiffusionGlobals.rolesToString(roles); } exports.rolesToString = rolesToString; /** * Returns an update constraint factory. * * @function diffusion#updateConstraints * @return {diffusion.topicUpdate.UpdateConstraintFactory} update constraint * factory * @since 6.2 */ function updateConstraints() { return update_constraint_factory_1.updateConstraintFactory; } exports.updateConstraints = updateConstraints; /** * Create a new {@link BranchMappingTableBuilder}. * * @since 6.7 * @see SessionTrees */ function newBranchMappingTableBuilder() { return DiffusionGlobals.newBranchMappingTableBuilder(); } exports.newBranchMappingTableBuilder = newBranchMappingTableBuilder; /** * Create a new {@link SessionMetricCollectorBuilder}. * * @since 6.7 */ function newSessionMetricCollectorBuilder() { return DiffusionGlobals.newSessionMetricCollectorBuilder(); } exports.newSessionMetricCollectorBuilder = newSessionMetricCollectorBuilder; /** * Create a new {@link TopicMetricCollectorBuilder}. * * @since 6.7 */ function newTopicMetricCollectorBuilder() { return DiffusionGlobals.newTopicMetricCollectorBuilder(); } exports.newTopicMetricCollectorBuilder = newTopicMetricCollectorBuilder; /** * Access to the selectors namespace */ exports.selectors = new topic_selectors_1.TopicSelectors(); /** * Create a new {@link RemoteServerBuilder}. * * @since 6.7 */ function newRemoteServerBuilder(type // eslint-disable-next-line deprecation/deprecation ) { return DiffusionGlobals.newRemoteServerBuilder(exports.selectors, type); } exports.newRemoteServerBuilder = newRemoteServerBuilder; /** * Create a new {@link SessionEventParametersBuilder}. * * @return the builder * * @since 6.11 */ function newSessionEventParametersBuilder() { return DiffusionGlobals.newSessionEventParametersBuilder(); } exports.newSessionEventParametersBuilder = newSessionEventParametersBuilder; /** * Access to the datatypes namespace */ exports.datatypes = datatypes_1.DataTypes; /** * Access to the topics namespace */ exports.topics = topics_1.topicsNamespace; /** * Access to the topicUpdate namespace */ exports.topicUpdate = topic_update_1.TopicUpdateNamespace; /** * The ErrorReason enum */ exports.errors = error_reason_1.ErrorReason; /** * Access to PropertyKeys */ exports.clients = client_control_options_1.ClientControlOptions; /** * Access to the locks namespace */ exports.locks = session_lock_options_1.SessionLockOptions; var close_reason_1 = require("./client/close-reason"); Object.defineProperty(exports, "ClientCloseReason", { enumerable: true, get: function () { return close_reason_1.ClientCloseReason; } }); Object.defineProperty(exports, "CloseReasonEnum", { enumerable: true, get: function () { return close_reason_1.CloseReasonEnum; } }); // eslint-disable-next-line deprecation/deprecation var client_control_1 = require("./features/client-control"); Object.defineProperty(exports, "SessionEventType", { enumerable: true, get: function () { return client_control_1.SessionEventType; } }); var remote_servers_1 = require("./features/remote-servers"); Object.defineProperty(exports, "ConnectionOption", { enumerable: true, get: function () { return remote_servers_1.ConnectionOption; } }); Object.defineProperty(exports, "ConnectionState", { enumerable: true, get: function () { return remote_servers_1.ConnectionState; } }); Object.defineProperty(exports, "RemoteServerType", { enumerable: true, get: function () { return remote_servers_1.RemoteServerType; } }); var security_1 = require("./features/security"); Object.defineProperty(exports, "GlobalPermission", { enumerable: true, get: function () { return security_1.GlobalPermission; } }); Object.defineProperty(exports, "PathPermission", { enumerable: true, get: function () { return security_1.PathPermission; } }); Object.defineProperty(exports, "ValidationType", { enumerable: true, get: function () { return security_1.ValidationType; } }); var session_lock_options_2 = require("./features/session-lock-options"); Object.defineProperty(exports, "SessionLockScope", { enumerable: true, get: function () { return session_lock_options_2.SessionLockScope; } }); var topic_notifications_1 = require("./features/topic-notifications"); Object.defineProperty(exports, "TopicNotificationType", { enumerable: true, get: function () { return topic_notifications_1.TopicNotificationType; } }); var topic_selector_1 = require("./selectors/topic-selector"); Object.defineProperty(exports, "Prefix", { enumerable: true, get: function () { return topic_selector_1.Prefix; } }); Object.defineProperty(exports, "Type", { enumerable: true, get: function () { return topic_selector_1.Type; } }); var topic_update_2 = require("./topic-update/topic-update"); Object.defineProperty(exports, "TopicCreationResult", { enumerable: true, get: function () { return topic_update_2.TopicCreationResult; } }); var logger_1 = require("./util/logger"); Object.defineProperty(exports, "LogLevel", { enumerable: true, get: function () { return logger_1.LogLevel; } }); var gateway_control_1 = require("./internal/features/gateway-control"); Object.defineProperty(exports, "StatusLevel", { enumerable: true, get: function () { return gateway_control_1.StatusLevel; } }); var metrics_1 = require("./features/metrics"); Object.defineProperty(exports, "MetricType", { enumerable: true, get: function () { return metrics_1.MetricType; } }); /** * Access to the Buffer API that is packaged with diffusion. * * This can be used in browsers that don't have a native Buffer class. It allows * the creation of buffers for use with {@link BinaryDataType binary} datatypes. * * @example * ``` * const buf = diffusion.buffer.from('Hello World', 'utf8') * ``` * * @deprecated since version 6.11. Buffer has been replaced by Uint8Array.This * will be removed in a future release. */ exports.buffer = Buffer;