69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
import { WS_METHODS } from "@t3tools/contracts";
|
|
import * as Effect from "effect/Effect";
|
|
import type { RpcServer } from "effect/rpc";
|
|
|
|
const MAX_PENDING_CHUNKS = 8;
|
|
const MAX_PENDING_BYTES = 64 * 1024;
|
|
const isFull = (sizes: number[]) =>
|
|
sizes.length >= MAX_PENDING_CHUNKS ||
|
|
sizes.reduce((total, size) => total + size, 0) >= MAX_PENDING_BYTES;
|
|
|
|
export function withTerminalOutputWindow(
|
|
protocol: RpcServer.Protocol["Service"],
|
|
): RpcServer.Protocol["Service"] {
|
|
const windows = new Map<string, number[]>();
|
|
let receive: Parameters<RpcServer.Protocol["Service"]["run"]>[0];
|
|
return {
|
|
...protocol,
|
|
run: (write) => {
|
|
receive = write;
|
|
return protocol.run((clientId, message) =>
|
|
Effect.suspend(() => {
|
|
if (
|
|
message._tag === "Request" &&
|
|
(message.tag === WS_METHODS.terminalAttach ||
|
|
message.tag === WS_METHODS.terminalObserve ||
|
|
message.tag === WS_METHODS.subscribeTerminalEvents)
|
|
) {
|
|
const key = `${clientId}:${message.id}`;
|
|
if (!windows.has(key)) windows.set(key, []);
|
|
} else if (message._tag === "Ack") {
|
|
const window = windows.get(`${clientId}:${message.requestId}`);
|
|
if (window) {
|
|
const wasFull = isFull(window);
|
|
window.shift();
|
|
if (!wasFull || isFull(window)) return Effect.void;
|
|
}
|
|
}
|
|
return write(clientId, message);
|
|
}),
|
|
);
|
|
},
|
|
send: (clientId, response, transferables) =>
|
|
Effect.suspend(() => {
|
|
const send = protocol.send(clientId, response, transferables);
|
|
if (response._tag === "Exit") {
|
|
windows.delete(`${clientId}:${response.requestId}`);
|
|
} else if (response._tag === "Chunk") {
|
|
const window = windows.get(`${clientId}:${response.requestId}`);
|
|
if (window) {
|
|
// @effect-diagnostics-next-line preferSchemaOverJson:off
|
|
const size = Buffer.byteLength(JSON.stringify(response));
|
|
window.push(size);
|
|
if (!isFull(window)) {
|
|
return send.pipe(
|
|
Effect.andThen(() =>
|
|
receive(clientId, {
|
|
_tag: "Ack",
|
|
requestId: response.requestId,
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
return send;
|
|
}),
|
|
};
|
|
}
|