feat: updates

This commit is contained in:
2026-01-11 20:29:07 +08:00
parent 4936f76de4
commit ea3f50b1fb
3 changed files with 93 additions and 9 deletions

View File

@@ -3,12 +3,13 @@
Check scripts:
```shell
cmd script check --type deno-mod
hatter script check --type deno-mod
```
Publish script:
```shell
cmd script pub --type deno-mod deno-commons-mod.ts
hatter script pub --type deno-mod deno-commons-mod.ts
```

View File

@@ -6,7 +6,7 @@ import { assertEquals } from "jsr:@std/assert";
import { dirname } from "https://deno.land/std@0.208.0/path/mod.ts";
export async function sleep(timeoutMillis: number): Promise<void> {
await new Promise(resolve => setTimeout(resolve, timeoutMillis))
await new Promise((resolve) => setTimeout(resolve, timeoutMillis));
}
export function compareVersion(ver1: string, ver2: string): 0 | 1 | -1 {
@@ -236,6 +236,14 @@ class Logger {
export const log = new Logger();
export function getHomeDirOrDie(): string {
const homeDir = getHomeDir();
if (homeDir === null) {
throw new Error("Cannot find home dir");
}
return homeDir;
}
export function getHomeDir(): string | null {
if (Deno.build.os === "windows") {
const userProfile = Deno.env.get("USERPROFILE");
@@ -268,32 +276,62 @@ export async function existsPath(path: string): Promise<boolean> {
}
}
export async function readFileToString(filename: string): Promise<string | null> {
export async function readFileToString(
filename: string,
): Promise<string | null> {
try {
return await Deno.readTextFile(resolveFilename(filename));
} catch (e) {
if (e instanceof Error && e.name == 'NotFound') {
if (e instanceof Error && e.name == "NotFound") {
return null;
}
throw e;
}
}
export async function writeStringToFile(filename: string, data: string | null): Promise<void> {
export async function writeStringToFile(
filename: string,
data: string | null,
): Promise<void> {
const newFilename = resolveFilename(filename);
if (data == null) {
if (await existsPath(newFilename)) {
await Deno.remove(newFilename)
await Deno.remove(newFilename);
}
} else {
const parentDirname = dirname(newFilename);
if (!await existsPath(parentDirname)) {
await Deno.mkdir(parentDirname, {recursive: true});
await Deno.mkdir(parentDirname, { recursive: true });
}
await Deno.writeTextFile(newFilename, data);
}
}
export function uint8ArrayToHexString(uint8: Uint8Array): string {
return Array.from(uint8)
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
}
export function hexStringToUint8Array(hex: string): Uint8Array {
hex = hex.trim();
if (hex.startsWith("0x") || hex.startsWith("0X")) {
hex = hex.slice(2);
}
if (hex.length % 2 !== 0) {
throw new Error("Hex string must have an even number of characters");
}
if (!/^[0-9a-fA-F]*$/.test(hex)) {
throw new Error("Invalid hex string");
}
const byteLength = hex.length / 2;
const uint8 = new Uint8Array(byteLength);
for (let i = 0; i < byteLength; i++) {
uint8[i] = parseInt(hex.substring(i * 2, (i + 1) * 2), 16);
}
return uint8;
}
Deno.test("isOn", () => {
assertEquals(false, isOn(undefined));
assertEquals(false, isOn(""));
@@ -354,7 +392,7 @@ Deno.test("formatPercent", () => {
Deno.test("sleep", async () => {
const t1 = new Date().getTime();
await sleep(1000)
await sleep(1000);
const t2 = new Date().getTime();
assert(Math.abs(1000 - (t2 - t1)) < 20);
});
});

View File

@@ -0,0 +1,45 @@
import {
getHomeDirOrDie,
hexStringToUint8Array,
uint8ArrayToHexString,
} from "https://global.hatter.ink/script/get/@6/deno-commons-mod.ts";
const COMMONS_LOCAL_ENCRYPT_TINY_ENCRYPT_MASTER_KEY_FILE = getHomeDirOrDie() +
"/.cache/commons-local-encrypt-tiny-encrypt-master-key";
interface TinyEncryptSimpleDecryptObject {
code: number;
result: string;
}
async function loadMasterKey(): Promise<Uint8Array> {
const masterKeyContent = Deno.readTextFileSync(
COMMONS_LOCAL_ENCRYPT_TINY_ENCRYPT_MASTER_KEY_FILE,
);
const command = new Deno.Command("tiny-encrypt", {
args: ["simple-decrypt", "--value", masterKeyContent],
});
const { code, stdout, stderr } = command.outputSync();
if (code !== 0) {
console.error(`Execute command tiny-encrypt simple-decrypt failed:
code: ${code}
stdout: ${new TextDecoder().decode(stdout)}
stderr: ${new TextDecoder().decode(stderr)}`);
throw new Error(`Decrypt master key failed, code: ${code}`);
}
const tinyEncryptSimpleDecryptObject = JSON.parse(
new TextDecoder().decode(stdout),
) as TinyEncryptSimpleDecryptObject;
if (tinyEncryptSimpleDecryptObject.code !== 0) {
throw new Error(
`Decrypt master key failed, response code: ${tinyEncryptSimpleDecryptObject.code}`,
);
}
return hexStringToUint8Array(tinyEncryptSimpleDecryptObject.result);
}
async function main() {
// TODO ...
console.log(uint8ArrayToHexString(await loadMasterKey()));
}
await main();