|
|
|
@ -3,7 +3,7 @@ import { Buffer } from "buffer";
|
|
|
|
|
|
|
|
|
|
import { bodyToBuffer, getAuthHeader, isWriteable } from "./util";
|
|
|
|
|
import { Pointer } from "./pointers";
|
|
|
|
|
import { HTTPRequest } from "./types";
|
|
|
|
|
import { HTTPRequest, BasicAuth } from "./types";
|
|
|
|
|
import { PromiseFsClient } from "isomorphic-git";
|
|
|
|
|
|
|
|
|
|
interface LFSInfoResponse {
|
|
|
|
@ -28,23 +28,27 @@ function isValidLFSInfoResponseData(
|
|
|
|
|
* Uses already cached object, if size matches.
|
|
|
|
|
*/
|
|
|
|
|
export default async function downloadBlobFromPointer(
|
|
|
|
|
{ promises: fs }: PromiseFsClient,
|
|
|
|
|
{ http: { request }, headers = {}, url, auth }: HTTPRequest,
|
|
|
|
|
fs: PromiseFsClient | null,
|
|
|
|
|
url: string,
|
|
|
|
|
auth: BasicAuth | {},
|
|
|
|
|
{ info, objectPath }: Pointer
|
|
|
|
|
): Promise<Buffer> {
|
|
|
|
|
try {
|
|
|
|
|
const cached = await fs.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;
|
|
|
|
|
): Promise<Blob> {
|
|
|
|
|
if (fs) {
|
|
|
|
|
try {
|
|
|
|
|
const cached = await fs?.promises.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 ? getAuthHeader(auth) : {};
|
|
|
|
|
const authHeaders: Record<string, string> =
|
|
|
|
|
"username" in auth ? getAuthHeader(auth) : {};
|
|
|
|
|
|
|
|
|
|
// Request LFS transfer
|
|
|
|
|
|
|
|
|
@ -54,29 +58,18 @@ export default async function downloadBlobFromPointer(
|
|
|
|
|
objects: [info],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { body: lfsInfoBody } = await request({
|
|
|
|
|
url: `${url}/info/lfs/objects/batch`,
|
|
|
|
|
const lfsInfoRes = await fetch(`${url}/info/lfs/objects/batch`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
// Github LFS doesn’t seem to accept this UA, but works fine without any
|
|
|
|
|
// 'User-Agent': `git/isomorphic-git@${git.version()}`,
|
|
|
|
|
...headers,
|
|
|
|
|
...authHeaders,
|
|
|
|
|
Accept: "application/vnd.git-lfs+json",
|
|
|
|
|
"Content-Type": "application/vnd.git-lfs+json",
|
|
|
|
|
},
|
|
|
|
|
body: [Buffer.from(JSON.stringify(lfsInfoRequestData))],
|
|
|
|
|
body: JSON.stringify(lfsInfoRequestData),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const lfsInfoResponseRaw = (await bodyToBuffer(lfsInfoBody)).toString();
|
|
|
|
|
let lfsInfoResponseData: any;
|
|
|
|
|
try {
|
|
|
|
|
lfsInfoResponseData = JSON.parse(lfsInfoResponseRaw);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`Unexpected structure received from LFS server: unable to parse JSON ${lfsInfoResponseRaw}`
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
const lfsInfoResponseData = await lfsInfoRes.json();
|
|
|
|
|
|
|
|
|
|
if (isValidLFSInfoResponseData(lfsInfoResponseData)) {
|
|
|
|
|
// Request the actual blob
|
|
|
|
@ -85,24 +78,21 @@ export default async function downloadBlobFromPointer(
|
|
|
|
|
const lfsObjectDownloadURL = downloadAction.href;
|
|
|
|
|
const lfsObjectDownloadHeaders = downloadAction.header ?? {};
|
|
|
|
|
|
|
|
|
|
const dlHeaders = {
|
|
|
|
|
...headers,
|
|
|
|
|
...authHeaders,
|
|
|
|
|
...lfsObjectDownloadHeaders,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const { body: lfsObjectBody } = await request({
|
|
|
|
|
url: lfsObjectDownloadURL,
|
|
|
|
|
const lfsObjectRes = await fetch(lfsObjectDownloadURL, {
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: dlHeaders,
|
|
|
|
|
headers: {
|
|
|
|
|
...authHeaders,
|
|
|
|
|
...lfsObjectDownloadHeaders,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const blob = await lfsObjectRes.blob();
|
|
|
|
|
|
|
|
|
|
const blob = await bodyToBuffer(lfsObjectBody);
|
|
|
|
|
|
|
|
|
|
// Write LFS cache for this object, if cache path is accessible.
|
|
|
|
|
if (await isWriteable({ promises: fs }, objectPath)) {
|
|
|
|
|
await fs.mkdir(path.dirname(objectPath), { recursive: true });
|
|
|
|
|
await fs.writeFile(objectPath, blob);
|
|
|
|
|
if (fs) {
|
|
|
|
|
// Write LFS cache for this object, if cache path is accessible.
|
|
|
|
|
if (await isWriteable(fs, objectPath)) {
|
|
|
|
|
await fs.promises.mkdir(path.dirname(objectPath), { recursive: true });
|
|
|
|
|
await fs.promises.writeFile(objectPath, blob);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return blob;
|
|
|
|
|