Skip to content

Commit 7e7feb1

Browse files
authored
chore(url-parser-node): use WHATWG URL instead of url.parse (#1766)
1 parent 21646e3 commit 7e7feb1

File tree

2 files changed

+55
-38
lines changed

2 files changed

+55
-38
lines changed

packages/url-parser-node/src/index.spec.ts

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,61 @@ import { Endpoint } from "@aws-sdk/types";
33
import { parseUrl } from "./";
44

55
describe("parseUrl", () => {
6-
const testCases = new Map<string, Endpoint>([
7-
[
8-
"https://www.example.com/path/to%20the/file.ext?snap=cr%C3%A4ckle&snap=p%C3%B4p&fizz=buzz&quux",
9-
{
10-
protocol: "https:",
11-
hostname: "www.example.com",
12-
path: "/path/to%20the/file.ext",
13-
query: {
14-
snap: ["cräckle", "pôp"],
15-
fizz: "buzz",
16-
quux: null,
6+
describe("should correctly parse string", () => {
7+
const successCases: [string, Endpoint][] = [
8+
[
9+
"https://www.example.com/path/to%20the/file.ext?snap=cr%C3%A4ckle&snap=p%C3%B4p&fizz=buzz&quux",
10+
{
11+
protocol: "https:",
12+
hostname: "www.example.com",
13+
path: "/path/to%20the/file.ext",
14+
query: {
15+
snap: ["cräckle", "pôp"],
16+
fizz: "buzz",
17+
quux: null,
18+
},
1719
},
18-
},
19-
],
20-
[
21-
"http://example.com:54321",
22-
{
23-
protocol: "http:",
24-
hostname: "example.com",
25-
port: 54321,
26-
path: "/",
27-
},
28-
],
29-
[
30-
"https://example.com?foo=bar",
31-
{
32-
protocol: "https:",
33-
hostname: "example.com",
34-
path: "/",
35-
query: { foo: "bar" },
36-
},
37-
],
38-
]);
20+
],
21+
[
22+
"http://example.com:54321",
23+
{
24+
protocol: "http:",
25+
hostname: "example.com",
26+
port: 54321,
27+
path: "/",
28+
},
29+
],
30+
[
31+
"https://example.com?foo=bar",
32+
{
33+
protocol: "https:",
34+
hostname: "example.com",
35+
path: "/",
36+
query: { foo: "bar" },
37+
},
38+
],
39+
];
40+
41+
successCases.forEach(([url, parsed]) => {
42+
it(url, () => {
43+
expect(parseUrl(url)).toEqual(parsed);
44+
});
45+
});
46+
});
47+
48+
describe("should throw error", () => {
49+
const failureCases: [string, Error][] = [
50+
["example.com", new TypeError("Invalid URL: example.com")],
51+
["endpoint", new TypeError("Invalid URL: endpoint")],
52+
];
3953

40-
for (const [url, parsed] of testCases) {
41-
it(`should correctly parse ${url}`, () => {
42-
expect(parseUrl(url)).toEqual(parsed);
54+
failureCases.forEach(([url, error]) => {
55+
it(url, () => {
56+
expect(() => {
57+
const output = parseUrl(url);
58+
console.log(output);
59+
}).toThrow(error);
60+
});
4361
});
44-
}
62+
});
4563
});

packages/url-parser-node/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { parseQueryString } from "@aws-sdk/querystring-parser";
22
import { Endpoint, QueryParameterBag, UrlParser } from "@aws-sdk/types";
3-
import { parse } from "url";
43

54
export const parseUrl: UrlParser = (url: string): Endpoint => {
6-
const { hostname = "localhost", pathname = "/", port, protocol = "https:", search } = parse(url);
5+
const { hostname = "localhost", pathname = "/", port, protocol = "https:", search } = new URL(url);
76

87
let query: QueryParameterBag | undefined;
98
if (search) {

0 commit comments

Comments
 (0)