s3si.ts/utils.ts

43 lines
901 B
TypeScript

import { base64, io } from "./deps.ts";
const stdinLines = io.readLines(Deno.stdin);
export function urlBase64Encode(data: ArrayBuffer) {
return base64.encode(data)
.replaceAll("+", "_")
.replaceAll("/", "-")
.replaceAll("=", "");
}
export function urlBase64Decode(data: string) {
return base64.decode(
data
.replaceAll("_", "+")
.replaceAll("-", "/"),
);
}
export async function readline() {
for await (const line of stdinLines) {
if (line !== "") {
return line;
}
}
}
type PromiseReturnType<T> = T extends () => Promise<infer R> ? R : never;
export async function retry<F extends () => Promise<unknown>>(
f: F,
times = 2,
): Promise<PromiseReturnType<F>> {
let lastError;
for (let i = 0; i < times; i++) {
try {
return await f() as PromiseReturnType<F>;
} catch (e) {
lastError = e;
}
}
throw lastError;
}