Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Follow redirects when generating source for DefinitelyTyped #172

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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);
});
}