feat: update libraries
This commit is contained in:
@@ -3,8 +3,13 @@
|
||||
|
||||
import { assert } from "jsr:@std/assert/assert";
|
||||
import { assertEquals } from "jsr:@std/assert";
|
||||
import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
|
||||
import { dirname } from "https://deno.land/std@0.208.0/path/mod.ts";
|
||||
|
||||
// reference: https://docs.deno.com/examples/hex_base64_encoding/
|
||||
// import { decodeBase64, encodeBase64 } from "jsr:@std/encoding/base64";
|
||||
// import { decodeHex, encodeHex } from "jsr:@std/encoding/hex";
|
||||
|
||||
export async function sleep(timeoutMillis: number): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, timeoutMillis));
|
||||
}
|
||||
@@ -28,7 +33,7 @@ export function compareVersion(ver1: string, ver2: string): 0 | 1 | -1 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
export function isOn(val: string | undefined): boolean {
|
||||
export function isOn(val: string | undefined | null): boolean {
|
||||
if ((val === null) || (val === undefined)) {
|
||||
return false;
|
||||
}
|
||||
@@ -37,7 +42,7 @@ export function isOn(val: string | undefined): boolean {
|
||||
lowerVal === "true";
|
||||
}
|
||||
|
||||
export async function getEnv(key: string): string {
|
||||
export async function getEnv(key: string): Promise<string | null> {
|
||||
const homeDir = getHomeDir();
|
||||
if ((homeDir !== null) && key) {
|
||||
const envValue = await readFileToString(
|
||||
@@ -47,11 +52,15 @@ export async function getEnv(key: string): string {
|
||||
return envValue.trim();
|
||||
}
|
||||
}
|
||||
return Deno.env.get(key);
|
||||
return Deno.env.get(key) || null;
|
||||
}
|
||||
|
||||
export function isEnvOn(envKey: string): boolean {
|
||||
return isOn(getEnv(envKey));
|
||||
return isOn(Deno.env.get(envKey));
|
||||
}
|
||||
|
||||
export async function isEnvOnAsync(envKey: string): Promise<boolean> {
|
||||
return isOn(await getEnv(envKey));
|
||||
}
|
||||
|
||||
export function formatHumanTime(timeMillis: number): string {
|
||||
@@ -345,6 +354,22 @@ export function hexStringToUint8Array(hex: string): Uint8Array {
|
||||
return uint8;
|
||||
}
|
||||
|
||||
export function decodeBase64Url(base64UrlString: string): Uint8Array {
|
||||
let standardBase64 = base64UrlString.replace(/-/g, "+").replace(/_/g, "/");
|
||||
while (standardBase64.length % 4) {
|
||||
standardBase64 += "=";
|
||||
}
|
||||
return decodeBase64(standardBase64);
|
||||
}
|
||||
|
||||
export function encodeBase64Url(input: ArrayBufferLike): string {
|
||||
let standardBased64 = encodeBase64(input);
|
||||
return standardBased64.replace(/\+/g, "-").replace(/\//g, "_").replace(
|
||||
/=/g,
|
||||
"",
|
||||
);
|
||||
}
|
||||
|
||||
Deno.test("isOn", () => {
|
||||
assertEquals(false, isOn(undefined));
|
||||
assertEquals(false, isOn(""));
|
||||
@@ -409,3 +434,18 @@ Deno.test("sleep", async () => {
|
||||
const t2 = new Date().getTime();
|
||||
assert(Math.abs(1000 - (t2 - t1)) < 20);
|
||||
});
|
||||
|
||||
Deno.test("base64Url", () => {
|
||||
assertEquals(
|
||||
"_dxhVwI3qd9fMBlpEMmi6Q",
|
||||
encodeBase64Url(decodeBase64Url("_dxhVwI3qd9fMBlpEMmi6Q")),
|
||||
);
|
||||
assertEquals(
|
||||
"1dxJeD7erjAYUNEmdVNE8KdhpPZs0pAHtb-kbSqYIe5j039PkTHbrQYOEoeEWN4UsDERhnUg7mY",
|
||||
encodeBase64Url(
|
||||
decodeBase64Url(
|
||||
"1dxJeD7erjAYUNEmdVNE8KdhpPZs0pAHtb-kbSqYIe5j039PkTHbrQYOEoeEWN4UsDERhnUg7mY",
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user