From ab345c02df476d76ef2c22b5639c5c21ad8a0d9f Mon Sep 17 00:00:00 2001 From: ZhongFuze Date: Fri, 8 Nov 2024 18:24:11 +0800 Subject: [PATCH] [!] fix texts v2 --- sns_process/index.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/sns_process/index.js b/sns_process/index.js index b054c4b..a21a95a 100644 --- a/sns_process/index.js +++ b/sns_process/index.js @@ -995,6 +995,22 @@ async function retryGetTextsV2(domainName, retries = 3) { return {}; // Return empty object as a fallback } + +const isValidUTF8 = (str) => { + try { + // Attempt to encode the string as UTF-8 + new TextEncoder().encode(str); + return true; + } catch (e) { + return false; + } +}; + +const sanitizeText = (text) => { + // Replace non-printable characters with an empty string or a placeholder (e.g., space) + return text.replace(/[\x00-\x1F\x7F]/g, ''); +}; + const getTextsV2 = limiter.wrap(async (domainName) => { const record_keys = [ Record.IPNS, @@ -1022,16 +1038,22 @@ const getTextsV2 = limiter.wrap(async (domainName) => { const texts = {}; // json object for (const record of record_keys) { try { - const { retrievedRecord } = await getRecordV2(SOLANA_MAIN_CLIENT, domainName, record, {deserialize: true}); - if (retrievedRecord) { - const value = retrievedRecord.getContent().toString(); - texts[record.toLowerCase()] = value; + const { deserializedContent } = await getRecordV2(SOLANA_MAIN_CLIENT, domainName, record, { deserialize: true }); + if (deserializedContent) { + // const value = retrievedRecord.getContent().toString(); + texts[record.toLowerCase()] = deserializedContent; + // if (isValidUTF8(value)) { + // texts[record.toLowerCase()] = value; + // } else { + // console.warn(`Invalid UTF-8 detected in record ${record} for domain ${domainName}. Skipping.`); + // } } } catch (error) { + // console.error(`Error retrieving record ${record} for domain ${domainName}:`, error); continue; - } + } } - // console.log(`domain ${domainName} getRecordV2 texts:`, texts); + console.log(`domain ${domainName} getRecordV2 texts:`, texts); return texts; } catch (error) { console.error(`Error fetching texts getRecordV2 error:`, error);