fix(interceptor): guard XHR double-wrap and thread currentAttr into regex fallback
This commit is contained in:
+9
-7
@@ -14,7 +14,7 @@ export function startRequestInterceptor({ prevValue, currentAttr, onTrain, onPar
|
|||||||
let warnedFor = null;
|
let warnedFor = null;
|
||||||
|
|
||||||
const handle = (text, url) => {
|
const handle = (text, url) => {
|
||||||
const parsed = parseTrainResponse(text, url);
|
const parsed = parseTrainResponse(text, url, currentAttr);
|
||||||
if (!parsed) {
|
if (!parsed) {
|
||||||
if (warnedFor !== url) {
|
if (warnedFor !== url) {
|
||||||
warnedFor = url;
|
warnedFor = url;
|
||||||
@@ -22,7 +22,7 @@ export function startRequestInterceptor({ prevValue, currentAttr, onTrain, onPar
|
|||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (parsed.attr !== currentAttr) return;
|
if (parsed.attr && currentAttr && parsed.attr !== currentAttr) return;
|
||||||
const delta = parsed.newValue - lastValue;
|
const delta = parsed.newValue - lastValue;
|
||||||
lastValue = parsed.newValue;
|
lastValue = parsed.newValue;
|
||||||
if (delta > 0) onTrain({ attr: parsed.attr, delta, ts: Date.now() });
|
if (delta > 0) onTrain({ attr: parsed.attr, delta, ts: Date.now() });
|
||||||
@@ -36,7 +36,7 @@ export function startRequestInterceptor({ prevValue, currentAttr, onTrain, onPar
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseTrainResponse(text, url) {
|
function parseTrainResponse(text, url, fallbackAttr) {
|
||||||
// Try JSON
|
// Try JSON
|
||||||
try {
|
try {
|
||||||
const j = JSON.parse(text);
|
const j = JSON.parse(text);
|
||||||
@@ -48,20 +48,21 @@ function parseTrainResponse(text, url) {
|
|||||||
}
|
}
|
||||||
} catch { /* not JSON */ }
|
} catch { /* not JSON */ }
|
||||||
|
|
||||||
// Fallback: scan text for a number formatted like an attribute.
|
// Regex fallback: scan text for a number formatted like an attribute.
|
||||||
|
// If we find one and the caller passed a fallbackAttr, use it; otherwise
|
||||||
|
// the caller can choose to ignore the result.
|
||||||
const m = text.match(/(\d{1,3}(?:,\d{3})+|\d{4,})/);
|
const m = text.match(/(\d{1,3}(?:,\d{3})+|\d{4,})/);
|
||||||
if (m) {
|
if (m) {
|
||||||
const newValue = parseInt(m[1].replace(/,/g, ''), 10);
|
const newValue = parseInt(m[1].replace(/,/g, ''), 10);
|
||||||
if (Number.isFinite(newValue) && newValue > 0) {
|
if (Number.isFinite(newValue) && newValue > 0) {
|
||||||
// Without a confirmed attr, fall back to whatever currentAttr
|
return { newValue, attr: fallbackAttr || null };
|
||||||
// the caller passed in.
|
|
||||||
return { newValue, attr: null };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapXhr(handle) {
|
function wrapXhr(handle) {
|
||||||
|
if (XMLHttpRequest.prototype.send.__tatWrapped) return;
|
||||||
const origOpen = XMLHttpRequest.prototype.open;
|
const origOpen = XMLHttpRequest.prototype.open;
|
||||||
const origSend = XMLHttpRequest.prototype.send;
|
const origSend = XMLHttpRequest.prototype.send;
|
||||||
XMLHttpRequest.prototype.open = function (method, url) {
|
XMLHttpRequest.prototype.open = function (method, url) {
|
||||||
@@ -74,6 +75,7 @@ function wrapXhr(handle) {
|
|||||||
});
|
});
|
||||||
return origSend.apply(this, arguments);
|
return origSend.apply(this, arguments);
|
||||||
};
|
};
|
||||||
|
XMLHttpRequest.prototype.send.__tatWrapped = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapFetch(handle) {
|
function wrapFetch(handle) {
|
||||||
|
|||||||
Reference in New Issue
Block a user