Skip to content

Commit

Permalink
fix customEndpointAddress parsing in browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
chradek committed Dec 17, 2020
1 parent 06f1d8c commit 91e4215
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
3 changes: 1 addition & 2 deletions sdk/eventhub/event-hubs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
}
},
"browser": {
"./dist-esm/src/util/runtimeInfo.js": "./dist-esm/src/util/runtimeInfo.browser.js",
"./dist-esm/src/util/url.js": "./dist-esm/src/util/url.browser.js"
"./dist-esm/src/util/runtimeInfo.js": "./dist-esm/src/util/runtimeInfo.browser.js"
},
"engine": {
"node": ">=8.0.0"
Expand Down
4 changes: 2 additions & 2 deletions sdk/eventhub/event-hubs/src/eventhubConnectionConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* eslint-disable eqeqeq */

import { ConnectionConfig } from "@azure/core-amqp";
import { URL } from "./util/url";
import { parseEndpoint } from "./util/parseEndpoint";

/**
* Describes the connection config object that is created after parsing an EventHub connection
Expand Down Expand Up @@ -153,7 +153,7 @@ export const EventHubConnectionConfig = {
setCustomEndpointAddress(config: EventHubConnectionConfig, customEndpointAddress: string): void {
// The amqpHostname should match the host prior to using the custom endpoint.
config.amqpHostname = config.host;
const { hostname, port } = new URL(customEndpointAddress);
const { hostname, port } = parseEndpoint(customEndpointAddress);
// Since we specify the port separately, set host to the customEndpointAddress hostname.
config.host = hostname;
if (port) {
Expand Down
20 changes: 20 additions & 0 deletions sdk/eventhub/event-hubs/src/util/parseEndpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

/**
* Parses the host, hostname, and port from an endpoint.
* @param endpoint And endpoint to parse.
* @hidden
* @internal
*/
export function parseEndpoint(endpoint: string): { host: string; hostname: string; port?: string } {
const hostMatch = endpoint.match(/.*:\/\/([^\/]*)/i);
if (!hostMatch) {
throw new TypeError(`Invalid endpoint missing host: ${endpoint}`);
}

const [, host] = hostMatch;
const [hostname, port] = host.split(":");

return { host, hostname, port };
}
8 changes: 0 additions & 8 deletions sdk/eventhub/event-hubs/src/util/url.browser.ts

This file was deleted.

4 changes: 0 additions & 4 deletions sdk/eventhub/event-hubs/src/util/url.ts

This file was deleted.

40 changes: 40 additions & 0 deletions sdk/eventhub/event-hubs/test/impl/parseEndpoint.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { parseEndpoint } from "../../src/util/parseEndpoint";
import chai from "chai";
const should = chai.should();

describe("parseEndpoint", () => {
it("throws an error for invalid inputs", () => {
should.throw(() => parseEndpoint(""), /Invalid endpoint/);
should.throw(() => parseEndpoint("missing-protocol"), /Invalid endpoint/);
should.throw(() => parseEndpoint("//missing-protocol"), /Invalid endpoint/);
});

it("extracts host, hostname, and port", () => {
parseEndpoint("sb://test.servicebus.windows.net:5671").should.eql({
host: "test.servicebus.windows.net:5671",
hostname: "test.servicebus.windows.net",
port: "5671"
} as ReturnType<typeof parseEndpoint>);

parseEndpoint("https://127.0.0.1:5671").should.eql({
host: "127.0.0.1:5671",
hostname: "127.0.0.1",
port: "5671"
} as ReturnType<typeof parseEndpoint>);

parseEndpoint("amqps://127.0.0.1:5671/path/?query=foo").should.eql({
host: "127.0.0.1:5671",
hostname: "127.0.0.1",
port: "5671"
} as ReturnType<typeof parseEndpoint>);

parseEndpoint("wss://127.0.0.1/path/?query=foo").should.eql({
host: "127.0.0.1",
hostname: "127.0.0.1",
port: undefined
} as ReturnType<typeof parseEndpoint>);
});
});

0 comments on commit 91e4215

Please sign in to comment.