From 0945e2659b4ae9151544c8ed555f3208fc810728 Mon Sep 17 00:00:00 2001 From: Anton Strogonoff Date: Thu, 2 Dec 2021 14:15:13 +0100 Subject: [PATCH] feat(download): return cached object, if possible --- src/download.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/download.ts b/src/download.ts index 0d9ae34..54af881 100644 --- a/src/download.ts +++ b/src/download.ts @@ -24,12 +24,25 @@ function isValidLFSInfoResponseData(val: Record): val is LFSInfoRes /** * Downloads, caches and returns a blob corresponding to given LFS pointer. + * Uses already cached object, if size matches. */ export default async function downloadBlobFromPointer( { http: { request }, headers = {}, url, auth }: HTTPRequest, { info, objectPath }: Pointer, ): Promise { + try { + const cached = await fsp.readFile(objectPath); + if (cached.byteLength === info.size) { + return cached; + } + } catch (e) { + // Silence file not found errors (implies cache miss) + if ((e as any).code !== 'ENOENT') { + throw e; + } + } + const authHeaders: Record = auth ? getAuthHeader(auth) : {};