{"version":3,"file":"utils.cjs","names":[],"sources":["../src/utils.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { Attributes } from '@opentelemetry/api';\nimport type { InstrumentationScope } from '@opentelemetry/core';\n\nexport type Maybe<T> = T | undefined;\n\n/**\n * Converting the unordered attributes into unique identifier string.\n * @param attributes user provided unordered Attributes.\n */\nexport function hashAttributes(attributes: Attributes): string {\n  let keys = Object.keys(attributes);\n  if (keys.length === 0) return '';\n\n  // Return a string that is stable on key orders.\n  keys = keys.sort();\n  return JSON.stringify(keys.map(key => [key, attributes[key]]));\n}\n\n/**\n * Converting the instrumentation scope object to a unique identifier string.\n * @param instrumentationScope\n */\nexport function instrumentationScopeId(\n  instrumentationScope: InstrumentationScope\n): string {\n  return `${instrumentationScope.name}:${instrumentationScope.version ?? ''}:${\n    instrumentationScope.schemaUrl ?? ''\n  }`;\n}\n\n/**\n * Error that is thrown on timeouts.\n */\nexport class TimeoutError extends Error {\n  constructor(message?: string) {\n    super(message);\n\n    // manually adjust prototype to retain `instanceof` functionality when targeting ES5, see:\n    // https://github.com/Microsoft/TypeScript-wiki/blob/main/Breaking-Changes.md#extending-built-ins-like-error-array-and-map-may-no-longer-work\n    Object.setPrototypeOf(this, TimeoutError.prototype);\n  }\n}\n\n/**\n * Adds a timeout to a promise and rejects if the specified timeout has elapsed. Also rejects if the specified promise\n * rejects, and resolves if the specified promise resolves.\n *\n * <p> NOTE: this operation will continue even after it throws a {@link TimeoutError}.\n *\n * @param promise promise to use with timeout.\n * @param timeout the timeout in milliseconds until the returned promise is rejected.\n */\nexport function callWithTimeout<T>(\n  promise: Promise<T>,\n  timeout: number\n): Promise<T> {\n  let timeoutHandle: ReturnType<typeof setTimeout>;\n\n  const timeoutPromise = new Promise<never>(function timeoutFunction(\n    _resolve,\n    reject\n  ) {\n    timeoutHandle = setTimeout(function timeoutHandler() {\n      reject(new TimeoutError('Operation timed out.'));\n    }, timeout);\n  });\n\n  return Promise.race([promise, timeoutPromise]).then(\n    result => {\n      clearTimeout(timeoutHandle);\n      return result;\n    },\n    reason => {\n      clearTimeout(timeoutHandle);\n      throw reason;\n    }\n  );\n}\n\nexport function setEquals(lhs: Set<unknown>, rhs: Set<unknown>): boolean {\n  if (lhs.size !== rhs.size) {\n    return false;\n  }\n  for (const item of lhs) {\n    if (!rhs.has(item)) {\n      return false;\n    }\n  }\n  return true;\n}\n\n/**\n * Binary search the sorted array to the find upper bound for the value.\n * @param arr\n * @param value\n * @returns\n */\nexport function binarySearchUB(arr: number[], value: number): number {\n  let lo = 0;\n  let hi = arr.length - 1;\n  let ret = arr.length;\n\n  while (hi >= lo) {\n    const mid = lo + Math.trunc((hi - lo) / 2);\n    if (arr[mid] < value) {\n      lo = mid + 1;\n    } else {\n      ret = mid;\n      hi = mid - 1;\n    }\n  }\n\n  return ret;\n}\n\nexport function equalsCaseInsensitive(lhs: string, rhs: string): boolean {\n  return lhs.toLowerCase() === rhs.toLowerCase();\n}\n"],"mappings":";;;;;AAcA,SAAgB,eAAe,YAAgC;CAC7D,IAAI,OAAO,OAAO,KAAK,UAAU;CACjC,IAAI,KAAK,WAAW,GAAG,OAAO;CAG9B,OAAO,KAAK,KAAK;CACjB,OAAO,KAAK,UAAU,KAAK,KAAI,QAAO,CAAC,KAAK,WAAW,IAAI,CAAC,CAAC;AAC/D;;;;;AAMA,SAAgB,uBACd,sBACQ;CACR,OAAO,GAAG,qBAAqB,KAAK,GAAG,qBAAqB,WAAW,GAAG,GACxE,qBAAqB,aAAa;AAEtC;;;;AAKA,IAAa,eAAb,MAAa,qBAAqB,MAAM;CACtC,YAAY,SAAkB;EAC5B,MAAM,OAAO;EAIb,OAAO,eAAe,MAAM,aAAa,SAAS;CACpD;AACF;;;;;;;;;;AAWA,SAAgB,gBACd,SACA,SACY;CACZ,IAAI;CAEJ,MAAM,iBAAiB,IAAI,QAAe,SAAS,gBACjD,UACA,QACA;EACA,gBAAgB,WAAW,SAA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