fix(protocol): 同步公开插件分享的图标字段

Signed-off-by: liugaowei <liugaowei@xd.com>
This commit is contained in:
liugaowei
2026-10-26 13:59:58 +08:00
parent ae84cb8db1
commit cd71d42633
2 changed files with 21 additions and 0 deletions

View File

@@ -26,6 +26,19 @@ describe("public plugin presentation boundary", () => {
}),
).toEqual(publicView);
});
it("preserves an embedded current plugin icon without exposing its storage location", () => {
const icon = "data:image/png;base64,iVBORw0KGgo=";
expect(parsePublicPluginPresentation({ ...publicView, icon })).toEqual({ ...publicView, icon });
expect(parsePublicPluginPresentation({ ...publicView, icon: null }).icon).toBeNull();
});
it.each([
"https://example.test/icon.png",
"data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=",
"data:image/png;base64,invalid!",
`data:image/png;base64,${"A".repeat(700_000)}`,
])("uses the missing-icon state for an unsafe or oversized image", (icon) => {
expect(parsePublicPluginPresentation({ ...publicView, icon }).icon).toBeNull();
});
it.each([
{ schemaVersion: 2 },
{ pluginId: "../../private" },

View File

@@ -15,6 +15,8 @@ export interface PublicPluginPresentation {
author: string | null;
version: string;
publishedAt: string;
/** Current verified raster icon, embedded without a storage URL; absent on older servers. */
icon?: string | null;
}
export function parsePublicPluginPresentation(
@@ -26,6 +28,11 @@ export function parsePublicPluginPresentation(
}
const nullableText = (value: unknown, path: string, max: number) =>
value === null || value === "" ? null : string(value, path, max);
const image = typeof row.icon === "string" && row.icon.length <= Math.ceil(512 * 1024 / 3) * 4 + 32
? /^data:image\/(png|jpeg|webp|gif);base64,([A-Za-z0-9+/]+={0,2})$/.exec(row.icon) : null;
const base64 = image?.[2] ?? "";
const size = base64.length / 4 * 3 - (base64.endsWith("==") ? 2 : base64.endsWith("=") ? 1 : 0);
const icon = image && base64.length % 4 === 0 && size <= 512 * 1024 ? row.icon as string : null;
return {
schemaVersion: 1,
pluginId: row.pluginId,
@@ -34,5 +41,6 @@ export function parsePublicPluginPresentation(
author: nullableText(row.author, "author", 256),
version: string(row.version, "version", 128),
publishedAt: isoDate(row.publishedAt, "publishedAt"),
...(row.icon === undefined ? {} : { icon }),
};
}