From 91e4215e427bb992376cce93537693bc0cc238b1 Mon Sep 17 00:00:00 2001 From: chradek Date: Thu, 17 Dec 2020 15:04:05 -0800 Subject: [PATCH] fix customEndpointAddress parsing in browsers --- sdk/eventhub/event-hubs/package.json | 3 +- .../src/eventhubConnectionConfig.ts | 4 +- .../event-hubs/src/util/parseEndpoint.ts | 20 ++++++++++ .../event-hubs/src/util/url.browser.ts | 8 ---- sdk/eventhub/event-hubs/src/util/url.ts | 4 -- .../test/impl/parseEndpoint.spec.ts | 40 +++++++++++++++++++ 6 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 sdk/eventhub/event-hubs/src/util/parseEndpoint.ts delete mode 100644 sdk/eventhub/event-hubs/src/util/url.browser.ts delete mode 100644 sdk/eventhub/event-hubs/src/util/url.ts create mode 100644 sdk/eventhub/event-hubs/test/impl/parseEndpoint.spec.ts diff --git a/sdk/eventhub/event-hubs/package.json b/sdk/eventhub/event-hubs/package.json index f441764b8054..6cd3cb7a515e 100644 --- a/sdk/eventhub/event-hubs/package.json +++ b/sdk/eventhub/event-hubs/package.json @@ -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" diff --git a/sdk/eventhub/event-hubs/src/eventhubConnectionConfig.ts b/sdk/eventhub/event-hubs/src/eventhubConnectionConfig.ts index fd777d1a327c..c88cf472f81c 100644 --- a/sdk/eventhub/event-hubs/src/eventhubConnectionConfig.ts +++ b/sdk/eventhub/event-hubs/src/eventhubConnectionConfig.ts @@ -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 @@ -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) { diff --git a/sdk/eventhub/event-hubs/src/util/parseEndpoint.ts b/sdk/eventhub/event-hubs/src/util/parseEndpoint.ts new file mode 100644 index 000000000000..a396df342ce7 --- /dev/null +++ b/sdk/eventhub/event-hubs/src/util/parseEndpoint.ts @@ -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 }; +} diff --git a/sdk/eventhub/event-hubs/src/util/url.browser.ts b/sdk/eventhub/event-hubs/src/util/url.browser.ts deleted file mode 100644 index 71dd9f759cf6..000000000000 --- a/sdk/eventhub/event-hubs/src/util/url.browser.ts +++ /dev/null @@ -1,8 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -/// - -const url = URL; - -export { url as URL }; diff --git a/sdk/eventhub/event-hubs/src/util/url.ts b/sdk/eventhub/event-hubs/src/util/url.ts deleted file mode 100644 index 71fcc22b49c0..000000000000 --- a/sdk/eventhub/event-hubs/src/util/url.ts +++ /dev/null @@ -1,4 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT license. - -export { URL } from "url"; diff --git a/sdk/eventhub/event-hubs/test/impl/parseEndpoint.spec.ts b/sdk/eventhub/event-hubs/test/impl/parseEndpoint.spec.ts new file mode 100644 index 000000000000..5ea1f31f6add --- /dev/null +++ b/sdk/eventhub/event-hubs/test/impl/parseEndpoint.spec.ts @@ -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); + + parseEndpoint("https://127.0.0.1:5671").should.eql({ + host: "127.0.0.1:5671", + hostname: "127.0.0.1", + port: "5671" + } as ReturnType); + + 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); + + 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); + }); +});