✨ Add Base32 encoding support and enhance caching logic
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
// - https://docs.deno.com/runtime/fundamentals/testing/
|
||||
|
||||
import {decodeBase64, encodeBase64} from "jsr:@std/encoding/base64";
|
||||
import {encodeBase32} from "jsr:@std/encoding/base32";
|
||||
import {dirname, fromFileUrl} from "jsr:@std/path";
|
||||
import {toArrayBuffer} from "jsr:@std/streams";
|
||||
import {spawn, SpawnOptionsWithoutStdio} from "node:child_process";
|
||||
@@ -812,12 +813,14 @@ export function joinPath(path1: string, ...paths: string[]): string {
|
||||
if (paths != null && paths.length > 0) {
|
||||
for (let i = 0; i < paths.length; i++) {
|
||||
const path2 = paths[i];
|
||||
if (basePath.endsWith("/") && path2.startsWith("/")) {
|
||||
basePath += path2.substring(1);
|
||||
} else if (basePath.endsWith("/") || path2.startsWith("/")) {
|
||||
basePath += path2;
|
||||
} else {
|
||||
basePath += "/" + path2;
|
||||
if (path2) {
|
||||
if (basePath.endsWith("/") && path2.startsWith("/")) {
|
||||
basePath += path2.substring(1);
|
||||
} else if (basePath.endsWith("/") || path2.startsWith("/")) {
|
||||
basePath += path2;
|
||||
} else {
|
||||
basePath += "/" + path2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -952,6 +955,10 @@ export function uint8ArrayToHexString(uint8: Uint8Array): string {
|
||||
.join("");
|
||||
}
|
||||
|
||||
export function uint8ArrayToBase32String(uint8: Unit8Array): string {
|
||||
return encodeBase32(uint8).replaceAll(/=/g, "").toLowerCase();
|
||||
}
|
||||
|
||||
export function hexStringToUint8Array(hex: string): Uint8Array {
|
||||
hex = hex.trim();
|
||||
if (hex.startsWith("0x") || hex.startsWith("0X")) {
|
||||
@@ -1149,6 +1156,12 @@ export async function sha256AndHexOfString(input: string): Promise<string> {
|
||||
return uint8ArrayToHexString(new Uint8Array(await sha256OfString(input)));
|
||||
}
|
||||
|
||||
export async function sha256AndBase32OfString(input: string): Promise<string> {
|
||||
return uint8ArrayToBase32String(
|
||||
new Uint8Array(await sha256OfString(input)),
|
||||
);
|
||||
}
|
||||
|
||||
export async function sha256OfString(input: string): Promise<ArrayBuffer> {
|
||||
const data = new TextEncoder().encode(input);
|
||||
return await crypto.subtle.digest("SHA-256", data);
|
||||
@@ -1165,6 +1178,8 @@ export interface FetchFileWithCacheMeta {
|
||||
|
||||
export interface FetchFileWithCacheOptions {
|
||||
baseDir?: string;
|
||||
hierarchicalCacheDir?: boolean;
|
||||
base32Filename?: boolean;
|
||||
tag?: string;
|
||||
timeoutMillis?: number;
|
||||
check_cache_file?: (
|
||||
@@ -1190,9 +1205,13 @@ export async function fetchFileWithCache(
|
||||
url: string,
|
||||
options?: FetchFileWithCacheOptions,
|
||||
): Promise<FetchFileWithCacheMeta> {
|
||||
const urlSha256 = await sha256AndHexOfString(url);
|
||||
const urlSha256 = options?.base32Filename
|
||||
? await sha256AndBase32OfString(url)
|
||||
: await sha256AndHexOfString(url);
|
||||
const fileCacheDir = joinPath(
|
||||
resolveFilename(options?.baseDir ?? BASE_FILE_CACHE_DIR),
|
||||
options?.hierarchicalCacheDir ? urlSha256.substring(0, 2) : null,
|
||||
options?.hierarchicalCacheDir ? urlSha256.substring(2, 4) : null,
|
||||
urlSha256,
|
||||
);
|
||||
const fileCacheMetaFile = fileCacheDir + ".meta";
|
||||
|
||||
Reference in New Issue
Block a user