/
connectionStringUtils.ts
96 lines (91 loc) · 3.75 KB
/
connectionStringUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { parseConnectionString } from "@azure/core-amqp";
/**
* The set of properties that comprise a Service Bus connection string.
*/
export interface ServiceBusConnectionStringProperties {
/**
* The fully qualified Service Bus namespace extracted from the "Endpoint" in the
* connection string. This is likely to be similar to "{yournamespace}.servicebus.windows.net".
* This is typically used to construct the ServiceBusClient.
*/
fullyQualifiedNamespace: string;
/**
* The value for "Endpoint" in the connection string.
*/
endpoint: string;
/**
* The value for "EntityPath" in the connection string which would be the name of the queue or
* topic associated with the connection string.
* Connection string from a Shared Access Policy created at the namespace level
* will not have the EntityPath in it.
*/
entityPath?: string;
/**
* The value for "SharedAccessKey" in the connection string. This along with the "SharedAccessKeyName"
* in the connection string is used to generate a SharedAccessSignature which can be used authorize
* the connection to the service.
*/
sharedAccessKey?: string;
/**
* The value for "SharedAccessKeyName" in the connection string. This along with the "SharedAccessKey"
* in the connection string is used to generate a SharedAccessSignature which can be used authorize
* the connection to the service.
*/
sharedAccessKeyName?: string;
/**
* The value for "SharedAccessSignature" in the connection string. This is typically not present in the
* connection string generated for a Shared Access Policy. It is instead generated by the
* user and appended to the connection string for ease of use.
*/
sharedAccessSignature?: string;
}
/**
* Parses given connection string into the different properties applicable to Azure Service Bus.
* The properties are useful to then construct a ServiceBusClient.
* @param connectionString The connection string associated with the Shared Access Policy created
* for the Service Bus namespace, queue or topic.
*/
export function parseServiceBusConnectionString(
connectionString: string
): ServiceBusConnectionStringProperties {
const parsedResult = parseConnectionString<{
Endpoint: string;
EntityPath?: string;
SharedAccessSignature?: string;
SharedAccessKey?: string;
SharedAccessKeyName?: string;
}>(connectionString);
if (!parsedResult.Endpoint) {
throw new Error("Connection string should have an Endpoint key.");
}
if (parsedResult.SharedAccessSignature) {
if (parsedResult.SharedAccessKey || parsedResult.SharedAccessKeyName) {
throw new Error(
"Connection string cannot have both SharedAccessSignature and SharedAccessKey keys."
);
}
} else if (parsedResult.SharedAccessKey && !parsedResult.SharedAccessKeyName) {
throw new Error("Connection string with SharedAccessKey should have SharedAccessKeyName.");
} else if (!parsedResult.SharedAccessKey && parsedResult.SharedAccessKeyName) {
throw new Error(
"Connection string with SharedAccessKeyName should have SharedAccessKey as well."
);
}
const output: ServiceBusConnectionStringProperties = {
fullyQualifiedNamespace: (parsedResult.Endpoint.match(".*://([^/]*)") || [])[1],
endpoint: parsedResult.Endpoint
};
if (parsedResult.EntityPath) {
output.entityPath = parsedResult.EntityPath;
}
if (parsedResult.SharedAccessSignature) {
output.sharedAccessSignature = parsedResult.SharedAccessSignature;
}
if (parsedResult.SharedAccessKey && parsedResult.SharedAccessKeyName) {
output.sharedAccessKey = parsedResult.SharedAccessKey;
output.sharedAccessKeyName = parsedResult.SharedAccessKeyName;
}
return output;
}