fix(chat): 解决token传输、本地文件存储顺序、读取的问题
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
|
||||
import { basename, dirname, join, relative } from "node:path";
|
||||
|
||||
@@ -13,9 +13,14 @@ export const ensureDirectory = async (path: string) => {
|
||||
|
||||
export const atomicWriteFile = async (path: string, content: string) => {
|
||||
await ensureDirectory(dirname(path));
|
||||
const tempPath = `${path}.${process.pid}.${Date.now().toString(36)}.tmp`;
|
||||
await writeFile(tempPath, content, "utf8");
|
||||
await rename(tempPath, path);
|
||||
const tempPath = `${path}.${process.pid}.${Date.now().toString(36)}.${randomUUID()}.tmp`;
|
||||
try {
|
||||
await writeFile(tempPath, content, "utf8");
|
||||
await rename(tempPath, path);
|
||||
} catch (error) {
|
||||
await removeFileIfExists(tempPath);
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
|
||||
type HistoricalWriteOptions = {
|
||||
|
||||
Reference in New Issue
Block a user