diff --git a/src/base32.ts b/src/base32.ts index b6578df..a45a87b 100644 --- a/src/base32.ts +++ b/src/base32.ts @@ -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. @@ -34,7 +34,7 @@ function createDecodeMap(alphabet: string): Map { * @returns The Base32 encoded string. */ function base32Encode( - data: Uint8Array, + data: Uint8Array_, alphabet: string, padding: boolean, ): string { @@ -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; @@ -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); } @@ -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); }, diff --git a/src/base64.ts b/src/base64.ts index 56e7216..24da282 100644 --- a/src/base64.ts +++ b/src/base64.ts @@ -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 @@ -9,7 +9,7 @@ function getAlphabet(urlSafe: boolean): string { } function base64Encode( - data: Uint8Array, + data: Uint8Array_, alphabet: string, padding: boolean, ): string { @@ -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(); for (let i = 0; i < alphabet.length; i++) { decodeMap.set(alphabet[i]!, i); diff --git a/src/type.ts b/src/type.ts index 3e4eee8..e71f4d2 100644 --- a/src/type.ts +++ b/src/type.ts @@ -10,6 +10,22 @@ export type TypedArray = | BigInt64Array | BigUint64Array; +/** + * Equivalent to `Uint8Array` before TypeScript 5.7, and `Uint8Array` 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` 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; + export type SHAFamily = "SHA-1" | "SHA-256" | "SHA-384" | "SHA-512"; export type EncodingFormat = | "hex"