Skip to content

Commit

Permalink
fix: avoid long paths in cache & lower the number of folders created (#…
Browse files Browse the repository at this point in the history
…186)

Uses a sha256 checksum instead of filenamify for cache paths
Fixes #185
  • Loading branch information
joaomoreno authored Feb 6, 2021
1 parent b42749a commit 1165598
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 40 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"dependencies": {
"debug": "^4.1.1",
"env-paths": "^2.2.0",
"filenamify": "^4.1.0",
"fs-extra": "^8.1.0",
"got": "^9.6.0",
"progress": "^2.0.3",
Expand Down
19 changes: 13 additions & 6 deletions src/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import debug from 'debug';
import envPaths from 'env-paths';
import * as filenamify from 'filenamify';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as url from 'url';
import * as crypto from 'crypto';

const d = debug('@electron/get:cache');

Expand All @@ -14,13 +14,20 @@ const defaultCacheRoot = envPaths('electron', {
export class Cache {
constructor(private cacheRoot = defaultCacheRoot) {}

public getCachePath(downloadUrl: string, fileName: string): string {
public static getCacheDirectory(downloadUrl: string): string {
const parsedDownloadUrl = url.parse(downloadUrl);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { search, hash, ...rest } = url.parse(downloadUrl);
const strippedUrl = url.format(rest);
const { search, hash, pathname, ...rest } = parsedDownloadUrl;
const strippedUrl = url.format({ ...rest, pathname: path.dirname(pathname || 'electron') });

return crypto
.createHash('sha256')
.update(strippedUrl)
.digest('hex');
}

const sanitizedUrl = filenamify(strippedUrl, { maxLength: 255, replacement: '' });
return path.resolve(this.cacheRoot, sanitizedUrl, fileName);
public getCachePath(downloadUrl: string, fileName: string): string {
return path.resolve(this.cacheRoot, Cache.getCacheDirectory(downloadUrl), fileName);
}

public async getPathForFileInCache(url: string, fileName: string): Promise<string | null> {
Expand Down
5 changes: 2 additions & 3 deletions test/Cache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import * as fs from 'fs-extra';
import * as os from 'os';
import * as path from 'path';

import { Cache } from '../src/Cache';

describe('Cache', () => {
let cacheDir: string;
let cache: Cache;

const dummyUrl = 'dummy://';
const sanitizedDummyUrl = 'dummy';
const dummyUrl = 'dummy://dummypath';
const sanitizedDummyUrl = '0c57d948bd4829db99d75c3b4a5d6836c37bc335f38012981baf5d1193b5a612';

beforeEach(async () => {
cacheDir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-download-spec-'));
Expand Down
30 changes: 0 additions & 30 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2624,20 +2624,6 @@ file-entry-cache@^5.0.1:
dependencies:
flat-cache "^2.0.1"

filename-reserved-regex@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/filename-reserved-regex/-/filename-reserved-regex-2.0.0.tgz#abf73dfab735d045440abfea2d91f389ebbfa229"
integrity sha1-q/c9+rc10EVECr/qLZHzieu/oik=

filenamify@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/filenamify/-/filenamify-4.1.0.tgz#54d110810ae74eebfe115c1b995bd07e03cf2184"
integrity sha512-KQV/uJDI9VQgN7sHH1Zbk6+42cD6mnQ2HONzkXUfPJ+K2FC8GZ1dpewbbHw0Sz8Tf5k3EVdHVayM4DoAwWlmtg==
dependencies:
filename-reserved-regex "^2.0.0"
strip-outer "^1.0.1"
trim-repeated "^1.0.0"

fill-range@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-4.0.0.tgz#d544811d428f98eb06a63dc402d2403c328c38f7"
Expand Down Expand Up @@ -6762,8 +6748,6 @@ rxjs@^6.3.3:
version "6.4.0"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.4.0.tgz#f3bb0fe7bda7fb69deac0c16f17b50b0b8790504"
integrity sha512-Z9Yfa11F6B9Sg/BK9MnqnQ+aQYicPLtilXBp2yUtDt2JRCE0h26d33EnfO3ZxoNxG0T92OUucP3Ct7cpfkdFfw==
dependencies:
tslib "^1.9.0"

rxjs@^6.5.3:
version "6.6.3"
Expand Down Expand Up @@ -7410,13 +7394,6 @@ strip-json-comments@~2.0.1:
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
integrity sha1-PFMZQukIwml8DsNEhYwobHygpgo=

strip-outer@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/strip-outer/-/strip-outer-1.0.1.tgz#b2fd2abf6604b9d1e6013057195df836b8a9d631"
integrity sha512-k55yxKHwaXnpYGsOzg4Vl8+tDrWylxDEpknGjhTiZB8dFRU5rTo9CAzeycivxV3s+zlTKwrs6WxMxR95n26kwg==
dependencies:
escape-string-regexp "^1.0.2"

sumchecker@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/sumchecker/-/sumchecker-3.0.1.tgz#6377e996795abb0b6d348e9b3e1dfb24345a8e42"
Expand Down Expand Up @@ -7633,13 +7610,6 @@ trim-off-newlines@^1.0.0:
resolved "https://registry.yarnpkg.com/trim-off-newlines/-/trim-off-newlines-1.0.1.tgz#9f9ba9d9efa8764c387698bcbfeb2c848f11adb3"
integrity sha1-n5up2e+odkw4dpi8v+sshI8RrbM=

trim-repeated@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/trim-repeated/-/trim-repeated-1.0.0.tgz#e3646a2ea4e891312bf7eace6cfb05380bc01c21"
integrity sha1-42RqLqTokTEr9+rObPsFOAvAHCE=
dependencies:
escape-string-regexp "^1.0.2"

trim-right@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
Expand Down

0 comments on commit 1165598

Please sign in to comment.