Skip to content

Commit c1db92f

Browse files
authored
fix: hang on redirecting url scheme (#182)
* fix: hang on redirecting url scheme * add test
1 parent c203983 commit c1db92f

File tree

2 files changed

+25
-2
lines changed
  • src/utils/http-client/get-modules
  • tests/src/utils/http-client

2 files changed

+25
-2
lines changed

src/utils/http-client/get-modules/http.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ function get0(
3939
res.statusCode < 400 &&
4040
redirectCount < 3 // max redirect
4141
) {
42-
const redirectUrl = res.headers.location!;
43-
resolve(get0(redirectUrl, options, redirectCount + 1));
42+
const location = res.headers.location!;
43+
try {
44+
const redirectUrl = new URL(location, url).toString();
45+
resolve(get0(redirectUrl, options, redirectCount + 1));
46+
} catch (e) {
47+
reject(e);
48+
}
4449
return;
4550
}
4651
resolve(result);

tests/src/utils/http-client/http.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,22 @@ describe("HTTP GET.", () => {
2222
"eslint-plugin-json-schema-validator"
2323
);
2424
});
25+
it("Should to receive a request with a redirect.", async () => {
26+
const res = await get(
27+
"https://unpkg.com/eslint-plugin-json-schema-validator/package.json"
28+
);
29+
assert.deepStrictEqual(
30+
JSON.parse(res).name,
31+
"eslint-plugin-json-schema-validator"
32+
);
33+
});
34+
it("Should to receive a request with a redirect (2).", async () => {
35+
const res = await get(
36+
"https://raw.github.com/ota-meshi/eslint-plugin-json-schema-validator/main/package.json"
37+
);
38+
assert.deepStrictEqual(
39+
JSON.parse(res).name,
40+
"eslint-plugin-json-schema-validator"
41+
);
42+
});
2543
});

0 commit comments

Comments
 (0)