Skip to content

Commit 45ae69c

Browse files
committed
Revert "fix(deps): remove validator (after refactor) (#384)"
This reverts commit b837fa8.
1 parent d5ad53c commit 45ae69c

File tree

4 files changed

+48
-67
lines changed

4 files changed

+48
-67
lines changed

index.ts

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import validator from "validator";
12
import { fetch as undiciFetch, EnvHttpProxyAgent } from "undici";
23
import {
34
EventSource,
45
type FetchLike,
56
type EventSourceFetchInit,
67
type ErrorEvent,
78
} from "eventsource";
9+
import url from "node:url";
10+
import querystring from "node:querystring";
811

912
type Severity = "info" | "error";
1013

@@ -20,26 +23,6 @@ interface Options {
2023

2124
const proxyAgent = new EnvHttpProxyAgent();
2225

23-
const trimTrailingSlash = (url: string): string => {
24-
return url.lastIndexOf("/") === url.length - 1 ? url.slice(0, -1) : url;
25-
};
26-
27-
function validateURL(urlString: string): asserts urlString is string {
28-
if (URL.canParse(urlString) === false) {
29-
throw new Error(`The provided URL is invalid.`);
30-
}
31-
32-
const url = new URL(urlString);
33-
34-
if (!url.protocol || !["http:", "https:"].includes(url.protocol)) {
35-
throw new Error(`The provided URL is invalid.`);
36-
}
37-
38-
if (!url.host) {
39-
throw new Error(`The provided URL is invalid.`);
40-
}
41-
}
42-
4326
class SmeeClient {
4427
#source: string;
4528
#target: string;
@@ -67,13 +50,11 @@ class SmeeClient {
6750

6851
const data = JSON.parse(msg.data);
6952

70-
const target = new URL(this.#target);
53+
const target = url.parse(this.#target, true);
7154

72-
if (this.#queryForwarding && data.query) {
73-
Object.keys(data.query).forEach((key) => {
74-
target.searchParams.set(key, data.query[key]);
75-
});
76-
target.search = target.searchParams.toString();
55+
if (this.#queryForwarding) {
56+
const mergedQuery = { ...target.query, ...data.query };
57+
target.search = querystring.stringify(mergedQuery);
7758
}
7859

7960
delete data.query;
@@ -95,7 +76,7 @@ class SmeeClient {
9576
headers["content-type"] = "application/json";
9677

9778
try {
98-
const response = await this.#fetch(target, {
79+
const response = await this.#fetch(url.format(target), {
9980
method: "POST",
10081
mode: data["sec-fetch-mode"],
10182
body,
@@ -121,16 +102,21 @@ class SmeeClient {
121102
queryForwarding = true,
122103
forward,
123104
}: Options) {
124-
validateURL(target);
125-
validateURL(source);
126-
127-
this.#source = trimTrailingSlash(new URL(source).toString());
128-
this.#target = trimTrailingSlash(new URL(target).toString());
105+
this.#source = source;
106+
this.#target = target;
129107
this.#logger = logger!;
130108
this.#fetch = fetch;
131109
this.#queryForwarding = queryForwarding;
132110
this.#maxConnectionTimeout = maxConnectionTimeout;
133111
this.#forward = forward;
112+
113+
if (
114+
!validator.isURL(this.#source, {
115+
require_tld: false,
116+
})
117+
) {
118+
throw new Error("The provided URL is invalid.");
119+
}
134120
}
135121

136122
static async createChannel({

package-lock.json

Lines changed: 19 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
"license": "ISC",
2929
"dependencies": {
3030
"eventsource": "^4.0.0",
31-
"undici": "^7.0.0"
31+
"undici": "^7.0.0",
32+
"validator": "^13.11.0"
3233
},
3334
"devDependencies": {
3435
"@octokit/tsconfig": "^4.0.0",
3536
"@types/node": "^24.0.0",
37+
"@types/validator": "^13.11.6",
3638
"@vitest/coverage-v8": "^4.0.0",
3739
"get-port": "^7.1.0",
3840
"prettier": "^3.1.0",

test/index.test.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,13 @@ describe("client", () => {
3232
describe("constructor", () => {
3333
describe("source", () => {
3434
test("throws if source is not a valid URL", () => {
35-
[
36-
"mailto:do-not-reply@example.com",
37-
"ftp://www.google.com/",
38-
"123455",
39-
].forEach((source) => {
40-
expect(
41-
() =>
42-
new Client({
43-
source,
44-
target: "https://example.com",
45-
}),
46-
source,
47-
).toThrow("The provided URL is invalid.");
48-
});
49-
[
50-
"https:/smee.io/CHANNEL",
51-
"http://www.example.com:80",
52-
"https://www.example.com:443",
53-
].forEach((source) => {
54-
expect(
55-
() =>
56-
new Client({
57-
source,
58-
target: "https://example.com",
59-
}),
60-
source,
61-
).not.toThrow("The provided URL is invalid.");
62-
});
35+
expect(
36+
() =>
37+
new Client({
38+
source: "mailto:do-not-reply@example.com",
39+
target: "https://example.com",
40+
}),
41+
).toThrow("The provided URL is invalid.");
6342
});
6443
});
6544
});
@@ -122,7 +101,7 @@ describe("client", () => {
122101

123102
await client.start();
124103

125-
await fetch(target, {
104+
await fetch(target + "/", {
126105
method: "POST",
127106
body: JSON.stringify({ hello: "world" }),
128107
headers: {

0 commit comments

Comments
 (0)