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