diff --git a/lib/util/cache/repository/impl/base.ts b/lib/util/cache/repository/impl/base.ts index a60086b2f69aa086d1c82e032d71c6a500efcda1..6e3d94b110a177fe59fa9ef2a7b87f93dd18a49b 100644 --- a/lib/util/cache/repository/impl/base.ts +++ b/lib/util/cache/repository/impl/base.ts @@ -10,7 +10,14 @@ import { isValidRev11, isValidRev12, } from '../common'; -import type { RepoCache, RepoCacheData, RepoCacheRecord } from '../types'; +import type { + RepoCache, + RepoCacheData, + RepoCacheRecord, + RepoCacheRecordV10, + RepoCacheRecordV11, + RepoCacheRecordV12, +} from '../types'; const compress = promisify(zlib.brotliCompress); const decompress = promisify(zlib.brotliDecompress); @@ -26,6 +33,24 @@ export abstract class RepoCacheBase implements RepoCache { protected abstract write(data: RepoCacheRecord): Promise<void>; + private async restoreFromRev12(oldCache: RepoCacheRecordV12): Promise<void> { + const compressed = Buffer.from(oldCache.payload, 'base64'); + const uncompressed = await decompress(compressed); + const jsonStr = uncompressed.toString('utf8'); + this.data = JSON.parse(jsonStr); + this.oldHash = oldCache.hash; + } + + private restoreFromRev11(oldCache: RepoCacheRecordV11): void { + this.data = oldCache.data; + } + + private restoreFromRev10(oldCache: RepoCacheRecordV10): void { + delete oldCache.repository; + delete oldCache.revision; + this.data = oldCache; + } + async load(): Promise<void> { try { const data = await this.read(); @@ -35,29 +60,23 @@ export abstract class RepoCacheBase implements RepoCache { ); return; } - const oldCache = JSON.parse(data); + const oldCache = JSON.parse(data) as unknown; if (isValidRev12(oldCache, this.repository)) { - const compressed = Buffer.from(oldCache.payload, 'base64'); - const uncompressed = await decompress(compressed); - const jsonStr = uncompressed.toString('utf8'); - this.data = JSON.parse(jsonStr); - this.oldHash = oldCache.hash; - logger.debug('Repository cache is valid'); + await this.restoreFromRev12(oldCache); + logger.debug('Repository cache is restored from revision 12'); return; } if (isValidRev11(oldCache, this.repository)) { - this.data = oldCache.data; - logger.debug('Repository cache is migrated from 11 revision'); + this.restoreFromRev11(oldCache); + logger.debug('Repository cache is restored from revision 11'); return; } if (isValidRev10(oldCache, this.repository)) { - delete oldCache.repository; - delete oldCache.revision; - this.data = oldCache; - logger.debug('Repository cache is migrated from 10 revision'); + this.restoreFromRev10(oldCache); + logger.debug('Repository cache is restored from revision 10'); return; }