diff --git a/lib/util/cache/repository/impl/base.ts b/lib/util/cache/repository/impl/base.ts index c9279744548c892514226ed399da8fec25ee25fc..c9a21e9a72a8afa06157412e70c4d59c46f30e9c 100644 --- a/lib/util/cache/repository/impl/base.ts +++ b/lib/util/cache/repository/impl/base.ts @@ -1,5 +1,6 @@ import { promisify } from 'util'; import zlib from 'zlib'; +import is from '@sindresorhus/is'; import hasha from 'hasha'; import { GlobalConfig } from '../../../../config/global'; import { logger } from '../../../../logger'; @@ -27,7 +28,14 @@ export abstract class RepoCacheBase implements RepoCache { async load(): Promise<void> { try { - const oldCache = await this.read(); + const data = await this.read(); + if (!is.string(data)) { + logger.debug( + `RepoCacheBase.load() - expecting data of type 'string' received '${typeof data}' instead - skipping` + ); + return; + } + const oldCache = JSON.parse(data); if (isValidRev12(oldCache, this.repository)) { const compressed = Buffer.from(oldCache.payload, 'base64'); diff --git a/lib/util/cache/repository/impl/local.spec.ts b/lib/util/cache/repository/impl/local.spec.ts index a3ab46953c293edbcd79f5279c88ba124f999a6f..92baef1f21fa5d350eefd49e46088e2954f3f986 100644 --- a/lib/util/cache/repository/impl/local.spec.ts +++ b/lib/util/cache/repository/impl/local.spec.ts @@ -35,6 +35,14 @@ describe('util/cache/repository/impl/local', () => { expect(localRepoCache.getData()).toBeEmpty(); }); + it('skip when receives non-string data', async () => { + const localRepoCache = CacheFactory.get('some/repo', 'local'); + await localRepoCache.load(); // readCacheFile is mocked but has no return value set - therefore returns undefined + expect(logger.debug).toHaveBeenCalledWith( + "RepoCacheBase.load() - expecting data of type 'string' received 'undefined' instead - skipping" + ); + }); + it('loads previously stored cache from disk', async () => { const data: RepoCacheData = { semanticCommits: 'enabled' }; const cacheRecord = await createCacheRecord(data); diff --git a/lib/util/cache/repository/impl/local.ts b/lib/util/cache/repository/impl/local.ts index b06ac07571690743c712747254aff1eb2ebbe7db..217ea0023a12181a30aca2daf2da07abdd4e980e 100644 --- a/lib/util/cache/repository/impl/local.ts +++ b/lib/util/cache/repository/impl/local.ts @@ -12,15 +12,12 @@ export class RepoCacheLocal extends RepoCacheBase { protected async read(): Promise<unknown> { const cacheFileName = this.getCacheFileName(); - let data: unknown; try { - const rawCache = await readCacheFile(cacheFileName, 'utf8'); - data = JSON.parse(rawCache); + return await readCacheFile(cacheFileName, 'utf8'); } catch (err) { logger.debug({ cacheFileName }, 'Repository local cache not found'); throw err; } - return data; } protected async write(data: RepoCacheRecord): Promise<void> {