Skip to content

Commit 69f5661

Browse files
authored
Git LFS support for Live View
Enables loading files stored with Git LFS in Live View.
2 parents 16f486c + 6909d5a commit 69f5661

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

git/gitapi.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,39 @@ let git = {
150150

151151
return buffer;
152152

153+
},
154+
155+
// get public LFS file content as ReadableStream
156+
'getPublicLFSFileAsStream': async (treeLoc, fileName) => {
157+
158+
// map tree location
159+
let query = 'https://media.githubusercontent.com/media';
160+
const [user, repo, contents] = treeLoc;
161+
162+
// get repository branch
163+
let [repoName, branch] = repo.split(':');
164+
165+
query += '/' + user + '/' + repoName +
166+
'/' + branch +
167+
'/' + contents + '/' + fileName;
168+
169+
// get the query
170+
const resp = await fetch(query);
171+
172+
// if received an error
173+
if (String(resp.status).startsWith('4')) {
174+
175+
return {
176+
errorCode: resp.status
177+
};
178+
179+
}
180+
181+
182+
const buffer = await resp.arrayBuffer();
183+
184+
return buffer;
185+
153186
},
154187

155188
// get items in tree
@@ -184,7 +217,7 @@ let git = {
184217
return resp;
185218

186219
},
187-
220+
188221
// get a repository
189222
'getRepo': async (treeLoc) => {
190223

live-view/live-view.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,7 @@ async function handleLiveViewRequest(requestPath) {
10091009

10101010
// get public file from git as ReadableStream
10111011
respObj = await git.getPublicFileAsStream(liveFileDir, fileName);
1012+
10121013

10131014
// if couldn't fetch file
10141015
if (respObj.errorCode) {
@@ -1023,6 +1024,35 @@ async function handleLiveViewRequest(requestPath) {
10231024
};
10241025

10251026
}
1027+
1028+
1029+
// if the file's stored with Git LFS
1030+
// (see: https://github.com/git-lfs/git-lfs/blob/main/docs/spec.md)
1031+
1032+
const fileContentStr = new TextDecoder().decode(respObj);
1033+
1034+
const isLFS = fileContentStr.startsWith('version https://git-lfs.github.com/spec/');
1035+
1036+
if (isLFS) {
1037+
1038+
respObj = await git.getPublicLFSFileAsStream(liveFileDir, fileName);
1039+
1040+
1041+
// if couldn't fetch file
1042+
if (respObj.errorCode) {
1043+
1044+
// return an error
1045+
1046+
const respStatus = respObj.errorCode;
1047+
1048+
return {
1049+
fileContent: '',
1050+
respStatus: respStatus
1051+
};
1052+
1053+
}
1054+
1055+
}
10261056

10271057
} else {
10281058

service-worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
// update worker name when updating cached files
3-
const WORKER_NAME = 'codeit-worker-v769';
3+
const WORKER_NAME = 'codeit-worker-v770';
44

55

66
self.importScripts('/worker/client-channel.js');

worker/client-channel.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const INTERNAL_PATHS = {
2222

2323
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
2424

25-
const isDev = (self.origin === 'https://dev.codeit.codes');
25+
const isDev = (self.location.origin === 'https://dev.codeit.codes');
2626

2727

2828
// key : value
@@ -295,4 +295,3 @@ self.addEventListener('fetch', (evt) => {
295295
evt.respondWith(handleFetchRequest(evt.request, evt));
296296

297297
});
298-

0 commit comments

Comments
 (0)