diff --git a/.chronus/changes/fix-init-never-ending-2024-3-17-20-24-14.md b/.chronus/changes/fix-init-never-ending-2024-3-17-20-24-14.md new file mode 100644 index 00000000000..649ce5c4c7e --- /dev/null +++ b/.chronus/changes/fix-init-never-ending-2024-3-17-20-24-14.md @@ -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 diff --git a/packages/compiler/package.json b/packages/compiler/package.json index bfd13fa8060..267ea986bcc 100644 --- a/packages/compiler/package.json +++ b/packages/compiler/package.json @@ -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", @@ -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", diff --git a/packages/compiler/src/core/fetch.ts b/packages/compiler/src/core/fetch.ts index da358e51f9b..dbf26a80158 100644 --- a/packages/compiler/src/core/fetch.ts +++ b/packages/compiler/src/core/fetch.ts @@ -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 { @@ -19,14 +18,15 @@ export function fetch(uri: string): Promise { 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) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b8754fb6df9..0df60a92c6f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -225,6 +225,9 @@ importers: change-case: specifier: ~5.4.4 version: 5.4.4 + follow-redirects: + specifier: ^1.15.6 + version: 1.15.6 globby: specifier: ~14.0.1 version: 14.0.1 @@ -259,6 +262,9 @@ importers: '@types/babel__code-frame': specifier: ~7.0.6 version: 7.0.6 + '@types/follow-redirects': + specifier: ^1.14.4 + version: 1.14.4 '@types/mustache': specifier: ~4.2.5 version: 4.2.5 @@ -7716,6 +7722,12 @@ packages: '@types/serve-static': 1.15.5 dev: false + /@types/follow-redirects@1.14.4: + resolution: {integrity: sha512-GWXfsD0Jc1RWiFmMuMFCpXMzi9L7oPDVwxUnZdg89kDNnqsRfUKXEtUYtA98A6lig1WXH/CYY/fvPW9HuN5fTA==} + dependencies: + '@types/node': 18.11.19 + dev: true + /@types/gtag.js@0.0.12: resolution: {integrity: sha512-YQV9bUsemkzG81Ea295/nF/5GijnD2Af7QhEofh7xu+kvCN6RdodgNwwGWXB5GMI3NoyvQo0odNctoH/qLMIpg==} dev: false