JSON5.parse in on_upload_progress handler responses

This commit is contained in:
Masen Furer 2024-12-12 03:45:22 -08:00
parent 50a4cadfcf
commit f422157016
No known key found for this signature in database
GPG Key ID: B0008AD22B3B3A95

View File

@ -445,8 +445,7 @@ export const connect = async (
}); });
// On each received message, queue the updates and events. // On each received message, queue the updates and events.
socket.current.on("event", async (message) => { socket.current.on("event", async (update) => {
const update = message;
for (const substate in update.delta) { for (const substate in update.delta) {
dispatch[substate](update.delta[substate]); dispatch[substate](update.delta[substate]);
} }
@ -458,7 +457,7 @@ export const connect = async (
}); });
socket.current.on("reload", async (event) => { socket.current.on("reload", async (event) => {
event_processing = false; event_processing = false;
queueEvents([...initialEvents(), JSON5.parse(event)], socket); queueEvents([...initialEvents(), event], socket);
}); });
document.addEventListener("visibilitychange", checkVisibility); document.addEventListener("visibilitychange", checkVisibility);
@ -499,23 +498,31 @@ export const uploadFiles = async (
// Whenever called, responseText will contain the entire response so far. // Whenever called, responseText will contain the entire response so far.
const chunks = progressEvent.event.target.responseText.trim().split("\n"); const chunks = progressEvent.event.target.responseText.trim().split("\n");
// So only process _new_ chunks beyond resp_idx. // So only process _new_ chunks beyond resp_idx.
chunks.slice(resp_idx).map((chunk) => { chunks.slice(resp_idx).map((chunk_json) => {
event_callbacks.map((f, ix) => { try {
f(chunk) const chunk = JSON5.parse(chunk_json);
.then(() => { event_callbacks.map((f, ix) => {
if (ix === event_callbacks.length - 1) { f(chunk)
// Mark this chunk as processed. .then(() => {
resp_idx += 1; if (ix === event_callbacks.length - 1) {
} // Mark this chunk as processed.
}) resp_idx += 1;
.catch((e) => { }
if (progressEvent.progress === 1) { })
// Chunk may be incomplete, so only report errors when full response is available. .catch((e) => {
console.log("Error parsing chunk", chunk, e); if (progressEvent.progress === 1) {
} // Chunk may be incomplete, so only report errors when full response is available.
return; console.log("Error processing chunk", chunk, e);
}); }
}); return;
});
});
} catch (e) {
if (progressEvent.progress === 1) {
console.log("Error parsing chunk", chunk_json, e);
}
return;
}
}); });
}; };