Enhance visit filtering and batching for remote data submission
This commit is contained in:
1 parent
0ca53e634c
commit
6badceaf46
1 file changed
+24
-2
@@ -24,16 +24,38 @@ async function archiveLocally(visits) {
|
|||||||
async function sendToCollector(visits) {
|
async function sendToCollector(visits) {
|
||||||
// Empty settings deliberately disable sending; the local SQLite archive still works.
|
// Empty settings deliberately disable sending; the local SQLite archive still works.
|
||||||
if (!CONFIG.collectorUrl || !CONFIG.apiToken) return true;
|
if (!CONFIG.collectorUrl || !CONFIG.apiToken) return true;
|
||||||
|
const remoteVisits = visits
|
||||||
|
.filter((visit) => {
|
||||||
|
if (typeof visit?.url !== "string") return false;
|
||||||
try {
|
try {
|
||||||
|
const protocol = new URL(visit.url).protocol;
|
||||||
|
return protocol === "http:" || protocol === "https:";
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.map((visit) => ({
|
||||||
|
...visit,
|
||||||
|
title: String(visit.title ?? "").slice(0, 1000)
|
||||||
|
}));
|
||||||
|
try {
|
||||||
|
for (let offset = 0; offset < remoteVisits.length; offset += 100) {
|
||||||
const response = await fetch(CONFIG.collectorUrl, {
|
const response = await fetch(CONFIG.collectorUrl, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"Authorization": `Bearer ${CONFIG.apiToken}`
|
"Authorization": `Bearer ${CONFIG.apiToken}`
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ device_id: CONFIG.deviceId, visits })
|
body: JSON.stringify({
|
||||||
|
device_id: CONFIG.deviceId,
|
||||||
|
visits: remoteVisits.slice(offset, offset + 100)
|
||||||
|
})
|
||||||
});
|
});
|
||||||
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
if (!response.ok) {
|
||||||
|
const detail = (await response.text()).trim().slice(0, 1000);
|
||||||
|
throw new Error(`HTTP ${response.status}${detail ? `: ${detail}` : ""}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("Collector unavailable", error.message);
|
console.warn("Collector unavailable", error.message);
|
||||||
|
|||||||
Reference in new issue
Block a user