Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions src/base32.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//inspired by oslo implementation by pilcrowonpaper: https://github.com/pilcrowonpaper/oslo/blob/main/src/encoding/base32.ts

import type { TypedArray } from "./type";
import type { TypedArray, Uint8Array_ } from "./type";

/**
* Returns the Base32 alphabet based on the encoding type.
Expand Down Expand Up @@ -34,7 +34,7 @@ function createDecodeMap(alphabet: string): Map<string, number> {
* @returns The Base32 encoded string.
*/
function base32Encode(
data: Uint8Array,
data: Uint8Array_,
alphabet: string,
padding: boolean,
): string {
Expand Down Expand Up @@ -69,7 +69,7 @@ function base32Encode(
* @param alphabet - The Base32 alphabet to use.
* @returns The decoded Uint8Array.
*/
function base32Decode(data: string, alphabet: string): Uint8Array {
function base32Decode(data: string, alphabet: string): Uint8Array_ {
const decodeMap = createDecodeMap(alphabet);
const result: number[] = [];
let buffer = 0;
Expand Down Expand Up @@ -120,7 +120,7 @@ export const base32 = {
* @param data - The Base32 encoded string or ArrayBuffer/TypedArray.
* @returns The decoded Uint8Array.
*/
decode(data: string | ArrayBuffer | TypedArray): Uint8Array {
decode(data: string | ArrayBuffer | TypedArray): Uint8Array_ {
if (typeof data !== "string") {
data = new TextDecoder().decode(data);
}
Expand Down Expand Up @@ -156,7 +156,7 @@ export const base32hex = {
* @param data - The Base32hex encoded string.
* @returns The decoded Uint8Array.
*/
decode(data: string): Uint8Array {
decode(data: string): Uint8Array_ {
const alphabet = getAlphabet(true);
return base32Decode(data, alphabet);
},
Expand Down
6 changes: 3 additions & 3 deletions src/base64.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//inspired by oslo implementation by pilcrowonpaper: https://github.com/pilcrowonpaper/oslo/blob/main/src/encoding/base64.ts

import type { TypedArray } from "./type";
import type { TypedArray, Uint8Array_ } from "./type";

function getAlphabet(urlSafe: boolean): string {
return urlSafe
Expand All @@ -9,7 +9,7 @@ function getAlphabet(urlSafe: boolean): string {
}

function base64Encode(
data: Uint8Array,
data: Uint8Array_,
alphabet: string,
padding: boolean,
): string {
Expand Down Expand Up @@ -38,7 +38,7 @@ function base64Encode(
return result;
}

function base64Decode(data: string, alphabet: string): Uint8Array {
function base64Decode(data: string, alphabet: string): Uint8Array_ {
const decodeMap = new Map<string, number>();
for (let i = 0; i < alphabet.length; i++) {
decodeMap.set(alphabet[i]!, i);
Expand Down
16 changes: 16 additions & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ export type TypedArray =
| BigInt64Array
| BigUint64Array;

/**
* Equivalent to `Uint8Array` before TypeScript 5.7, and `Uint8Array<ArrayBuffer>` in TypeScript 5.7
* and beyond.
*
* **Context**
*
* `Uint8Array` became a generic type in TypeScript 5.7, requiring types defined simply as
* `Uint8Array` to be refactored to `Uint8Array<ArrayBuffer>` starting in Deno 2.2. `Uint8Array` is
* _not_ generic in Deno 2.1.x and earlier, though, so this type helps bridge this gap.
*
* Inspired by Deno's std library:
*
* https://github.com/denoland/std/blob/b5a5fe4f96b91c1fe8dba5cc0270092dd11d3287/bytes/_types.ts#L11
*/
export type Uint8Array_ = ReturnType<Uint8Array["slice"]>;

export type SHAFamily = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512";
export type EncodingFormat =
| "hex"
Expand Down