async function downloadVSCodeArchive()

in lib/download.ts [69:102]


async function downloadVSCodeArchive(options: DownloadOptions) {
	if (!fs.existsSync(options.cachePath)) {
		fs.mkdirSync(options.cachePath);
	}

	const downloadUrl = getVSCodeDownloadUrl(options.version, options.platform, options.architecture);
	options.reporter?.report({ stage: ProgressReportStage.ResolvingCDNLocation, url: downloadUrl });
	const res = await request.getStream(downloadUrl)
	if (res.statusCode !== 302) {
		throw 'Failed to get VS Code archive location';
	}
	const url = res.headers.location;
	if (!url) {
		throw 'Failed to get VS Code archive location';
	}

	res.destroy();

	const download = await request.getStream(url);
	const totalBytes = Number(download.headers['content-length']);
	options.reporter?.report({ stage: ProgressReportStage.Downloading, url, bytesSoFar: 0, totalBytes });

	let bytesSoFar = 0;
	download.on('data', chunk => {
		bytesSoFar += chunk.length;
		options.reporter?.report({ stage: ProgressReportStage.Downloading, url, bytesSoFar, totalBytes });
	});

	download.on('end', () => {
		options.reporter?.report({ stage: ProgressReportStage.Downloading, url, bytesSoFar: totalBytes, totalBytes });
	});

	return { stream: download, format: url.endsWith('.zip') ? 'zip' : 'tgz' } as const;
}