UNPKG

2.4 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 });
14exports.Cache = void 0;
15const debug_1 = require("debug");
16const env_paths_1 = require("env-paths");
17const fs = require("fs-extra");
18const path = require("path");
19const url = require("url");
20const crypto = require("crypto");
21const d = (0, debug_1.default)('@electron/get:cache');
22const defaultCacheRoot = (0, env_paths_1.default)('electron', {
23 suffix: '',
24}).cache;
25class Cache {
26 constructor(cacheRoot = defaultCacheRoot) {
27 this.cacheRoot = cacheRoot;
28 }
29 static getCacheDirectory(downloadUrl) {
30 const parsedDownloadUrl = url.parse(downloadUrl);
31 // eslint-disable-next-line @typescript-eslint/no-unused-vars
32 const { search, hash, pathname } = parsedDownloadUrl, rest = __rest(parsedDownloadUrl, ["search", "hash", "pathname"]);
33 const strippedUrl = url.format(Object.assign(Object.assign({}, rest), { pathname: path.dirname(pathname || 'electron') }));
34 return crypto
35 .createHash('sha256')
36 .update(strippedUrl)
37 .digest('hex');
38 }
39 getCachePath(downloadUrl, fileName) {
40 return path.resolve(this.cacheRoot, Cache.getCacheDirectory(downloadUrl), fileName);
41 }
42 async getPathForFileInCache(url, fileName) {
43 const cachePath = this.getCachePath(url, fileName);
44 if (await fs.pathExists(cachePath)) {
45 return cachePath;
46 }
47 return null;
48 }
49 async putFileInCache(url, currentPath, fileName) {
50 const cachePath = this.getCachePath(url, fileName);
51 d(`Moving ${currentPath} to ${cachePath}`);
52 if (await fs.pathExists(cachePath)) {
53 d('* Replacing existing file');
54 await fs.remove(cachePath);
55 }
56 await fs.move(currentPath, cachePath);
57 return cachePath;
58 }
59}
60exports.Cache = Cache;
61//# sourceMappingURL=Cache.js.map
\No newline at end of file