Skip to content

Commit

Permalink
[web-pubsub] Migrate @azure/web-pubsub to new core pipeline (Azure#…
Browse files Browse the repository at this point in the history
…16010)

Update to latest core packages and improve pipeline robustness.
  • Loading branch information
xirzec authored Jun 28, 2021
1 parent 13772c2 commit 21f28dc
Show file tree
Hide file tree
Showing 44 changed files with 1,124 additions and 794 deletions.
6 changes: 6 additions & 0 deletions dataplane.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@
{
"name": "iot-device-update",
"path": "sdk/deviceupdate/iot-device-update"
},
{
"path": "sdk/web-pubsub/web-pubsub"
},
{
"path": "sdk/web-pubsub/web-pubsub-express"
}
],
"settings": {
Expand Down
13 changes: 12 additions & 1 deletion sdk/core/core-rest-pipeline/src/nodeHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ class NodeHttpClient implements HttpClient {
request
};

// Responses to HEAD must not have a body.
// If they do return a body, that body must be ignored.
if (request.method === "HEAD") {
resolve(response);
return;
}

responseStream = shouldDecompress ? getDecodedResponseStream(res, headers) : res;

const onDownloadProgress = request.onDownloadProgress;
Expand All @@ -142,7 +149,11 @@ class NodeHttpClient implements HttpClient {
if (request.streamResponseStatusCodes?.has(response.status)) {
response.readableStreamBody = responseStream;
} else {
response.bodyAsText = await streamToText(responseStream);
try {
response.bodyAsText = await streamToText(responseStream);
} catch (e) {
reject(e);
}
}

resolve(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export function decompressResponsePolicy(): PipelinePolicy {
return {
name: decompressResponsePolicyName,
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
request.headers.set("Accept-Encoding", "gzip,deflate");
// HEAD requests have no body
if (request.method !== "HEAD") {
request.headers.set("Accept-Encoding", "gzip,deflate");
}
return next(request);
}
};
Expand Down
4 changes: 4 additions & 0 deletions sdk/web-pubsub/web-pubsub-express/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release History

## 1.0.0-beta.2 (UNRELEASED)

- Removed unnecessary dependencies.

## 1.0.0-beta.1 (2021-04-23)

This is the first release of the @azure/web-pubsub-express package.
8 changes: 1 addition & 7 deletions sdk/web-pubsub/web-pubsub-express/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/web-pubsub-express",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.2",
"description": "Azure Web PubSub CloudEvents handlers",
"sdk-type": "client",
"main": "dist/index.js",
Expand Down Expand Up @@ -57,18 +57,12 @@
"homepage": "https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/search/search/",
"sideEffects": false,
"dependencies": {
"@azure/core-auth": "^1.3.0",
"@azure/core-http": "^2.0.0",
"@azure/core-tracing": "1.0.0-preview.12",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0",
"jsonwebtoken": "^8.5.1",
"cloudevents": "^4.0.0"
},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^1.1.0",
"@azure/test-utils-recorder": "^1.0.0",
"@microsoft/api-extractor": "7.7.11",
"@rollup/plugin-commonjs": "11.0.2",
Expand Down
5 changes: 5 additions & 0 deletions sdk/web-pubsub/web-pubsub/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

## 1.0.0-beta.3 (Unreleased)

### Breaking Changes

- `hasUser` has been removed from `GroupClient` as that operation is no longer supported by the service.
- Updated to have void returns for most operations. If you were previously using `RestResponse`, you can instead use the `onResponse` callback in the operation options. See README for an example.

## 1.0.0-beta.2 (2021-05-19)

Remove "url" dependency

## 1.0.0-beta.1 (2021-04-23)
Expand Down
12 changes: 12 additions & 0 deletions sdk/web-pubsub/web-pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ const payload = new Uint8Array(10);
await serviceClient.sendToAll(payload.buffer);
```

### Access the raw HTTP response for an operation

```js
const { WebPubSubServiceClient } = require("@azure/web-pubsub");

function onResponse(rawResponse: FullOperationResponse): void {
console.log(rawResponse);
}
const serviceClient = new WebPubSubServiceClient("<ConnectionString>", "<hubName>");
await serviceClient.sendToAll({ message: "Hello world!" }, { onResponse });
```

## Troubleshooting

### Enable logs
Expand Down
7 changes: 4 additions & 3 deletions sdk/web-pubsub/web-pubsub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"test:node": "npm run build:test && npm run unit-test:node && npm run integration-test:node",
"test": "npm run build:test && npm run unit-test && npm run integration-test",
"unit-test:browser": "echo \"Browser is not supported.\" && exit 0",
"unit-test:node": "mocha --reporter ../../../common/tools/mocha-multi-reporter.js dist-test/index.node.js",
"unit-test:node": "mocha --exit --reporter ../../../common/tools/mocha-multi-reporter.js dist-test/index.node.js",
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --stripInternal --mode file --out ./dist/docs ./src"
},
Expand Down Expand Up @@ -61,7 +61,8 @@
"sideEffects": false,
"dependencies": {
"@azure/core-auth": "^1.3.0",
"@azure/core-http": "^2.0.0",
"@azure/core-client": "^1.0.0",
"@azure/core-rest-pipeline": "^1.1.0",
"@azure/core-tracing": "1.0.0-preview.12",
"@azure/logger": "^1.0.0",
"tslib": "^2.2.0",
Expand All @@ -78,12 +79,12 @@
"@rollup/plugin-multi-entry": "^3.0.0",
"@rollup/plugin-node-resolve": "^8.0.0",
"@rollup/plugin-replace": "^2.2.0",
"@types/chai": "^4.1.6",
"@types/jsonwebtoken": "~8.5.0",
"@types/mocha": "^7.0.2",
"@types/node": "^8.0.0",
"@types/query-string": "6.2.0",
"@types/sinon": "^9.0.4",
"assert": "^1.4.1",
"chai": "^4.2.0",
"cross-env": "^7.0.2",
"dotenv": "^8.2.0",
Expand Down
54 changes: 26 additions & 28 deletions sdk/web-pubsub/web-pubsub/review/web-pubsub.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
```ts

import { AzureKeyCredential } from '@azure/core-auth';
import { HttpRequestBody } from '@azure/core-http';
import { OperationOptions } from '@azure/core-http';
import { PipelineOptions } from '@azure/core-http';
import { RestResponse } from '@azure/core-http';
import { CommonClientOptions } from '@azure/core-client';
import { OperationOptions } from '@azure/core-client';
import { RequestBodyType } from '@azure/core-rest-pipeline';

export { AzureKeyCredential }

Expand Down Expand Up @@ -40,7 +39,7 @@ export interface GroupAddUserOptions extends OperationOptions {
}

// @public
export interface GroupAdminClientOptions extends PipelineOptions {
export interface GroupAdminClientOptions extends CommonClientOptions {
}

// @public
Expand Down Expand Up @@ -72,7 +71,7 @@ export interface HasConnectionOptions extends OperationOptions {
}

// @public
export interface HubAdminClientOptions extends PipelineOptions {
export interface HubAdminClientOptions extends CommonClientOptions {
}

// @public
Expand Down Expand Up @@ -141,46 +140,45 @@ export type Permission = "joinLeaveGroup" | "sendToGroup";

// @public (undocumented)
export interface WebPubSubGroup {
addConnection(connectionId: string, options?: GroupAddConnectionOptions): Promise<RestResponse>;
addUser(username: string, options?: GroupAddUserOptions): Promise<RestResponse>;
addConnection(connectionId: string, options?: GroupAddConnectionOptions): Promise<void>;
addUser(username: string, options?: GroupAddUserOptions): Promise<void>;
readonly apiVersion: string;
readonly endpoint: string;
readonly groupName: string;
hasUser(username: string, options?: GroupHasUserOptions): Promise<boolean>;
readonly hubName: string;
removeConnection(connectionId: string, options?: GroupRemoveConnectionOptions): Promise<RestResponse>;
removeUser(username: string, options?: GroupRemoveUserOptions): Promise<RestResponse>;
sendToAll(message: string, options: GroupSendTextToAllOptions): Promise<RestResponse>;
sendToAll(message: JSONTypes, options?: GroupSendToAllOptions): Promise<RestResponse>;
sendToAll(message: HttpRequestBody, options?: GroupSendToAllOptions): Promise<RestResponse>;
removeConnection(connectionId: string, options?: GroupRemoveConnectionOptions): Promise<void>;
removeUser(username: string, options?: GroupRemoveUserOptions): Promise<void>;
sendToAll(message: string, options: GroupSendTextToAllOptions): Promise<void>;
sendToAll(message: JSONTypes, options?: GroupSendToAllOptions): Promise<void>;
sendToAll(message: RequestBodyType, options?: GroupSendToAllOptions): Promise<void>;
}

// @public
export class WebPubSubServiceClient {
constructor(connectionString: string, hubName: string, options?: HubAdminClientOptions);
constructor(endpoint: string, credential: AzureKeyCredential, hubName: string, options?: HubAdminClientOptions);
readonly apiVersion: string;
closeConnection(connectionId: string, options?: CloseConnectionOptions): Promise<RestResponse>;
closeConnection(connectionId: string, options?: CloseConnectionOptions): Promise<void>;
endpoint: string;
getAuthenticationToken(options?: GetAuthenticationTokenOptions): Promise<GetAuthenticationTokenResponse>;
grantPermission(connectionId: string, permission: Permission, options?: HubGrantPermissionOptions): Promise<RestResponse>;
grantPermission(connectionId: string, permission: Permission, options?: HubGrantPermissionOptions): Promise<void>;
group(groupName: string): WebPubSubGroup;
hasConnection(connectionId: string, options?: HasConnectionOptions): Promise<boolean>;
hasGroup(groupName: string, options?: HubHasGroupOptions): Promise<boolean>;
hasPermission(connectionId: string, permission: Permission, options?: HubHasPermissionOptions): Promise<RestResponse>;
hasPermission(connectionId: string, permission: Permission, options?: HubHasPermissionOptions): Promise<boolean>;
hasUser(username: string, options?: HubHasUserOptions): Promise<boolean>;
readonly hubName: string;
removeUserFromAllGroups(userId: string, options?: CloseConnectionOptions): Promise<RestResponse>;
revokePermission(connectionId: string, permission: Permission, options?: HubRevokePermissionOptions): Promise<RestResponse>;
sendToAll(message: string, options: HubSendTextToAllOptions): Promise<RestResponse>;
sendToAll(message: JSONTypes, options?: HubSendToAllOptions): Promise<RestResponse>;
sendToAll(message: HttpRequestBody, options?: HubSendToAllOptions): Promise<RestResponse>;
sendToConnection(connectionId: string, message: string, options: HubSendTextToConnectionOptions): Promise<RestResponse>;
sendToConnection(connectionId: string, message: JSONTypes, options?: HubSendToConnectionOptions): Promise<RestResponse>;
sendToConnection(connectionId: string, message: HttpRequestBody | JSONTypes, options?: HubSendToConnectionOptions | HubSendTextToConnectionOptions): Promise<RestResponse>;
sendToUser(username: string, message: string, options: HubSendTextToUserOptions): Promise<RestResponse>;
sendToUser(username: string, message: JSONTypes, options?: HubSendToUserOptions): Promise<RestResponse>;
sendToUser(username: string, message: HttpRequestBody, options?: HubSendToUserOptions | HubSendTextToUserOptions): Promise<RestResponse>;
removeUserFromAllGroups(userId: string, options?: CloseConnectionOptions): Promise<void>;
revokePermission(connectionId: string, permission: Permission, options?: HubRevokePermissionOptions): Promise<void>;
sendToAll(message: string, options: HubSendTextToAllOptions): Promise<void>;
sendToAll(message: JSONTypes, options?: HubSendToAllOptions): Promise<void>;
sendToAll(message: RequestBodyType, options?: HubSendToAllOptions): Promise<void>;
sendToConnection(connectionId: string, message: string, options: HubSendTextToConnectionOptions): Promise<void>;
sendToConnection(connectionId: string, message: JSONTypes, options?: HubSendToConnectionOptions): Promise<void>;
sendToConnection(connectionId: string, message: RequestBodyType, options?: HubSendToConnectionOptions | HubSendTextToConnectionOptions): Promise<void>;
sendToUser(username: string, message: string, options: HubSendTextToUserOptions): Promise<void>;
sendToUser(username: string, message: JSONTypes, options?: HubSendToUserOptions): Promise<void>;
sendToUser(username: string, message: RequestBodyType, options?: HubSendToUserOptions | HubSendTextToUserOptions): Promise<void>;
}


Expand Down
5 changes: 4 additions & 1 deletion sdk/web-pubsub/web-pubsub/samples-dev/broadcasting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ async function main() {
chatHub.sendToAll(data.buffer);
}

main();
main().catch((e) => {
console.error("Sample encountered an error", e);
process.exit(1);
});
5 changes: 4 additions & 1 deletion sdk/web-pubsub/web-pubsub/samples-dev/directMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ async function main() {
await chatHub.sendToUser("Tn3XcrAbHI0OE36XvbWwige4ac096c1", "Hi there!");
}

main();
main().catch((e) => {
console.error("Sample encountered an error", e);
process.exit(1);
});
6 changes: 4 additions & 2 deletions sdk/web-pubsub/web-pubsub/samples-dev/managingGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const adminGroup = chatHub.group("admin");
async function main() {
// adding and removing users
await adminGroup.addUser("bterlson");
await adminGroup.hasUser("bterlson"); // true
await adminGroup.removeUser("xirzec");

// adding and removing specific connections
await adminGroup.addConnection("Tn3XcrAbHI0OE36XvbWwige4ac096c1");
await adminGroup.removeConnection("Tn3XcrAbHI0OE36XvbWwige4ac096c1");
}

main();
main().catch((e) => {
console.error("Sample encountered an error", e);
process.exit(1);
});
11 changes: 8 additions & 3 deletions sdk/web-pubsub/web-pubsub/samples/v1/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,13 @@ Alternatively, run a single sample with the correct environment variables set (s
npx cross-env WPS_CONNECTION_STRING="<wps connection string>" node broadcasting.js
```

[broadcasting]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub/samples/v1/javascript/broadcasting.js
[directmessage]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub/samples/v1/javascript/directMessage.js
[managinggroups]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub/samples/v1/javascript/managingGroups.js
## Next Steps

Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.

[broadcasting]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/web-pubsub/web-pubsub/samples/v1/javascript/broadcasting.js
[directmessage]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/web-pubsub/web-pubsub/samples/v1/javascript/directMessage.js
[managinggroups]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/web-pubsub/web-pubsub/samples/v1/javascript/managingGroups.js
[apiref]: https://docs.microsoft.com/javascript/api/@azure/web-pubsub
[freesub]: https://azure.microsoft.com/free/
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/web-pubsub/web-pubsub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ async function main() {
chatHub.sendToAll(data.buffer);
}

main();
main().catch((e) => {
console.error("Sample encountered an error", e);
process.exit(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ async function main() {
await chatHub.sendToUser("Tn3XcrAbHI0OE36XvbWwige4ac096c1", "Hi there!");
}

main();
main().catch((e) => {
console.error("Sample encountered an error", e);
process.exit(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const adminGroup = chatHub.group("admin");
async function main() {
// adding and removing users
await adminGroup.addUser("bterlson");
await adminGroup.hasUser("bterlson"); // true
await adminGroup.removeUser("xirzec");

// adding and removing specific connections
await adminGroup.addConnection("Tn3XcrAbHI0OE36XvbWwige4ac096c1");
await adminGroup.removeConnection("Tn3XcrAbHI0OE36XvbWwige4ac096c1");
}

main();
main().catch((e) => {
console.error("Sample encountered an error", e);
process.exit(1);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"version": "1.0.0",
"description": "Azure Web PubSub client library samples for JavaScript",
"engine": {
"engines": {
"node": ">=12.0.0"
},
"repository": {
Expand Down
13 changes: 9 additions & 4 deletions sdk/web-pubsub/web-pubsub/samples/v1/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ npm run build
4. Run whichever samples you like (note that some samples may require additional setup, see the table above):

```bash
node dist/broadcasting.ts
node dist/broadcasting.js
```

Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform):
Expand All @@ -63,9 +63,14 @@ Alternatively, run a single sample with the correct environment variables set (s
npx cross-env WPS_CONNECTION_STRING="<wps connection string>" node dist/broadcasting.js
```

[broadcasting]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub/samples/v1/typescript/src/broadcasting.ts
[directmessage]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub/samples/v1/typescript/src/directMessage.ts
[managinggroups]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/web-pubsub/web-pubsub/samples/v1/typescript/src/managingGroups.ts
## Next Steps

Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.

[broadcasting]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/web-pubsub/web-pubsub/samples/v1/typescript/src/broadcasting.ts
[directmessage]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/web-pubsub/web-pubsub/samples/v1/typescript/src/directMessage.ts
[managinggroups]: https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/web-pubsub/web-pubsub/samples/v1/typescript/src/managingGroups.ts
[apiref]: https://docs.microsoft.com/javascript/api/@azure/web-pubsub
[freesub]: https://azure.microsoft.com/free/
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/web-pubsub/web-pubsub/README.md
[typescript]: https://www.typescriptlang.org/docs/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"version": "1.0.0",
"description": "Azure Web PubSub client library samples for TypeScript",
"engine": {
"engines": {
"node": ">=12.0.0"
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ async function main() {
chatHub.sendToAll(data.buffer);
}

main();
main().catch((e) => {
console.error("Sample encountered an error", e);
process.exit(1);
});
Loading

0 comments on commit 21f28dc

Please sign in to comment.