Files
Netcatty/electron/plugins/viewResourceService.cjs
T
陈大猫 f4a087e890 feat(plugins): add host-rendered IDE primitives, STARTTLS/SSH bindings, file/secret APIs, and pooled image rasterizer (#4437)
- Add host-rendered native IDE primitives (PluginCodeEditor/Monaco, PluginDataGrid/@tanstack/react-virtual, PluginSplitPane, PluginErCanvas) with Shadow DOM slot projection (zero iframes, zero plugin JS in renderer)
- Expand Remote DOM budgets, expose full 22 Netcatty semantic CSS tokens, add dblclick/contextmenu/scroll/selection events, cursor-anchored PluginPopover, database theme icon, and full-bleed chrome flag
- Add dynamic user-approved TCP/TLS byte streams (incl. localhost opt-in blocking internal control ports), in-band STARTTLS upgrade with cert fingerprint confirmation, durable revocable Vault SSH host bindings, plugin-owned encrypted secrets, native ui.openFile/saveFile streaming handles, 4 MiB clipboard, and prefix-filtered storage.keys
- Optimize plugin view image pipeline with pooled sandboxed rasterizer window (auto-destroyed on idle/error/dispose), LRU version+hash cache in PluginViewResources, transient failure eviction in HostDrawnViewHost, renderer-side session deduplication, and RemoteImageElement cache + retry
- Temporarily enable plugin host by default in hostBootstrap.cjs (marked TODO(revert), opt-out via NETCATTY_PLUGIN_DEV=0)
2026-11-26 17:52:27 +08:00

89 lines
3.6 KiB
JavaScript

"use strict";
const path = require("node:path");
const { readContainedFile } = require("./pluginProtocol.cjs");
const { inspectContributionIconSource, createBoundedIconRasterizer } = require("./contributionIconService.cjs");
const { createPooledContributionIconRasterizer } = require("./contributionIconRasterizer.cjs");
const MAX_VIEW_RESOURCE_CACHE_ENTRIES = 256;
const MAX_VIEW_RESOURCE_CACHE_BYTES = 24 * 1024 * 1024;
function buildCacheKey(plugin, resource) {
if (!plugin || typeof plugin.id !== "string" || typeof plugin.activeVersion !== "string") return null;
return JSON.stringify([plugin.id, plugin.activeVersion, plugin.archiveSha256 ?? "", resource]);
}
class PluginViewResources {
constructor({ electron, packageStore }) {
this.packageStore = packageStore;
this.pooledRasterizer = createPooledContributionIconRasterizer(electron);
this.rasterize = createBoundedIconRasterizer(this.pooledRasterizer, { maxConcurrent: 8, maxQueued: 256 });
this.cache = new Map();
this.cacheBytes = 0;
this.pending = new Map();
}
#getCached(key) {
if (!key || !this.cache.has(key)) return undefined;
const value = this.cache.get(key);
this.cache.delete(key);
this.cache.set(key, value);
return value;
}
#setCached(key, value) {
if (!key) return;
const size = value.url.length;
if (size > MAX_VIEW_RESOURCE_CACHE_BYTES) return;
if (this.cache.has(key)) {
this.cacheBytes -= this.cache.get(key).url.length;
this.cache.delete(key);
}
while (this.cache.size >= MAX_VIEW_RESOURCE_CACHE_ENTRIES || this.cacheBytes + size > MAX_VIEW_RESOURCE_CACHE_BYTES) {
const oldestKey = this.cache.keys().next().value;
if (oldestKey === undefined) break;
this.cacheBytes -= this.cache.get(oldestKey).url.length;
this.cache.delete(oldestKey);
}
this.cache.set(key, value);
this.cacheBytes += size;
}
async image(plugin, resource) {
if (typeof resource !== 'string' || resource.length > 256 || !/^[a-zA-Z0-9_-][a-zA-Z0-9_./-]*$/.test(resource)
|| resource.split('/').some((part) => !part || part === '.' || part === '..')) throw new TypeError('Invalid plugin image path');
const cacheKey = buildCacheKey(plugin, resource);
const cached = this.#getCached(cacheKey);
if (cached) return cached;
if (cacheKey && this.pending.has(cacheKey)) return this.pending.get(cacheKey);
const task = (async () => {
const root = await this.packageStore.preparePackageRoot(plugin);
const file = await readContainedFile(root, resource.split('/'), 512 * 1024);
if (path.extname(resource).toLowerCase() === '.svg' && /<!DOCTYPE|<\?xml-stylesheet|\b(?:href|src)\s*=|@import|url\s*\(/i.test(file.body.toString('utf8'))) throw new Error('Plugin SVG references are unavailable');
const inspected = inspectContributionIconSource(file.body, path.extname(resource).toLowerCase());
const png = await this.rasterize({ body: file.body, ...inspected, maxEdge: 2048 });
if (png.length > 4 * 1024 * 1024) throw new Error('Plugin image exceeds size limit');
inspectContributionIconSource(png, '.png', 2048);
const result = { url: `data:image/png;base64,${png.toString('base64')}` };
this.#setCached(cacheKey, result);
return result;
})();
if (cacheKey) {
this.pending.set(cacheKey, task);
task.finally(() => {
if (this.pending.get(cacheKey) === task) this.pending.delete(cacheKey);
}).catch(() => {});
}
return task;
}
dispose() {
this.pooledRasterizer?.dispose?.();
this.cache.clear();
this.cacheBytes = 0;
this.pending.clear();
}
}
module.exports = { PluginViewResources };