UNPKG

2.37 kBJavaScriptView Raw
1"use strict";
2var __rest = (this && this.__rest) || function (s, e) {
3 var t = {};
4 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5 t[p] = s[p];
6 if (s != null && typeof Object.getOwnPropertySymbols === "function")
7 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9 t[p[i]] = s[p[i]];
10 }
11 return t;
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14const debug_1 = require("debug");
15const env_paths_1 = require("env-paths");
16const fs = require("fs-extra");
17const path = require("path");
18const url = require("url");
19const crypto = require("crypto");
20const d = debug_1.default('@electron/get:cache');
21const defaultCacheRoot = env_paths_1.default('electron', {
22 suffix: '',
23}).cache;
24class Cache {
25 constructor(cacheRoot = defaultCacheRoot) {
26 this.cacheRoot = cacheRoot;
27 }
28 static getCacheDirectory(downloadUrl) {
29 const parsedDownloadUrl = url.parse(downloadUrl);
30 // eslint-disable-next-line @typescript-eslint/no-unused-vars
31 const { search, hash, pathname } = parsedDownloadUrl, rest = __rest(parsedDownloadUrl, ["search", "hash", "pathname"]);
32 const strippedUrl = url.format(Object.assign(Object.assign({}, rest), { pathname: path.dirname(pathname || 'electron') }));
33 return crypto
34 .createHash('sha256')
35 .update(strippedUrl)
36 .digest('hex');
37 }
38 getCachePath(downloadUrl, fileName) {
39 return path.resolve(this.cacheRoot, Cache.getCacheDirectory(downloadUrl), fileName);
40 }
41 async getPathForFileInCache(url, fileName) {
42 const cachePath = this.getCachePath(url, fileName);
43 if (await fs.pathExists(cachePath)) {
44 return cachePath;
45 }
46 return null;
47 }
48 async putFileInCache(url, currentPath, fileName) {
49 const cachePath = this.getCachePath(url, fileName);
50 d(`Moving ${currentPath} to ${cachePath}`);
51 if (await fs.pathExists(cachePath)) {
52 d('* Replacing existing file');
53 await fs.remove(cachePath);
54 }
55 await fs.move(currentPath, cachePath);
56 return cachePath;
57 }
58}
59exports.Cache = Cache;
60//# sourceMappingURL=Cache.js.map
\No newline at end of file