1 | "use strict";
|
2 | var __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 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.Cache = void 0;
|
15 | const debug_1 = require("debug");
|
16 | const env_paths_1 = require("env-paths");
|
17 | const fs = require("fs-extra");
|
18 | const path = require("path");
|
19 | const url = require("url");
|
20 | const crypto = require("crypto");
|
21 | const d = (0, debug_1.default)('@electron/get:cache');
|
22 | const defaultCacheRoot = (0, env_paths_1.default)('electron', {
|
23 | suffix: '',
|
24 | }).cache;
|
25 | class Cache {
|
26 | constructor(cacheRoot = defaultCacheRoot) {
|
27 | this.cacheRoot = cacheRoot;
|
28 | }
|
29 | static getCacheDirectory(downloadUrl) {
|
30 | const parsedDownloadUrl = url.parse(downloadUrl);
|
31 |
|
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 | }
|
60 | exports.Cache = Cache;
|
61 |
|
\ | No newline at end of file |