mirror of
https://github.com/nexu-io/openmotion.git
synced 2026-12-02 01:59:36 +08:00
* fix(fonts): require an imported licence beside its font Asset import accepts a font sourcePath and an optional licensePath. The licence reader required only an absolute path and valid UTF-8 text under 128 KiB, then wrote the bytes into the project as assets/fonts/<id>.license.txt. Project reads serve text extensions back to the caller, so the same install could read any readable text file through a two-step detour: import it as a licence, then read it as project source. The argument is genuinely useful for the normal case, where a font download ships with OFL.txt next to it, but it was not limited to that case. A licence now has to sit in the same directory as the font the user selected. The phase-one call site passes the original font path so the importer has something to compare against, and a mismatch fails with FONT_LICENSE_NOT_BESIDE_FONT. Nothing else about font import changes: the parsed family, weight and style, the relative @font-face CSS, the version transaction and the optional licence text all behave as before. Review follow-up: the constraint is now documented where callers read it, because the new rejection changes the accepted input of a public tool. The licensePath schema description in shared/phase1-tools.ts names the same-directory requirement and the error code, and the MCP contract document, the native authoring reference and the font preparation reference say the same thing next to their existing licence instructions. Validation: tests/font-import.test.ts passes with a negative case that copies the fixture licence into another directory and expects FONT_LICENSE_NOT_BESIDE_FONT, while the existing Orbitron plus OFL positive case still passes. tests/mcp-onboarding.test.ts passes 9/9 after the description change, and npm run check passes. * fix(fonts): require the source path for license validation --------- Co-authored-by: lefarcen <935902669@qq.com>
39 lines
2.6 KiB
TypeScript
39 lines
2.6 KiB
TypeScript
import { createHash } from 'node:crypto';
|
|
import { constants } from 'node:fs';
|
|
import { open, writeFile } from 'node:fs/promises';
|
|
import path from 'node:path';
|
|
import { loadFontParser, prepareFont, readFontFile } from './studio-fonts.ts';
|
|
|
|
export const isFontFilePath = (filename: string) => /\.(?:ttf|otf|ttc|woff2?)$/i.test(filename);
|
|
|
|
/** Prepare font bytes and optional license in staging; the caller owns the project transaction. */
|
|
export async function prepareImportedFont(staged: string, licensePath: string | undefined, assetId: string, staging: string, runtimeRoot: string, fontPath: string) {
|
|
const { data, format, ...font } = prepareFont(await readFontFile(staged), loadFontParser(runtimeRoot));
|
|
await writeFile(staged, data);
|
|
const relative = `assets/fonts/${assetId}.${format}`;
|
|
const changes: Array<{ path: string; source: string }> = [{ path: relative, source: staged }];
|
|
let license: { path: string; sha256: string } | undefined;
|
|
if (licensePath) {
|
|
if (!path.isAbsolute(licensePath)) throw new Error('FONT_LICENSE_NOT_READABLE: absolute path required');
|
|
// A licence belongs to the font file the user selected. Requiring the same directory stops this
|
|
// argument from copying an unrelated readable file (keys, .env) into the project.
|
|
if (path.dirname(path.resolve(licensePath)) !== path.dirname(path.resolve(fontPath)))
|
|
throw new Error('FONT_LICENSE_NOT_BESIDE_FONT: place the licence next to the font file');
|
|
const handle = await open(licensePath, constants.O_RDONLY | constants.O_NOFOLLOW | (constants.O_NONBLOCK ?? 0));
|
|
let bytes: Buffer;
|
|
try {
|
|
const info = await handle.stat();
|
|
if (!info.isFile() || info.size > 131072) throw new Error('INVALID_FONT_LICENSE: expected text at most 128 KiB');
|
|
bytes = await handle.readFile();
|
|
if (bytes.length > 131072 || !new TextDecoder('utf-8', { fatal: true }).decode(bytes).trim()) throw new Error('INVALID_FONT_LICENSE');
|
|
} finally { await handle.close(); }
|
|
const source = path.join(staging, assetId + '.license.txt');
|
|
await writeFile(source, bytes);
|
|
license = { path: `assets/fonts/${assetId}.license.txt`, sha256: createHash('sha256').update(bytes).digest('hex') };
|
|
changes.push({ path: license.path, source });
|
|
}
|
|
const cssString = (value: string) => JSON.stringify(value).replace(/</g, '\\3c ');
|
|
const fontFaceCss = `@font-face { font-family: ${cssString(font.family)}; src: url(${cssString(relative)}); font-weight: ${font.weight}; font-style: ${font.style}; }`;
|
|
return { changes, path: relative, mimeType: `font/${format}`, font, fontFaceCss, ...(license ? { license } : {}) };
|
|
}
|