Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[!] fix texts v2 #6

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions sns_process/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
Loading