Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

sanitizedResponseText

Steven Teyc35103beb0d9
diff --git a/apps/web/app/api/cron/import/bitly/fetch-utils.ts b/apps/web/app/api/cron/import/bitly/fetch-utils.ts
index fb99566251..fda484188c 100644
--- a/apps/web/app/api/cron/import/bitly/fetch-utils.ts
+++ b/apps/web/app/api/cron/import/bitly/fetch-utils.ts
@@ -88,13 +88,14 @@ const fetchBitlyLinksStandard = async ({
// Get response as text first
const responseText = await response.text();
+ const sanitizedResponseText = sanitizeBitlyJson(responseText);
let data;
try {
// Sanitize the JSON and then parse it
- data = JSON.parse(sanitizeBitlyJson(responseText));
+ data = JSON.parse(sanitizedResponseText);
} catch (error) {
console.error("JSON parsing error:", error);
- console.error(`Failed to parse response: ${responseText}`);
+ console.error(`Failed to parse response: ${sanitizedResponseText}`);
throw new Error("Failed to parse JSON response from Bitly API");
}
@@ -176,13 +177,14 @@ const fetchBitlyLinksBatch = async ({
// Get response as text first
const responseText = await response.text();
+ const sanitizedResponseText = sanitizeBitlyJson(responseText);
let data;
try {
// Sanitize the JSON and then parse it
- data = JSON.parse(sanitizeBitlyJson(responseText));
+ data = JSON.parse(sanitizedResponseText);
} catch (error) {
console.error("JSON parsing error:", error);
- console.error(`Failed to parse response: ${responseText}`);
+ console.error(`Failed to parse response: ${sanitizedResponseText}`);
if (i === 0) {
throw new Error("Failed to parse JSON response from Bitly API");
diff --git a/apps/web/app/api/cron/import/bitly/sanitize-json.ts b/apps/web/app/api/cron/import/bitly/sanitize-json.ts
index a2064f1d8b..66099bf68d 100644
--- a/apps/web/app/api/cron/import/bitly/sanitize-json.ts
+++ b/apps/web/app/api/cron/import/bitly/sanitize-json.ts
@@ -1,29 +1,33 @@
export const sanitizeBitlyJson = (json: string): string => {
- return (
- json
- // remove "title" field which can sometimes contain invalid values that break the JSON parsing
- .replace(
- /"long_url":"([^"]+)".+?"archived"/g,
- '"long_url":"$1","archived"',
- )
- // handle certain JSON escape sequences
- .replace(/[\u0000-\u001F\u007F-\u009F]/g, (char) => {
- // Convert to proper JSON escape sequence if it's a common one
- switch (char) {
- case "\n":
- return "\\n";
- case "\r":
- return "\\r";
- case "\t":
- return "\\t";
- case "\b":
- return "\\b";
- case "\f":
- return "\\f";
- // Remove or replace other control characters
- default:
- return "";
- }
- })
- );
+ try {
+ // Apply the regex first to simplify the structure
+ const simplifiedJson = json.replace(
+ /"long_url":"([^"]+)".+?"archived"/g,
+ '"long_url":"$1","archived"',
+ );
+
+ // Then handle control characters
+ return simplifiedJson.replace(/[\u0000-\u001F\u007F-\u009F]/g, (char) => {
+ // Convert to proper JSON escape sequence if it's a common one
+ switch (char) {
+ case "\n":
+ return "\\n";
+ case "\r":
+ return "\\r";
+ case "\t":
+ return "\\t";
+ case "\b":
+ return "\\b";
+ case "\f":
+ return "\\f";
+ // Remove or replace other control characters
+ default:
+ return "";
+ }
+ });
+ } catch (error) {
+ console.error("Error in sanitizing JSON:", error);
+ // Return the original JSON if sanitization fails
+ return json;
+ }
};