index.js 6.7 KB

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