Skip to content
This repository was archived by the owner on Dec 15, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/definitely-typed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { existsSync, mkdirSync, writeFileSync } from 'fs';
import { STATUS_CODES } from 'http';
import { IncomingMessage, STATUS_CODES } from "http";
import { get } from "https";
import { homedir } from 'os';
import parseGitConfig = require('parse-git-config');
Expand Down Expand Up @@ -134,15 +134,31 @@ interface Package {
}

function loadString(url: string): Promise<string> {
// This is horrible, and it would be better to just use node-fetch or something:
return new Promise((resolve, reject) => {
get(url, res => {
const doGet = (url: string) => {
get(url, onResponse).on('error', reject);
};

const onResponse = (res: IncomingMessage) => {
if (res.statusCode !== 200) {
if (res.statusCode! >= 300 && res.statusCode! < 400) {
// Follow redirects:
const { location } = res.headers;
if (location) {
return doGet(location);
}
}

return reject(
new Error(`HTTP Error ${res.statusCode}: ${STATUS_CODES[res.statusCode || 500]} for ${url}`));
}

let rawData = "";
res.on("data", (chunk: any) => rawData += chunk);
res.on("end", () => resolve(rawData));
}).on("error", (e: Error) => reject(e));
};

return doGet(url);
});
}