Skip to content

Commit a595cb2

Browse files
authored
@azure/communication-common Azure Core 2.0 Migration (Azure#20337)
* migrated clientArguments, removed circular deps * removed the dependency on @azure/core-http * policy converted to core rest pipeline * removed the old policy, bumped major version * added a changelog entry * added test * updated pnpm file
1 parent c2ae230 commit a595cb2

23 files changed

+471
-443
lines changed

common/config/rush/pnpm-lock.yaml

Lines changed: 216 additions & 210 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/communication/communication-common/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Release History
22

3-
## 1.2.0 (Unreleased)
3+
## 2.0.0 (Unreleased)
44

55
### Features Added
66

77
- Optimization added: When the proactive refreshing is enabled and the token refresher fails to provide a token that's not about to expire soon, the subsequent refresh attempts will be scheduled for when the token reaches half of its remaining lifetime until a token with long enough validity (>10 minutes) is obtained.
88

99
### Breaking Changes
1010

11+
- Migrated from using `@azure/core-http` to `@azure/core-rest-pipeline` for the handling of HTTP requests. See [Azure Core v1 vs v2](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-rest-pipeline/documentation/core2.md) for more on the difference and benefits of the move.
12+
- `createCommunicationAccessKeyCredentialPolicy` and `createCommunicationAuthPolicy` newly return `PipelinePolicy` instead of `RequestPolicyFactory`.
13+
1114
### Bugs Fixed
1215

1316
### Other Changes

sdk/communication/communication-common/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "@azure/communication-common",
3-
"version": "1.2.0",
3+
"version": "2.0.0",
44
"description": "Common package for Azure Communication services.",
55
"sdk-type": "client",
66
"main": "dist/index.js",
77
"module": "dist-esm/src/index.js",
88
"types": "types/communication-common.d.ts",
99
"browser": {
10-
"./dist-esm/src/credential/cryptoUtils.js": "./dist-esm/src/credential/cryptoUtils.browser.js"
10+
"./dist-esm/src/credential/cryptoUtils.js": "./dist-esm/src/credential/cryptoUtils.browser.js",
11+
"./dist-esm/src/credential/isNode.js": "./dist-esm/src/credential/isNode.browser.js"
1112
},
1213
"scripts": {
1314
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
@@ -63,7 +64,7 @@
6364
"dependencies": {
6465
"@azure/abort-controller": "^1.0.0",
6566
"@azure/core-auth": "^1.3.0",
66-
"@azure/core-http": "^2.0.0",
67+
"@azure/core-rest-pipeline": "^1.3.2",
6768
"@azure/core-tracing": "1.0.0-preview.13",
6869
"events": "^3.0.0",
6970
"jwt-decode": "^3.1.2",

sdk/communication/communication-common/review/communication-common.api.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
55
```ts
66

7-
import { AbortSignalLike } from '@azure/core-http';
8-
import { AccessToken } from '@azure/core-http';
7+
import { AbortSignalLike } from '@azure/abort-controller';
8+
import { AccessToken } from '@azure/core-auth';
99
import { KeyCredential } from '@azure/core-auth';
10-
import { RequestPolicyFactory } from '@azure/core-http';
10+
import { PipelinePolicy } from '@azure/core-rest-pipeline';
1111
import { TokenCredential } from '@azure/core-auth';
1212

1313
// @public
@@ -16,7 +16,7 @@ export class AzureCommunicationTokenCredential implements CommunicationTokenCred
1616
constructor(refreshOptions: CommunicationTokenRefreshOptions);
1717
dispose(): void;
1818
getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken>;
19-
}
19+
}
2020

2121
// @public
2222
export interface CommunicationGetTokenOptions {
@@ -53,10 +53,10 @@ export interface CommunicationUserKind extends CommunicationUserIdentifier {
5353
}
5454

5555
// @public
56-
export const createCommunicationAccessKeyCredentialPolicy: (credential: KeyCredential) => RequestPolicyFactory;
56+
export function createCommunicationAccessKeyCredentialPolicy(credential: KeyCredential): PipelinePolicy;
5757

5858
// @public
59-
export const createCommunicationAuthPolicy: (credential: KeyCredential | TokenCredential) => RequestPolicyFactory;
59+
export function createCommunicationAuthPolicy(credential: KeyCredential | TokenCredential): PipelinePolicy;
6060

6161
// @public
6262
export const deserializeCommunicationIdentifier: (serializedIdentifier: SerializedCommunicationIdentifier) => CommunicationIdentifierKind;
@@ -162,7 +162,6 @@ export type UrlWithCredential = {
162162
credential: TokenCredential | KeyCredential;
163163
};
164164

165-
166165
// (No @packageDocumentation comment for this package)
167166

168167
```

sdk/communication/communication-common/src/autoRefreshTokenCredential.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { AbortSignalLike, AccessToken } from "@azure/core-http";
4+
import { CommunicationGetTokenOptions, TokenCredential } from "./communicationTokenCredential";
5+
import { AbortSignalLike } from "@azure/abort-controller";
6+
import { AccessToken } from "@azure/core-auth";
57
import { parseToken } from "./tokenParser";
6-
import { TokenCredential, CommunicationGetTokenOptions } from "./communicationTokenCredential";
78

89
/**
910
* Options for auto-refreshing a Communication Token credential.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import {
5+
AutoRefreshTokenCredential,
6+
CommunicationTokenRefreshOptions,
7+
} from "./autoRefreshTokenCredential";
8+
import {
9+
CommunicationGetTokenOptions,
10+
CommunicationTokenCredential,
11+
TokenCredential,
12+
} from "./communicationTokenCredential";
13+
import { AccessToken } from "@azure/core-auth";
14+
import { StaticTokenCredential } from "./staticTokenCredential";
15+
import { parseToken } from "./tokenParser";
16+
17+
/**
18+
* The CommunicationTokenCredential implementation with support for proactive token refresh.
19+
*/
20+
21+
export class AzureCommunicationTokenCredential implements CommunicationTokenCredential {
22+
private readonly tokenCredential: TokenCredential;
23+
private disposed = false;
24+
25+
/**
26+
* Creates an instance of CommunicationTokenCredential with a static token and no proactive refreshing.
27+
* @param token - A user access token issued by Communication Services.
28+
*/
29+
constructor(token: string);
30+
/**
31+
* Creates an instance of CommunicationTokenCredential with a lambda to get a token and options
32+
* to configure proactive refreshing.
33+
* @param refreshOptions - Options to configure refresh and opt-in to proactive refreshing.
34+
*/
35+
constructor(refreshOptions: CommunicationTokenRefreshOptions);
36+
constructor(tokenOrRefreshOptions: string | CommunicationTokenRefreshOptions) {
37+
if (typeof tokenOrRefreshOptions === "string") {
38+
this.tokenCredential = new StaticTokenCredential(parseToken(tokenOrRefreshOptions));
39+
} else {
40+
this.tokenCredential = new AutoRefreshTokenCredential(tokenOrRefreshOptions);
41+
}
42+
}
43+
44+
/**
45+
* Gets an `AccessToken` for the user. Throws if already disposed.
46+
* @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.
47+
*/
48+
public async getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken> {
49+
this.throwIfDisposed();
50+
const token = await this.tokenCredential.getToken(options);
51+
this.throwIfDisposed();
52+
return token;
53+
}
54+
55+
/**
56+
* Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.
57+
*/
58+
public dispose(): void {
59+
this.disposed = true;
60+
this.tokenCredential.dispose();
61+
}
62+
63+
private throwIfDisposed(): void {
64+
if (this.disposed) {
65+
throw new Error("User credential is disposed");
66+
}
67+
}
68+
}
Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { AbortSignalLike, AccessToken } from "@azure/core-http";
5-
import { parseToken } from "./tokenParser";
6-
import { StaticTokenCredential } from "./staticTokenCredential";
7-
import {
8-
AutoRefreshTokenCredential,
9-
CommunicationTokenRefreshOptions,
10-
} from "./autoRefreshTokenCredential";
4+
import { AbortSignalLike } from "@azure/abort-controller";
5+
import { AccessToken } from "@azure/core-auth";
116

12-
export type TokenCredential = Pick<AzureCommunicationTokenCredential, "getToken" | "dispose">;
7+
export type TokenCredential = Pick<CommunicationTokenCredential, "getToken" | "dispose">;
138

149
/**
1510
* Options for `CommunicationTokenCredential`'s `getToken` function.
@@ -35,55 +30,3 @@ export interface CommunicationTokenCredential {
3530
*/
3631
dispose(): void;
3732
}
38-
39-
/**
40-
* The CommunicationTokenCredential implementation with support for proactive token refresh.
41-
*/
42-
export class AzureCommunicationTokenCredential implements CommunicationTokenCredential {
43-
private readonly tokenCredential: TokenCredential;
44-
private disposed = false;
45-
46-
/**
47-
* Creates an instance of CommunicationTokenCredential with a static token and no proactive refreshing.
48-
* @param token - A user access token issued by Communication Services.
49-
*/
50-
constructor(token: string);
51-
/**
52-
* Creates an instance of CommunicationTokenCredential with a lambda to get a token and options
53-
* to configure proactive refreshing.
54-
* @param refreshOptions - Options to configure refresh and opt-in to proactive refreshing.
55-
*/
56-
constructor(refreshOptions: CommunicationTokenRefreshOptions);
57-
constructor(tokenOrRefreshOptions: string | CommunicationTokenRefreshOptions) {
58-
if (typeof tokenOrRefreshOptions === "string") {
59-
this.tokenCredential = new StaticTokenCredential(parseToken(tokenOrRefreshOptions));
60-
} else {
61-
this.tokenCredential = new AutoRefreshTokenCredential(tokenOrRefreshOptions);
62-
}
63-
}
64-
65-
/**
66-
* Gets an `AccessToken` for the user. Throws if already disposed.
67-
* @param abortSignal - An implementation of `AbortSignalLike` to cancel the operation.
68-
*/
69-
public async getToken(options?: CommunicationGetTokenOptions): Promise<AccessToken> {
70-
this.throwIfDisposed();
71-
const token = await this.tokenCredential.getToken(options);
72-
this.throwIfDisposed();
73-
return token;
74-
}
75-
76-
/**
77-
* Disposes the CommunicationTokenCredential and cancels any internal auto-refresh operation.
78-
*/
79-
public dispose(): void {
80-
this.disposed = true;
81-
this.tokenCredential.dispose();
82-
}
83-
84-
private throwIfDisposed(): void {
85-
if (this.disposed) {
86-
throw new Error("User credential is disposed");
87-
}
88-
}
89-
}

sdk/communication/communication-common/src/credential/clientArguments.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT license.
33

4-
import { isTokenCredential, KeyCredential, TokenCredential } from "@azure/core-auth";
5-
import { URLBuilder } from "@azure/core-http";
4+
import { KeyCredential, TokenCredential, isTokenCredential } from "@azure/core-auth";
65
import { parseConnectionString } from "./connectionString";
76

87
const isValidEndpoint = (host: string): boolean => {
9-
const url = URLBuilder.parse(host);
8+
const url = new URL(host);
109

1110
return (
12-
!!url.getScheme()?.match(/^http[s]?/) &&
13-
url.getHost() !== undefined &&
14-
url.getHost() !== "" &&
15-
(url.getPath() === undefined || url.getPath() === "" || url.getPath() === "/")
11+
!!url.protocol?.match(/^http[s]?/) &&
12+
url.host !== undefined &&
13+
url.host !== "" &&
14+
(url.pathname === undefined || url.pathname === "" || url.pathname === "/")
1615
);
1716
};
1817

0 commit comments

Comments
 (0)