Skip to content

Commit

Permalink
Fix tsp init hanging when done due to unclosed connection (#3185)
Browse files Browse the repository at this point in the history
fix [#3102](#3102)

Problem was that when following redirect we need to explicitly close the
response watch otherwise we always await on it

However I switched to using `follow-redirects` library that will more
safely handle redirect for us.(e.g. max redirects)
With node 22 the fetch api should not be experimental anymore so we
could potentially migrate to it next year when node 20 is out of support
  • Loading branch information
timotheeguerin authored Apr 23, 2024
1 parent 7517c91 commit 36c339b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/fix-init-never-ending-2024-3-17-20-24-14.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix tsp init hanging when done due to unclosed connection
2 changes: 2 additions & 0 deletions packages/compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"@babel/code-frame": "~7.24.2",
"ajv": "~8.12.0",
"change-case": "~5.4.4",
"follow-redirects": "^1.15.6",
"globby": "~14.0.1",
"mustache": "~4.2.0",
"picocolors": "~1.0.0",
Expand All @@ -98,6 +99,7 @@
},
"devDependencies": {
"@types/babel__code-frame": "~7.0.6",
"@types/follow-redirects": "^1.14.4",
"@types/mustache": "~4.2.5",
"@types/node": "~18.11.19",
"@types/prompts": "~2.4.9",
Expand Down
12 changes: 6 additions & 6 deletions packages/compiler/src/core/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import http from "http";
import https from "https";
import followRedirects from "follow-redirects";
import { compilerAssert } from "./diagnostics.js";

export interface FetchResponse {
Expand All @@ -19,14 +18,15 @@ export function fetch(uri: string): Promise<FetchResponse> {
function request(uri: string, resolve: (arg: FetchResponse) => void, reject: (arg: any) => void) {
const url = new URL(uri);
const protocol =
url.protocol === "https:" ? https : url.protocol === "http:" ? http : undefined;
url.protocol === "https:"
? followRedirects.https
: url.protocol === "http:"
? followRedirects.http
: undefined;
compilerAssert(protocol, `Protocol '${url.protocol}' is not supported`);

protocol
.get(url, (res) => {
if ((res.statusCode === 301 || res.statusCode === 302) && res.headers.location) {
return request(res.headers.location, resolve, reject);
}
let data = "";

res.on("data", (chunk) => {
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 36c339b

Please sign in to comment.