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{ Socket } from \"net\"\nimport { IncomingMessage, ClientRequest } from \"http\"\nimport * as https from \"https\"\nimport { createWriteStream } from \"fs\"\nimport { parse as parseUrl } from \"url\"\nimport { Promise as BluebirdPromise } from \"bluebird\"\n\nconst maxRedirects = 10\n\nexport const download = BluebirdPromise.promisify(_download)\n\nfunction _download(url: string, destination: string, callback: (error: Error) => void): void {\n doDownload(url, destination, 0, callback)\n}\n\nexport function addTimeOutHandler(request: ClientRequest, callback: (error: Error | string) => void) {\n request.on(\"socket\", function (socket: Socket) {\n socket.setTimeout(60 * 1000, () => {\n callback(\"Request timed out\")\n request.abort()\n })\n })\n}\n\nfunction doDownload(url: string, destination: string, redirectCount: number, callback: (error: Error) => void) {\n const parsedUrl = parseUrl(url)\n // user-agent must be specified, otherwise some host can return 401 unauthorised\n const request = https.request({\n hostname: parsedUrl.hostname,\n path: parsedUrl.path,\n headers: {\n \"User-Agent\": \"electron-complete-builder\"\n }\n }, (response: IncomingMessage) => {\n if (response.statusCode >= 400) {\n callback(new Error(\"Request error, status \" + response.statusCode + \": \" + response.statusMessage))\n return\n }\n\n const redirectUrl = response.headers.location\n if (redirectUrl != null) {\n if (redirectCount < maxRedirects) {\n doDownload(redirectUrl, destination, redirectCount++, callback)\n }\n else {\n callback(new Error(\"Too many redirects (> \" + maxRedirects + \")\"))\n }\n return\n }\n\n const downloadStream = createWriteStream(destination)\n response.pipe(downloadStream)\n downloadStream.on(\"finish\", () => downloadStream.close(callback))\n\n let ended = false\n response.on(\"end\", () => {\n ended = true\n })\n\n response.on(\"close\", () => {\n if (!ended) {\n callback(new Error(\"Request aborted\"))\n }\n })\n })\n addTimeOutHandler(request, callback)\n request.on(\"error\", callback)\n request.end()\n}"]} |