UNPKG

6.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const debug_1 = require("debug");
4const fs = require("fs-extra");
5const path = require("path");
6const semver = require("semver");
7const sumchecker = require("sumchecker");
8const artifact_utils_1 = require("./artifact-utils");
9const Cache_1 = require("./Cache");
10const downloader_resolver_1 = require("./downloader-resolver");
11const proxy_1 = require("./proxy");
12const utils_1 = require("./utils");
13var utils_2 = require("./utils");
14exports.getHostArch = utils_2.getHostArch;
15var proxy_2 = require("./proxy");
16exports.initializeProxy = proxy_2.initializeProxy;
17const d = debug_1.default('@electron/get:index');
18if (process.env.ELECTRON_GET_USE_PROXY) {
19 proxy_1.initializeProxy();
20}
21/**
22 * Downloads an artifact from an Electron release and returns an absolute path
23 * to the downloaded file.
24 *
25 * @param artifactDetails - The information required to download the artifact
26 */
27async function downloadArtifact(_artifactDetails) {
28 const artifactDetails = Object.assign({}, _artifactDetails);
29 if (!_artifactDetails.isGeneric) {
30 const platformArtifactDetails = artifactDetails;
31 if (!platformArtifactDetails.platform) {
32 d('No platform found, defaulting to the host platform');
33 platformArtifactDetails.platform = process.platform;
34 }
35 if (platformArtifactDetails.arch) {
36 platformArtifactDetails.arch = utils_1.getNodeArch(platformArtifactDetails.arch);
37 }
38 else {
39 d('No arch found, defaulting to the host arch');
40 platformArtifactDetails.arch = utils_1.getHostArch();
41 }
42 }
43 utils_1.ensureIsTruthyString(artifactDetails, 'version');
44 artifactDetails.version = utils_1.normalizeVersion(process.env.ELECTRON_CUSTOM_VERSION || artifactDetails.version);
45 const fileName = artifact_utils_1.getArtifactFileName(artifactDetails);
46 const url = await artifact_utils_1.getArtifactRemoteURL(artifactDetails);
47 const cache = new Cache_1.Cache(artifactDetails.cacheRoot);
48 // Do not check if the file exists in the cache when force === true
49 if (!artifactDetails.force) {
50 d(`Checking the cache (${artifactDetails.cacheRoot}) for ${fileName} (${url})`);
51 const cachedPath = await cache.getPathForFileInCache(url, fileName);
52 if (cachedPath === null) {
53 d('Cache miss');
54 }
55 else {
56 d('Cache hit');
57 return cachedPath;
58 }
59 }
60 if (!artifactDetails.isGeneric &&
61 utils_1.isOfficialLinuxIA32Download(artifactDetails.platform, artifactDetails.arch, artifactDetails.version, artifactDetails.mirrorOptions)) {
62 console.warn('Official Linux/ia32 support is deprecated.');
63 console.warn('For more info: https://electronjs.org/blog/linux-32bit-support');
64 }
65 return await utils_1.withTempDirectoryIn(artifactDetails.tempDirectory, async (tempFolder) => {
66 const tempDownloadPath = path.resolve(tempFolder, artifact_utils_1.getArtifactFileName(artifactDetails));
67 const downloader = artifactDetails.downloader || (await downloader_resolver_1.getDownloaderForSystem());
68 d(`Downloading ${url} to ${tempDownloadPath} with options: ${JSON.stringify(artifactDetails.downloadOptions)}`);
69 await downloader.download(url, tempDownloadPath, artifactDetails.downloadOptions);
70 // Don't try to verify the hash of the hash file itself
71 // and for older versions that don't have a SHASUMS256.txt
72 if (!artifactDetails.artifactName.startsWith('SHASUMS256') &&
73 !artifactDetails.unsafelyDisableChecksums &&
74 semver.gte(artifactDetails.version, '1.3.2')) {
75 await utils_1.withTempDirectory(async (tmpDir) => {
76 let shasumPath;
77 const checksums = artifactDetails.checksums;
78 if (checksums) {
79 shasumPath = path.resolve(tmpDir, 'SHASUMS256.txt');
80 const fileNames = Object.keys(checksums);
81 if (fileNames.length === 0) {
82 throw new Error('Provided "checksums" object is empty, cannot generate a valid SHASUMS256.txt');
83 }
84 const generatedChecksums = fileNames
85 .map(fileName => `${checksums[fileName]} *${fileName}`)
86 .join('\n');
87 await fs.writeFile(shasumPath, generatedChecksums);
88 }
89 else {
90 shasumPath = await downloadArtifact({
91 isGeneric: true,
92 version: artifactDetails.version,
93 artifactName: 'SHASUMS256.txt',
94 force: artifactDetails.force,
95 downloadOptions: artifactDetails.downloadOptions,
96 cacheRoot: artifactDetails.cacheRoot,
97 downloader: artifactDetails.downloader,
98 mirrorOptions: artifactDetails.mirrorOptions,
99 });
100 }
101 // For versions 1.3.2 - 1.3.4, need to overwrite the `defaultTextEncoding` option:
102 // https://github.com/electron/electron/pull/6676#discussion_r75332120
103 if (semver.satisfies(artifactDetails.version, '1.3.2 - 1.3.4')) {
104 const validatorOptions = {};
105 validatorOptions.defaultTextEncoding = 'binary';
106 const checker = new sumchecker.ChecksumValidator('sha256', shasumPath, validatorOptions);
107 await checker.validate(path.dirname(tempDownloadPath), path.basename(tempDownloadPath));
108 }
109 else {
110 await sumchecker('sha256', shasumPath, path.dirname(tempDownloadPath), [
111 path.basename(tempDownloadPath),
112 ]);
113 }
114 });
115 }
116 return await cache.putFileInCache(url, tempDownloadPath, fileName);
117 });
118}
119exports.downloadArtifact = downloadArtifact;
120/**
121 * Downloads a specific version of Electron and returns an absolute path to a
122 * ZIP file.
123 *
124 * @param version - The version of Electron you want to download
125 */
126function download(version, options) {
127 return downloadArtifact(Object.assign(Object.assign({}, options), { version, platform: process.platform, arch: process.arch, artifactName: 'electron' }));
128}
129exports.download = download;
130//# sourceMappingURL=index.js.map
\No newline at end of file