Files
Netcatty/electron/plugins/runtime/byteStreamClient.mjs
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

65 lines
3.1 KiB
JavaScript

import { PluginError } from "@netcatty/plugin-sdk";
import { PLUGIN_BYTE_STREAM_FEATURE } from "@netcatty/plugin-contract";
export async function connectByteStream({ client, streams, subscriptions, enabledFeatures }, request, cancellationToken, viaHost = false) {
if (!enabledFeatures.includes(PLUGIN_BYTE_STREAM_FEATURE)) throw new PluginError("unsupported", "Byte stream feature is unavailable");
if (viaHost && !enabledFeatures.includes("network.host-byte-streams")) throw new PluginError("unsupported", "Host byte stream feature is unavailable");
const descriptor = await client.request(viaHost ? "network.stream.connectViaHost" : "network.stream.connect", request, { cancellationToken, ...(viaHost ? { deadlineMs: 120_000 } : {}) });
if (descriptor.cancelled) throw new PluginError("cancelled", "Host connection cancelled");
let readable;
let writable;
let readablePromise;
let closing;
let cancellation;
let connection;
const close = () => {
closing ??= client.request("network.stream.close", { handleId: descriptor.handleId })
.finally(() => {
cancellation?.dispose();
streams.rejectReadable(descriptor.outputStreamId);
if (connection) subscriptions.delete(connection);
});
return closing;
};
const cancel = () => { void close().catch(() => {}); };
const checkCancelled = async () => {
if (!cancellationToken?.isCancellationRequested) return;
await close().catch(() => {});
throw new PluginError("cancelled", "Byte stream connection cancelled");
};
const upgradeTls = (options = {}) => client.request("network.stream.upgradeTls", {
handleId: descriptor.handleId,
...(options?.servername === undefined ? {} : { servername: options.servername }),
...(options?.allowSelfSigned === undefined ? {} : { allowSelfSigned: options.allowSelfSigned }),
...(options?.caPem === undefined ? {} : { caPem: options.caPem }),
}, { cancellationToken, deadlineMs: 60_000 });
try {
await checkCancelled();
readablePromise = streams.acceptReadable(descriptor.outputStreamId);
void readablePromise.catch(() => {});
writable = await streams.openWritable(descriptor.inputStreamId, descriptor.windowBytes);
const attaching = client.request("network.stream.attach", { handleId: descriptor.handleId }, { cancellationToken });
[readable] = await Promise.all([readablePromise, attaching]);
await checkCancelled();
connection = Object.freeze({
endpoint: descriptor.endpoint,
...(descriptor.hostBindingToken ? { hostBindingToken: descriptor.hostBindingToken } : {}),
readable: Object.freeze({
id: readable.id, read: () => readable.read(), cancel, dispose: cancel,
}),
writable: Object.freeze({
id: writable.id, write: (bytes) => writable.write(bytes), end: () => writable.end(),
fail: cancel, cancel, dispose: cancel,
}),
upgradeTls,
close, dispose: cancel,
});
cancellation = cancellationToken?.onCancellationRequested(cancel);
subscriptions.add(connection);
return connection;
} catch (error) {
await close().catch(() => {});
throw error;
}
}