feat(download): return cached object, if possible

This commit is contained in:
Anton Strogonoff 2021-12-02 14:15:13 +01:00
parent 2d6eed4c83
commit 0945e2659b

View file

@ -24,12 +24,25 @@ function isValidLFSInfoResponseData(val: Record<string, any>): val is LFSInfoRes
/** /**
* Downloads, caches and returns a blob corresponding to given LFS pointer. * Downloads, caches and returns a blob corresponding to given LFS pointer.
* Uses already cached object, if size matches.
*/ */
export default async function downloadBlobFromPointer( export default async function downloadBlobFromPointer(
{ http: { request }, headers = {}, url, auth }: HTTPRequest, { http: { request }, headers = {}, url, auth }: HTTPRequest,
{ info, objectPath }: Pointer, { info, objectPath }: Pointer,
): Promise<Buffer> { ): Promise<Buffer> {
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<string, string> = auth const authHeaders: Record<string, string> = auth
? getAuthHeader(auth) ? getAuthHeader(auth)
: {}; : {};