-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Allows passing encoding callbacks (#20)
* ✨ Allows passing encoding callbacks and base64, base64url encoders * 🐛 Fixes js sourcemaps
- Loading branch information
Showing
12 changed files
with
742 additions
and
755 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export type Encoding<T> = { | ||
to(value: T): PrimitivesToStrings<T>; | ||
from(value: PrimitivesToStrings<T>): T; | ||
}; | ||
|
||
export type PrimitivesToStrings<T> = T extends string | number | boolean | null | ||
? `${T}` | ||
: T extends Array<infer U> | ||
? Array<PrimitivesToStrings<U>> | ||
: T extends object | ||
? { | ||
[K in keyof T]: PrimitivesToStrings<T[K]>; | ||
} | ||
: T; | ||
|
||
globalThis.btoa ??= (str: string) => Buffer.from(str).toString('base64'); | ||
globalThis.atob ??= (str: string) => Buffer.from(str, 'base64').toString(); | ||
|
||
export const base64: Encoding<string> = { | ||
to: (value) => btoa(value), | ||
from: (value) => atob(value), | ||
}; | ||
|
||
export const base64URL: Encoding<string> = { | ||
to: (value) => | ||
btoa(value).replaceAll('+', '-').replaceAll('/', '_').replaceAll('=', ''), | ||
from: (value) => atob(value.replaceAll('-', '+').replaceAll('_', '/')), | ||
}; | ||
|
||
if (import.meta.vitest) { | ||
describe('Encoding', () => { | ||
it('should encode and decode base64', () => { | ||
expect(base64.to('hello world')).toBe('aGVsbG8gd29ybGQ='); | ||
expect(base64.to('<<???>>')).toBe('PDw/Pz8+Pg=='); | ||
expect(base64.from('aGVsbG8gd29ybGQ=')).toBe('hello world'); | ||
expect(base64.from('PDw/Pz8+Pg==')).toBe('<<???>>'); | ||
}); | ||
it('should encode and decode base64URL', () => { | ||
expect(base64URL.to('hello world')).toBe('aGVsbG8gd29ybGQ'); | ||
expect(base64URL.to('<<???>>')).toBe('PDw_Pz8-Pg'); | ||
expect(base64URL.from('aGVsbG8gd29ybGQ')).toBe('hello world'); | ||
expect(base64URL.from('PDw_Pz8-Pg')).toBe('<<???>>'); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.