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.

fix sanitizeJson

Steven Tey3e93a757284b
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 b9780215bf..fb99566251 100644
--- a/apps/web/app/api/cron/import/bitly/fetch-utils.ts
+++ b/apps/web/app/api/cron/import/bitly/fetch-utils.ts
@@ -2,7 +2,7 @@
* Utilities for fetching links from Bitly API
*/
-import { sanitizeJson } from "./sanitize-json";
+import { sanitizeBitlyJson } from "./sanitize-json";
interface FetchBitlyLinksResult {
links: any[];
@@ -91,7 +91,7 @@ const fetchBitlyLinksStandard = async ({
let data;
try {
// Sanitize the JSON and then parse it
- data = JSON.parse(sanitizeJson(responseText));
+ data = JSON.parse(sanitizeBitlyJson(responseText));
} catch (error) {
console.error("JSON parsing error:", error);
console.error(`Failed to parse response: ${responseText}`);
@@ -179,7 +179,7 @@ const fetchBitlyLinksBatch = async ({
let data;
try {
// Sanitize the JSON and then parse it
- data = JSON.parse(sanitizeJson(responseText));
+ data = JSON.parse(sanitizeBitlyJson(responseText));
} catch (error) {
console.error("JSON parsing error:", error);
console.error(`Failed to parse response: ${responseText}`);
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 c9dc1ef52b..a2064f1d8b 100644
--- a/apps/web/app/api/cron/import/bitly/sanitize-json.ts
+++ b/apps/web/app/api/cron/import/bitly/sanitize-json.ts
@@ -1,21 +1,29 @@
-export const sanitizeJson = (json: string): string => {
- // Replace control characters that are not allowed in JSON string literals
- return json.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 "";
- }
- });
+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 "";
+ }
+ })
+ );
};