fix(Smartjson): Cross-platform buffer/base64 handling, safer folding with cycle detection, parsing fixes, docs and dependency updates

This commit is contained in:
2025-09-12 19:16:52 +00:00
parent 9bb6e0b497
commit 2f012fc0ad
9 changed files with 5547 additions and 3223 deletions

View File

@@ -16,10 +16,23 @@ type TParseReplacer = (this: any, key: string, value: any) => any;
// Utility functions to handle base64 encoding/decoding in a cross-platform way
function base64Encode(data: Uint8Array): string {
// Prefer Node's Buffer when available
if (typeof Buffer !== 'undefined') {
// @ts-ignore Buffer might not exist in browser builds
return Buffer.from(data).toString('base64');
}
// Fallback for browsers
return btoa(String.fromCharCode(...data));
}
function base64Decode(str: string): Uint8Array {
// Prefer Node's Buffer when available
if (typeof Buffer !== 'undefined') {
// @ts-ignore Buffer might not exist in browser builds
const buf = Buffer.from(str, 'base64');
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
}
// Fallback for browsers
return new Uint8Array(Array.from(atob(str)).map((char) => char.charCodeAt(0)));
}