UNPKG

@azure/core-amqp

Version:

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

96 lines 3.15 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. import { CancellableAsyncLockImpl } from "./lock.js"; import { delay as wrapperDelay } from "@azure/core-util"; /** * Parses the connection string and returns an object of type T. * * Connection strings have the following syntax: * * ConnectionString ::= `Part { ";" Part } [ ";" ] [ WhiteSpace ]` * Part ::= [ PartLiteral [ "=" PartLiteral ] ] * PartLiteral ::= [ WhiteSpace ] Literal [ WhiteSpace ] * Literal ::= ? any sequence of characters except ; or = or WhiteSpace ? * WhiteSpace ::= ? all whitespace characters including `\r` and `\n` ? * * @param connectionString - The connection string to be parsed. * @returns ParsedOutput<T>. */ export function parseConnectionString(connectionString) { const output = {}; const parts = connectionString.trim().split(";"); for (let part of parts) { part = part.trim(); if (part === "") { // parts can be empty continue; } const splitIndex = part.indexOf("="); if (splitIndex === -1) { throw new Error("Connection string malformed: each part of the connection string must have an `=` assignment."); } const key = part.substring(0, splitIndex).trim(); if (key === "") { throw new Error("Connection string malformed: missing key for assignment"); } const value = part.substring(splitIndex + 1).trim(); output[key] = value; } return output; } /** * The cancellable async lock instance. */ export const defaultCancellableLock = new CancellableAsyncLockImpl(); /** * A wrapper for setTimeout that resolves a promise after t milliseconds. * @param delayInMs - The number of milliseconds to be delayed. * @param abortSignal - The abortSignal associated with containing operation. * @param abortErrorMsg - The abort error message associated with containing operation. * @param value - The value to be resolved with after a timeout of t milliseconds. * @returns - Resolved promise */ export async function delay(delayInMs, abortSignal, abortErrorMsg, value) { await wrapperDelay(delayInMs, { abortSignal: abortSignal, abortErrorMsg: abortErrorMsg, }); if (value !== undefined) { return value; } } /** * Checks if an address is localhost. * @param address - The address to check. * @returns true if the address is localhost, false otherwise. */ export function isLoopbackAddress(address) { return /^(.*:\/\/)?(127\.[\d.]+|[0:]+1|localhost)/.test(address.toLowerCase()); } /** * @internal */ export function isString(s) { return typeof s === "string"; } /** * @internal */ export function isNumber(n) { return typeof n === "number"; } /** * @internal * Safely read a property from the global object by key. * Returns undefined if the property or global object is unavailable. */ export function getGlobalProperty(key) { try { const g = globalThis; return g?.[key]; } catch { return undefined; } } //# sourceMappingURL=utils.js.map