diff --git a/packages/legacy/core/App/constants.ts b/packages/legacy/core/App/constants.ts index 453754b41..c09b6147d 100644 --- a/packages/legacy/core/App/constants.ts +++ b/packages/legacy/core/App/constants.ts @@ -86,6 +86,6 @@ export const hitSlop = { top: 44, bottom: 44, left: 44, right: 44 } export const templateBundleStorageDirectory = 'templates' -export const templateCacheDataFileName = 'proof-templates.json' +export const templateCacheDataFileName = 'index.json' export const templateBundleIndexFileName = 'proof-templates.json' diff --git a/packages/legacy/core/App/utils/fileCache.ts b/packages/legacy/core/App/utils/fileCache.ts index 57f88feb7..50b82a0a3 100644 --- a/packages/legacy/core/App/utils/fileCache.ts +++ b/packages/legacy/core/App/utils/fileCache.ts @@ -43,6 +43,21 @@ export class FileCache { return this._fileEtag || '' } + protected loadCacheData = async (): Promise => { + const cacheFileExists = await this.checkFileExists(this.cacheFileName) + if (!cacheFileExists) { + return + } + + const data = await this.loadFileFromLocalStorage(this.cacheFileName) + if (!data) { + return + } + + const cacheData: CacheDataFile = JSON.parse(data) + return cacheData + } + protected saveCacheData = async (cacheData: CacheDataFile): Promise => { const cacheDataAsString = JSON.stringify(cacheData) @@ -119,4 +134,19 @@ export class FileCache { return false } + + protected stripWeakEtag = (etag: string): string => { + if (etag.startsWith('W/')) { + return etag.slice(2).replace(/"/g, '') + } + + return etag.replace(/"/g, '') + } + + protected compareWeakEtags = (etag1: string, etag2: string): boolean => { + const cleanEtag1 = this.stripWeakEtag(etag1) + const cleanEtag2 = this.stripWeakEtag(etag2) + + return cleanEtag1 === cleanEtag2 + } } diff --git a/packages/legacy/core/App/utils/proofBundle.ts b/packages/legacy/core/App/utils/proofBundle.ts index c9a4b8d35..904d85211 100644 --- a/packages/legacy/core/App/utils/proofBundle.ts +++ b/packages/legacy/core/App/utils/proofBundle.ts @@ -99,7 +99,7 @@ export class RemoteProofBundleResolver extends FileCache implements IProofBundle private cacheDataFileName = templateCacheDataFileName public constructor(indexFileBaseUrl: string, log?: BifoldLogger) { - super(indexFileBaseUrl, templateBundleStorageDirectory, templateBundleIndexFileName, log) + super(indexFileBaseUrl, templateBundleStorageDirectory, templateCacheDataFileName, log) } public async resolve(acceptDevRestrictions: boolean): Promise { @@ -154,10 +154,11 @@ export class RemoteProofBundleResolver extends FileCache implements IProofBundle } this.log?.info('Loading index now') - await this.loadBundleIndex(this.cacheDataFileName) + await this.loadBundleIndex(templateBundleIndexFileName) } private loadBundleIndex = async (filePath: string) => { + let remoteFetchSucceeded = false try { const response = await this.axiosInstance.get(filePath) const { status } = response @@ -165,18 +166,11 @@ export class RemoteProofBundleResolver extends FileCache implements IProofBundle if (status !== 200) { this.log?.error(`Failed to fetch remote resource at ${filePath}`) - // failed to fetch, use the cached index file - // if available - const data = await this.loadFileFromLocalStorage(filePath) - if (data) { - this.log?.info(`Using index file ${filePath} from cache`) - this.templateData = JSON.parse(data) - } - return + throw new Error('Failed to fetch remote resource') } - if (etag && etag === this.fileEtag) { + if (etag && this.compareWeakEtags(this.fileEtag, etag)) { this.log?.info(`Index file ${filePath} has not changed, etag ${etag}`) // etag is the same, no need to refresh this.templateData = response.data @@ -186,28 +180,25 @@ export class RemoteProofBundleResolver extends FileCache implements IProofBundle this.fileEtag = etag this.templateData = response.data + remoteFetchSucceeded = true - this.log?.info(`Saving file ${filePath}, etag ${etag}`) await this.saveFileToLocalStorage(filePath, JSON.stringify(this.templateData)) } catch (error) { - this.log?.error(`Failed to fetch file index ${filePath}`) - } - } + this.log?.error(`Failed to fetch remote file index ${filePath}`) + } finally { + if (remoteFetchSucceeded) { + return + } - private loadCacheData = async (): Promise => { - const cacheFileExists = await this.checkFileExists(this.cacheDataFileName) - if (!cacheFileExists) { - return - } + const data = await this.loadFileFromLocalStorage(filePath) + if (!data) { + this.log?.error(`Failed to load index file ${filePath} from cache`) + return + } - const data = await this.loadFileFromLocalStorage(this.cacheDataFileName) - if (!data) { - return + this.log?.info(`Using index file ${filePath} from cache`) + this.templateData = JSON.parse(data) } - - const cacheData: CacheDataFile = JSON.parse(data) - - return cacheData } }