File size: 13,488 Bytes
0d97019 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
var Fetch = {
async openDatabase(dbname, dbversion) {
return new Promise((resolve, reject) => {
try {
var openRequest = indexedDB.open(dbname, dbversion)
} catch (e) {
return reject(e)
}
openRequest.onupgradeneeded = event => {
var db = event.target.result;
if (db.objectStoreNames.contains("FILES")) {
db.deleteObjectStore("FILES")
}
db.createObjectStore("FILES")
};
openRequest.onsuccess = event => resolve(event.target.result);
openRequest.onerror = reject
})
},
async init() {
Fetch.xhrs = new HandleAllocator;
addRunDependency("library_fetch_init");
try {
var db = await Fetch.openDatabase("emscripten_filesystem", 1);
Fetch.dbInstance = db
} catch (e) {
Fetch.dbInstance = false
} finally {
removeRunDependency("library_fetch_init")
}
}
};
function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
var url = HEAPU32[fetch + 8 >> 2];
if (!url) {
onerror(fetch, "no url specified!");
return
}
var url_ = replaceFetch(UTF8ToString(url));
var fetch_attr = fetch + 108;
var requestMethod = UTF8ToString(fetch_attr + 0);
requestMethod ||= "GET";
var timeoutMsecs = HEAPU32[fetch_attr + 56 >> 2];
var userName = HEAPU32[fetch_attr + 68 >> 2];
var password = HEAPU32[fetch_attr + 72 >> 2];
var requestHeaders = HEAPU32[fetch_attr + 76 >> 2];
var overriddenMimeType = HEAPU32[fetch_attr + 80 >> 2];
var dataPtr = HEAPU32[fetch_attr + 84 >> 2];
var dataLength = HEAPU32[fetch_attr + 88 >> 2];
var fetchAttributes = HEAPU32[fetch_attr + 52 >> 2];
var fetchAttrLoadToMemory = !!(fetchAttributes & 1);
var fetchAttrStreamData = !!(fetchAttributes & 2);
var fetchAttrSynchronous = !!(fetchAttributes & 64);
var userNameStr = userName ? UTF8ToString(userName) : undefined;
var passwordStr = password ? UTF8ToString(password) : undefined;
var xhr = new XMLHttpRequest;
xhr.withCredentials = !!HEAPU8[fetch_attr + 60];
xhr.open(requestMethod, url_, !fetchAttrSynchronous, userNameStr, passwordStr);
if (!fetchAttrSynchronous) xhr.timeout = timeoutMsecs;
xhr.url_ = url_;
xhr.responseType = "arraybuffer";
if (overriddenMimeType) {
var overriddenMimeTypeStr = UTF8ToString(overriddenMimeType);
xhr.overrideMimeType(overriddenMimeTypeStr)
}
if (requestHeaders) {
for (;;) {
var key = HEAPU32[requestHeaders >> 2];
if (!key) break;
var value = HEAPU32[requestHeaders + 4 >> 2];
if (!value) break;
requestHeaders += 8;
var keyStr = UTF8ToString(key);
var valueStr = UTF8ToString(value);
xhr.setRequestHeader(keyStr, valueStr)
}
}
var id = Fetch.xhrs.allocate(xhr);
HEAPU32[fetch >> 2] = id;
var data = dataPtr && dataLength ? HEAPU8.slice(dataPtr, dataPtr + dataLength) : null;
function saveResponseAndStatus() {
var ptr = 0;
var ptrLen = 0;
if (xhr.response && fetchAttrLoadToMemory && HEAPU32[fetch + 12 >> 2] === 0) {
ptrLen = xhr.response.byteLength
}
if (ptrLen > 0) {
ptr = _realloc(HEAPU32[fetch + 12 >> 2], ptrLen);
HEAPU8.set(new Uint8Array(xhr.response), ptr)
}
HEAPU32[fetch + 12 >> 2] = ptr;
writeI53ToI64(fetch + 16, ptrLen);
writeI53ToI64(fetch + 24, 0);
var len = xhr.response ? xhr.response.byteLength : 0;
if (len) {
writeI53ToI64(fetch + 32, len)
}
HEAP16[fetch + 40 >> 1] = xhr.readyState;
HEAP16[fetch + 42 >> 1] = xhr.status;
if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + 44, 64);
if (fetchAttrSynchronous) {
var ruPtr = stringToNewUTF8(xhr.responseURL);
HEAPU32[fetch + 200 >> 2] = ruPtr
}
}
xhr.onload = e => {
if (!Fetch.xhrs.has(id)) {
return
}
saveResponseAndStatus();
if (xhr.status >= 200 && xhr.status < 300) {
onsuccess(fetch, xhr, e)
} else {
onerror(fetch, e)
}
};
xhr.onerror = e => {
if (!Fetch.xhrs.has(id)) {
return
}
saveResponseAndStatus();
onerror(fetch, e)
};
xhr.ontimeout = e => {
if (!Fetch.xhrs.has(id)) {
return
}
onerror(fetch, e)
};
xhr.onprogress = e => {
if (!Fetch.xhrs.has(id)) {
return
}
var ptrLen = fetchAttrLoadToMemory && fetchAttrStreamData && xhr.response ? xhr.response.byteLength : 0;
var ptr = 0;
if (ptrLen > 0 && fetchAttrLoadToMemory && fetchAttrStreamData) {
ptr = _realloc(HEAPU32[fetch + 12 >> 2], ptrLen);
HEAPU8.set(new Uint8Array(xhr.response), ptr)
}
HEAPU32[fetch + 12 >> 2] = ptr;
writeI53ToI64(fetch + 16, ptrLen);
writeI53ToI64(fetch + 24, e.loaded - ptrLen);
writeI53ToI64(fetch + 32, e.total);
HEAP16[fetch + 40 >> 1] = xhr.readyState;
var status = xhr.status;
if (xhr.readyState >= 3 && xhr.status === 0 && e.loaded > 0) status = 200;
HEAP16[fetch + 42 >> 1] = status;
if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + 44, 64);
onprogress(fetch, e)
};
xhr.onreadystatechange = e => {
if (!Fetch.xhrs.has(id)) {
return
}
HEAP16[fetch + 40 >> 1] = xhr.readyState;
if (xhr.readyState >= 2) {
HEAP16[fetch + 42 >> 1] = xhr.status
}
if (!fetchAttrSynchronous && (xhr.readyState === 2 && xhr.responseURL.length > 0)) {
var ruPtr = stringToNewUTF8(xhr.responseURL);
HEAPU32[fetch + 200 >> 2] = ruPtr
}
onreadystatechange(fetch, e)
};
try {
xhr.send(data)
} catch (e) {
onerror(fetch, e)
}
}
function fetchCacheData(db, fetch, data, onsuccess, onerror) {
if (!db) {
onerror(fetch, 0, "IndexedDB not available!");
return
}
var fetch_attr = fetch + 108;
var destinationPath = HEAPU32[fetch_attr + 64 >> 2];
destinationPath ||= HEAPU32[fetch + 8 >> 2];
var destinationPathStr = UTF8ToString(destinationPath);
try {
var transaction = db.transaction(["FILES"], "readwrite");
var packages = transaction.objectStore("FILES");
var putRequest = packages.put(data, destinationPathStr);
putRequest.onsuccess = event => {
HEAP16[fetch + 40 >> 1] = 4;
HEAP16[fetch + 42 >> 1] = 200;
stringToUTF8("OK", fetch + 44, 64);
onsuccess(fetch, 0, destinationPathStr)
};
putRequest.onerror = error => {
HEAP16[fetch + 40 >> 1] = 4;
HEAP16[fetch + 42 >> 1] = 413;
stringToUTF8("Payload Too Large", fetch + 44, 64);
onerror(fetch, 0, error)
}
} catch (e) {
onerror(fetch, 0, e)
}
}
function fetchLoadCachedData(db, fetch, onsuccess, onerror) {
if (!db) {
onerror(fetch, 0, "IndexedDB not available!");
return
}
var fetch_attr = fetch + 108;
var path = HEAPU32[fetch_attr + 64 >> 2];
path ||= HEAPU32[fetch + 8 >> 2];
var pathStr = UTF8ToString(path);
try {
var transaction = db.transaction(["FILES"], "readonly");
var packages = transaction.objectStore("FILES");
var getRequest = packages.get(pathStr);
getRequest.onsuccess = event => {
if (event.target.result) {
var value = event.target.result;
var len = value.byteLength || value.length;
var ptr = _malloc(len);
HEAPU8.set(new Uint8Array(value), ptr);
HEAPU32[fetch + 12 >> 2] = ptr;
writeI53ToI64(fetch + 16, len);
writeI53ToI64(fetch + 24, 0);
writeI53ToI64(fetch + 32, len);
HEAP16[fetch + 40 >> 1] = 4;
HEAP16[fetch + 42 >> 1] = 200;
stringToUTF8("OK", fetch + 44, 64);
onsuccess(fetch, 0, value)
} else {
HEAP16[fetch + 40 >> 1] = 4;
HEAP16[fetch + 42 >> 1] = 404;
stringToUTF8("Not Found", fetch + 44, 64);
onerror(fetch, 0, "no data")
}
};
getRequest.onerror = error => {
HEAP16[fetch + 40 >> 1] = 4;
HEAP16[fetch + 42 >> 1] = 404;
stringToUTF8("Not Found", fetch + 44, 64);
onerror(fetch, 0, error)
}
} catch (e) {
onerror(fetch, 0, e)
}
}
function fetchDeleteCachedData(db, fetch, onsuccess, onerror) {
if (!db) {
onerror(fetch, 0, "IndexedDB not available!");
return
}
var fetch_attr = fetch + 108;
var path = HEAPU32[fetch_attr + 64 >> 2];
path ||= HEAPU32[fetch + 8 >> 2];
var pathStr = UTF8ToString(path);
try {
var transaction = db.transaction(["FILES"], "readwrite");
var packages = transaction.objectStore("FILES");
var request = packages.delete(pathStr);
request.onsuccess = event => {
var value = event.target.result;
HEAPU32[fetch + 12 >> 2] = 0;
writeI53ToI64(fetch + 16, 0);
writeI53ToI64(fetch + 24, 0);
writeI53ToI64(fetch + 32, 0);
HEAP16[fetch + 40 >> 1] = 4;
HEAP16[fetch + 42 >> 1] = 200;
stringToUTF8("OK", fetch + 44, 64);
onsuccess(fetch, 0, value)
};
request.onerror = error => {
HEAP16[fetch + 40 >> 1] = 4;
HEAP16[fetch + 42 >> 1] = 404;
stringToUTF8("Not Found", fetch + 44, 64);
onerror(fetch, 0, error)
}
} catch (e) {
onerror(fetch, 0, e)
}
}
function _emscripten_start_fetch(fetch, successcb, errorcb, progresscb, readystatechangecb) {
var fetch_attr = fetch + 108;
var onsuccess = HEAPU32[fetch_attr + 36 >> 2];
var onerror = HEAPU32[fetch_attr + 40 >> 2];
var onprogress = HEAPU32[fetch_attr + 44 >> 2];
var onreadystatechange = HEAPU32[fetch_attr + 48 >> 2];
var fetchAttributes = HEAPU32[fetch_attr + 52 >> 2];
var fetchAttrSynchronous = !!(fetchAttributes & 64);
function doCallback(f) {
if (fetchAttrSynchronous) {
f()
} else {
callUserCallback(f)
}
}
var reportSuccess = (fetch, xhr, e) => {
doCallback(() => {
if (onsuccess) getWasmTableEntry(onsuccess)(fetch);
else successcb?.(fetch)
})
};
var reportProgress = (fetch, e) => {
doCallback(() => {
if (onprogress) getWasmTableEntry(onprogress)(fetch);
else progresscb?.(fetch)
})
};
var reportError = (fetch, e) => {
doCallback(() => {
if (onerror) getWasmTableEntry(onerror)(fetch);
else errorcb?.(fetch)
})
};
var reportReadyStateChange = (fetch, e) => {
doCallback(() => {
if (onreadystatechange) getWasmTableEntry(onreadystatechange)(fetch);
else readystatechangecb?.(fetch)
})
};
var performUncachedXhr = (fetch, xhr, e) => {
fetchXHR(fetch, reportSuccess, reportError, reportProgress, reportReadyStateChange)
};
var cacheResultAndReportSuccess = (fetch, xhr, e) => {
var storeSuccess = (fetch, xhr, e) => {
doCallback(() => {
if (onsuccess) getWasmTableEntry(onsuccess)(fetch);
else successcb?.(fetch)
})
};
var storeError = (fetch, xhr, e) => {
doCallback(() => {
if (onsuccess) getWasmTableEntry(onsuccess)(fetch);
else successcb?.(fetch)
})
};
fetchCacheData(Fetch.dbInstance, fetch, xhr.response, storeSuccess, storeError)
};
var performCachedXhr = (fetch, xhr, e) => {
fetchXHR(fetch, cacheResultAndReportSuccess, reportError, reportProgress, reportReadyStateChange)
};
var requestMethod = UTF8ToString(fetch_attr + 0);
var fetchAttrReplace = !!(fetchAttributes & 16);
var fetchAttrPersistFile = !!(fetchAttributes & 4);
var fetchAttrNoDownload = !!(fetchAttributes & 32);
if (requestMethod === "EM_IDB_STORE") {
var ptr = HEAPU32[fetch_attr + 84 >> 2];
var size = HEAPU32[fetch_attr + 88 >> 2];
fetchCacheData(Fetch.dbInstance, fetch, HEAPU8.slice(ptr, ptr + size), reportSuccess, reportError)
} else if (requestMethod === "EM_IDB_DELETE") {
fetchDeleteCachedData(Fetch.dbInstance, fetch, reportSuccess, reportError)
} else if (!fetchAttrReplace) {
fetchLoadCachedData(Fetch.dbInstance, fetch, reportSuccess, fetchAttrNoDownload ? reportError : fetchAttrPersistFile ? performCachedXhr : performUncachedXhr)
} else if (!fetchAttrNoDownload) {
fetchXHR(fetch, fetchAttrPersistFile ? cacheResultAndReportSuccess : reportSuccess, reportError, reportProgress, reportReadyStateChange)
} else {
return 0
}
return fetch
}
|