fix: Add JSON sanitization to handle NaN values in streaming requests (#8736)
🔧 (api.tsx): add a helper function sanitizeJsonString to replace NaN values with null in JSON strings for proper parsing
This commit is contained in:
parent
ceb98af542
commit
d2d774d11c
1 changed files with 14 additions and 2 deletions
|
|
@ -272,6 +272,16 @@ export type StreamingRequestParams = {
|
||||||
eventDeliveryConfig?: EventDeliveryType;
|
eventDeliveryConfig?: EventDeliveryType;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Helper function to sanitize JSON strings
|
||||||
|
function sanitizeJsonString(jsonStr: string): string {
|
||||||
|
// Replace NaN with null (valid JSON)
|
||||||
|
return jsonStr
|
||||||
|
.replace(/:\s*NaN\b/g, ": null")
|
||||||
|
.replace(/\[\s*NaN\s*\]/g, "[null]")
|
||||||
|
.replace(/,\s*NaN\s*,/g, ", null,")
|
||||||
|
.replace(/,\s*NaN\s*\]/g, ", null]");
|
||||||
|
}
|
||||||
|
|
||||||
async function performStreamingRequest({
|
async function performStreamingRequest({
|
||||||
method,
|
method,
|
||||||
url,
|
url,
|
||||||
|
|
@ -323,7 +333,8 @@ async function performStreamingRequest({
|
||||||
const allString = current.join("") + string;
|
const allString = current.join("") + string;
|
||||||
let data: object;
|
let data: object;
|
||||||
try {
|
try {
|
||||||
data = JSON.parse(allString);
|
const sanitizedJson = sanitizeJsonString(allString);
|
||||||
|
data = JSON.parse(sanitizedJson);
|
||||||
current = [];
|
current = [];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
current.push(string);
|
current.push(string);
|
||||||
|
|
@ -342,7 +353,8 @@ async function performStreamingRequest({
|
||||||
if (current.length > 0) {
|
if (current.length > 0) {
|
||||||
const allString = current.join("");
|
const allString = current.join("");
|
||||||
if (allString) {
|
if (allString) {
|
||||||
const data = JSON.parse(current.join(""));
|
const sanitizedJson = sanitizeJsonString(allString);
|
||||||
|
const data = JSON.parse(sanitizedJson);
|
||||||
await onData(data);
|
await onData(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue