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

Buffer is not available in the browser #59

Open
oskarski opened this issue Mar 19, 2024 · 5 comments
Open

Buffer is not available in the browser #59

oskarski opened this issue Mar 19, 2024 · 5 comments

Comments

@oskarski
Copy link

oskarski commented Mar 19, 2024

The decodeCursor() may be invoked within the browser and it fails, since the Buffer is not available. I've stumbled upon this issue, while passing schema for request query params based on GET_CONTENT_MANAGER_PAGINATION_SCHEMA

sendGet(authorizedApiClient(token), {
		path: `...`,
		queryParams: query,
		responseBodySchema: GET_CONTENT_MANAGER_RESPONSE_SCHEMA,
                queryParamsSchema: GET_CONTENT_MANAGER_QUERY_SCHEMA, // <- This is based on `GET_CONTENT_MANAGER_PAGINATION_SCHEMA` and fails before request is being sent  
	})

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

@kibertoad
Copy link
Collaborator

kibertoad commented Mar 19, 2024

@oskarski Should we replace buffer usage with base64 encoding, maybe? we are already using it for base64 encoding 😂

@kibertoad
Copy link
Collaborator

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);

@oskarski
Copy link
Author

oskarski commented Mar 19, 2024

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))

@kibertoad
Copy link
Collaborator

@oskarski Would that provide sufficient amount of escaping?

@oskarski
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants