feat: updates
This commit is contained in:
@@ -3,12 +3,13 @@
|
|||||||
Check scripts:
|
Check scripts:
|
||||||
```shell
|
```shell
|
||||||
cmd script check --type deno-mod
|
cmd script check --type deno-mod
|
||||||
|
hatter script check --type deno-mod
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Publish script:
|
Publish script:
|
||||||
```shell
|
```shell
|
||||||
cmd script pub --type deno-mod deno-commons-mod.ts
|
hatter script pub --type deno-mod deno-commons-mod.ts
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { assertEquals } from "jsr:@std/assert";
|
|||||||
import { dirname } from "https://deno.land/std@0.208.0/path/mod.ts";
|
import { dirname } from "https://deno.land/std@0.208.0/path/mod.ts";
|
||||||
|
|
||||||
export async function sleep(timeoutMillis: number): Promise<void> {
|
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 {
|
export function compareVersion(ver1: string, ver2: string): 0 | 1 | -1 {
|
||||||
@@ -236,6 +236,14 @@ class Logger {
|
|||||||
|
|
||||||
export const log = new 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 {
|
export function getHomeDir(): string | null {
|
||||||
if (Deno.build.os === "windows") {
|
if (Deno.build.os === "windows") {
|
||||||
const userProfile = Deno.env.get("USERPROFILE");
|
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 {
|
try {
|
||||||
return await Deno.readTextFile(resolveFilename(filename));
|
return await Deno.readTextFile(resolveFilename(filename));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Error && e.name == 'NotFound') {
|
if (e instanceof Error && e.name == "NotFound") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
throw e;
|
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);
|
const newFilename = resolveFilename(filename);
|
||||||
if (data == null) {
|
if (data == null) {
|
||||||
if (await existsPath(newFilename)) {
|
if (await existsPath(newFilename)) {
|
||||||
await Deno.remove(newFilename)
|
await Deno.remove(newFilename);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const parentDirname = dirname(newFilename);
|
const parentDirname = dirname(newFilename);
|
||||||
if (!await existsPath(parentDirname)) {
|
if (!await existsPath(parentDirname)) {
|
||||||
await Deno.mkdir(parentDirname, {recursive: true});
|
await Deno.mkdir(parentDirname, { recursive: true });
|
||||||
}
|
}
|
||||||
await Deno.writeTextFile(newFilename, data);
|
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", () => {
|
Deno.test("isOn", () => {
|
||||||
assertEquals(false, isOn(undefined));
|
assertEquals(false, isOn(undefined));
|
||||||
assertEquals(false, isOn(""));
|
assertEquals(false, isOn(""));
|
||||||
@@ -354,7 +392,7 @@ Deno.test("formatPercent", () => {
|
|||||||
|
|
||||||
Deno.test("sleep", async () => {
|
Deno.test("sleep", async () => {
|
||||||
const t1 = new Date().getTime();
|
const t1 = new Date().getTime();
|
||||||
await sleep(1000)
|
await sleep(1000);
|
||||||
const t2 = new Date().getTime();
|
const t2 = new Date().getTime();
|
||||||
assert(Math.abs(1000 - (t2 - t1)) < 20);
|
assert(Math.abs(1000 - (t2 - t1)) < 20);
|
||||||
});
|
});
|
||||||
|
|||||||
45
libraries/deno-teencrypt-mod.ts
Normal file
45
libraries/deno-teencrypt-mod.ts
Normal 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();
|
||||||
Reference in New Issue
Block a user