UNPKG

@azure/core-amqp

Version:

Common library for amqp based azure sdks like @azure/event-hubs.

149 lines (148 loc) • 7.06 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var ConnectionContextBase_exports = {}; __export(ConnectionContextBase_exports, { ConnectionContextBase: () => ConnectionContextBase }); module.exports = __toCommonJS(ConnectionContextBase_exports); var import_rhea_promise = require("rhea-promise"); var import_runtimeInfo = require("./util/runtimeInfo.js"); var import_utils = require("./util/utils.js"); var import_cbs = require("./cbs.js"); var import_connectionConfig = require("./connectionConfig/connectionConfig.js"); var import_constants = require("./util/constants.js"); var import_core_util = require("@azure/core-util"); const maxListenerLimit = 1e3; class CoreAmqpConnection extends import_rhea_promise.Connection { /** * Creates an amqp sender link. Max listener limit on the sender is set to 1000 because the * default value of 10 in NodeJS is too low. * @param options - Optional parameters to create a sender link. * @returns Promise<Sender>. */ async createSender(options) { const sender = await super.createSender(options); sender.setMaxListeners(maxListenerLimit); return sender; } /** * Creates an awaitable amqp sender. Max listener limit on the sender is set to 1000 because the * default value of 10 in NodeJS is too low. * @param options - Optional parameters to create an awaitable sender link. * - If `onError` and `onSessionError` handlers are not provided then the `AwaitableSender` will * clear the timer and reject the Promise for all the entries of inflight send operation in its * `deliveryDispositionMap`. * - If the user is handling the reconnection of sender link or the underlying connection in it's * app, then the `onError` and `onSessionError` handlers must be provided by the user and (s)he * shall be responsible of clearing the `deliveryDispositionMap` of inflight `send()` operation. * * @returns Promise<AwaitableSender>. */ async createAwaitableSender(options) { const sender = await super.createAwaitableSender(options); sender.setMaxListeners(maxListenerLimit); return sender; } /** * Creates an amqp receiver link. Max listener limit on the sender is set to 1000 because the * default value of 10 in NodeJS is too low. * @param options - Optional parameters to create a receiver link. * @returns Promise<Receiver>. */ async createReceiver(options) { const receiver = await super.createReceiver(options); receiver.setMaxListeners(maxListenerLimit); return receiver; } } const ConnectionContextBase = { /** * Creates the base connection context. * @param parameters - Parameters to be provided to create * the base connection context. */ create(parameters) { import_connectionConfig.ConnectionConfig.validate(parameters.config, { isEntityPathRequired: parameters.isEntityPathRequired || false }); const userAgent = parameters.connectionProperties.userAgent; if (userAgent.length > import_constants.Constants.maxUserAgentLength) { throw new Error( `The user-agent string cannot be more than ${import_constants.Constants.maxUserAgentLength} characters in length.The given user-agent string is: ${userAgent} with length: ${userAgent.length}` ); } const connectionOptions = { transport: parameters.config.useDevelopmentEmulator ? import_constants.Constants.TCP : import_constants.Constants.TLS, host: parameters.config.host, hostname: parameters.config.amqpHostname ?? parameters.config.host, username: parameters.config.sharedAccessKeyName, port: parameters.config.port ?? (parameters.config.useDevelopmentEmulator ? 5672 : 5671), reconnect: false, properties: { product: parameters.connectionProperties.product, version: parameters.connectionProperties.version, "user-agent": userAgent, platform: (0, import_runtimeInfo.getPlatformInfo)(), framework: (0, import_runtimeInfo.getFrameworkInfo)() }, idle_time_out: import_constants.Constants.defaultConnectionIdleTimeoutInMs, operationTimeoutInSeconds: parameters.operationTimeoutInMs ? parameters.operationTimeoutInMs / 1e3 : void 0 }; const globalWebSocket = !import_core_util.isNodeLike && (0, import_utils.getGlobalProperty)("WebSocket") || void 0; if (parameters.config.webSocket || globalWebSocket) { const socket = parameters.config.webSocket || globalWebSocket; const host = parameters.config.host; const endpoint = parameters.config.webSocketEndpointPath || ""; const socketOptions = parameters.config.webSocketConstructorOptions || {}; const port = parameters.config.port ?? 443; connectionOptions.webSocketOptions = { webSocket: socket, url: `wss://${host}:${port}/${endpoint}`, protocol: ["AMQPWSB10"], options: socketOptions }; } const connection = new CoreAmqpConnection(connectionOptions); const connectionLock = `${import_constants.Constants.establishConnection}-${(0, import_rhea_promise.generate_uuid)()}`; const connectionContextBase = { wasConnectionCloseCalled: false, connectionLock, negotiateClaimLock: `${import_constants.Constants.negotiateClaim}-${(0, import_rhea_promise.generate_uuid)()}`, connection, connectionId: connection.id, cbsSession: new import_cbs.CbsClient(connection, connectionLock), config: parameters.config, refreshConnection() { const newConnection = new CoreAmqpConnection(connectionOptions); const newConnectionLock = `${import_constants.Constants.establishConnection}-${(0, import_rhea_promise.generate_uuid)()}`; this.wasConnectionCloseCalled = false; this.connectionLock = newConnectionLock; this.negotiateClaimLock = `${import_constants.Constants.negotiateClaim} - ${(0, import_rhea_promise.generate_uuid)()}`; this.connection = newConnection; this.connectionId = newConnection.id; this.cbsSession = new import_cbs.CbsClient(newConnection, newConnectionLock); } }; return connectionContextBase; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ConnectionContextBase }); //# sourceMappingURL=ConnectionContextBase.js.map