feat(download): return cached object, if possible
This commit is contained in:
parent
2d6eed4c83
commit
0945e2659b
1 changed files with 13 additions and 0 deletions
|
@ -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)
|
||||||
: {};
|
: {};
|
||||||
|
|
Loading…
Reference in a new issue