export async function downloadFile()

in src/Utils/index.ts [41:89]


export async function downloadFile(targetUrl: string, readContent?: boolean, customHeaders?: {}): Promise<string> {
    const tempFilePath: string = path.join(getTempFolder(), md5(targetUrl));
    await fse.ensureDir(getTempFolder());
    if (await fse.pathExists(tempFilePath)) {
        await fse.remove(tempFilePath);
    }

    return await new Promise((resolve: (res: string) => void, reject: (e: Error) => void): void => {
        const urlObj: url.Url = url.parse(targetUrl);
        const options = Object.assign({ headers: Object.assign({}, customHeaders, { "User-Agent": `vscode/${getVersion()}` }) }, urlObj);
        let client: any;
        if (urlObj.protocol === "https:") {
            client = https;
            // tslint:disable-next-line:no-http-string
        } else if (urlObj.protocol === "http:") {
            client = http;
        } else {
            return reject(new Error("Unsupported protocol."));
        }
        client.get(options, (res: http.IncomingMessage) => {
            let rawData: string;
            let ws: fse.WriteStream;
            if (readContent) {
                rawData = "";
            } else {
                ws = fse.createWriteStream(tempFilePath);
            }
            res.on("data", (chunk: string | Buffer) => {
                if (readContent) {
                    rawData += chunk;
                } else {
                    ws.write(chunk);
                }
            });
            res.on("end", () => {
                if (readContent) {
                    resolve(rawData);
                } else {
                    ws.end();
                    ws.on("close", () => {
                        resolve(tempFilePath);
                    });
                }
            });
        }).on("error", (err: Error) => {
            reject(err);
        });
    });
}