@azure/core-amqp
Version:
Common library for amqp based azure sdks like @azure/event-hubs.
133 lines (132 loc) • 4.84 kB
JavaScript
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 connectionConfig_exports = {};
__export(connectionConfig_exports, {
ConnectionConfig: () => ConnectionConfig,
isSharedAccessSignature: () => isSharedAccessSignature
});
module.exports = __toCommonJS(connectionConfig_exports);
var import_core_util = require("@azure/core-util");
var import_utils = require("../util/utils.js");
const specialLocalIPs = ["::1", "0:0:0:0:0:0:0:1"];
function getHost(endpoint) {
for (const ip of specialLocalIPs) {
if (endpoint.includes(ip)) {
return ip;
}
}
const matches = /.*:\/\/([^/:]*)/.exec(endpoint);
const match = matches?.[1];
return !match ? endpoint : match;
}
function extractPort(ep) {
const matches = /.*:(\d*)/.exec(ep);
const match = matches?.[1];
return match ? parseInt(match, 10) : void 0;
}
function getPort(endpoint) {
for (const ip of specialLocalIPs) {
if (endpoint.includes(ip)) {
return extractPort(endpoint.replace(ip, ""));
}
}
return extractPort(endpoint);
}
const ConnectionConfig = {
/**
* Creates the connection config.
* @param connectionString - The connection string for a given service like
* EventHub/ServiceBus.
* @param path - The name/path of the entity (hub name) to which the
* connection needs to happen. This will override the EntityPath in the connectionString
* if present.
* @returns ConnectionConfig
*/
create(connectionString, path) {
connectionString = String(connectionString);
const parsedCS = (0, import_utils.parseConnectionString)(connectionString);
if (!parsedCS.Endpoint) {
throw new TypeError("Missing Endpoint in Connection String.");
}
if (!parsedCS.Endpoint.endsWith("/")) parsedCS.Endpoint += "/";
let port;
if (parsedCS.Endpoint.includes(":")) {
port = getPort(parsedCS.Endpoint);
}
const result = {
connectionString,
endpoint: parsedCS.Endpoint,
host: getHost(parsedCS.Endpoint),
sharedAccessKeyName: parsedCS.SharedAccessKeyName,
sharedAccessKey: parsedCS.SharedAccessKey,
useDevelopmentEmulator: parsedCS.UseDevelopmentEmulator === "true",
...port !== void 0 ? { port } : void 0
};
if (path || parsedCS.EntityPath) {
result.entityPath = path || parsedCS.EntityPath;
}
return result;
},
/**
* Validates the properties of connection config.
* @param config - The connection config to be validated.
* @returns void
*/
validate(config, options) {
if (!options) options = {};
if (!config) {
throw new TypeError("Missing configuration");
}
if (!config.endpoint) {
throw new TypeError("Missing 'endpoint' in configuration");
}
config.endpoint = String(config.endpoint);
if (!config.host) {
throw new TypeError("Missing 'host' in configuration");
}
config.host = String(config.host);
if (config.port !== void 0 && !(config.port >= 0 && config.port <= 65535)) {
throw new TypeError(`Invalid 'port' of ${config.port} in configuration`);
}
if (options.isEntityPathRequired && !config.entityPath) {
throw new TypeError("Missing 'entityPath' in configuration");
}
if ((0, import_core_util.isDefined)(config.entityPath)) {
config.entityPath = String(config.entityPath);
}
if (!isSharedAccessSignature(config.connectionString)) {
if (!config.sharedAccessKeyName) {
throw new TypeError("Missing 'sharedAccessKeyName' in configuration");
}
config.sharedAccessKeyName = String(config.sharedAccessKeyName);
if (!config.sharedAccessKey) {
throw new TypeError("Missing 'sharedAccessKey' in configuration");
}
config.sharedAccessKey = String(config.sharedAccessKey);
}
}
};
function isSharedAccessSignature(connectionString) {
return connectionString.match(/;{0,1}SharedAccessSignature=SharedAccessSignature /) != null;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ConnectionConfig,
isSharedAccessSignature
});
//# sourceMappingURL=connectionConfig.js.map