MCPcopy Index your code
hub / github.com/EmulatorJS/EmulatorJS / downloadFile

Method downloadFile

data/src/emulator.js:92–161  ·  view source on GitHub ↗
(path, progressCB, notWithPath, opts)

Source from the content-addressed store, hash-verified

90 }
91 }
92 downloadFile(path, progressCB, notWithPath, opts) {
93 return new Promise(async cb => {
94 const data = this.toData(path); //check other data types
95 if (data) {
96 data.then((game) => {
97 if (opts.method === "HEAD") {
98 cb({ headers: {} });
99 } else {
100 cb({ headers: {}, data: game });
101 }
102 })
103 return;
104 }
105 const basePath = notWithPath ? "" : this.config.dataPath;
106 path = basePath + path;
107 if (!notWithPath && this.config.filePaths && typeof this.config.filePaths[path.split("/").pop()] === "string") {
108 path = this.config.filePaths[path.split("/").pop()];
109 }
110 let url;
111 try { url = new URL(path) } catch(e) {};
112 if (url && !["http:", "https:"].includes(url.protocol)) {
113 //Most commonly blob: urls. Not sure what else it could be
114 if (opts.method === "HEAD") {
115 cb({ headers: {} });
116 return;
117 }
118 try {
119 let res = await fetch(path)
120 if ((opts.type && opts.type.toLowerCase() === "arraybuffer") || !opts.type) {
121 res = await res.arrayBuffer();
122 } else {
123 res = await res.text();
124 try { res = JSON.parse(res) } catch(e) {}
125 }
126 if (path.startsWith("blob:")) URL.revokeObjectURL(path);
127 cb({ data: res, headers: {} });
128 } catch(e) {
129 cb(-1);
130 }
131 return;
132 }
133 const xhr = new XMLHttpRequest();
134 if (progressCB instanceof Function) {
135 xhr.addEventListener("progress", (e) => {
136 const progress = e.total ? " " + Math.floor(e.loaded / e.total * 100).toString() + "%" : " " + (e.loaded / 1048576).toFixed(2) + "MB";
137 progressCB(progress);
138 });
139 }
140 xhr.onload = function() {
141 if (xhr.readyState === xhr.DONE) {
142 let data = xhr.response;
143 if (xhr.status.toString().startsWith("4") || xhr.status.toString().startsWith("5")) {
144 cb(-1);
145 return;
146 }
147 try { data = JSON.parse(data) } catch(e) {}
148 cb({
149 data: data,

Callers 7

downloadGameCoreMethod · 0.95
downloadStartStateMethod · 0.95
downloadGameFileMethod · 0.95
downloadRomMethod · 0.95
loadExternalFilesMethod · 0.80
loadPpssppAssetsMethod · 0.80
getWorkerFileMethod · 0.80

Calls 6

toDataMethod · 0.95
localizationMethod · 0.95
startGameErrorMethod · 0.95
addEventListenerMethod · 0.80
putMethod · 0.45

Tested by

no test coverage detected