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: base64url react native #33

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
14 changes: 10 additions & 4 deletions packages/utils/src/base64url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class Base64url {
*
*/
public static encode(input: string | Uint8Array | Buffer): string {
return Buffer.from(input).toString('base64url')
return base64ToBase64URL(Buffer.from(input).toString('base64'))
}

/**
Expand All @@ -18,7 +18,9 @@ export class Base64url {
public static encodeFromJson(
input: Record<string, unknown> | Array<unknown>
): string {
return Buffer.from(JSON.stringify(input)).toString('base64url')
return base64ToBase64URL(
Buffer.from(JSON.stringify(input)).toString('base64')
)
}

/**
Expand All @@ -32,7 +34,7 @@ export class Base64url {
unknown
>
>(input: string): T {
return JSON.parse(Buffer.from(input, 'base64url').toString()) as T
return JSON.parse(Buffer.from(input, 'base64').toString()) as T
}

/**
Expand All @@ -41,6 +43,10 @@ export class Base64url {
*
*/
public static decode(input: string): Uint8Array {
return Uint8Array.from(Buffer.from(input, 'base64url'))
return Uint8Array.from(Buffer.from(input, 'base64'))
}
}

export function base64ToBase64URL(base64: string) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Biggest nit ever. The function does not have to be exported :). Should be the absolute slightest reduction in codegen :').

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed within the GitHub Mobile app 😎

return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')
}
Loading