fix(Smartjson): Cross-platform buffer/base64 handling, safer folding with cycle detection, parsing fixes, docs and dependency updates
This commit is contained in:
@@ -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)));
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user