@azure/communication-common 2.4.2-alpha.20260211.1 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/README.md +47 -1
  2. package/dist/browser/autoRefreshTokenCredential.d.ts +20 -4
  3. package/dist/browser/autoRefreshTokenCredential.d.ts.map +1 -1
  4. package/dist/browser/autoRefreshTokenCredential.js +13 -4
  5. package/dist/browser/autoRefreshTokenCredential.js.map +1 -1
  6. package/dist/browser/azureCommunicationTokenCredential.d.ts +3 -2
  7. package/dist/browser/azureCommunicationTokenCredential.d.ts.map +1 -1
  8. package/dist/browser/azureCommunicationTokenCredential.js +3 -2
  9. package/dist/browser/azureCommunicationTokenCredential.js.map +1 -1
  10. package/dist/browser/credential/communicationAccessKeyCredentialPolicy.d.ts.map +1 -1
  11. package/dist/browser/credential/communicationAccessKeyCredentialPolicy.js +6 -10
  12. package/dist/browser/credential/communicationAccessKeyCredentialPolicy.js.map +1 -1
  13. package/dist/browser/entraTokenCredential.d.ts.map +1 -1
  14. package/dist/browser/entraTokenCredential.js +12 -7
  15. package/dist/browser/entraTokenCredential.js.map +1 -1
  16. package/dist/browser/index.d.ts +4 -3
  17. package/dist/browser/index.d.ts.map +1 -1
  18. package/dist/browser/index.js +1 -0
  19. package/dist/browser/index.js.map +1 -1
  20. package/dist/browser/logger.d.ts +3 -0
  21. package/dist/browser/logger.d.ts.map +1 -0
  22. package/dist/browser/logger.js +6 -0
  23. package/dist/browser/logger.js.map +1 -0
  24. package/dist/browser/tokenParser.d.ts +9 -1
  25. package/dist/browser/tokenParser.d.ts.map +1 -1
  26. package/dist/browser/tokenParser.js +64 -3
  27. package/dist/browser/tokenParser.js.map +1 -1
  28. package/dist/commonjs/autoRefreshTokenCredential.d.ts +20 -4
  29. package/dist/commonjs/autoRefreshTokenCredential.d.ts.map +1 -1
  30. package/dist/commonjs/autoRefreshTokenCredential.js +118 -95
  31. package/dist/commonjs/autoRefreshTokenCredential.js.map +7 -1
  32. package/dist/commonjs/azureCommunicationTokenCredential.d.ts +3 -2
  33. package/dist/commonjs/azureCommunicationTokenCredential.d.ts.map +1 -1
  34. package/dist/commonjs/azureCommunicationTokenCredential.js +64 -47
  35. package/dist/commonjs/azureCommunicationTokenCredential.js.map +7 -1
  36. package/dist/commonjs/communicationTokenCredential.js +16 -5
  37. package/dist/commonjs/communicationTokenCredential.js.map +7 -1
  38. package/dist/commonjs/credential/clientArguments.js +46 -40
  39. package/dist/commonjs/credential/clientArguments.js.map +7 -1
  40. package/dist/commonjs/credential/communicationAccessKeyCredentialPolicy.d.ts.map +1 -1
  41. package/dist/commonjs/credential/communicationAccessKeyCredentialPolicy.js +53 -42
  42. package/dist/commonjs/credential/communicationAccessKeyCredentialPolicy.js.map +7 -1
  43. package/dist/commonjs/credential/communicationAuthPolicy.js +39 -26
  44. package/dist/commonjs/credential/communicationAuthPolicy.js.map +7 -1
  45. package/dist/commonjs/credential/connectionString.js +39 -28
  46. package/dist/commonjs/credential/connectionString.js.map +7 -1
  47. package/dist/commonjs/credential/index.js +28 -10
  48. package/dist/commonjs/credential/index.js.map +7 -1
  49. package/dist/commonjs/entraTokenCredential.d.ts.map +1 -1
  50. package/dist/commonjs/entraTokenCredential.js +137 -113
  51. package/dist/commonjs/entraTokenCredential.js.map +7 -1
  52. package/dist/commonjs/identifierModelSerializer.js +172 -159
  53. package/dist/commonjs/identifierModelSerializer.js.map +7 -1
  54. package/dist/commonjs/identifierModels.js +183 -205
  55. package/dist/commonjs/identifierModels.js.map +7 -1
  56. package/dist/commonjs/index.d.ts +4 -3
  57. package/dist/commonjs/index.d.ts.map +1 -1
  58. package/dist/commonjs/index.js +40 -12
  59. package/dist/commonjs/index.js.map +7 -1
  60. package/dist/commonjs/logger.d.ts +3 -0
  61. package/dist/commonjs/logger.d.ts.map +1 -0
  62. package/dist/commonjs/logger.js +29 -0
  63. package/dist/commonjs/logger.js.map +7 -0
  64. package/dist/commonjs/staticTokenCredential.js +36 -20
  65. package/dist/commonjs/staticTokenCredential.js.map +7 -1
  66. package/dist/commonjs/tokenParser.d.ts +9 -1
  67. package/dist/commonjs/tokenParser.d.ts.map +1 -1
  68. package/dist/commonjs/tokenParser.js +71 -13
  69. package/dist/commonjs/tokenParser.js.map +7 -1
  70. package/dist/commonjs/tsdoc-metadata.json +1 -1
  71. package/dist/esm/autoRefreshTokenCredential.d.ts +20 -4
  72. package/dist/esm/autoRefreshTokenCredential.d.ts.map +1 -1
  73. package/dist/esm/autoRefreshTokenCredential.js +13 -4
  74. package/dist/esm/autoRefreshTokenCredential.js.map +1 -1
  75. package/dist/esm/azureCommunicationTokenCredential.d.ts +3 -2
  76. package/dist/esm/azureCommunicationTokenCredential.d.ts.map +1 -1
  77. package/dist/esm/azureCommunicationTokenCredential.js +3 -2
  78. package/dist/esm/azureCommunicationTokenCredential.js.map +1 -1
  79. package/dist/esm/credential/communicationAccessKeyCredentialPolicy.d.ts.map +1 -1
  80. package/dist/esm/credential/communicationAccessKeyCredentialPolicy.js +6 -10
  81. package/dist/esm/credential/communicationAccessKeyCredentialPolicy.js.map +1 -1
  82. package/dist/esm/entraTokenCredential.d.ts.map +1 -1
  83. package/dist/esm/entraTokenCredential.js +12 -7
  84. package/dist/esm/entraTokenCredential.js.map +1 -1
  85. package/dist/esm/index.d.ts +4 -3
  86. package/dist/esm/index.d.ts.map +1 -1
  87. package/dist/esm/index.js +1 -0
  88. package/dist/esm/index.js.map +1 -1
  89. package/dist/esm/logger.d.ts +3 -0
  90. package/dist/esm/logger.d.ts.map +1 -0
  91. package/dist/esm/logger.js +6 -0
  92. package/dist/esm/logger.js.map +1 -0
  93. package/dist/esm/tokenParser.d.ts +9 -1
  94. package/dist/esm/tokenParser.d.ts.map +1 -1
  95. package/dist/esm/tokenParser.js +64 -3
  96. package/dist/esm/tokenParser.js.map +1 -1
  97. package/dist/react-native/autoRefreshTokenCredential.d.ts +20 -4
  98. package/dist/react-native/autoRefreshTokenCredential.d.ts.map +1 -1
  99. package/dist/react-native/autoRefreshTokenCredential.js +13 -4
  100. package/dist/react-native/autoRefreshTokenCredential.js.map +1 -1
  101. package/dist/react-native/azureCommunicationTokenCredential.d.ts +3 -2
  102. package/dist/react-native/azureCommunicationTokenCredential.d.ts.map +1 -1
  103. package/dist/react-native/azureCommunicationTokenCredential.js +3 -2
  104. package/dist/react-native/azureCommunicationTokenCredential.js.map +1 -1
  105. package/dist/react-native/credential/communicationAccessKeyCredentialPolicy.d.ts.map +1 -1
  106. package/dist/react-native/credential/communicationAccessKeyCredentialPolicy.js +6 -10
  107. package/dist/react-native/credential/communicationAccessKeyCredentialPolicy.js.map +1 -1
  108. package/dist/react-native/entraTokenCredential.d.ts.map +1 -1
  109. package/dist/react-native/entraTokenCredential.js +12 -7
  110. package/dist/react-native/entraTokenCredential.js.map +1 -1
  111. package/dist/react-native/index.d.ts +4 -3
  112. package/dist/react-native/index.d.ts.map +1 -1
  113. package/dist/react-native/index.js +1 -0
  114. package/dist/react-native/index.js.map +1 -1
  115. package/dist/react-native/logger.d.ts +3 -0
  116. package/dist/react-native/logger.d.ts.map +1 -0
  117. package/dist/react-native/logger.js +6 -0
  118. package/dist/react-native/logger.js.map +1 -0
  119. package/dist/react-native/tokenParser.d.ts +9 -1
  120. package/dist/react-native/tokenParser.d.ts.map +1 -1
  121. package/dist/react-native/tokenParser.js +64 -3
  122. package/dist/react-native/tokenParser.js.map +1 -1
  123. package/package.json +34 -41
  124. package/dist/browser/credential/cryptoUtils-browser.d.mts.map +0 -1
  125. package/dist/browser/credential/cryptoUtils-browser.mjs.map +0 -1
  126. package/dist/browser/credential/cryptoUtils.d.ts +0 -3
  127. package/dist/browser/credential/cryptoUtils.js +0 -19
  128. package/dist/browser/credential/encodeUtils-browser.d.mts +0 -4
  129. package/dist/browser/credential/encodeUtils-browser.d.mts.map +0 -1
  130. package/dist/browser/credential/encodeUtils-browser.mjs +0 -19
  131. package/dist/browser/credential/encodeUtils-browser.mjs.map +0 -1
  132. package/dist/commonjs/credential/cryptoUtils.d.ts +0 -3
  133. package/dist/commonjs/credential/cryptoUtils.d.ts.map +0 -1
  134. package/dist/commonjs/credential/cryptoUtils.js +0 -14
  135. package/dist/commonjs/credential/cryptoUtils.js.map +0 -1
  136. package/dist/esm/credential/cryptoUtils.d.ts +0 -3
  137. package/dist/esm/credential/cryptoUtils.d.ts.map +0 -1
  138. package/dist/esm/credential/cryptoUtils.js +0 -9
  139. package/dist/esm/credential/cryptoUtils.js.map +0 -1
  140. package/dist/esm/credential/encodeUtils-browser.d.mts +0 -4
  141. package/dist/esm/credential/encodeUtils-browser.d.mts.map +0 -1
  142. package/dist/esm/credential/encodeUtils-browser.mjs +0 -19
  143. package/dist/esm/credential/encodeUtils-browser.mjs.map +0 -1
  144. package/dist/react-native/credential/cryptoUtils.d.ts +0 -3
  145. package/dist/react-native/credential/cryptoUtils.d.ts.map +0 -1
  146. package/dist/react-native/credential/cryptoUtils.js +0 -9
  147. package/dist/react-native/credential/cryptoUtils.js.map +0 -1
  148. package/dist/react-native/credential/encodeUtils-browser.d.mts +0 -4
  149. package/dist/react-native/credential/encodeUtils-browser.d.mts.map +0 -1
  150. package/dist/react-native/credential/encodeUtils-browser.mjs +0 -19
  151. package/dist/react-native/credential/encodeUtils-browser.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"tokenParser.js","sourceRoot":"","sources":["../../src/tokenParser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAMvC,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAa,EAAe,EAAE;IACvD,MAAM,EAAE,GAAG,EAAE,GAAG,SAAS,CAAW,KAAK,CAAC,CAAC;IAC3C,OAAO;QACL,KAAK;QACL,kBAAkB,EAAE,GAAG,GAAG,IAAI;KAC/B,CAAC;AACJ,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { AccessToken } from \"@azure/core-auth\";\nimport { jwtDecode } from \"jwt-decode\";\n\ninterface JwtToken {\n exp: number;\n}\n\nexport const parseToken = (token: string): AccessToken => {\n const { exp } = jwtDecode<JwtToken>(token);\n return {\n token,\n expiresOnTimestamp: exp * 1000,\n };\n};\n"]}
1
+ {"version":3,"file":"tokenParser.js","sourceRoot":"","sources":["../../src/tokenParser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAGlC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAMrC;;;;;GAKG;AACH,MAAM,8CAA8C,GAAG,EAAE,GAAG,EAAE,CAAC;AAE/D,IAAI,8BAA8B,GAAG,KAAK,CAAC;AAE3C,8FAA8F;AAC9F,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAc,EAAwB,EAAE,CACpE,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,KAAK,IAAI;IACd,OAAQ,KAAqB,CAAC,KAAK,KAAK,QAAQ;IAC/C,KAAqB,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC;IACvC,OAAQ,KAAqB,CAAC,kBAAkB,KAAK,QAAQ;IAC7D,MAAM,CAAC,QAAQ,CAAE,KAAqB,CAAC,kBAAkB,CAAC,CAAC;AAE7D;;;;GAIG;AACH,MAAM,kBAAkB,GAAG,CACzB,KAAa,EACb,uCAA+C,EAClC,EAAE;IACf,IACE,CAAC,MAAM,CAAC,QAAQ,CAAC,uCAAuC,CAAC;QACzD,uCAAuC,IAAI,CAAC,EAC5C,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,oEAAoE,CAAC,CAAC;IACxF,CAAC;IACD,IAAI,CAAC,8BAA8B,EAAE,CAAC;QACpC,8BAA8B,GAAG,IAAI,CAAC;QACtC,MAAM,CAAC,OAAO,CACZ,oFAAoF,uCAAuC,cAAc;YACvI,0HAA0H,CAC7H,CAAC;IACJ,CAAC;IACD,OAAO;QACL,KAAK;QACL,kBAAkB,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,uCAAuC,GAAG,IAAI;KAChF,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,KAA2B,EAC3B,0CAAkD,8CAA8C,EACnF,EAAE;IACf,qDAAqD;IACrD,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CACb,4GAA4G,CAC7G,CAAC;IACJ,CAAC;IACD,IAAI,KAAK,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC7D,CAAC;IAED,0CAA0C;IAC1C,IAAI,CAAC;QACH,MAAM,EAAE,GAAG,EAAE,GAAG,SAAS,CAAW,KAAK,CAAC,CAAC;QAC3C,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YACpD,OAAO;gBACL,KAAK;gBACL,kBAAkB,EAAE,GAAG,GAAG,IAAI;aAC/B,CAAC;QACJ,CAAC;IACH,CAAC;IAAC,MAAM,CAAC;QACP,oDAAoD;IACtD,CAAC;IAED,OAAO,kBAAkB,CAAC,KAAK,EAAE,uCAAuC,CAAC,CAAC;AAC5E,CAAC,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { AccessToken } from \"@azure/core-auth\";\nimport { jwtDecode } from \"jwt-decode\";\nimport { logger } from \"./logger.js\";\n\ninterface JwtToken {\n exp: number;\n}\n\n/**\n * Default lifetime (in seconds) assumed for an encrypted/undecodable token when no expiry is\n * supplied. Must stay in step with `defaultExpiringSoonInterval` (10 min) in\n * autoRefreshTokenCredential.ts: the \"refresh an opaque token at half-life\" behavior relies on a\n * fresh fallback token immediately landing in the expiring-soon window.\n */\nconst defaultUndecodableTokenExpiryIntervalInSeconds = 10 * 60;\n\nlet undecodableTokenWarningEmitted = false;\n\n/** Type guard for a caller-supplied {@link AccessToken} (carries a valid explicit expiry). */\nexport const isAccessToken = (value: unknown): value is AccessToken =>\n typeof value === \"object\" &&\n value !== null &&\n typeof (value as AccessToken).token === \"string\" &&\n (value as AccessToken).token.length > 0 &&\n typeof (value as AccessToken).expiresOnTimestamp === \"number\" &&\n Number.isFinite((value as AccessToken).expiresOnTimestamp);\n\n/**\n * Accepts a token that can't be decoded (encrypted/opaque, or a JWT without a usable `exp`) and\n * assigns a fallback expiry of `now + undecodableTokenExpiryIntervalInSeconds`. Emits a one-time\n * warning; throws if the interval is not a positive, finite number.\n */\nconst resolveOpaqueToken = (\n token: string,\n undecodableTokenExpiryIntervalInSeconds: number,\n): AccessToken => {\n if (\n !Number.isFinite(undecodableTokenExpiryIntervalInSeconds) ||\n undecodableTokenExpiryIntervalInSeconds <= 0\n ) {\n throw new Error(\"undecodableTokenExpiryIntervalInSeconds must be a positive number.\");\n }\n if (!undecodableTokenWarningEmitted) {\n undecodableTokenWarningEmitted = true;\n logger.warning(\n `Received a token that could not be decoded; treating it as opaque and assuming a ${undecodableTokenExpiryIntervalInSeconds}s lifetime. ` +\n `Supply an AccessToken with an explicit expiry (e.g. from the token response 'expires_in') to control refresh scheduling.`,\n );\n }\n return {\n token,\n expiresOnTimestamp: Date.now() + undecodableTokenExpiryIntervalInSeconds * 1000,\n };\n};\n\n/**\n * Resolves a token into an {@link AccessToken}. Expiry is taken from a caller-supplied\n * `AccessToken` if present, otherwise from the JWT `exp` claim. A token that cannot be decoded\n * (e.g. an encrypted/opaque token) or that lacks a usable `exp` is accepted as-is with a fallback\n * expiry of `now + undecodableTokenExpiryIntervalInSeconds`; an empty/blank token throws.\n */\nexport const parseToken = (\n token: string | AccessToken,\n undecodableTokenExpiryIntervalInSeconds: number = defaultUndecodableTokenExpiryIntervalInSeconds,\n): AccessToken => {\n // Caller supplied an explicit expiry — use it as-is.\n if (isAccessToken(token)) {\n return token;\n }\n if (typeof token !== \"string\") {\n throw new Error(\n \"Invalid token: expected a string or an AccessToken with a non-empty token and a finite expiresOnTimestamp.\",\n );\n }\n if (token.trim().length === 0) {\n throw new Error(\"Invalid token: token must not be empty.\");\n }\n\n // Prefer the expiry from a decodable JWT.\n try {\n const { exp } = jwtDecode<JwtToken>(token);\n if (typeof exp === \"number\" && Number.isFinite(exp)) {\n return {\n token,\n expiresOnTimestamp: exp * 1000,\n };\n }\n } catch {\n // Not decodable — treated as an opaque token below.\n }\n\n return resolveOpaqueToken(token, undecodableTokenExpiryIntervalInSeconds);\n};\n"]}
@@ -6,25 +6,41 @@ import type { AccessToken } from "@azure/core-auth";
6
6
  */
7
7
  export interface CommunicationTokenRefreshOptions {
8
8
  /**
9
- * Callback function that returns a string JWT token acquired from the Communication Identity API.
9
+ * Callback that returns a token acquired from the Communication Identity API.
10
10
  * The returned token must be valid (expiration date must be in the future).
11
+ *
12
+ * Return a `string` to have the expiry read from the JWT, or an `AccessToken`
13
+ * to supply the expiry explicitly (required for encrypted access tokens that
14
+ * cannot be decoded).
11
15
  */
12
- tokenRefresher: (abortSignal?: AbortSignalLike) => Promise<string>;
16
+ tokenRefresher: (abortSignal?: AbortSignalLike) => Promise<string | AccessToken>;
13
17
  /**
14
- * Optional token to initialize.
18
+ * Optional token to initialize. May be a `string` or an `AccessToken` carrying
19
+ * an explicit expiry.
15
20
  */
16
- token?: string;
21
+ token?: string | AccessToken;
17
22
  /**
18
23
  * Indicates whether the token should be proactively renewed prior to expiry or only renew on demand.
19
24
  * By default false.
20
25
  */
21
26
  refreshProactively?: boolean;
27
+ /**
28
+ * Lifetime, in seconds, assumed for a returned token that cannot be decoded and
29
+ * carries no explicit expiry (e.g. an encrypted access token). Defaults to 600 (10 minutes).
30
+ *
31
+ * Set this to your tokens' real lifetime. It is not capped: a value larger than the token's
32
+ * actual lifetime means the credential refreshes later than it should and the service will
33
+ * reject the expired token. To avoid guessing, prefer returning an `AccessToken` with an
34
+ * explicit `expiresOnTimestamp` (e.g. derived from the token response's `expires_in`).
35
+ */
36
+ undecodableTokenExpiryIntervalInSeconds?: number;
22
37
  }
23
38
  export declare class AutoRefreshTokenCredential implements TokenCredential {
24
39
  private readonly refresh;
25
40
  private readonly refreshProactively;
26
41
  private readonly expiringSoonIntervalInMs;
27
42
  private readonly refreshAfterLifetimePercentage;
43
+ private readonly undecodableTokenExpiryIntervalInSeconds;
28
44
  private currentToken;
29
45
  private activeTimeout;
30
46
  private activeTokenFetching;
@@ -1 +1 @@
1
- {"version":3,"file":"autoRefreshTokenCredential.d.ts","sourceRoot":"","sources":["../../src/autoRefreshTokenCredential.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,4BAA4B,EAC5B,eAAe,EAChB,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGpD;;GAEG;AACH,MAAM,WAAW,gCAAgC;IAC/C;;;OAGG;IACH,cAAc,EAAE,CAAC,WAAW,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAEnE;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAOD,qBAAa,0BAA2B,YAAW,eAAe;IAChE,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqD;IAC7E,OAAO,CAAC,QAAQ,CAAC,kBAAkB,CAAU;IAC7C,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAAuC;IAChF,OAAO,CAAC,QAAQ,CAAC,8BAA8B,CAAyC;IAExF,OAAO,CAAC,YAAY,CAAc;IAClC,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CAAC,QAAQ,CAAC,uCAAuC,CAAqB;IAE7E,OAAO,CAAC,YAAY,CAAc;IAClC,OAAO,CAAC,aAAa,CAA4C;IACjE,OAAO,CAAC,mBAAmB,CAA8C;IACzE,OAAO,CAAC,mBAAmB,CAA8B;IACzD,OAAO,CAAC,QAAQ,CAAS;gBAEb,WAAW,EAAE,gCAAgC;IAgB5C,QAAQ,CAAC,OAAO,CAAC,EAAE,4BAA4B,GAAG,OAAO,CAAC,WAAW,CAAC;IAa5E,OAAO,IAAI,IAAI;YAUR,wBAAwB;YAexB,yBAAyB;YAazB,YAAY;IAc1B,OAAO,CAAC,eAAe;IAqBvB,OAAO,CAAC,YAAY;IAIpB,OAAO,CAAC,mBAAmB;CAG5B"}
@@ -1,109 +1,132 @@
1
- "use strict";
2
- // Copyright (c) Microsoft Corporation.
3
- // Licensed under the MIT License.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.AutoRefreshTokenCredential = void 0;
6
- const tokenParser_js_1 = require("./tokenParser.js");
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var autoRefreshTokenCredential_exports = {};
19
+ __export(autoRefreshTokenCredential_exports, {
20
+ AutoRefreshTokenCredential: () => AutoRefreshTokenCredential
21
+ });
22
+ module.exports = __toCommonJS(autoRefreshTokenCredential_exports);
23
+ var import_tokenParser = require("./tokenParser.js");
7
24
  const expiredToken = { token: "", expiresOnTimestamp: -10 };
8
- const minutesToMs = (minutes) => minutes * 1000 * 60;
25
+ const minutesToMs = (minutes) => minutes * 1e3 * 60;
9
26
  const defaultExpiringSoonInterval = minutesToMs(10);
10
27
  const defaultRefreshAfterLifetimePercentage = 0.5;
11
28
  class AutoRefreshTokenCredential {
12
- refresh;
13
- refreshProactively;
14
- expiringSoonIntervalInMs = defaultExpiringSoonInterval;
15
- refreshAfterLifetimePercentage = defaultRefreshAfterLifetimePercentage;
16
- currentToken;
17
- activeTimeout;
18
- activeTokenFetching = null;
19
- activeTokenUpdating = null;
20
- disposed = false;
21
- constructor(refreshArgs) {
22
- const { tokenRefresher, token, refreshProactively } = refreshArgs;
23
- this.refresh = tokenRefresher;
24
- this.currentToken = token ? (0, tokenParser_js_1.parseToken)(token) : expiredToken;
25
- this.refreshProactively = refreshProactively ?? false;
26
- if (this.refreshProactively) {
27
- this.scheduleRefresh();
28
- }
29
+ refresh;
30
+ refreshProactively;
31
+ expiringSoonIntervalInMs = defaultExpiringSoonInterval;
32
+ refreshAfterLifetimePercentage = defaultRefreshAfterLifetimePercentage;
33
+ undecodableTokenExpiryIntervalInSeconds;
34
+ currentToken;
35
+ activeTimeout;
36
+ activeTokenFetching = null;
37
+ activeTokenUpdating = null;
38
+ disposed = false;
39
+ constructor(refreshArgs) {
40
+ const { tokenRefresher, token, refreshProactively, undecodableTokenExpiryIntervalInSeconds } = refreshArgs;
41
+ this.refresh = tokenRefresher;
42
+ this.undecodableTokenExpiryIntervalInSeconds = undecodableTokenExpiryIntervalInSeconds;
43
+ this.currentToken = token ? (0, import_tokenParser.parseToken)(token, this.undecodableTokenExpiryIntervalInSeconds) : expiredToken;
44
+ this.refreshProactively = refreshProactively ?? false;
45
+ if (this.refreshProactively) {
46
+ this.scheduleRefresh();
29
47
  }
30
- async getToken(options) {
31
- if (!this.isTokenExpiringSoon(this.currentToken)) {
32
- return this.currentToken;
33
- }
34
- if (!this.isTokenValid(this.currentToken)) {
35
- const updatePromise = this.updateTokenAndReschedule(options?.abortSignal);
36
- await updatePromise;
37
- }
38
- return this.currentToken;
48
+ }
49
+ async getToken(options) {
50
+ if (!this.isTokenExpiringSoon(this.currentToken)) {
51
+ return this.currentToken;
39
52
  }
40
- dispose() {
41
- this.disposed = true;
42
- this.activeTokenFetching = null;
43
- this.activeTokenUpdating = null;
44
- this.currentToken = expiredToken;
45
- if (this.activeTimeout) {
46
- clearTimeout(this.activeTimeout);
47
- }
53
+ if (!this.isTokenValid(this.currentToken)) {
54
+ const updatePromise = this.updateTokenAndReschedule(options?.abortSignal);
55
+ await updatePromise;
48
56
  }
49
- async updateTokenAndReschedule(abortSignal) {
50
- if (this.activeTokenUpdating) {
51
- return this.activeTokenUpdating;
52
- }
53
- this.activeTokenUpdating = this.refreshTokenAndReschedule(abortSignal);
54
- try {
55
- await this.activeTokenUpdating;
56
- }
57
- finally {
58
- this.activeTokenUpdating = null;
59
- }
57
+ return this.currentToken;
58
+ }
59
+ dispose() {
60
+ this.disposed = true;
61
+ this.activeTokenFetching = null;
62
+ this.activeTokenUpdating = null;
63
+ this.currentToken = expiredToken;
64
+ if (this.activeTimeout !== void 0) {
65
+ clearTimeout(this.activeTimeout);
60
66
  }
61
- async refreshTokenAndReschedule(abortSignal) {
62
- const newToken = await this.refreshToken(abortSignal);
63
- if (!this.isTokenValid(newToken)) {
64
- throw new Error("The token returned from the tokenRefresher is expired.");
65
- }
66
- this.currentToken = newToken;
67
- if (this.refreshProactively) {
68
- this.scheduleRefresh();
69
- }
67
+ }
68
+ async updateTokenAndReschedule(abortSignal) {
69
+ if (this.disposed) {
70
+ return;
70
71
  }
71
- async refreshToken(abortSignal) {
72
- try {
73
- if (!this.activeTokenFetching) {
74
- this.activeTokenFetching = this.refresh(abortSignal);
75
- }
76
- return (0, tokenParser_js_1.parseToken)(await this.activeTokenFetching);
77
- }
78
- finally {
79
- this.activeTokenFetching = null;
80
- }
72
+ if (this.activeTokenUpdating) {
73
+ return this.activeTokenUpdating;
81
74
  }
82
- scheduleRefresh() {
83
- if (this.disposed) {
84
- return;
85
- }
86
- if (this.activeTimeout) {
87
- clearTimeout(this.activeTimeout);
88
- }
89
- const tokenTtlInMs = this.currentToken.expiresOnTimestamp - Date.now();
90
- let timespanInMs = null;
91
- if (this.isTokenExpiringSoon(this.currentToken)) {
92
- // Schedule the next refresh for when it reaches a certain percentage of the remaining lifetime.
93
- timespanInMs = tokenTtlInMs * this.refreshAfterLifetimePercentage;
94
- }
95
- else {
96
- // Schedule the next refresh for when it gets in to the soon-to-expire window.
97
- timespanInMs = tokenTtlInMs - this.expiringSoonIntervalInMs;
98
- }
99
- this.activeTimeout = setTimeout(() => this.updateTokenAndReschedule(), timespanInMs);
75
+ this.activeTokenUpdating = this.refreshTokenAndReschedule(abortSignal);
76
+ try {
77
+ await this.activeTokenUpdating;
78
+ } finally {
79
+ this.activeTokenUpdating = null;
100
80
  }
101
- isTokenValid(token) {
102
- return token && Date.now() < token.expiresOnTimestamp;
81
+ }
82
+ async refreshTokenAndReschedule(abortSignal) {
83
+ const newToken = await this.refreshToken(abortSignal);
84
+ if (!this.isTokenValid(newToken)) {
85
+ throw new Error("The token returned from the tokenRefresher is expired.");
103
86
  }
104
- isTokenExpiringSoon(token) {
105
- return !token || Date.now() >= token.expiresOnTimestamp - this.expiringSoonIntervalInMs;
87
+ this.currentToken = newToken;
88
+ if (this.refreshProactively) {
89
+ this.scheduleRefresh();
106
90
  }
91
+ }
92
+ async refreshToken(abortSignal) {
93
+ try {
94
+ if (!this.activeTokenFetching) {
95
+ this.activeTokenFetching = this.refresh(abortSignal);
96
+ }
97
+ return (0, import_tokenParser.parseToken)(
98
+ await this.activeTokenFetching,
99
+ this.undecodableTokenExpiryIntervalInSeconds
100
+ );
101
+ } finally {
102
+ this.activeTokenFetching = null;
103
+ }
104
+ }
105
+ scheduleRefresh() {
106
+ if (this.disposed) {
107
+ return;
108
+ }
109
+ if (this.activeTimeout) {
110
+ clearTimeout(this.activeTimeout);
111
+ }
112
+ const tokenTtlInMs = this.currentToken.expiresOnTimestamp - Date.now();
113
+ let timespanInMs = null;
114
+ if (this.isTokenExpiringSoon(this.currentToken)) {
115
+ timespanInMs = tokenTtlInMs * this.refreshAfterLifetimePercentage;
116
+ } else {
117
+ timespanInMs = tokenTtlInMs - this.expiringSoonIntervalInMs;
118
+ }
119
+ this.activeTimeout = setTimeout(() => this.updateTokenAndReschedule(), timespanInMs);
120
+ }
121
+ isTokenValid(token) {
122
+ return token && Date.now() < token.expiresOnTimestamp;
123
+ }
124
+ isTokenExpiringSoon(token) {
125
+ return !token || Date.now() >= token.expiresOnTimestamp - this.expiringSoonIntervalInMs;
126
+ }
107
127
  }
108
- exports.AutoRefreshTokenCredential = AutoRefreshTokenCredential;
109
- //# sourceMappingURL=autoRefreshTokenCredential.js.map
128
+ // Annotate the CommonJS export names for ESM import in node:
129
+ 0 && (module.exports = {
130
+ AutoRefreshTokenCredential
131
+ });
132
+ //# sourceMappingURL=autoRefreshTokenCredential.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"autoRefreshTokenCredential.js","sourceRoot":"","sources":["../../src/autoRefreshTokenCredential.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAQlC,qDAA8C;AAwB9C,MAAM,YAAY,GAAG,EAAE,KAAK,EAAE,EAAE,EAAE,kBAAkB,EAAE,CAAC,EAAE,EAAE,CAAC;AAC5D,MAAM,WAAW,GAAG,CAAC,OAAe,EAAU,EAAE,CAAC,OAAO,GAAG,IAAI,GAAG,EAAE,CAAC;AACrE,MAAM,2BAA2B,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;AACpD,MAAM,qCAAqC,GAAG,GAAG,CAAC;AAElD,MAAa,0BAA0B;IACpB,OAAO,CAAqD;IAC5D,kBAAkB,CAAU;IAC5B,wBAAwB,GAAW,2BAA2B,CAAC;IAC/D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AClB,OAAO;QACT,CAAC;QACD,IAAI,IAAI,CAAC,aAAa,EAAE,CAAC;YACvB,YAAY,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,CAAC,kBAAkB,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QACvE,IAAI,YAAY,GAAG,IAAI,CAAC;QAExB,IAAI,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,CAAC;YAChD,gGAAgG;YAChG,YAAY,GAAG,YAAY,GAAG,IAAI,CAAC,8BAA8B,CAAC;QACpE,CAAC;aAAM,CAAC;YACN,8EAA8E;YAC9E,YAAY,GAAG,YAAY,GAAG,IAAI,CAAC,wBAAwB,CAAC;QAC9D,CAAC;QAED,IAAI,CAAC,aAAa,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,wBAAwB,EAAE,EAAE,YAAY,CAAC,CAAC;IACvF,CAAC;IAEO,YAAY,CAAC,KAAkB;QACrC,OAAO,KAAK,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,kBAAkB,CAAC;IACxD,CAAC;IAEO,mBAAmB,CAAC,KAAkB;QAC5C,OAAO,CAAC,KAAK,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,KAAK,CAAC,kBAAkB,GAAG,IAAI,CAAC,wBAAwB,CAAC;IAC1F,CAAC;CACF;AA/GD,gEA+GC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type {\n CommunicationGetTokenOptions,\n TokenCredential,\n} from \"./communicationTokenCredential.js\";\nimport type { AbortSignalLike } from \"@azure/abort-controller\";\nimport type { AccessToken } from \"@azure/core-auth\";\nimport { parseToken } from \"./tokenParser.js\";\n\n/**\n * Options for auto-refreshing a Communication Token credential.\n */\nexport interface CommunicationTokenRefreshOptions {\n /**\n * Callback function that returns a string JWT token acquired from the Communication Identity API.\n * The returned token must be valid (expiration date must be in the future).\n */\n tokenRefresher: (abortSignal?: AbortSignalLike) => Promise<string>;\n\n /**\n * Optional token to initialize.\n */\n token?: string;\n\n /**\n * Indicates whether the token should be proactively renewed prior to expiry or only renew on demand.\n * By default false.\n */\n refreshProactively?: boolean;\n}\n\nconst expiredToken = { token: \"\", expiresOnTimestamp: -10 };\nconst minutesToMs = (minutes: number): number => minutes * 1000 * 60;\nconst defaultExpiringSoonInterval = minutesToMs(10);\nconst defaultRefreshAfterLifetimePercentage = 0.5;\n\nexport class AutoRefreshTokenCredential implements TokenCredential {\n private readonly refresh: (abortSignal?: AbortSignalLike) => Promise<string>;\n private readonly refreshProactively: boolean;\n private readonly expiringSoonIntervalInMs: number = defaultExpiringSoonInterval;\n private readonly refreshAfterLifetimePercentage = defaultRefreshAfterLifetimePercentage;\n\n private currentToken: AccessToken;\n private activeTimeout: ReturnType<typeof setTimeout> | undefined;\n private activeTokenFetching: Promise<string> | null = null;\n private activeTokenUpdating: Promise<void> | null = null;\n private disposed = false;\n\n constructor(refreshArgs: CommunicationTokenRefreshOptions) {\n const { tokenRefresher, token, refreshProactively } = refreshArgs;\n\n this.refresh = tokenRefresher;\n this.currentToken = token ? parseToken(token) : expiredToken;\n this.refreshProactively = refreshProactively ?? false;\n\n if (this.refreshProactively) {\n this.scheduleRefresh();\n }\n }\n\n public async getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken> {\n if (!this.isTokenExpiringSoon(this.currentToken)) {\n return this.currentToken;\n }\n\n if (!this.isTokenValid(this.currentToken)) {\n const updatePromise = this.updateTokenAndReschedule(options?.abortSignal);\n await updatePromise;\n }\n\n return this.currentToken;\n }\n\n public dispose(): void {\n this.disposed = true;\n this.activeTokenFetching = null;\n this.activeTokenUpdating = null;\n this.currentToken = expiredToken;\n if (this.activeTimeout) {\n clearTimeout(this.activeTimeout);\n }\n }\n\n private async updateTokenAndReschedule(abortSignal?: AbortSignalLike): Promise<void> {\n if (this.activeTokenUpdating) {\n return this.activeTokenUpdating;\n }\n this.activeTokenUpdating = this.refreshTokenAndReschedule(abortSignal);\n try {\n await this.activeTokenUpdating;\n } finally {\n this.activeTokenUpdating = null;\n }\n }\n\n private async refreshTokenAndReschedule(abortSignal?: AbortSignalLike): Promise<void> {\n const newToken = await this.refreshToken(abortSignal);\n\n if (!this.isTokenValid(newToken)) {\n throw new Error(\"The token returned from the tokenRefresher is expired.\");\n }\n\n this.currentToken = newToken;\n if (this.refreshProactively) {\n this.scheduleRefresh();\n }\n }\n\n private async refreshToken(abortSignal?: AbortSignalLike): Promise<AccessToken> {\n try {\n if (!this.activeTokenFetching) {\n this.activeTokenFetching = this.refresh(abortSignal);\n }\n return parseToken(await this.activeTokenFetching);\n } finally {\n this.activeTokenFetching = null;\n }\n }\n\n private scheduleRefresh(): void {\n if (this.disposed) {\n return;\n }\n if (this.activeTimeout) {\n clearTimeout(this.activeTimeout);\n }\n const tokenTtlInMs = this.currentToken.expiresOnTimestamp - Date.now();\n let timespanInMs = null;\n\n if (this.isTokenExpiringSoon(this.currentToken)) {\n // Schedule the next refresh for when it reaches a certain percentage of the remaining lifetime.\n timespanInMs = tokenTtlInMs * this.refreshAfterLifetimePercentage;\n } else {\n // Schedule the next refresh for when it gets in to the soon-to-expire window.\n timespanInMs = tokenTtlInMs - this.expiringSoonIntervalInMs;\n }\n\n this.activeTimeout = setTimeout(() => this.updateTokenAndReschedule(), timespanInMs);\n }\n\n private isTokenValid(token: AccessToken): boolean {\n return token && Date.now() < token.expiresOnTimestamp;\n }\n\n private isTokenExpiringSoon(token: AccessToken): boolean {\n return !token || Date.now() >= token.expiresOnTimestamp - this.expiringSoonIntervalInMs;\n }\n}\n"]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/autoRefreshTokenCredential.ts"],
4
+ "sourcesContent": ["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type {\n CommunicationGetTokenOptions,\n TokenCredential,\n} from \"./communicationTokenCredential.js\";\nimport type { AbortSignalLike } from \"@azure/abort-controller\";\nimport type { AccessToken } from \"@azure/core-auth\";\nimport { parseToken } from \"./tokenParser.js\";\n\n/**\n * Options for auto-refreshing a Communication Token credential.\n */\nexport interface CommunicationTokenRefreshOptions {\n /**\n * Callback that returns a token acquired from the Communication Identity API.\n * The returned token must be valid (expiration date must be in the future).\n *\n * Return a `string` to have the expiry read from the JWT, or an `AccessToken`\n * to supply the expiry explicitly (required for encrypted access tokens that\n * cannot be decoded).\n */\n tokenRefresher: (abortSignal?: AbortSignalLike) => Promise<string | AccessToken>;\n\n /**\n * Optional token to initialize. May be a `string` or an `AccessToken` carrying\n * an explicit expiry.\n */\n token?: string | AccessToken;\n\n /**\n * Indicates whether the token should be proactively renewed prior to expiry or only renew on demand.\n * By default false.\n */\n refreshProactively?: boolean;\n\n /**\n * Lifetime, in seconds, assumed for a returned token that cannot be decoded and\n * carries no explicit expiry (e.g. an encrypted access token). Defaults to 600 (10 minutes).\n *\n * Set this to your tokens' real lifetime. It is not capped: a value larger than the token's\n * actual lifetime means the credential refreshes later than it should and the service will\n * reject the expired token. To avoid guessing, prefer returning an `AccessToken` with an\n * explicit `expiresOnTimestamp` (e.g. derived from the token response's `expires_in`).\n */\n undecodableTokenExpiryIntervalInSeconds?: number;\n}\n\nconst expiredToken = { token: \"\", expiresOnTimestamp: -10 };\nconst minutesToMs = (minutes: number): number => minutes * 1000 * 60;\n// Must stay in step with `defaultUndecodableTokenExpiryIntervalInSeconds` (600s) in tokenParser.ts \u2014\n// an opaque token's fallback expiry is expected to land in this expiring-soon window.\nconst defaultExpiringSoonInterval = minutesToMs(10);\nconst defaultRefreshAfterLifetimePercentage = 0.5;\n\nexport class AutoRefreshTokenCredential implements TokenCredential {\n private readonly refresh: (abortSignal?: AbortSignalLike) => Promise<string | AccessToken>;\n private readonly refreshProactively: boolean;\n private readonly expiringSoonIntervalInMs: number = defaultExpiringSoonInterval;\n private readonly refreshAfterLifetimePercentage = defaultRefreshAfterLifetimePercentage;\n private readonly undecodableTokenExpiryIntervalInSeconds: number | undefined;\n\n private currentToken: AccessToken;\n private activeTimeout: ReturnType<typeof setTimeout> | undefined;\n private activeTokenFetching: Promise<string | AccessToken> | null = null;\n private activeTokenUpdating: Promise<void> | null = null;\n private disposed = false;\n\n constructor(refreshArgs: CommunicationTokenRefreshOptions) {\n const { tokenRefresher, token, refreshProactively, undecodableTokenExpiryIntervalInSeconds } =\n refreshArgs;\n\n this.refresh = tokenRefresher;\n this.undecodableTokenExpiryIntervalInSeconds = undecodableTokenExpiryIntervalInSeconds;\n this.currentToken = token\n ? parseToken(token, this.undecodableTokenExpiryIntervalInSeconds)\n : expiredToken;\n this.refreshProactively = refreshProactively ?? false;\n\n if (this.refreshProactively) {\n this.scheduleRefresh();\n }\n }\n\n public async getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken> {\n if (!this.isTokenExpiringSoon(this.currentToken)) {\n return this.currentToken;\n }\n\n if (!this.isTokenValid(this.currentToken)) {\n const updatePromise = this.updateTokenAndReschedule(options?.abortSignal);\n await updatePromise;\n }\n\n return this.currentToken;\n }\n\n public dispose(): void {\n this.disposed = true;\n this.activeTokenFetching = null;\n this.activeTokenUpdating = null;\n this.currentToken = expiredToken;\n if (this.activeTimeout !== undefined) {\n clearTimeout(this.activeTimeout);\n }\n }\n\n private async updateTokenAndReschedule(abortSignal?: AbortSignalLike): Promise<void> {\n if (this.disposed) {\n return;\n }\n if (this.activeTokenUpdating) {\n return this.activeTokenUpdating;\n }\n this.activeTokenUpdating = this.refreshTokenAndReschedule(abortSignal);\n try {\n await this.activeTokenUpdating;\n } finally {\n this.activeTokenUpdating = null;\n }\n }\n\n private async refreshTokenAndReschedule(abortSignal?: AbortSignalLike): Promise<void> {\n const newToken = await this.refreshToken(abortSignal);\n\n if (!this.isTokenValid(newToken)) {\n throw new Error(\"The token returned from the tokenRefresher is expired.\");\n }\n\n this.currentToken = newToken;\n if (this.refreshProactively) {\n this.scheduleRefresh();\n }\n }\n\n private async refreshToken(abortSignal?: AbortSignalLike): Promise<AccessToken> {\n try {\n if (!this.activeTokenFetching) {\n this.activeTokenFetching = this.refresh(abortSignal);\n }\n return parseToken(\n await this.activeTokenFetching,\n this.undecodableTokenExpiryIntervalInSeconds,\n );\n } finally {\n this.activeTokenFetching = null;\n }\n }\n\n private scheduleRefresh(): void {\n if (this.disposed) {\n return;\n }\n if (this.activeTimeout) {\n clearTimeout(this.activeTimeout);\n }\n const tokenTtlInMs = this.currentToken.expiresOnTimestamp - Date.now();\n let timespanInMs = null;\n\n if (this.isTokenExpiringSoon(this.currentToken)) {\n // Schedule the next refresh for when it reaches a certain percentage of the remaining lifetime.\n timespanInMs = tokenTtlInMs * this.refreshAfterLifetimePercentage;\n } else {\n // Schedule the next refresh for when it gets in to the soon-to-expire window.\n timespanInMs = tokenTtlInMs - this.expiringSoonIntervalInMs;\n }\n\n this.activeTimeout = setTimeout(() => this.updateTokenAndReschedule(), timespanInMs);\n }\n\n private isTokenValid(token: AccessToken): boolean {\n return token && Date.now() < token.expiresOnTimestamp;\n }\n\n private isTokenExpiringSoon(token: AccessToken): boolean {\n return !token || Date.now() >= token.expiresOnTimestamp - this.expiringSoonIntervalInMs;\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,yBAA2B;AAwC3B,MAAM,eAAe,EAAE,OAAO,IAAI,oBAAoB,IAAI;AAC1D,MAAM,cAAc,CAAC,YAA4B,UAAU,MAAO;AAGlE,MAAM,8BAA8B,YAAY,EAAE;AAClD,MAAM,wCAAwC;AAEvC,MAAM,2BAAsD;AAAA,EAChD;AAAA,EACA;AAAA,EACA,2BAAmC;AAAA,EACnC,iCAAiC;AAAA,EACjC;AAAA,EAET;AAAA,EACA;AAAA,EACA,sBAA4D;AAAA,EAC5D,sBAA4C;AAAA,EAC5C,WAAW;AAAA,EAEnB,YAAY,aAA+C;AACzD,UAAM,EAAE,gBAAgB,OAAO,oBAAoB,wCAAwC,IACzF;AAE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K,sBAAsB,KAAK,QAAQ,WAAW;AAAA,MACrD;AACA,iBAAO;AAAA,QACL,MAAM,KAAK;AAAA,QACX,KAAK;AAAA,MACP;AAAA,IACF,UAAE;AACA,WAAK,sBAAsB;AAAA,IAC7B;AAAA,EACF;AAAA,EAEQ,kBAAwB;AAC9B,QAAI,KAAK,UAAU;AACjB;AAAA,IACF;AACA,QAAI,KAAK,eAAe;AACtB,mBAAa,KAAK,aAAa;AAAA,IACjC;AACA,UAAM,eAAe,KAAK,aAAa,qBAAqB,KAAK,IAAI;AACrE,QAAI,eAAe;AAEnB,QAAI,KAAK,oBAAoB,KAAK,YAAY,GAAG;AAE/C,qBAAe,eAAe,KAAK;AAAA,IACrC,OAAO;AAEL,qBAAe,eAAe,KAAK;AAAA,IACrC;AAEA,SAAK,gBAAgB,WAAW,MAAM,KAAK,yBAAyB,GAAG,YAAY;AAAA,EACrF;AAAA,EAEQ,aAAa,OAA6B;AAChD,WAAO,SAAS,KAAK,IAAI,IAAI,MAAM;AAAA,EACrC;AAAA,EAEQ,oBAAoB,OAA6B;AACvD,WAAO,CAAC,SAAS,KAAK,IAAI,KAAK,MAAM,qBAAqB,KAAK;AAAA,EACjE;AACF;",
6
+ "names": []
7
+ }
@@ -10,9 +10,10 @@ export declare class AzureCommunicationTokenCredential implements CommunicationT
10
10
  private disposed;
11
11
  /**
12
12
  * Creates an instance of CommunicationTokenCredential with a static token and no proactive refreshing.
13
- * @param token - A user access token issued by Communication Services.
13
+ * @param token - A user access token issued by Communication Services. Pass an `AccessToken`
14
+ * to supply the expiry explicitly when the token cannot be decoded (e.g. an encrypted token).
14
15
  */
15
- constructor(token: string);
16
+ constructor(token: string | AccessToken);
16
17
  /**
17
18
  * Creates an instance of CommunicationTokenCredential with a lambda to get a token and options
18
19
  * to configure proactive refreshing.
@@ -1 +1 @@
1
- {"version":3,"file":"azureCommunicationTokenCredential.d.ts","sourceRoot":"","sources":["../../src/azureCommunicationTokenCredential.ts"],"names":[],"mappings":"AAG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C5D;;;OAGG;gBACS,YAAY,EAAE,wCAAwC;IAsBlE;;;OAGG;IACU,QAAQ,CAAC,OAAO,CAAC,EAAE,4BAA4B,GAAG,OAAO,CAAC,WAAW,CAAC;IAOnF;;OAEG;IACI,OAAO,IAAI,IAAI;IAKtB,OAAO,CAAC,eAAe;CAKxB"}
@@ -1,51 +1,68 @@
1
- "use strict";
2
- // Copyright (c) Microsoft Corporation.
3
- // Licensed under the MIT License.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- exports.AzureCommunicationTokenCredential = void 0;
6
- const autoRefreshTokenCredential_js_1 = require("./autoRefreshTokenCredential.js");
7
- const staticTokenCredential_js_1 = require("./staticTokenCredential.js");
8
- const tokenParser_js_1 = require("./tokenParser.js");
9
- const entraTokenCredential_js_1 = require("./entraTokenCredential.js");
10
- /**
11
- * The CommunicationTokenCredential implementation with support for proactive token refresh.
12
- */
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var azureCommunicationTokenCredential_exports = {};
19
+ __export(azureCommunicationTokenCredential_exports, {
20
+ AzureCommunicationTokenCredential: () => AzureCommunicationTokenCredential
21
+ });
22
+ module.exports = __toCommonJS(azureCommunicationTokenCredential_exports);
23
+ var import_autoRefreshTokenCredential = require("./autoRefreshTokenCredential.js");
24
+ var import_staticTokenCredential = require("./staticTokenCredential.js");
25
+ var import_tokenParser = require("./tokenParser.js");
26
+ var import_entraTokenCredential = require("./entraTokenCredential.js");
13
27
  class AzureCommunicationTokenCredential {
14
- tokenCredential;
15
- disposed = false;
16
- constructor(tokenOrRefreshOptionsOrEntraOptions) {
17
- if (typeof tokenOrRefreshOptionsOrEntraOptions === "string") {
18
- this.tokenCredential = new staticTokenCredential_js_1.StaticTokenCredential((0, tokenParser_js_1.parseToken)(tokenOrRefreshOptionsOrEntraOptions));
19
- }
20
- else if ("tokenRefresher" in tokenOrRefreshOptionsOrEntraOptions) {
21
- this.tokenCredential = new autoRefreshTokenCredential_js_1.AutoRefreshTokenCredential(tokenOrRefreshOptionsOrEntraOptions);
22
- }
23
- else {
24
- this.tokenCredential = new entraTokenCredential_js_1.EntraTokenCredential(tokenOrRefreshOptionsOrEntraOptions);
25
- }
28
+ tokenCredential;
29
+ disposed = false;
30
+ constructor(tokenOrRefreshOptionsOrEntraOptions) {
31
+ if (typeof tokenOrRefreshOptionsOrEntraOptions === "string" || (0, import_tokenParser.isAccessToken)(tokenOrRefreshOptionsOrEntraOptions)) {
32
+ this.tokenCredential = new import_staticTokenCredential.StaticTokenCredential(
33
+ (0, import_tokenParser.parseToken)(tokenOrRefreshOptionsOrEntraOptions)
34
+ );
35
+ } else if ("tokenRefresher" in tokenOrRefreshOptionsOrEntraOptions) {
36
+ this.tokenCredential = new import_autoRefreshTokenCredential.AutoRefreshTokenCredential(tokenOrRefreshOptionsOrEntraOptions);
37
+ } else {
38
+ this.tokenCredential = new import_entraTokenCredential.EntraTokenCredential(tokenOrRefreshOptionsOrEntraOptions);
26
39
  }
27
- /**
28
- * Gets an `AccessToken` for the user. Throws if already disposed.
29
- * @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.
30
- */
31
- async getToken(options) {
32
- this.throwIfDisposed();
33
- const token = await this.tokenCredential.getToken(options);
34
- this.throwIfDisposed();
35
- return token;
36
- }
37
- /**
38
- * Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.
39
- */
40
- dispose() {
41
- this.disposed = true;
42
- this.tokenCredential.dispose();
43
- }
44
- throwIfDisposed() {
45
- if (this.disposed) {
46
- throw new Error("User credential is disposed");
47
- }
40
+ }
41
+ /**
42
+ * Gets an `AccessToken` for the user. Throws if already disposed.
43
+ * @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.
44
+ */
45
+ async getToken(options) {
46
+ this.throwIfDisposed();
47
+ const token = await this.tokenCredential.getToken(options);
48
+ this.throwIfDisposed();
49
+ return token;
50
+ }
51
+ /**
52
+ * Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.
53
+ */
54
+ dispose() {
55
+ this.disposed = true;
56
+ this.tokenCredential.dispose();
57
+ }
58
+ throwIfDisposed() {
59
+ if (this.disposed) {
60
+ throw new Error("User credential is disposed");
48
61
  }
62
+ }
49
63
  }
50
- exports.AzureCommunicationTokenCredential = AzureCommunicationTokenCredential;
51
- //# sourceMappingURL=azureCommunicationTokenCredential.js.map
64
+ // Annotate the CommonJS export names for ESM import in node:
65
+ 0 && (module.exports = {
66
+ AzureCommunicationTokenCredential
67
+ });
68
+ //# sourceMappingURL=azureCommunicationTokenCredential.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"azureCommunicationTokenCredential.js","sourceRoot":"","sources":["../../src/azureCommunicationTokenCredential.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAElC,mFAGyC;AAOzC,yEAAmE;AACnE,qDAA8C;AAC9C,uEAGmC;AAEnC;;GAEG;AAEH,MAAa,iCAAiC;IAC3B,eAAe,CAAkB;IAC1C,QAAQ,GAAG,KAAK,CAAC;IAkBzB,YACE,mCAG4C;QAE5C,IAAI,OAAO,mCAAmC,KAAK,QAAQ,EAAE,CAAC;YAC5D,IAAI,CAAC,eAAe,GAAG,IAAI,gDAAqB,CAC9C,IAAA,2BAAU,EAAC,mCAAmC,CAAC,CAChD,CAAC;QACJ,CAAC;aAAM,IAAI,gBAAgB,IAAI,mCAAmC,EAAE,CAAC;YACnE,IAAI,CAAC,eAAe,GAAG,IAAI,0DAA0B,CAAC,mCAAmC,CAAC,CAAC;QAC7F,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,eAAe,GAAG,IAAI,8CAAoB,CAAC,mCAAmC,CAAC,CAAC;QACvF,CAAC;IACH,CAAC;IAED;;;OAGG;IACI,KAAK,CAAC,QAAQ,CAAC,OAAsC;QAC1D,IAAI,CAAC,eAAe,EAAE,CAAC;QACvB,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC3D,IAAI,CAAC,eAAe,EAAE,CAAC;QACvB,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;OAEG;IACI,OAAO;QACZ,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC;QACrB,IAAI,CAAC,eAAe,CAAC,OAAO,EAAE,CAAC;IACjC,CAAC;IAEO,eAAe;QACrB,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAClB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;IACH,CAAC;CACF;AA7DD,8EA6DC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport {\n AutoRefreshTokenCredential,\n type CommunicationTokenRefreshOptions,\n} from \"./autoRefreshTokenCredential.js\";\nimport type {\n CommunicationGetTokenOptions,\n CommunicationTokenCredential,\n TokenCredential,\n} from \"./communicationTokenCredential.js\";\nimport type { AccessToken } from \"@azure/core-auth\";\nimport { StaticTokenCredential } from \"./staticTokenCredential.js\";\nimport { parseToken } from \"./tokenParser.js\";\nimport {\n type EntraCommunicationTokenCredentialOptions,\n EntraTokenCredential,\n} from \"./entraTokenCredential.js\";\n\n/**\n * The CommunicationTokenCredential implementation with support for proactive token refresh.\n */\n\nexport class AzureCommunicationTokenCredential implements CommunicationTokenCredential {\n private readonly tokenCredential: TokenCredential;\n private disposed = false;\n\n /**\n * Creates an instance of CommunicationTokenCredential with a static token and no proactive refreshing.\n * @param token - A user access token issued by Communication Services.\n */\n constructor(token: string);\n /**\n * Creates an instance of CommunicationTokenCredential with a lambda to get a token and options\n * to configure proactive refreshing.\n * @param refreshOptions - Options to configure refresh and opt-in to proactive refreshing.\n */\n constructor(refreshOptions: CommunicationTokenRefreshOptions);\n /**\n * Creates an instance of CommunicationTokenCredential with an Entra ID token credential. In most cases, you might want to use InteractiveBrowserCredential to sign in your user.\n * @param entraOptions - Options to configure the Entra ID token credential.\n */\n constructor(entraOptions: EntraCommunicationTokenCredentialOptions);\n constructor(\n tokenOrRefreshOptionsOrEntraOptions:\n | string\n | CommunicationTokenRefreshOptions\n | EntraCommunicationTokenCredentialOptions,\n ) {\n if (typeof tokenOrRefreshOptionsOrEntraOptions === \"string\") {\n this.tokenCredential = new StaticTokenCredential(\n parseToken(tokenOrRefreshOptionsOrEntraOptions),\n );\n } else if (\"tokenRefresher\" in tokenOrRefreshOptionsOrEntraOptions) {\n this.tokenCredential = new AutoRefreshTokenCredential(tokenOrRefreshOptionsOrEntraOptions);\n } else {\n this.tokenCredential = new EntraTokenCredential(tokenOrRefreshOptionsOrEntraOptions);\n }\n }\n\n /**\n * Gets an `AccessToken` for the user. Throws if already disposed.\n * @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.\n */\n public async getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken> {\n this.throwIfDisposed();\n const token = await this.tokenCredential.getToken(options);\n this.throwIfDisposed();\n return token;\n }\n\n /**\n * Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.\n */\n public dispose(): void {\n this.disposed = true;\n this.tokenCredential.dispose();\n }\n\n private throwIfDisposed(): void {\n if (this.disposed) {\n throw new Error(\"User credential is disposed\");\n }\n }\n}\n"]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/azureCommunicationTokenCredential.ts"],
4
+ "sourcesContent": ["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport {\n AutoRefreshTokenCredential,\n type CommunicationTokenRefreshOptions,\n} from \"./autoRefreshTokenCredential.js\";\nimport type {\n CommunicationGetTokenOptions,\n CommunicationTokenCredential,\n TokenCredential,\n} from \"./communicationTokenCredential.js\";\nimport type { AccessToken } from \"@azure/core-auth\";\nimport { StaticTokenCredential } from \"./staticTokenCredential.js\";\nimport { isAccessToken, parseToken } from \"./tokenParser.js\";\nimport {\n type EntraCommunicationTokenCredentialOptions,\n EntraTokenCredential,\n} from \"./entraTokenCredential.js\";\n\n/**\n * The CommunicationTokenCredential implementation with support for proactive token refresh.\n */\n\nexport class AzureCommunicationTokenCredential implements CommunicationTokenCredential {\n private readonly tokenCredential: TokenCredential;\n private disposed = false;\n\n /**\n * Creates an instance of CommunicationTokenCredential with a static token and no proactive refreshing.\n * @param token - A user access token issued by Communication Services. Pass an `AccessToken`\n * to supply the expiry explicitly when the token cannot be decoded (e.g. an encrypted token).\n */\n constructor(token: string | AccessToken);\n /**\n * Creates an instance of CommunicationTokenCredential with a lambda to get a token and options\n * to configure proactive refreshing.\n * @param refreshOptions - Options to configure refresh and opt-in to proactive refreshing.\n */\n constructor(refreshOptions: CommunicationTokenRefreshOptions);\n /**\n * Creates an instance of CommunicationTokenCredential with an Entra ID token credential. In most cases, you might want to use InteractiveBrowserCredential to sign in your user.\n * @param entraOptions - Options to configure the Entra ID token credential.\n */\n constructor(entraOptions: EntraCommunicationTokenCredentialOptions);\n constructor(\n tokenOrRefreshOptionsOrEntraOptions:\n | string\n | AccessToken\n | CommunicationTokenRefreshOptions\n | EntraCommunicationTokenCredentialOptions,\n ) {\n if (\n typeof tokenOrRefreshOptionsOrEntraOptions === \"string\" ||\n isAccessToken(tokenOrRefreshOptionsOrEntraOptions)\n ) {\n this.tokenCredential = new StaticTokenCredential(\n parseToken(tokenOrRefreshOptionsOrEntraOptions),\n );\n } else if (\"tokenRefresher\" in tokenOrRefreshOptionsOrEntraOptions) {\n this.tokenCredential = new AutoRefreshTokenCredential(tokenOrRefreshOptionsOrEntraOptions);\n } else {\n this.tokenCredential = new EntraTokenCredential(tokenOrRefreshOptionsOrEntraOptions);\n }\n }\n\n /**\n * Gets an `AccessToken` for the user. Throws if already disposed.\n * @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.\n */\n public async getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken> {\n this.throwIfDisposed();\n const token = await this.tokenCredential.getToken(options);\n this.throwIfDisposed();\n return token;\n }\n\n /**\n * Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.\n */\n public dispose(): void {\n this.disposed = true;\n this.tokenCredential.dispose();\n }\n\n private throwIfDisposed(): void {\n if (this.disposed) {\n throw new Error(\"User credential is disposed\");\n }\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,wCAGO;AAOP,mCAAsC;AACtC,yBAA0C;AAC1C,kCAGO;AAMA,MAAM,kCAA0E;AAAA,EACpE;AAAA,EACT,WAAW;AAAA,EAmBnB,YACE,qCAKA;AACA,QACE,OAAO,wCAAwC,gBAC/C,kCAAc,mCAAmC,GACjD;AACA,WAAK,kBAAkB,IAAI;AAAA,YACzB,+BAAW,mCAAmC;AAAA,MAChD;AAAA,IACF,WAAW,oBAAoB,qCAAqC;AAClE,WAAK,kBAAkB,IAAI,6DAA2B,mCAAmC;AAAA,IAC3F,OAAO;AACL,WAAK,kBAAkB,IAAI,iDAAqB,mCAAmC;AAAA,IACrF;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAa,SAAS,SAA8D;AAClF,SAAK,gBAAgB;AACrB,UAAM,QAAQ,MAAM,KAAK,gBAAgB,SAAS,OAAO;AACzD,SAAK,gBAAgB;AACrB,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKO,UAAgB;AACrB,SAAK,WAAW;AAChB,SAAK,gBAAgB,QAAQ;AAAA,EAC/B;AAAA,EAEQ,kBAAwB;AAC9B,QAAI,KAAK,UAAU;AACjB,YAAM,IAAI,MAAM,6BAA6B;AAAA,IAC/C;AAAA,EACF;AACF;",
6
+ "names": []
7
+ }
@@ -1,5 +1,16 @@
1
- "use strict";
2
- // Copyright (c) Microsoft Corporation.
3
- // Licensed under the MIT License.
4
- Object.defineProperty(exports, "__esModule", { value: true });
5
- //# sourceMappingURL=communicationTokenCredential.js.map
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var communicationTokenCredential_exports = {};
15
+ module.exports = __toCommonJS(communicationTokenCredential_exports);
16
+ //# sourceMappingURL=communicationTokenCredential.js.map
@@ -1 +1,7 @@
1
- {"version":3,"file":"communicationTokenCredential.js","sourceRoot":"","sources":["../../src/communicationTokenCredential.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { AbortSignalLike } from \"@azure/abort-controller\";\nimport type { AccessToken } from \"@azure/core-auth\";\n\nexport type TokenCredential = Pick<CommunicationTokenCredential, \"getToken\" | \"dispose\">;\n\n/**\n * Options for `CommunicationTokenCredential`'s `getToken` function.\n */\nexport interface CommunicationGetTokenOptions {\n /**\n * An implementation of `AbortSignalLike` to cancel the operation.\n */\n abortSignal?: AbortSignalLike;\n}\n\n/**\n * The Azure Communication Services token credential.\n */\nexport interface CommunicationTokenCredential {\n /**\n * Gets an `AccessToken` for the user. Throws if already disposed.\n * @param options - Additional options.\n */\n getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken>;\n /**\n * Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.\n */\n dispose(): void;\n}\n"]}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/communicationTokenCredential.ts"],
4
+ "sourcesContent": ["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nimport type { AbortSignalLike } from \"@azure/abort-controller\";\nimport type { AccessToken } from \"@azure/core-auth\";\n\nexport type TokenCredential = Pick<CommunicationTokenCredential, \"getToken\" | \"dispose\">;\n\n/**\n * Options for `CommunicationTokenCredential`'s `getToken` function.\n */\nexport interface CommunicationGetTokenOptions {\n /**\n * An implementation of `AbortSignalLike` to cancel the operation.\n */\n abortSignal?: AbortSignalLike;\n}\n\n/**\n * The Azure Communication Services token credential.\n */\nexport interface CommunicationTokenCredential {\n /**\n * Gets an `AccessToken` for the user. Throws if already disposed.\n * @param options - Additional options.\n */\n getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken>;\n /**\n * Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.\n */\n dispose(): void;\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;AAAA;AAAA;",
6
+ "names": []
7
+ }