@luxorai/sandbox
Version:
TypeScript SDK for Luxor Sandbox: cloud sandboxes (microVMs) for AI agents on Luxor Agent Cloud
87 lines • 4.42 kB
JavaScript
import { Code, ConnectError } from "@connectrpc/connect";
import { Http2SessionManager } from "@connectrpc/connect-node";
export const isBunRuntime = typeof process.versions.bun === "string";
// Matches the Go SDK's HTTP/2 ReadIdleTimeout/PingTimeout so a dead connection fails within ~35s.
export const KEEPALIVE_PING_OPTIONS = { pingIntervalMs: 30_000, pingTimeoutMs: 5_000 };
// Sessions on a data-plane host share one connection; Node and Bun count queued request bodies
// against maxSessionMemory (10 MB default) and reset responses past it with ENHANCE_YOUR_CALM.
// 320 MB fits eight max-size (32 MiB) unary writes queued at once (~268 MB) plus framing overhead.
// Node's default 64 KiB stream window caps one response at 64 KiB per round trip (~1 MB/s at 65 ms).
export const DATA_PLANE_SESSION_OPTIONS = {
maxSessionMemory: 320,
...(isBunRuntime ? {} : { settings: { initialWindowSize: 16 * 1024 * 1024 } }),
};
// Bun's node:http2 client destroys a stream as soon as END_STREAM arrives, even when
// DATA frames coalesced into the same socket read are still buffered, so the reader
// fails with ERR_STREAM_PREMATURE_CLOSE (oven-sh/bun#14249; still present in Bun 1.4.0).
// Node defers the destroy until the consumer drains the buffer; this restores that.
export function deferDestroyUntilDrained(stream) {
const destroy = stream.destroy.bind(stream);
stream.destroy = ((err) => {
const cleanClose = !err && (stream.rstCode === undefined || stream.rstCode === 0);
if (!cleanClose || stream.readableEnded || stream.readableLength === 0)
return destroy(err);
stream.once("end", () => {
if (!stream.destroyed)
destroy();
});
stream.read(0);
return stream;
});
}
// connect-node rejects only on non-zero reset codes, so a stream that ends cleanly before any
// response (an early RST_STREAM(NO_ERROR), or Node dropping headers over maxSessionMemory) would
// leave the RPC pending forever. Internal, not Unavailable: the server may already have run it.
export function failCloseWithoutResponse(stream) {
let answered = false;
stream.once("response", () => {
answered = true;
});
stream.once("error", () => {
answered = true;
});
stream.once("close", () => {
const rstCode = stream.rstCode ?? 0;
if (answered || (rstCode !== 0 && rstCode !== 5))
return;
stream.emit("error", new ConnectError(`http/2 stream closed without a response (rstCode ${rstCode})`, Code.Internal));
});
}
// Runtime-compatibility boundary: every stream the connect-node transport opens goes
// through request(), so this is the one place the Bun drain fix can be applied.
export class DrainingHttp2SessionManager extends Http2SessionManager {
drain;
enlargeWindow;
configuredSessions = new WeakSet();
constructor(url, pingOptions, http2SessionOptions, drain = isBunRuntime,
// Bun's setLocalWindowSize stalls every response larger than the default 64 KiB window.
enlargeWindow = !isBunRuntime) {
super(url, pingOptions, http2SessionOptions);
this.drain = drain;
this.enlargeWindow = enlargeWindow;
}
async request(method, path, headers, options) {
const stream = await super.request(method, path, headers, options);
const session = stream.session;
if (session && !session.destroyed && !session.closed && !this.configuredSessions.has(session)) {
this.configuredSessions.add(session);
// connect-node can destroy a session for a PING timeout after it detached its own error
// listener; streams still observe the failure, so keep the late session error from crashing.
session.on("error", () => { });
if (this.enlargeWindow) {
try {
// Concurrent streams share the connection's receive window.
session.setLocalWindowSize(Math.max(session.state.effectiveLocalWindowSize ?? 0, 16 * 1024 * 1024));
}
catch {
// Keep the default window rather than failing a stream that is already open.
}
}
}
failCloseWithoutResponse(stream);
if (this.drain)
deferDestroyUntilDrained(stream);
return stream;
}
}
//# sourceMappingURL=http2-drain.js.map