Files
Netcatty/electron/preload/terminalOutputPorts.cjs
T
陈大猫andryan-wong-coder 67f8108713 feat(plugins): add reviewed privileged terminal data pipeline (#2402)
* feat(plugins): add privileged terminal data pipeline

* fix(plugins): harden terminal interceptor lifecycle

* fix(plugins): close terminal interceptor lifecycle gaps

* fix(plugins): preserve terminal interceptor state transitions

* fix(plugin): close terminal pipeline lifecycle gaps

* fix(plugin): harden terminal interceptor startup

* fix(plugin): isolate terminal output route attempts

* fix(plugin): isolate reused terminal sessions

* fix(plugin): harden terminal interceptor boundaries

* fix(plugin): harden terminal replay metadata

* fix(plugin): preserve terminal replay provenance

* fix(plugin): close terminal pipeline lifecycle races

* fix(plugin): harden terminal route handoff

* fix(plugin): contain terminal handoff failures

* fix(plugin): route terminal attachment through RPC

* fix(plugin): validate terminal attachment RPC

* fix(plugin): close unexpected runtime ports

* fix(plugin): canonicalize terminal data frames

* fix(plugin): close terminal pipeline lifecycle gaps

* fix(plugin): close terminal interceptor review gaps

* fix(plugin): harden terminal interceptor protocol handling

* fix(plugin): preserve terminal flow compatibility

* fix(plugin): preserve terminal protocol accounting

* fix(plugin): bound terminal interceptor lifecycle

* fix(plugin): close terminal interceptor review gaps

---------

Co-authored-by: ryan-wong-coder <wangsiyuan19990317@gmail.com>
2026-07-23 09:42:47 +08:00

81 lines
2.3 KiB
JavaScript

"use strict";
const { TERMINAL_OUTPUT_PORT_CHANNEL } = require("../bridges/terminalOutputChannel.cjs");
const { hasPluginPipelineIngress } = require("./terminalDataBacklog.cjs");
function createTerminalOutputPortRegistry(options = {}) {
const {
ipcRenderer,
deliverToListeners,
filterData = null,
closedTerminalDataSessions = new Set(),
onPortError = console.error,
onDrain = null,
} = options;
const ports = new Map();
function closeSession(sessionId) {
const port = ports.get(sessionId);
if (!port) return;
try {
port.close?.();
} catch {
// Ignore close races while replacing or closing output ports.
}
ports.delete(sessionId);
}
function registerPort(sessionId, port) {
if (!sessionId || !port) return;
closeSession(sessionId);
ports.set(sessionId, port);
port.onmessage = (event) => {
const message = event?.data || {};
const targetSessionId = message.sessionId || sessionId;
if (message.kind === "drain" && message.requestId) {
onDrain?.(targetSessionId, message.requestId);
return;
}
if (closedTerminalDataSessions.has(targetSessionId)) return;
if (!message.data && !hasPluginPipelineIngress(message.meta)) return;
try {
const filtered = typeof filterData === "function"
? filterData(targetSessionId, message.data, message)
: message.data;
const data = filtered && typeof filtered === "object" && "data" in filtered
? filtered.data
: filtered;
const meta = filtered && typeof filtered === "object" && "data" in filtered
? filtered.meta
: message.meta;
if (data || hasPluginPipelineIngress(meta)) {
deliverToListeners?.(targetSessionId, data ?? "", meta);
}
} catch (err) {
onPortError("Terminal output port callback failed", err);
}
};
}
function register() {
ipcRenderer?.on?.(TERMINAL_OUTPUT_PORT_CHANNEL, (event, payload) => {
registerPort(payload?.sessionId, event?.ports?.[0]);
});
}
return {
register,
closeSession,
closeAll() {
for (const sessionId of Array.from(ports.keys())) {
closeSession(sessionId);
}
},
hasSessionForTest: (sessionId) => ports.has(sessionId),
};
}
module.exports = {
createTerminalOutputPortRegistry,
};