UNPKG

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