web-font/backend/interface.ts
崮生(子虚) fe61bace16 fix: 修复所有 TypeScript 类型错误
- 修复 fonteditor-core 模块声明(添加 FontEditor 命名空间、FontType 类型、SubsetOptions 接口)
- 修复 llrt.ts 中 mkdir 和 rm 方法的返回类型问题
- 修复 tcp_server.ts 中 chunk 类型问题(添加 Buffer 类型检查和转换)
- 修复 font_detail.ts 中缺失的 ORIGIN 导入
- 修复 subset.ts 中 subsetQueueTimeoutSeconds 的导入
- 为 Font 类添加 optimize() 和 write() 方法声明
2026-08-15 07:38:57 +08:00

92 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

export let stat: (path: string) => Promise<{
isFile: () => boolean;
isDirectory: () => boolean;
size: number;
/** 最后修改时间戳(毫秒),用于文件变更检测 */
mtimeMs: number;
}>;
export let readFile: (path: string) => Promise<Uint8Array>;
export let writeFile: (path: string, data: Uint8Array) => Promise<void>;
export let readdir: (path: string) => Promise<{
isFile: () => boolean;
name: string;
}[]>;
export let mkdir: (path: string) => Promise<void>;
export let unlink: (path: string) => Promise<void>;
export let rm: (path: string) => Promise<void>;
/** LLRT 专用:保存 rm 函数引用,避免闭包问题 */
let llrtRm: ((path: string) => Promise<void>) | undefined;
export const implInterface = (options: {
stat: typeof stat;
readFile: typeof readFile;
writeFile: typeof writeFile;
readdir: typeof readdir;
mkdir: typeof mkdir;
unlink?: typeof unlink;
rm?: typeof rm;
}) => {
stat = options.stat;
readFile = options.readFile;
writeFile = options.writeFile;
readdir = options.readdir;
mkdir = options.mkdir;
// 保存 rm 引用到模块级变量
llrtRm = options.rm;
/** LLRT 的 fs/promises 没有 unlink需要用 rm 代替 */
unlink = async (path) => {
if (options.unlink) {
await options.unlink(path);
} else if (llrtRm) {
await llrtRm(path);
}
};
/** 同时暴露 rm 方法 */
rm = async (path) => {
if (options.rm) {
await options.rm(path);
}
};
};
export function path_join(...paths: string[]) {
const sep = "/";
function trimSlashes(p: string) {
return p.replace(/\/+$/, "").replace(/^\/+/, "");
}
/** 将路径按 / 分割并解析 . 和 .. 段 */
function normalizeSegments(segments: string[]) {
const resolved: string[] = [];
for (const seg of segments) {
if (seg === "..") {
resolved.pop();
} else if (seg !== "." && seg !== "") {
resolved.push(seg);
}
}
return resolved;
}
const isAbsolute = paths[0] && paths[0].startsWith(sep);
const segments = paths
.map((path) => trimSlashes(path))
.join(sep)
.split(sep);
const resolved = normalizeSegments(segments);
if (!resolved.length) return isAbsolute ? sep : ".";
const result = resolved.join(sep);
return isAbsolute ? sep + result : result;
}