-
Notifications
You must be signed in to change notification settings - Fork 1
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
Buffer is not available in the browser #59
Comments
@oskarski |
Would something like this work? (this is a refined version of a ChatGPT proposal) function toBase64UrlWithReplaceAll(object) {
// Convert the object to a JSON string
const jsonString = JSON.stringify(object);
// Encode the JSON string into an Uint8Array using TextEncoder
const encoder = new TextEncoder();
const encodedData = encoder.encode(jsonString);
// Convert the Uint8Array to a base64 string
const base64 = btoa(String.fromCharCode.apply(null, encodedData));
// Convert base64 to base64url by replacing '+' with '-' and '/' with '_'
let base64url = base64.replaceAll('+', '-').replaceAll('/', '_');
// Remove any '=' padding
base64url = base64url.replaceAll('=', '');
return base64url;
}
// Example usage
const object = { hello: 'world' };
const base64url = toBase64UrlWithReplaceAll(object);
console.log(base64url); |
Hmm, and can't it be simplified to something like this? const encodeToBase64 = (data: any): string => btoa(JSON.stringify(data))
const decodeFromBase64 = <T = unknown>(encodedData: string): T => JSON.parse(atob(encodedData)) |
@oskarski Would that provide sufficient amount of escaping? |
I haven't used much these functions, but as far as I know it should be enough. Also proper tests should catch and cover escaping edge cases |
The
decodeCursor()
may be invoked within the browser and it fails, since theBuffer
is not available. I've stumbled upon this issue, while passing schema for request query params based onGET_CONTENT_MANAGER_PAGINATION_SCHEMA
Buffer
usage:https://github.com/lokalise/shared-ts-libs/blob/db67b57b5b370b9fac397cc0972101da27807b25/packages/app/api-common/src/cursorCodec.ts#L21C19-L21C25
Error screenshot from browser:
![image](https://private-user-images.githubusercontent.com/17511807/313966444-40aacf6e-caf2-435f-8009-db21977ea2e1.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk4OTcxOTIsIm5iZiI6MTczOTg5Njg5MiwicGF0aCI6Ii8xNzUxMTgwNy8zMTM5NjY0NDQtNDBhYWNmNmUtY2FmMi00MzVmLTgwMDktZGIyMTk3N2VhMmUxLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTglMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjE4VDE2NDEzMlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWZkMmRjMjc0OTExN2Y2OThjMjk1NzhjNGFhZmJiMDBiNGUyNDVlZDQ0MDc2NDE4NmE0NGI1Y2E4N2Q3MzhhYWYmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.5_0vlo69LgPRnVDrTiXRbRVC-E_SNAZqg6QKe61BgIs)
The text was updated successfully, but these errors were encountered: