index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /**
  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. */
  27. async 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 = artifact_utils_1.getArtifactVersion(artifactDetails);
  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. }
  119. exports.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. */
  126. function download(version, options) {
  127. return downloadArtifact(Object.assign(Object.assign({}, options), { version, platform: process.platform, arch: process.arch, artifactName: 'electron' }));
  128. }
  129. exports.download = download;
  130. //# sourceMappingURL=index.js.map