1 | "use strict";
|
2 | var __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 | }));
|
13 | var __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 | };
|
16 | Object.defineProperty(exports, "__esModule", { value: true });
|
17 | exports.download = exports.downloadArtifact = exports.initializeProxy = exports.getHostArch = void 0;
|
18 | const debug_1 = require("debug");
|
19 | const fs = require("fs-extra");
|
20 | const path = require("path");
|
21 | const semver = require("semver");
|
22 | const sumchecker = require("sumchecker");
|
23 | const artifact_utils_1 = require("./artifact-utils");
|
24 | const types_1 = require("./types");
|
25 | const Cache_1 = require("./Cache");
|
26 | const downloader_resolver_1 = require("./downloader-resolver");
|
27 | const proxy_1 = require("./proxy");
|
28 | const utils_1 = require("./utils");
|
29 | var utils_2 = require("./utils");
|
30 | Object.defineProperty(exports, "getHostArch", { enumerable: true, get: function () { return utils_2.getHostArch; } });
|
31 | var proxy_2 = require("./proxy");
|
32 | Object.defineProperty(exports, "initializeProxy", { enumerable: true, get: function () { return proxy_2.initializeProxy; } });
|
33 | __exportStar(require("./types"), exports);
|
34 | const d = (0, debug_1.default)('@electron/get:index');
|
35 | if (process.env.ELECTRON_GET_USE_PROXY) {
|
36 | (0, proxy_1.initializeProxy)();
|
37 | }
|
38 | async function validateArtifact(artifactDetails, downloadedAssetPath, _downloadArtifact) {
|
39 | return await (0, utils_1.withTempDirectoryIn)(artifactDetails.tempDirectory, async (tempFolder) => {
|
40 |
|
41 |
|
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 |
|
69 |
|
70 | cacheMode: types_1.ElectronDownloadCacheMode.Bypass,
|
71 | });
|
72 | }
|
73 | try {
|
74 |
|
75 |
|
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 |
|
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 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | async 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 |
|
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 |
|
143 |
|
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 | }
|
180 | exports.downloadArtifact = downloadArtifact;
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | function download(version, options) {
|
191 | return downloadArtifact(Object.assign(Object.assign({}, options), { version, platform: process.platform, arch: process.arch, artifactName: 'electron' }));
|
192 | }
|
193 | exports.download = download;
|
194 |
|
\ | No newline at end of file |