forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-download-directory.js
10 lines (10 loc) · 5 KB
/
gh-download-directory.js
1
2
3
4
5
6
7
8
9
10
const exports = {};
const module = {exports};
var parcelRequire=function(e,r,t,n){var i,o="function"==typeof parcelRequire&&parcelRequire,u="function"==typeof require&&require;function f(t,n){if(!r[t]){if(!e[t]){var i="function"==typeof parcelRequire&&parcelRequire;if(!n&&i)return i(t,!0);if(o)return o(t,!0);if(u&&"string"==typeof t)return u(t);var c=new Error("Cannot find module '"+t+"'");throw c.code="MODULE_NOT_FOUND",c}p.resolve=function(r){return e[t][1][r]||r},p.cache={};var l=r[t]=new f.Module(t);e[t][0].call(l.exports,p,l,l.exports,this)}return r[t].exports;function p(e){return f(p.resolve(e))}}f.isParcelRequire=!0,f.Module=function(e){this.id=e,this.bundle=f,this.exports={}},f.modules=e,f.cache=r,f.parent=o,f.register=function(r,t){e[r]=[function(e,r){r.exports=t},{}]};for(var c=0;c<t.length;c++)try{f(t[c])}catch(e){i||(i=e)}if(t.length){var l=f(t[t.length-1]);"object"==typeof exports&&"undefined"!=typeof module?module.exports=l:"function"==typeof define&&define.amd?define(function(){return l}):n&&(this[n]=l)}if(parcelRequire=f,i)throw i;return f}({"Yf0o":[function(require,module,exports) {
module.exports=fetch;
},{}],"B0HW":[function(require,module,exports) {
const e=require("node-fetch");async function t(t,r){return(await e(`https://api.github.com/repos/${t}`,{headers:r?{Authorization:`Bearer ${r}`}:void 0})).json()}async function r({user:e,repository:o,ref:s="HEAD",directory:a,token:i,getFullData:n=!1}){const u=[],p=[],c=await t(`${e}/${o}/contents/${a}?ref=${s}`,i);if("Not Found"===c.message)return[];if(c.message)throw new Error(c.message);for(const t of c)"file"===t.type?u.push(n?t:t.path):"dir"===t.type&&p.push(r({user:e,repository:o,ref:s,directory:t.path,token:i,getFullData:n}));return u.concat(...await Promise.all(p))}async function o({user:e,repository:r,ref:o="HEAD",directory:s,token:a,getFullData:i=!1}){s.endsWith("/")||(s+="/");const n=[],u=await t(`${e}/${r}/git/trees/${o}?recursive=1`,a);if(u.message)throw new Error(u.message);for(const t of u.tree)"blob"===t.type&&t.path.startsWith(s)&&n.push(i?t:t.path);return n.truncated=u.truncated,n}module.exports.viaContentsApi=r,module.exports.viaContentApi=r,module.exports.viaTreesApi=o,module.exports.viaTreeApi=o;
},{"node-fetch":"Yf0o"}],"Focm":[function(require,module,exports) {
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var e=t(require("list-github-dir-content"));function t(e){return e&&e.__esModule?e:{default:e}}const o=/^[/]([^/]+)[/]([^/]+)[/]tree[/]([^/]+)[/](.*)/;function n(e,...t){}async function r(){const e=document.querySelector("#token");if(e.addEventListener("input",()=>{e.checkValidity()&&(localStorage.token=e.value)}),localStorage.token)e.value=localStorage.token;else{const t=document.querySelector("#token-toggle");t.checked=!0,n("Waiting for token..."),await new Promise(o=>{e.addEventListener("input",function n(){e.checkValidity()&&(t.checked=!1,o(),e.removeEventListener("input",n))})})}}async function a(e){const t=await fetch(`https://api.github.com/repos/${e}`,localStorage.token?{headers:{Authorization:`Bearer ${localStorage.token}`}}:{});switch(t.status){case 401:throw n("⚠ The token provided is invalid or has been revoked.",{token:localStorage.token}),new Error("Invalid token");case 403:if("0"===t.headers.get("X-RateLimit-Remaining"))throw n("⚠ Your token rate limit has been exceeded.",{token:localStorage.token}),new Error("Rate limit exceeded");break;case 404:throw n("⚠ Repository was not found.",{repo:e}),new Error("Repository not found")}if(!t.ok)throw n("⚠ Could not obtain repository data from the GitHub API.",{repo:e,response:t}),new Error("Fetch error");return t.json()}async function i(){const{default:e}=await import("https://lib.webaverse.com/jszip.js");return new e}async function s(t){i();let s,c,l,u;try{const e=new URL(t);[,s,c,l,u]=o.exec(e.pathname)}catch{return n()}if(!navigator.onLine)throw n("⚠ You are offline."),new Error("You are offline");n("Retrieving directory info…");const{private:d}=await a(`${s}/${c}`),h=await e.default.viaTreesApi({user:s,repository:c,ref:l,directory:decodeURIComponent(u),token:localStorage.token,getFullData:!0});if(0===h.length)return void n("No files to download");n(`Downloading (0/${h.length}) files…`,"\n• "+h.map(e=>e.path).join("\n• "));const f=new AbortController;let p=0;return d&&await r(),await Promise.all(h.map(async e=>{const t=d?await(async e=>{const t=await fetch(e.url,{headers:{Authorization:`Bearer ${localStorage.token}`},signal:f.signal});if(!t.ok)throw new Error(`HTTP ${t.statusText} for ${e.path}`);const{content:o}=await t.json();return(await fetch(`data:application/octet-stream;base64,${o}`)).blob()})(e):await(async e=>{const t=await fetch(`https://raw.githubusercontent.com/${s}/${c}/${l}/${e.path}`,{signal:f.signal});if(!t.ok)throw new Error(`HTTP ${t.statusText} for ${e.path}`);return t.blob()})(e);return n(`Downloading (${++p}/${h.length}) files…`,e.path),{path:e.path.replace(u+"/",""),blob:t}}))}var c=s;exports.default=c;
},{"list-github-dir-content":"B0HW"}]},{},["Focm"], null)
export default module.exports;