@azure/web-pubsub 1.0.0-beta.2 → 1.0.1-alpha.20211215.2
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.
- package/CHANGELOG.md +40 -0
- package/README.md +118 -31
- package/dist/index.js +1315 -567
- package/dist/index.js.map +1 -1
- package/dist-esm/samples-dev/broadcasting.js +16 -16
- package/dist-esm/samples-dev/broadcasting.js.map +1 -1
- package/dist-esm/samples-dev/directMessage.js +9 -9
- package/dist-esm/samples-dev/directMessage.js.map +1 -1
- package/dist-esm/samples-dev/managingGroups.js +11 -12
- package/dist-esm/samples-dev/managingGroups.js.map +1 -1
- package/dist-esm/src/generated/generatedClient.js +23 -0
- package/dist-esm/src/generated/generatedClient.js.map +1 -0
- package/dist-esm/src/generated/generatedClientContext.js +41 -0
- package/dist-esm/src/generated/generatedClientContext.js.map +1 -0
- package/dist-esm/src/generated/index.js +10 -2
- package/dist-esm/src/generated/index.js.map +1 -1
- package/dist-esm/src/generated/models/index.js +13 -1
- package/dist-esm/src/generated/models/index.js.map +1 -1
- package/dist-esm/src/generated/models/mappers.js +368 -0
- package/dist-esm/src/generated/models/mappers.js.map +1 -0
- package/dist-esm/src/generated/models/parameters.js +124 -43
- package/dist-esm/src/generated/models/parameters.js.map +1 -1
- package/dist-esm/src/generated/operations/healthApi.js +16 -11
- package/dist-esm/src/generated/operations/healthApi.js.map +1 -1
- package/dist-esm/src/generated/operations/index.js +7 -0
- package/dist-esm/src/generated/operations/index.js.map +1 -1
- package/dist-esm/src/generated/operations/webPubSub.js +341 -175
- package/dist-esm/src/generated/operations/webPubSub.js.map +1 -1
- package/dist-esm/src/generated/operationsInterfaces/healthApi.js +9 -0
- package/dist-esm/src/generated/operationsInterfaces/healthApi.js.map +1 -0
- package/dist-esm/src/generated/operationsInterfaces/index.js +10 -0
- package/dist-esm/src/generated/operationsInterfaces/index.js.map +1 -0
- package/dist-esm/src/generated/operationsInterfaces/webPubSub.js +9 -0
- package/dist-esm/src/generated/operationsInterfaces/webPubSub.js.map +1 -0
- package/dist-esm/src/groupClient.js +74 -92
- package/dist-esm/src/groupClient.js.map +1 -1
- package/dist-esm/src/hubClient.js +280 -196
- package/dist-esm/src/hubClient.js.map +1 -1
- package/dist-esm/src/index.js.map +1 -1
- package/dist-esm/src/logger.js +1 -1
- package/dist-esm/src/logger.js.map +1 -1
- package/dist-esm/src/parseConnectionString.js +3 -1
- package/dist-esm/src/parseConnectionString.js.map +1 -1
- package/dist-esm/src/reverseProxyPolicy.js +21 -0
- package/dist-esm/src/reverseProxyPolicy.js.map +1 -0
- package/dist-esm/src/utils.js +8 -6
- package/dist-esm/src/utils.js.map +1 -1
- package/dist-esm/src/webPubSubCredentialPolicy.js +18 -18
- package/dist-esm/src/webPubSubCredentialPolicy.js.map +1 -1
- package/dist-esm/test/conn.spec.js +7 -8
- package/dist-esm/test/conn.spec.js.map +1 -1
- package/dist-esm/test/groups.spec.js +39 -38
- package/dist-esm/test/groups.spec.js.map +1 -1
- package/dist-esm/test/hubs.spec.js +95 -49
- package/dist-esm/test/hubs.spec.js.map +1 -1
- package/dist-esm/test/integration.spec.js +128 -0
- package/dist-esm/test/integration.spec.js.map +1 -0
- package/dist-esm/test/testEnv.js +5 -1
- package/dist-esm/test/testEnv.js.map +1 -1
- package/package.json +37 -36
- package/types/web-pubsub.d.ts +186 -138
- package/dist-esm/src/generated/azureWebPubSubServiceRestAPI.js +0 -15
- package/dist-esm/src/generated/azureWebPubSubServiceRestAPI.js.map +0 -1
- package/dist-esm/src/generated/azureWebPubSubServiceRestAPIContext.js +0 -34
- package/dist-esm/src/generated/azureWebPubSubServiceRestAPIContext.js.map +0 -1
- package/dist-esm/src/normalizeOptions.js +0 -8
- package/dist-esm/src/normalizeOptions.js.map +0 -1
@@ -1,31 +1,32 @@
|
|
1
1
|
// Copyright (c) Microsoft Corporation.
|
2
2
|
// Licensed under the MIT license.
|
3
|
-
import {
|
4
|
-
import {
|
5
|
-
import { AzureWebPubSubServiceRestAPI as GeneratedClient } from "./generated/azureWebPubSubServiceRestAPI";
|
3
|
+
import { RestError } from "@azure/core-rest-pipeline";
|
4
|
+
import { GeneratedClient } from "./generated/generatedClient";
|
6
5
|
import { WebPubSubGroupImpl } from "./groupClient";
|
7
|
-
import
|
8
|
-
import {
|
6
|
+
import { isTokenCredential } from "@azure/core-auth";
|
7
|
+
import { webPubSubKeyCredentialPolicy } from "./webPubSubCredentialPolicy";
|
9
8
|
import { createSpan } from "./tracing";
|
10
9
|
import { logger } from "./logger";
|
11
10
|
import { parseConnectionString } from "./parseConnectionString";
|
12
11
|
import jwt from "jsonwebtoken";
|
13
|
-
import {
|
12
|
+
import { getPayloadForMessage } from "./utils";
|
13
|
+
import { webPubSubReverseProxyPolicy } from "./reverseProxyPolicy";
|
14
14
|
/**
|
15
15
|
* Client for connecting to a Web PubSub hub
|
16
16
|
*/
|
17
17
|
export class WebPubSubServiceClient {
|
18
18
|
constructor(endpointOrConnectionString, credsOrHubName, hubNameOrOpts, opts) {
|
19
|
+
var _a, _b;
|
19
20
|
/**
|
20
21
|
* The Web PubSub API version being used by this client
|
21
22
|
*/
|
22
|
-
this.apiVersion = "
|
23
|
+
this.apiVersion = "2021-10-01";
|
23
24
|
// unpack constructor arguments
|
24
|
-
if (typeof credsOrHubName === "object"
|
25
|
+
if (typeof credsOrHubName === "object") {
|
25
26
|
this.endpoint = endpointOrConnectionString;
|
26
|
-
this.credential = credsOrHubName;
|
27
27
|
this.hubName = hubNameOrOpts;
|
28
28
|
this.clientOptions = opts;
|
29
|
+
this.credential = credsOrHubName;
|
29
30
|
}
|
30
31
|
else {
|
31
32
|
const parsedCs = parseConnectionString(endpointOrConnectionString);
|
@@ -34,254 +35,337 @@ export class WebPubSubServiceClient {
|
|
34
35
|
this.hubName = credsOrHubName;
|
35
36
|
this.clientOptions = hubNameOrOpts;
|
36
37
|
}
|
37
|
-
const internalPipelineOptions = Object.assign(Object.assign({}, this.clientOptions), {
|
38
|
+
const internalPipelineOptions = Object.assign(Object.assign(Object.assign({}, this.clientOptions), {
|
39
|
+
apiVersion: this.apiVersion,
|
38
40
|
loggingOptions: {
|
39
41
|
logger: logger.info
|
40
42
|
}
|
41
|
-
})
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
/**
|
46
|
-
* Auth the client connection with userId and custom claims if any
|
47
|
-
* @param options The options that the client has
|
48
|
-
*/
|
49
|
-
getAuthenticationToken(options) {
|
50
|
-
return __awaiter(this, void 0, void 0, function* () {
|
51
|
-
const endpoint = this.endpoint.endsWith("/") ? this.endpoint : this.endpoint + "/";
|
52
|
-
const key = this.credential.key;
|
53
|
-
const hub = this.hubName;
|
54
|
-
const clientEndpoint = endpoint.replace(/(http)(s?:\/\/)/gi, "ws$2");
|
55
|
-
const clientUrl = `${clientEndpoint}client/hubs/${hub}`;
|
56
|
-
const audience = `${endpoint}client/hubs/${hub}`;
|
57
|
-
const payload = { role: options === null || options === void 0 ? void 0 : options.roles };
|
58
|
-
const signOptions = {
|
59
|
-
audience: audience,
|
60
|
-
expiresIn: (options === null || options === void 0 ? void 0 : options.ttl) === undefined ? "1h" : `${options.ttl}m`,
|
61
|
-
algorithm: "HS256"
|
62
|
-
};
|
63
|
-
if (options === null || options === void 0 ? void 0 : options.userId) {
|
64
|
-
signOptions.subject = options === null || options === void 0 ? void 0 : options.userId;
|
43
|
+
}), (isTokenCredential(this.credential)
|
44
|
+
? {
|
45
|
+
credential: this.credential,
|
46
|
+
credentialScopes: ["https://webpubsub.azure.com/.default"]
|
65
47
|
}
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
}
|
48
|
+
: {}));
|
49
|
+
this.client = new GeneratedClient(this.endpoint, internalPipelineOptions);
|
50
|
+
if (!isTokenCredential(this.credential)) {
|
51
|
+
this.client.pipeline.addPolicy(webPubSubKeyCredentialPolicy(this.credential));
|
52
|
+
}
|
53
|
+
if ((_a = this.clientOptions) === null || _a === void 0 ? void 0 : _a.reverseProxyEndpoint) {
|
54
|
+
this.client.pipeline.addPolicy(webPubSubReverseProxyPolicy((_b = this.clientOptions) === null || _b === void 0 ? void 0 : _b.reverseProxyEndpoint));
|
55
|
+
}
|
74
56
|
}
|
75
57
|
/**
|
76
58
|
* Get a client for a group
|
77
|
-
* @param groupName The name of the group to connect to.
|
59
|
+
* @param groupName - The name of the group to connect to.
|
78
60
|
*/
|
79
61
|
group(groupName) {
|
80
62
|
return new WebPubSubGroupImpl(this.client, this.hubName, groupName);
|
81
63
|
}
|
82
|
-
sendToAll(message, options = {}) {
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
span.end();
|
92
|
-
}
|
93
|
-
});
|
64
|
+
async sendToAll(message, options = {}) {
|
65
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToAll", options);
|
66
|
+
const { contentType, payload } = getPayloadForMessage(message, updatedOptions);
|
67
|
+
try {
|
68
|
+
return await this.client.webPubSub.sendToAll(this.hubName, contentType, payload, updatedOptions);
|
69
|
+
}
|
70
|
+
finally {
|
71
|
+
span.end();
|
72
|
+
}
|
94
73
|
}
|
95
|
-
sendToUser(username, message, options = {}) {
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
}
|
105
|
-
});
|
74
|
+
async sendToUser(username, message, options = {}) {
|
75
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToUser", options);
|
76
|
+
const { contentType, payload } = getPayloadForMessage(message, updatedOptions);
|
77
|
+
try {
|
78
|
+
return await this.client.webPubSub.sendToUser(this.hubName, username, contentType, payload, updatedOptions);
|
79
|
+
}
|
80
|
+
finally {
|
81
|
+
span.end();
|
82
|
+
}
|
106
83
|
}
|
107
|
-
sendToConnection(connectionId, message, options = {}) {
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
}
|
117
|
-
});
|
84
|
+
async sendToConnection(connectionId, message, options = {}) {
|
85
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-sendToConnection", options);
|
86
|
+
const { contentType, payload } = getPayloadForMessage(message, updatedOptions);
|
87
|
+
try {
|
88
|
+
return await this.client.webPubSub.sendToConnection(this.hubName, connectionId, contentType, payload, updatedOptions);
|
89
|
+
}
|
90
|
+
finally {
|
91
|
+
span.end();
|
92
|
+
}
|
118
93
|
}
|
119
94
|
/**
|
120
95
|
* Check if a specific connection is connected to this hub
|
121
96
|
*
|
122
|
-
* @param connectionId Connection id to check
|
123
|
-
* @param options Additional options
|
97
|
+
* @param connectionId - Connection id to check
|
98
|
+
* @param options - Additional options
|
124
99
|
*/
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
throw new RestError(res._response.bodyAsText, undefined, res._response.status, res._response.request, res._response);
|
139
|
-
}
|
100
|
+
async connectionExists(connectionId, options = {}) {
|
101
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasConnection", options);
|
102
|
+
let response;
|
103
|
+
function onResponse(rawResponse, flatResponse) {
|
104
|
+
response = rawResponse;
|
105
|
+
if (updatedOptions.onResponse) {
|
106
|
+
updatedOptions.onResponse(rawResponse, flatResponse);
|
107
|
+
}
|
108
|
+
}
|
109
|
+
try {
|
110
|
+
await this.client.webPubSub.connectionExists(this.hubName, connectionId, Object.assign(Object.assign({}, updatedOptions), { onResponse }));
|
111
|
+
if (response.status === 200) {
|
112
|
+
return true;
|
140
113
|
}
|
141
|
-
|
142
|
-
|
114
|
+
else if (response.status === 404) {
|
115
|
+
return false;
|
143
116
|
}
|
144
|
-
|
117
|
+
else {
|
118
|
+
// this is sad - wish this was handled by autorest.
|
119
|
+
throw new RestError(response.bodyAsText, {
|
120
|
+
statusCode: response === null || response === void 0 ? void 0 : response.status,
|
121
|
+
request: response === null || response === void 0 ? void 0 : response.request,
|
122
|
+
response: response
|
123
|
+
});
|
124
|
+
}
|
125
|
+
}
|
126
|
+
finally {
|
127
|
+
span.end();
|
128
|
+
}
|
145
129
|
}
|
146
130
|
/**
|
147
131
|
* Close a specific connection to this hub
|
148
132
|
*
|
149
|
-
* @param connectionId Connection id to close
|
150
|
-
* @param options Additional options
|
133
|
+
* @param connectionId - Connection id to close
|
134
|
+
* @param options - Additional options
|
151
135
|
*/
|
152
|
-
closeConnection(connectionId, options = {}) {
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
136
|
+
async closeConnection(connectionId, options = {}) {
|
137
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-closeConnection", options);
|
138
|
+
try {
|
139
|
+
return await this.client.webPubSub.closeConnection(this.hubName, connectionId, updatedOptions);
|
140
|
+
}
|
141
|
+
finally {
|
142
|
+
span.end();
|
143
|
+
}
|
144
|
+
}
|
145
|
+
/**
|
146
|
+
* Close all connections to this hub
|
147
|
+
*
|
148
|
+
* @param options - Additional options
|
149
|
+
*/
|
150
|
+
async closeAllConnections(options = {}) {
|
151
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-closeAllConnections", options);
|
152
|
+
try {
|
153
|
+
return await this.client.webPubSub.closeAllConnections(this.hubName, updatedOptions);
|
154
|
+
}
|
155
|
+
finally {
|
156
|
+
span.end();
|
157
|
+
}
|
158
|
+
}
|
159
|
+
/**
|
160
|
+
* Close all connections with the given user id
|
161
|
+
*
|
162
|
+
* @param user - User id to close
|
163
|
+
* @param options - Additional options
|
164
|
+
*/
|
165
|
+
async closeUserConnections(userId, options = {}) {
|
166
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-closeUserConnections", options);
|
167
|
+
try {
|
168
|
+
return await this.client.webPubSub.closeUserConnections(this.hubName, userId, updatedOptions);
|
169
|
+
}
|
170
|
+
finally {
|
171
|
+
span.end();
|
172
|
+
}
|
162
173
|
}
|
163
174
|
/**
|
164
175
|
* Remove a specific user from all groups they are joined to
|
165
|
-
* @param userId The user id to remove from all groups
|
166
|
-
* @param options Additional options
|
176
|
+
* @param userId - The user id to remove from all groups
|
177
|
+
* @param options - Additional options
|
167
178
|
*/
|
168
|
-
removeUserFromAllGroups(userId, options = {}) {
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
}
|
177
|
-
});
|
179
|
+
async removeUserFromAllGroups(userId, options = {}) {
|
180
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-removeUserFromAllGroups", options);
|
181
|
+
try {
|
182
|
+
await this.client.webPubSub.removeUserFromAllGroups(this.hubName, userId, updatedOptions);
|
183
|
+
}
|
184
|
+
finally {
|
185
|
+
span.end();
|
186
|
+
}
|
178
187
|
}
|
179
188
|
/**
|
180
189
|
* Check if a particular group exists (i.e. has active connections).
|
181
190
|
*
|
182
|
-
* @param groupName The group name to check for
|
183
|
-
* @param options Additional options
|
191
|
+
* @param groupName - The group name to check for
|
192
|
+
* @param options - Additional options
|
184
193
|
*/
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
194
|
+
async groupExists(groupName, options = {}) {
|
195
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasGroup", options);
|
196
|
+
let response;
|
197
|
+
function onResponse(rawResponse, flatResponse) {
|
198
|
+
response = rawResponse;
|
199
|
+
if (updatedOptions.onResponse) {
|
200
|
+
updatedOptions.onResponse(rawResponse, flatResponse);
|
201
|
+
}
|
202
|
+
}
|
203
|
+
try {
|
204
|
+
await this.client.webPubSub.groupExists(this.hubName, groupName, Object.assign(Object.assign({}, updatedOptions), { onResponse }));
|
205
|
+
if (response.status === 200) {
|
206
|
+
return true;
|
207
|
+
}
|
208
|
+
else if (response.status === 404) {
|
209
|
+
return false;
|
199
210
|
}
|
200
|
-
|
201
|
-
|
211
|
+
else {
|
212
|
+
throw new RestError(response.bodyAsText, {
|
213
|
+
statusCode: response === null || response === void 0 ? void 0 : response.status,
|
214
|
+
request: response === null || response === void 0 ? void 0 : response.request,
|
215
|
+
response: response
|
216
|
+
});
|
202
217
|
}
|
203
|
-
}
|
218
|
+
}
|
219
|
+
finally {
|
220
|
+
span.end();
|
221
|
+
}
|
204
222
|
}
|
205
223
|
/**
|
206
224
|
* Check if a particular user is connected to this hub.
|
207
225
|
*
|
208
|
-
* @param username The user name to check for
|
209
|
-
* @param options Additional options
|
226
|
+
* @param username - The user name to check for
|
227
|
+
* @param options - Additional options
|
210
228
|
*/
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
}
|
219
|
-
else if (res._response.status === 404) {
|
220
|
-
return false;
|
221
|
-
}
|
222
|
-
else {
|
223
|
-
// this is sad - wish this was handled by autorest.
|
224
|
-
throw new RestError(res._response.bodyAsText, undefined, res._response.status, res._response.request, res._response);
|
225
|
-
}
|
229
|
+
async userExists(username, options = {}) {
|
230
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasUser", options);
|
231
|
+
let response;
|
232
|
+
function onResponse(rawResponse, flatResponse) {
|
233
|
+
response = rawResponse;
|
234
|
+
if (updatedOptions.onResponse) {
|
235
|
+
updatedOptions.onResponse(rawResponse, flatResponse);
|
226
236
|
}
|
227
|
-
|
228
|
-
|
237
|
+
}
|
238
|
+
try {
|
239
|
+
await this.client.webPubSub.userExists(this.hubName, username, Object.assign(Object.assign({}, updatedOptions), { onResponse }));
|
240
|
+
if (response.status === 200) {
|
241
|
+
return true;
|
229
242
|
}
|
230
|
-
|
243
|
+
else if (response.status === 404) {
|
244
|
+
return false;
|
245
|
+
}
|
246
|
+
else {
|
247
|
+
// this is sad - wish this was handled by autorest.
|
248
|
+
throw new RestError(response.bodyAsText, {
|
249
|
+
statusCode: response === null || response === void 0 ? void 0 : response.status,
|
250
|
+
request: response === null || response === void 0 ? void 0 : response.request,
|
251
|
+
response: response
|
252
|
+
});
|
253
|
+
}
|
254
|
+
}
|
255
|
+
finally {
|
256
|
+
span.end();
|
257
|
+
}
|
231
258
|
}
|
232
259
|
/**
|
233
260
|
* Grant permissions to a connection
|
234
261
|
*
|
235
|
-
* @param connectionId The connection id to grant permissions to
|
236
|
-
* @param Permission The permission to grant
|
237
|
-
* @param options Additional options
|
262
|
+
* @param connectionId - The connection id to grant permissions to
|
263
|
+
* @param Permission - The permission to grant
|
264
|
+
* @param options - Additional options
|
238
265
|
*/
|
239
|
-
grantPermission(connectionId, permission, options = {}) {
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
}
|
248
|
-
});
|
266
|
+
async grantPermission(connectionId, permission, options = {}) {
|
267
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-grantPermission", options);
|
268
|
+
try {
|
269
|
+
return await this.client.webPubSub.grantPermission(this.hubName, permission, connectionId, updatedOptions);
|
270
|
+
}
|
271
|
+
finally {
|
272
|
+
span.end();
|
273
|
+
}
|
249
274
|
}
|
250
275
|
/**
|
251
276
|
* Revoke permissions from a connection
|
252
277
|
*
|
253
|
-
* @param connectionId The connection id to revoke permissions from
|
254
|
-
* @param Permission The permission to revoke
|
255
|
-
* @param options Additional options
|
278
|
+
* @param connectionId - The connection id to revoke permissions from
|
279
|
+
* @param Permission - The permission to revoke
|
280
|
+
* @param options - Additional options
|
281
|
+
*/
|
282
|
+
async revokePermission(connectionId, permission, options = {}) {
|
283
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-revokePermission", options);
|
284
|
+
try {
|
285
|
+
return await this.client.webPubSub.revokePermission(this.hubName, permission, connectionId, updatedOptions);
|
286
|
+
}
|
287
|
+
finally {
|
288
|
+
span.end();
|
289
|
+
}
|
290
|
+
}
|
291
|
+
/**
|
292
|
+
* Check if the connection has the specified permission
|
293
|
+
*
|
294
|
+
* @param connectionId - The connection id to check permission
|
295
|
+
* @param Permission - The permission to check
|
296
|
+
* @param options - Additional options
|
256
297
|
*/
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
298
|
+
async hasPermission(connectionId, permission, options = {}) {
|
299
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-hasPermission", options);
|
300
|
+
let response;
|
301
|
+
function onResponse(rawResponse, flatResponse) {
|
302
|
+
response = rawResponse;
|
303
|
+
if (updatedOptions.onResponse) {
|
304
|
+
updatedOptions.onResponse(rawResponse, flatResponse);
|
305
|
+
}
|
306
|
+
}
|
307
|
+
try {
|
308
|
+
await this.client.webPubSub.checkPermission(this.hubName, permission, connectionId, Object.assign(Object.assign({}, updatedOptions), { onResponse }));
|
309
|
+
if (response.status === 200) {
|
310
|
+
return true;
|
262
311
|
}
|
263
|
-
|
264
|
-
|
312
|
+
else if (response.status === 404) {
|
313
|
+
return false;
|
265
314
|
}
|
266
|
-
|
315
|
+
else {
|
316
|
+
// this is sad - wish this was handled by autorest.
|
317
|
+
throw new RestError(response.bodyAsText, {
|
318
|
+
statusCode: response === null || response === void 0 ? void 0 : response.status,
|
319
|
+
request: response === null || response === void 0 ? void 0 : response.request,
|
320
|
+
response: response
|
321
|
+
});
|
322
|
+
}
|
323
|
+
}
|
324
|
+
finally {
|
325
|
+
span.end();
|
326
|
+
}
|
267
327
|
}
|
268
328
|
/**
|
269
|
-
*
|
329
|
+
* Generate a token for a client to connect to the Azure Web PubSub service.
|
270
330
|
*
|
271
|
-
* @param
|
272
|
-
* @param Permission The permission to check
|
273
|
-
* @param options Additional options
|
331
|
+
* @param options - Additional options
|
274
332
|
*/
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
333
|
+
async getClientAccessToken(options = {}) {
|
334
|
+
const { span, updatedOptions } = createSpan("WebPubSubServiceClient-hub-generateClientToken", options);
|
335
|
+
try {
|
336
|
+
const endpoint = this.endpoint.endsWith("/") ? this.endpoint : this.endpoint + "/";
|
337
|
+
const clientEndpoint = endpoint.replace(/(http)(s?:\/\/)/gi, "ws$2");
|
338
|
+
const baseUrl = `${clientEndpoint}client/hubs/${this.hubName}`;
|
339
|
+
let token;
|
340
|
+
if (isTokenCredential(this.credential)) {
|
341
|
+
const response = await this.client.webPubSub.generateClientToken(this.hubName, updatedOptions);
|
342
|
+
token = response.token;
|
280
343
|
}
|
281
|
-
|
282
|
-
|
344
|
+
else {
|
345
|
+
const key = this.credential.key;
|
346
|
+
const audience = `${endpoint}client/hubs/${this.hubName}`;
|
347
|
+
const payload = { role: options === null || options === void 0 ? void 0 : options.roles };
|
348
|
+
const signOptions = {
|
349
|
+
audience: audience,
|
350
|
+
expiresIn: (options === null || options === void 0 ? void 0 : options.expirationTimeInMinutes) === undefined
|
351
|
+
? "1h"
|
352
|
+
: `${options.expirationTimeInMinutes}m`,
|
353
|
+
algorithm: "HS256"
|
354
|
+
};
|
355
|
+
if (options === null || options === void 0 ? void 0 : options.userId) {
|
356
|
+
signOptions.subject = options === null || options === void 0 ? void 0 : options.userId;
|
357
|
+
}
|
358
|
+
token = jwt.sign(payload, key, signOptions);
|
283
359
|
}
|
284
|
-
|
360
|
+
return {
|
361
|
+
token,
|
362
|
+
baseUrl,
|
363
|
+
url: `${baseUrl}?access_token=${token}`
|
364
|
+
};
|
365
|
+
}
|
366
|
+
finally {
|
367
|
+
span.end();
|
368
|
+
}
|
285
369
|
}
|
286
370
|
}
|
287
371
|
//# sourceMappingURL=hubClient.js.map
|