mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
* plugins: open only the install-source repository link and render only host-served plugin images * i18n: drop the removed plugin Homepage label
81 lines
2.9 KiB
TypeScript
81 lines
2.9 KiB
TypeScript
/** Plugin images reach the renderer only through this scheme, served by main from bytes it has
|
|
* verified; the renderer never loads a plugin or catalog image from anywhere else. */
|
|
export const PLUGIN_IMAGE_SCHEME = 'orca-plugin-image'
|
|
|
|
export type PluginImageReference =
|
|
/** Bytes main verified and cached by hash (catalog images, preview images). */
|
|
| { kind: 'sha'; sha256: string }
|
|
/** An image an installed plugin declares, pinned to the installed content hash. */
|
|
| { kind: 'installed'; pluginKey: string; contentHash: string; slot: string }
|
|
|
|
export type PluginHostImage = { url: string; alt: string; width?: number; height?: number }
|
|
|
|
export type PluginHostImages = {
|
|
icon?: PluginHostImage
|
|
cover?: PluginHostImage
|
|
screenshots?: (PluginHostImage & { caption?: string; theme?: 'light' | 'dark' })[]
|
|
}
|
|
|
|
export function pluginShaImageUrl(sha256: string): string {
|
|
return `${PLUGIN_IMAGE_SCHEME}://sha/${sha256}`
|
|
}
|
|
|
|
/** `slot` is `icon`, `cover`, `screenshot-<index>`, or a contribution's icon: `editor-<index>`
|
|
* (custom editors) or `new-file-<index>` (new files). */
|
|
export function pluginInstalledImageUrl(
|
|
pluginKey: string,
|
|
contentHash: string,
|
|
slot: string
|
|
): string {
|
|
return `${PLUGIN_IMAGE_SCHEME}://installed/${encodeURIComponent(pluginKey)}/${contentHash}/${slot}`
|
|
}
|
|
|
|
export function parsePluginImageUrl(url: string): PluginImageReference | null {
|
|
let parsed: URL
|
|
try {
|
|
parsed = new URL(url)
|
|
} catch {
|
|
return null
|
|
}
|
|
if (parsed.protocol !== `${PLUGIN_IMAGE_SCHEME}:`) {
|
|
return null
|
|
}
|
|
const parts = parsed.pathname.split('/').filter(Boolean)
|
|
if (parsed.hostname === 'sha' && parts.length === 1 && /^[0-9a-f]{64}$/.test(parts[0] ?? '')) {
|
|
return { kind: 'sha', sha256: parts[0] ?? '' }
|
|
}
|
|
const [key, contentHash, slot] = parts
|
|
if (
|
|
parsed.hostname === 'installed' &&
|
|
parts.length === 3 &&
|
|
key &&
|
|
contentHash &&
|
|
slot &&
|
|
/^(?:[0-9a-f]{64}|dev)$/.test(contentHash) &&
|
|
/^(?:icon|cover|(?:screenshot|editor|new-file)-\d{1,2})$/.test(slot)
|
|
) {
|
|
return { kind: 'installed', pluginKey: decodeURIComponent(key), contentHash, slot }
|
|
}
|
|
return null
|
|
}
|
|
|
|
/** Only the images main serves over the plugin image scheme. Why: entries can come from a paired
|
|
* remote host, and the renderer's CSP allows web images, so any other URL could hotlink. */
|
|
export function servedPluginHostImages(
|
|
images: PluginHostImages | undefined
|
|
): PluginHostImages | undefined {
|
|
if (!images) {
|
|
return undefined
|
|
}
|
|
const served = <T extends PluginHostImage>(image: T | undefined): T | undefined =>
|
|
image && parsePluginImageUrl(image.url) ? image : undefined
|
|
const icon = served(images.icon)
|
|
const cover = served(images.cover)
|
|
const screenshots = images.screenshots?.filter((shot) => served(shot))
|
|
return {
|
|
...(icon ? { icon } : {}),
|
|
...(cover ? { cover } : {}),
|
|
...(screenshots?.length ? { screenshots } : {})
|
|
}
|
|
}
|