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,7 +498,9 @@ 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) => {
try {
const chunk = JSON5.parse(chunk_json);
event_callbacks.map((f, ix) => { event_callbacks.map((f, ix) => {
f(chunk) f(chunk)
.then(() => { .then(() => {
@ -511,11 +512,17 @@ export const uploadFiles = async (
.catch((e) => { .catch((e) => {
if (progressEvent.progress === 1) { if (progressEvent.progress === 1) {
// Chunk may be incomplete, so only report errors when full response is available. // Chunk may be incomplete, so only report errors when full response is available.
console.log("Error parsing chunk", chunk, e); console.log("Error processing chunk", chunk, e);
} }
return; return;
}); });
}); });
} catch (e) {
if (progressEvent.progress === 1) {
console.log("Error parsing chunk", chunk_json, e);
}
return;
}
}); });
}; };