From 68b879cdf270380e87fa76fd56b98ce07146d4e7 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 7 Jun 2020 12:54:38 -0700 Subject: [PATCH] refactor(Cache): replace sanitize-filename with filenamify It's extremely difficult to upgrade `sanitize-filename` from 1.6.2 to 1.6.3, as they merged their TypeScript typings into the package. As a result, the recommended import statement to use it does not seem to work with both CJS and ESM configurations. The options used with `filenamify` should be compatible with the default options of `sanitize-filename`. --- package.json | 3 +-- src/Cache.ts | 4 ++-- test/Cache.spec.ts | 3 +-- yarn.lock | 52 +++++++++++++++++++++++++--------------------- 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 8e37c3e80..dbdbaa523 100644 --- a/package.json +++ b/package.json @@ -26,10 +26,10 @@ "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", - "sanitize-filename": "^1.6.2", "sumchecker": "^3.0.1" }, "devDependencies": { @@ -40,7 +40,6 @@ "@types/jest": "^24.0.13", "@types/node": "^12.0.2", "@types/progress": "^2.0.3", - "@types/sanitize-filename": "^1.1.28", "husky": "^2.3.0", "jest": "^24.8.0", "lint-staged": "^8.1.7", diff --git a/src/Cache.ts b/src/Cache.ts index c2b4d638e..d6e3104ac 100644 --- a/src/Cache.ts +++ b/src/Cache.ts @@ -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 sanitize from 'sanitize-filename'; const d = debug('@electron/get:cache'); @@ -18,7 +18,7 @@ export class Cache { const { search, hash, ...rest } = url.parse(downloadUrl); const strippedUrl = url.format(rest); - const sanitizedUrl = sanitize(strippedUrl); + const sanitizedUrl = filenamify(strippedUrl, { maxLength: 255, replacement: '' }); return path.resolve(this.cacheRoot, sanitizedUrl, fileName); } diff --git a/test/Cache.spec.ts b/test/Cache.spec.ts index f092cc4cb..16a94d87e 100644 --- a/test/Cache.spec.ts +++ b/test/Cache.spec.ts @@ -1,7 +1,6 @@ import * as fs from 'fs-extra'; import * as os from 'os'; import * as path from 'path'; -import * as sanitize from 'sanitize-filename'; import { Cache } from '../src/Cache'; @@ -10,7 +9,7 @@ describe('Cache', () => { let cache: Cache; const dummyUrl = 'dummy://'; - const sanitizedDummyUrl = sanitize(dummyUrl); + const sanitizedDummyUrl = 'dummy'; beforeEach(async () => { cacheDir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-download-spec-')); diff --git a/yarn.lock b/yarn.lock index 1181cd282..8c2ffb557 100644 --- a/yarn.lock +++ b/yarn.lock @@ -602,11 +602,6 @@ dependencies: "@types/node" "*" -"@types/sanitize-filename@^1.1.28": - version "1.1.28" - resolved "https://registry.yarnpkg.com/@types/sanitize-filename/-/sanitize-filename-1.1.28.tgz#baa18f5ce4330fcbb3ea7b62f65550963d9aaa07" - integrity sha1-uqGPXOQzD8uz6nti9lVQlj2aqgc= - "@types/stack-utils@^1.0.1": version "1.0.1" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e" @@ -2224,6 +2219,20 @@ figures@^2.0.0: dependencies: escape-string-regexp "^1.0.5" +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" @@ -6118,13 +6127,6 @@ sane@^4.0.3: minimist "^1.1.1" walker "~1.0.5" -sanitize-filename@^1.6.2: - version "1.6.2" - resolved "https://registry.yarnpkg.com/sanitize-filename/-/sanitize-filename-1.6.2.tgz#01b4fc8809f14e9d22761fe70380fe7f3f902185" - integrity sha512-cmTzND7RMxUB+f7gI+4+KAVHWEg0lfXvQJdko+FXDP5bNbGIdx4KMP5pX6lv5jfT9jSf6OBbjyxjFtZQwYA/ig== - dependencies: - truncate-utf8-bytes "^1.0.0" - sax@^1.2.4: version "1.2.4" resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" @@ -6685,6 +6687,13 @@ 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" @@ -6885,18 +6894,18 @@ 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" integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM= -truncate-utf8-bytes@^1.0.0: - version "1.0.2" - resolved "https://registry.yarnpkg.com/truncate-utf8-bytes/-/truncate-utf8-bytes-1.0.2.tgz#405923909592d56f78a5818434b0b78489ca5f2b" - integrity sha1-QFkjkJWS1W94pYGENLC3hInKXys= - dependencies: - utf8-byte-length "^1.0.1" - ts-jest@^24.0.0: version "24.0.2" resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-24.0.2.tgz#8dde6cece97c31c03e80e474c749753ffd27194d" @@ -7120,11 +7129,6 @@ use@^3.1.0: resolved "https://registry.yarnpkg.com/use/-/use-3.1.1.tgz#d50c8cac79a19fbc20f2911f56eb973f4e10070f" integrity sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ== -utf8-byte-length@^1.0.1: - version "1.0.4" - resolved "https://registry.yarnpkg.com/utf8-byte-length/-/utf8-byte-length-1.0.4.tgz#f45f150c4c66eee968186505ab93fcbb8ad6bf61" - integrity sha1-9F8VDExm7uloGGUFq5P8u4rWv2E= - util-deprecate@^1.0.1, util-deprecate@~1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"