* add stt hook interface * fix crypto exported to browser * refactor use-transcribe * may use openai stt * refactor: remove decprecated codes * fix undefined method
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { createHash } from "crypto";
|
|
import { createReadStream } from "fs";
|
|
|
|
export function hashFile(
|
|
path: string,
|
|
options: { algo: string }
|
|
): Promise<string> {
|
|
const algo = options.algo || "md5";
|
|
return new Promise((resolve, reject) => {
|
|
const hash = createHash(algo);
|
|
const stream = createReadStream(path);
|
|
stream.on("error", reject);
|
|
stream.on("data", (chunk) => hash.update(chunk));
|
|
stream.on("end", () => resolve(hash.digest("hex")));
|
|
});
|
|
}
|
|
|
|
export function hashBlob(
|
|
blob: Blob,
|
|
options: { algo: string }
|
|
): Promise<string> {
|
|
const algo = options.algo || "md5";
|
|
return new Promise((resolve, reject) => {
|
|
const hash = createHash(algo);
|
|
const reader = new FileReader();
|
|
reader.onload = () => {
|
|
if (reader.result instanceof ArrayBuffer) {
|
|
const buffer = Buffer.from(reader.result);
|
|
hash.update(buffer);
|
|
resolve(hash.digest("hex"));
|
|
} else {
|
|
reject(new Error("Unexpected result from FileReader"));
|
|
}
|
|
};
|
|
reader.onerror = reject;
|
|
reader.readAsArrayBuffer(blob);
|
|
});
|
|
}
|