GSI - Employe Self Service Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
4.4 KiB

2 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.downloadExtensionVersion = exports.downloadEmulator = void 0;
  4. const crypto = require("crypto");
  5. const fs = require("fs-extra");
  6. const path = require("path");
  7. const tmp = require("tmp");
  8. const unzipper = require("unzipper");
  9. const emulatorLogger_1 = require("./emulatorLogger");
  10. const error_1 = require("../error");
  11. const downloadableEmulators = require("./downloadableEmulators");
  12. const downloadUtils = require("../downloadUtils");
  13. tmp.setGracefulCleanup();
  14. async function downloadEmulator(name) {
  15. const emulator = downloadableEmulators.getDownloadDetails(name);
  16. emulatorLogger_1.EmulatorLogger.forEmulator(name).logLabeled("BULLET", name, `downloading ${path.basename(emulator.downloadPath)}...`);
  17. fs.ensureDirSync(emulator.opts.cacheDir);
  18. const tmpfile = await downloadUtils.downloadToTmp(emulator.opts.remoteUrl);
  19. if (!emulator.opts.skipChecksumAndSize) {
  20. await validateSize(tmpfile, emulator.opts.expectedSize);
  21. await validateChecksum(tmpfile, emulator.opts.expectedChecksum);
  22. }
  23. if (emulator.opts.skipCache) {
  24. removeOldFiles(name, emulator, true);
  25. }
  26. fs.copySync(tmpfile, emulator.downloadPath);
  27. if (emulator.unzipDir) {
  28. await unzip(emulator.downloadPath, emulator.unzipDir);
  29. }
  30. await new Promise((f) => setTimeout(f, 2000));
  31. const executablePath = emulator.binaryPath || emulator.downloadPath;
  32. fs.chmodSync(executablePath, 0o755);
  33. removeOldFiles(name, emulator);
  34. }
  35. exports.downloadEmulator = downloadEmulator;
  36. async function downloadExtensionVersion(extensionVersionRef, sourceDownloadUri, targetDir) {
  37. const emulatorLogger = emulatorLogger_1.EmulatorLogger.forExtension({ ref: extensionVersionRef });
  38. emulatorLogger.logLabeled("BULLET", "extensions", `Starting download for ${extensionVersionRef} source code to ${targetDir}..`);
  39. try {
  40. fs.mkdirSync(targetDir);
  41. }
  42. catch (err) {
  43. emulatorLogger.logLabeled("BULLET", "extensions", `cache directory for ${extensionVersionRef} already exists...`);
  44. }
  45. emulatorLogger.logLabeled("BULLET", "extensions", `downloading ${sourceDownloadUri}...`);
  46. const sourceCodeZip = await downloadUtils.downloadToTmp(sourceDownloadUri);
  47. await unzip(sourceCodeZip, targetDir);
  48. fs.chmodSync(targetDir, 0o755);
  49. emulatorLogger.logLabeled("BULLET", "extensions", `Downloaded to ${targetDir}...`);
  50. await new Promise((resolve) => setTimeout(resolve, 1000));
  51. }
  52. exports.downloadExtensionVersion = downloadExtensionVersion;
  53. function unzip(zipPath, unzipDir) {
  54. return new Promise((resolve, reject) => {
  55. fs.createReadStream(zipPath)
  56. .pipe(unzipper.Extract({ path: unzipDir }))
  57. .on("error", reject)
  58. .on("close", resolve);
  59. });
  60. }
  61. function removeOldFiles(name, emulator, removeAllVersions = false) {
  62. const currentLocalPath = emulator.downloadPath;
  63. const currentUnzipPath = emulator.unzipDir;
  64. const files = fs.readdirSync(emulator.opts.cacheDir);
  65. for (const file of files) {
  66. const fullFilePath = path.join(emulator.opts.cacheDir, file);
  67. if (file.indexOf(emulator.opts.namePrefix) < 0) {
  68. continue;
  69. }
  70. if ((fullFilePath !== currentLocalPath && fullFilePath !== currentUnzipPath) ||
  71. removeAllVersions) {
  72. emulatorLogger_1.EmulatorLogger.forEmulator(name).logLabeled("BULLET", name, `Removing outdated emulator files: ${file}`);
  73. fs.removeSync(fullFilePath);
  74. }
  75. }
  76. }
  77. function validateSize(filepath, expectedSize) {
  78. return new Promise((resolve, reject) => {
  79. const stat = fs.statSync(filepath);
  80. return stat.size === expectedSize
  81. ? resolve()
  82. : reject(new error_1.FirebaseError(`download failed, expected ${expectedSize} bytes but got ${stat.size}`, { exit: 1 }));
  83. });
  84. }
  85. function validateChecksum(filepath, expectedChecksum) {
  86. return new Promise((resolve, reject) => {
  87. const hash = crypto.createHash("md5");
  88. const stream = fs.createReadStream(filepath);
  89. stream.on("data", (data) => hash.update(data));
  90. stream.on("end", () => {
  91. const checksum = hash.digest("hex");
  92. return checksum === expectedChecksum
  93. ? resolve()
  94. : reject(new error_1.FirebaseError(`download failed, expected checksum ${expectedChecksum} but got ${checksum}`, { exit: 1 }));
  95. });
  96. });
  97. }