UNPKG

9.78 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __exportStar = (this && this.__exportStar) || function(m, exports) {
14 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15};
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.download = exports.downloadArtifact = exports.initializeProxy = exports.getHostArch = void 0;
18const debug_1 = require("debug");
19const fs = require("fs-extra");
20const path = require("path");
21const semver = require("semver");
22const sumchecker = require("sumchecker");
23const artifact_utils_1 = require("./artifact-utils");
24const types_1 = require("./types");
25const Cache_1 = require("./Cache");
26const downloader_resolver_1 = require("./downloader-resolver");
27const proxy_1 = require("./proxy");
28const utils_1 = require("./utils");
29var utils_2 = require("./utils");
30Object.defineProperty(exports, "getHostArch", { enumerable: true, get: function () { return utils_2.getHostArch; } });
31var proxy_2 = require("./proxy");
32Object.defineProperty(exports, "initializeProxy", { enumerable: true, get: function () { return proxy_2.initializeProxy; } });
33__exportStar(require("./types"), exports);
34const d = (0, debug_1.default)('@electron/get:index');
35if (process.env.ELECTRON_GET_USE_PROXY) {
36 (0, proxy_1.initializeProxy)();
37}
38async function validateArtifact(artifactDetails, downloadedAssetPath, _downloadArtifact) {
39 return await (0, utils_1.withTempDirectoryIn)(artifactDetails.tempDirectory, async (tempFolder) => {
40 // Don't try to verify the hash of the hash file itself
41 // and for older versions that don't have a SHASUMS256.txt
42 if (!artifactDetails.artifactName.startsWith('SHASUMS256') &&
43 !artifactDetails.unsafelyDisableChecksums &&
44 semver.gte(artifactDetails.version, '1.3.2')) {
45 let shasumPath;
46 const checksums = artifactDetails.checksums;
47 if (checksums) {
48 shasumPath = path.resolve(tempFolder, 'SHASUMS256.txt');
49 const fileNames = Object.keys(checksums);
50 if (fileNames.length === 0) {
51 throw new Error('Provided "checksums" object is empty, cannot generate a valid SHASUMS256.txt');
52 }
53 const generatedChecksums = fileNames
54 .map(fileName => `${checksums[fileName]} *${fileName}`)
55 .join('\n');
56 await fs.writeFile(shasumPath, generatedChecksums);
57 }
58 else {
59 shasumPath = await _downloadArtifact({
60 isGeneric: true,
61 version: artifactDetails.version,
62 artifactName: 'SHASUMS256.txt',
63 force: false,
64 downloadOptions: artifactDetails.downloadOptions,
65 cacheRoot: artifactDetails.cacheRoot,
66 downloader: artifactDetails.downloader,
67 mirrorOptions: artifactDetails.mirrorOptions,
68 // Never use the cache for loading checksums, load
69 // them fresh every time
70 cacheMode: types_1.ElectronDownloadCacheMode.Bypass,
71 });
72 }
73 try {
74 // For versions 1.3.2 - 1.3.4, need to overwrite the `defaultTextEncoding` option:
75 // https://github.com/electron/electron/pull/6676#discussion_r75332120
76 if (semver.satisfies(artifactDetails.version, '1.3.2 - 1.3.4')) {
77 const validatorOptions = {};
78 validatorOptions.defaultTextEncoding = 'binary';
79 const checker = new sumchecker.ChecksumValidator('sha256', shasumPath, validatorOptions);
80 await checker.validate(path.dirname(downloadedAssetPath), path.basename(downloadedAssetPath));
81 }
82 else {
83 await sumchecker('sha256', shasumPath, path.dirname(downloadedAssetPath), [
84 path.basename(downloadedAssetPath),
85 ]);
86 }
87 }
88 finally {
89 // Once we're done make sure we clean up the shasum temp dir
90 await fs.remove(path.dirname(shasumPath));
91 }
92 }
93 }, (0, utils_1.doesCallerOwnTemporaryOutput)((0, utils_1.effectiveCacheMode)(artifactDetails))
94 ? utils_1.TempDirCleanUpMode.ORPHAN
95 : utils_1.TempDirCleanUpMode.CLEAN);
96}
97/**
98 * Downloads an artifact from an Electron release and returns an absolute path
99 * to the downloaded file.
100 *
101 * Each release of Electron comes with artifacts, many of which are
102 * platform/arch-specific (e.g. `ffmpeg-v31.0.0-darwin-arm64.zip`) and others that
103 * are generic (e.g. `SHASUMS256.txt`).
104 *
105 *
106 * @param artifactDetails - The information required to download the artifact
107 * @category Download Artifact
108 */
109async function downloadArtifact(artifactDetails) {
110 const details = Object.assign({}, artifactDetails);
111 if (!artifactDetails.isGeneric) {
112 const platformArtifactDetails = details;
113 if (!platformArtifactDetails.platform) {
114 d('No platform found, defaulting to the host platform');
115 platformArtifactDetails.platform = process.platform;
116 }
117 if (platformArtifactDetails.arch) {
118 platformArtifactDetails.arch = (0, utils_1.getNodeArch)(platformArtifactDetails.arch);
119 }
120 else {
121 d('No arch found, defaulting to the host arch');
122 platformArtifactDetails.arch = (0, utils_1.getHostArch)();
123 }
124 }
125 (0, utils_1.ensureIsTruthyString)(details, 'version');
126 details.version = (0, artifact_utils_1.getArtifactVersion)(details);
127 const fileName = (0, artifact_utils_1.getArtifactFileName)(details);
128 const url = await (0, artifact_utils_1.getArtifactRemoteURL)(details);
129 const cache = new Cache_1.Cache(details.cacheRoot);
130 const cacheMode = (0, utils_1.effectiveCacheMode)(details);
131 // Do not check if the file exists in the cache when force === true
132 if ((0, utils_1.shouldTryReadCache)(cacheMode)) {
133 d(`Checking the cache (${details.cacheRoot}) for ${fileName} (${url})`);
134 const cachedPath = await cache.getPathForFileInCache(url, fileName);
135 if (cachedPath === null) {
136 d('Cache miss');
137 }
138 else {
139 d('Cache hit');
140 let artifactPath = cachedPath;
141 if ((0, utils_1.doesCallerOwnTemporaryOutput)(cacheMode)) {
142 // Copy out of cache into temporary directory if readOnly cache so
143 // that the caller can take ownership of the returned file
144 const tempDir = await (0, utils_1.mkdtemp)(artifactDetails.tempDirectory);
145 artifactPath = path.resolve(tempDir, fileName);
146 await fs.copyFile(cachedPath, artifactPath);
147 }
148 try {
149 await validateArtifact(details, artifactPath, downloadArtifact);
150 return artifactPath;
151 }
152 catch (err) {
153 if ((0, utils_1.doesCallerOwnTemporaryOutput)(cacheMode)) {
154 await fs.remove(path.dirname(artifactPath));
155 }
156 d("Artifact in cache didn't match checksums", err);
157 d('falling back to re-download');
158 }
159 }
160 }
161 if (!details.isGeneric &&
162 (0, utils_1.isOfficialLinuxIA32Download)(details.platform, details.arch, details.version, details.mirrorOptions)) {
163 console.warn('Official Linux/ia32 support is deprecated.');
164 console.warn('For more info: https://electronjs.org/blog/linux-32bit-support');
165 }
166 return await (0, utils_1.withTempDirectoryIn)(details.tempDirectory, async (tempFolder) => {
167 const tempDownloadPath = path.resolve(tempFolder, (0, artifact_utils_1.getArtifactFileName)(details));
168 const downloader = details.downloader || (await (0, downloader_resolver_1.getDownloaderForSystem)());
169 d(`Downloading ${url} to ${tempDownloadPath} with options: ${JSON.stringify(details.downloadOptions)}`);
170 await downloader.download(url, tempDownloadPath, details.downloadOptions);
171 await validateArtifact(details, tempDownloadPath, downloadArtifact);
172 if ((0, utils_1.doesCallerOwnTemporaryOutput)(cacheMode)) {
173 return tempDownloadPath;
174 }
175 else {
176 return await cache.putFileInCache(url, tempDownloadPath, fileName);
177 }
178 }, (0, utils_1.doesCallerOwnTemporaryOutput)(cacheMode) ? utils_1.TempDirCleanUpMode.ORPHAN : utils_1.TempDirCleanUpMode.CLEAN);
179}
180exports.downloadArtifact = downloadArtifact;
181/**
182 * Downloads the Electron binary for a specific version and returns an absolute path to a
183 * ZIP file.
184 *
185 * @param version - The version of Electron you want to download (e.g. `31.0.0`)
186 * @param options - Options to customize the download behavior
187 * @returns An absolute path to the downloaded ZIP file
188 * @category Download Electron
189 */
190function download(version, options) {
191 return downloadArtifact(Object.assign(Object.assign({}, options), { version, platform: process.platform, arch: process.arch, artifactName: 'electron' }));
192}
193exports.download = download;
194//# sourceMappingURL=index.js.map
\No newline at end of file