From 734986ede31ef209c8bf68d088569f734dee2fb0 Mon Sep 17 00:00:00 2001 From: Nulo Date: Tue, 16 May 2023 19:46:18 -0300 Subject: [PATCH] 0.3.0 - usar fetch y coso --- package.json | 2 +- src/download.ts | 41 +++++++++++++---------------------------- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index d22200d..3aa2e96 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nulo/isogit-lfs", - "version": "0.2.9", + "version": "0.3.0", "description": "LFS helpers for Isomorphic Git", "main": "dist/index.js", "repository": "git@github.com:riboseinc/isogit-lfs.git", diff --git a/src/download.ts b/src/download.ts index d6adbb5..1e6ac94 100644 --- a/src/download.ts +++ b/src/download.ts @@ -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 { @@ -29,9 +29,10 @@ function isValidLFSInfoResponseData( */ export default async function downloadBlobFromPointer( fs: PromiseFsClient | null, - { http: { request }, headers = {}, url, auth }: HTTPRequest, + url: string, + auth: BasicAuth, { info, objectPath }: Pointer -): Promise { +): Promise { if (fs) { try { const cached = await fs?.promises.readFile(objectPath); @@ -56,29 +57,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 @@ -87,19 +77,14 @@ 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 bodyToBuffer(lfsObjectBody); + const blob = await lfsObjectRes.blob(); if (fs) { // Write LFS cache for this object, if cache path is accessible.