Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Improve timeout handling

Tomas Valenta2a0258f0eb33
diff --git a/apps/docs/src/code/python/basics/get_url.py b/apps/docs/src/code/python/basics/get_url.py
index afab231d0..f71d79377 100644
--- a/apps/docs/src/code/python/basics/get_url.py
+++ b/apps/docs/src/code/python/basics/get_url.py
@@ -2,7 +2,7 @@ from e2b import Sandbox
sandbox = Sandbox(template="base")
-url = sandbox.get_hostname() # $HighlightLine
+url = sandbox.get_host() # $HighlightLine
print("https://" + url)
sandbox.close()
diff --git a/apps/docs/src/code/python/basics/get_url_port.py b/apps/docs/src/code/python/basics/get_url_port.py
index 3e273eace..bb99d750e 100644
--- a/apps/docs/src/code/python/basics/get_url_port.py
+++ b/apps/docs/src/code/python/basics/get_url_port.py
@@ -3,7 +3,7 @@ from e2b import Sandbox
sandbox = Sandbox(template="base")
open_port = 3000
-url = sandbox.get_hostname(open_port) # $HighlightLine
+url = sandbox.get_host(open_port) # $HighlightLine
print("https://" + url)
sandbox.close()
diff --git a/packages/js-sdk/src/connectionConfig.ts b/packages/js-sdk/src/connectionConfig.ts
index 1796e963c..e9e3ffa02 100644
--- a/packages/js-sdk/src/connectionConfig.ts
+++ b/packages/js-sdk/src/connectionConfig.ts
@@ -8,19 +8,22 @@ export interface ConnectionOpts {
apiKey?: string
domain?: string
debug?: boolean
+ requestTimeoutMs?: number
}
export class ConnectionConfig {
readonly debug: boolean
readonly domain: string
readonly apiUrl: string
+ readonly requestTimeoutMs?: number
private readonly _apiKey?: string
- constructor(opts: ConnectionOpts) {
- this._apiKey = opts.apiKey || API_KEY
- this.debug = opts.debug || DEBUG
- this.domain = opts.domain || DOMAIN
+ constructor(opts?: ConnectionOpts) {
+ this._apiKey = opts?.apiKey || API_KEY
+ this.debug = opts?.debug || DEBUG
+ this.domain = opts?.domain || DOMAIN
+ this.requestTimeoutMs = opts?.requestTimeoutMs
this.apiUrl = this.debug ? 'http://localhost:3000' : `https://api.${this.domain}`
}
diff --git a/packages/js-sdk/src/envd/network/v1/network_connect.ts b/packages/js-sdk/src/envd/network/v1/network_connect.ts
deleted file mode 100644
index 18dbf282c..000000000
--- a/packages/js-sdk/src/envd/network/v1/network_connect.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-// @generated by protoc-gen-connect-es v1.4.0 with parameter "target=ts"
-// @generated from file envd/network/v1/network.proto (package envd.network.v1, syntax proto3)
-/* eslint-disable */
-// @ts-nocheck
-
-import { ListPortsRequest, ListPortsResponse, WatchPortsRequest, WatchPortsResponse } from "./network_pb.js";
-import { MethodKind } from "@bufbuild/protobuf";
-
-/**
- * @generated from service envd.network.v1.NetworkService
- */
-export const NetworkService = {
- typeName: "envd.network.v1.NetworkService",
- methods: {
- /**
- * @generated from rpc envd.network.v1.NetworkService.ListPorts
- */
- listPorts: {
- name: "ListPorts",
- I: ListPortsRequest,
- O: ListPortsResponse,
- kind: MethodKind.Unary,
- },
- /**
- * @generated from rpc envd.network.v1.NetworkService.WatchPorts
- */
- watchPorts: {
- name: "WatchPorts",
- I: WatchPortsRequest,
- O: WatchPortsResponse,
- kind: MethodKind.ServerStreaming,
- },
- }
-} as const;
-
diff --git a/packages/js-sdk/src/envd/network/v1/network_pb.ts b/packages/js-sdk/src/envd/network/v1/network_pb.ts
deleted file mode 100644
index 66e5d7bb3..000000000
--- a/packages/js-sdk/src/envd/network/v1/network_pb.ts
+++ /dev/null
@@ -1,256 +0,0 @@
-// @generated by protoc-gen-es v1.9.0 with parameter "target=ts"
-// @generated from file envd/network/v1/network.proto (package envd.network.v1, syntax proto3)
-/* eslint-disable */
-// @ts-nocheck
-
-import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf";
-import { Message, proto3 } from "@bufbuild/protobuf";
-
-/**
- * @generated from enum envd.network.v1.PortState
- */
-export enum PortState {
- /**
- * @generated from enum value: PORT_STATE_UNSPECIFIED = 0;
- */
- UNSPECIFIED = 0,
-
- /**
- * @generated from enum value: PORT_STATE_OPEN = 1;
- */
- OPEN = 1,
-
- /**
- * @generated from enum value: PORT_STATE_CLOSED = 2;
- */
- CLOSED = 2,
-}
-// Retrieve enum metadata with: proto3.getEnumType(PortState)
-proto3.util.setEnumType(PortState, "envd.network.v1.PortState", [
- { no: 0, name: "PORT_STATE_UNSPECIFIED" },
- { no: 1, name: "PORT_STATE_OPEN" },
- { no: 2, name: "PORT_STATE_CLOSED" },
-]);
-
-/**
- * @generated from message envd.network.v1.ListPortsRequest
- */
-export class ListPortsRequest extends Message<ListPortsRequest> {
- constructor(data?: PartialMessage<ListPortsRequest>) {
- super();
- proto3.util.initPartial(data, this);
- }
-
- static readonly runtime: typeof proto3 = proto3;
- static readonly typeName = "envd.network.v1.ListPortsRequest";
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
- ]);
-
- static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): ListPortsRequest {
- return new ListPortsRequest().fromBinary(bytes, options);
- }
-
- static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): ListPortsRequest {
- return new ListPortsRequest().fromJson(jsonValue, options);
- }
-
- static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): ListPortsRequest {
- return new ListPortsRequest().fromJsonString(jsonString, options);
- }
-
- static equals(a: ListPortsRequest | PlainMessage<ListPortsRequest> | undefined, b: ListPortsRequest | PlainMessage<ListPortsRequest> | undefined): boolean {
- return proto3.util.equals(ListPortsRequest, a, b);
- }
-}
-
-/**
- * @generated from message envd.network.v1.ListPortsResponse
- */
-export class ListPortsResponse extends Message<ListPortsResponse> {
- /**
- * @generated from field: repeated envd.network.v1.Port ports = 1;
- */
- ports: Port[] = [];
-
- constructor(data?: PartialMessage<ListPortsResponse>) {
- super();
- proto3.util.initPartial(data, this);
- }
-
- static readonly runtime: typeof proto3 = proto3;
- static readonly typeName = "envd.network.v1.ListPortsResponse";
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
- { no: 1, name: "ports", kind: "message", T: Port, repeated: true },
- ]);
-
- static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): ListPortsResponse {
- return new ListPortsResponse().fromBinary(bytes, options);
- }
-
- static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): ListPortsResponse {
- return new ListPortsResponse().fromJson(jsonValue, options);
- }
-
- static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): ListPortsResponse {
- return new ListPortsResponse().fromJsonString(jsonString, options);
- }
-
- static equals(a: ListPortsResponse | PlainMessage<ListPortsResponse> | undefined, b: ListPortsResponse | PlainMessage<ListPortsResponse> | undefined): boolean {
- return proto3.util.equals(ListPortsResponse, a, b);
- }
-}
-
-/**
- * @generated from message envd.network.v1.WatchPortsRequest
- */
-export class WatchPortsRequest extends Message<WatchPortsRequest> {
- /**
- * @generated from field: envd.network.v1.PortFilter filter = 1;
- */
- filter?: PortFilter;
-
- constructor(data?: PartialMessage<WatchPortsRequest>) {
- super();
- proto3.util.initPartial(data, this);
- }
-
- static readonly runtime: typeof proto3 = proto3;
- static readonly typeName = "envd.network.v1.WatchPortsRequest";
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
- { no: 1, name: "filter", kind: "message", T: PortFilter },
- ]);
-
- static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): WatchPortsRequest {
- return new WatchPortsRequest().fromBinary(bytes, options);
- }
-
- static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): WatchPortsRequest {
- return new WatchPortsRequest().fromJson(jsonValue, options);
- }
-
- static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): WatchPortsRequest {
- return new WatchPortsRequest().fromJsonString(jsonString, options);
- }
-
- static equals(a: WatchPortsRequest | PlainMessage<WatchPortsRequest> | undefined, b: WatchPortsRequest | PlainMessage<WatchPortsRequest> | undefined): boolean {
- return proto3.util.equals(WatchPortsRequest, a, b);
- }
-}
-
-/**
- * @generated from message envd.network.v1.PortFilter
- */
-export class PortFilter extends Message<PortFilter> {
- /**
- * @generated from field: repeated uint32 ports = 1;
- */
- ports: number[] = [];
-
- constructor(data?: PartialMessage<PortFilter>) {
- super();
- proto3.util.initPartial(data, this);
- }
-
- static readonly runtime: typeof proto3 = proto3;
- static readonly typeName = "envd.network.v1.PortFilter";
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
- { no: 1, name: "ports", kind: "scalar", T: 13 /* ScalarType.UINT32 */, repeated: true },
- ]);
-
- static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): PortFilter {
- return new PortFilter().fromBinary(bytes, options);
- }
-
- static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): PortFilter {
- return new PortFilter().fromJson(jsonValue, options);
- }
-
- static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): PortFilter {
- return new PortFilter().fromJsonString(jsonString, options);
- }
-
- static equals(a: PortFilter | PlainMessage<PortFilter> | undefined, b: PortFilter | PlainMessage<PortFilter> | undefined): boolean {
- return proto3.util.equals(PortFilter, a, b);
- }
-}
-
-/**
- * @generated from message envd.network.v1.WatchPortsResponse
- */
-export class WatchPortsResponse extends Message<WatchPortsResponse> {
- /**
- * @generated from field: envd.network.v1.Port event = 1;
- */
- event?: Port;
-
- constructor(data?: PartialMessage<WatchPortsResponse>) {
- super();
- proto3.util.initPartial(data, this);
- }
-
- static readonly runtime: typeof proto3 = proto3;
- static readonly typeName = "envd.network.v1.WatchPortsResponse";
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
- { no: 1, name: "event", kind: "message", T: Port },
- ]);
-
- static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): WatchPortsResponse {
- return new WatchPortsResponse().fromBinary(bytes, options);
- }
-
- static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): WatchPortsResponse {
- return new WatchPortsResponse().fromJson(jsonValue, options);
- }
-
- static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): WatchPortsResponse {
- return new WatchPortsResponse().fromJsonString(jsonString, options);
- }
-
- static equals(a: WatchPortsResponse | PlainMessage<WatchPortsResponse> | undefined, b: WatchPortsResponse | PlainMessage<WatchPortsResponse> | undefined): boolean {
- return proto3.util.equals(WatchPortsResponse, a, b);
- }
-}
-
-/**
- * @generated from message envd.network.v1.Port
- */
-export class Port extends Message<Port> {
- /**
- * @generated from field: uint32 port = 1;
- */
- port = 0;
-
- /**
- * @generated from field: envd.network.v1.PortState state = 2;
- */
- state = PortState.UNSPECIFIED;
-
- constructor(data?: PartialMessage<Port>) {
- super();
- proto3.util.initPartial(data, this);
- }
-
- static readonly runtime: typeof proto3 = proto3;
- static readonly typeName = "envd.network.v1.Port";
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
- { no: 1, name: "port", kind: "scalar", T: 13 /* ScalarType.UINT32 */ },
- { no: 2, name: "state", kind: "enum", T: proto3.getEnumType(PortState) },
- ]);
-
- static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): Port {
- return new Port().fromBinary(bytes, options);
- }
-
- static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): Port {
- return new Port().fromJson(jsonValue, options);
- }
-
- static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): Port {
- return new Port().fromJsonString(jsonString, options);
- }
-
- static equals(a: Port | PlainMessage<Port> | undefined, b: Port | PlainMessage<Port> | undefined): boolean {
- return proto3.util.equals(Port, a, b);
- }
-}
-
diff --git a/packages/js-sdk/src/envd/permissions/v1/permissions_pb.ts b/packages/js-sdk/src/envd/permissions/v1/permissions_pb.ts
deleted file mode 100644
index ba7531e4d..000000000
--- a/packages/js-sdk/src/envd/permissions/v1/permissions_pb.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-// @generated by protoc-gen-es v1.9.0 with parameter "target=ts"
-// @generated from file envd/permissions/v1/permissions.proto (package envd.permissions.v1, syntax proto3)
-/* eslint-disable */
-// @ts-nocheck
-
-import type { BinaryReadOptions, FieldList, JsonReadOptions, JsonValue, PartialMessage, PlainMessage } from "@bufbuild/protobuf";
-import { Message, proto3 } from "@bufbuild/protobuf";
-
-/**
- * @generated from message envd.permissions.v1.Credentials
- */
-export class Credentials extends Message<Credentials> {
- /**
- * @generated from oneof envd.permissions.v1.Credentials.credentials
- */
- credentials: {
- /**
- * @generated from field: string username = 1;
- */
- value: string;
- case: "username";
- } | { case: undefined; value?: undefined } = { case: undefined };
-
- constructor(data?: PartialMessage<Credentials>) {
- super();
- proto3.util.initPartial(data, this);
- }
-
- static readonly runtime: typeof proto3 = proto3;
- static readonly typeName = "envd.permissions.v1.Credentials";
- static readonly fields: FieldList = proto3.util.newFieldList(() => [
- { no: 1, name: "username", kind: "scalar", T: 9 /* ScalarType.STRING */, oneof: "credentials" },
- ]);
-
- static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): Credentials {
- return new Credentials().fromBinary(bytes, options);
- }
-
- static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): Credentials {
- return new Credentials().fromJson(jsonValue, options);
- }
-
- static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): Credentials {
- return new Credentials().fromJsonString(jsonString, options);
- }
-
- static equals(a: Credentials | PlainMessage<Credentials> | undefined, b: Credentials | PlainMessage<Credentials> | undefined): boolean {
- return proto3.util.equals(Credentials, a, b);
- }
-}
-
diff --git a/packages/js-sdk/src/sandbox/filesystem.ts b/packages/js-sdk/src/sandbox/filesystem.ts
index 29d6b7c03..7d8eb21ba 100644
--- a/packages/js-sdk/src/sandbox/filesystem.ts
+++ b/packages/js-sdk/src/sandbox/filesystem.ts
@@ -5,6 +5,7 @@ import {
PromiseClient,
} from '@connectrpc/connect'
+import { ConnectionOpts } from '../connectionConfig'
import { FilesystemService } from '../envd/filesystem/v1/filesystem_connect'
import {
FilesystemEvent as FsFilesystemEvent,
@@ -29,35 +30,42 @@ export class Filesystem {
constructor(private readonly transport: Transport) { }
- async list(path: string): Promise<EntryInfo[]> {
+ async list(path: string, opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>): Promise<EntryInfo[]> {
const params: PlainMessage<ListRequest> = {
path,
}
- const res = await this.service.list(params)
+ const res = await this.service.list(params, {
+ timeoutMs: opts?.requestTimeoutMs,
+ })
return res.entries
}
- async remove(path: string): Promise<void> {
+ async remove(path: string, opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>): Promise<void> {
const params: PlainMessage<RemoveRequest> = {
path,
}
- await this.service.remove(params)
+ await this.service.remove(params, {
+ timeoutMs: opts?.requestTimeoutMs,
+ })
}
- async exists(path: string): Promise<EntryInfo> {
+ async exists(path: string, opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>): Promise<EntryInfo> {
const params: PlainMessage<StatRequest> = {
path,
}
- const res = await this.service.stat(params)
+ const res = await this.service.stat(params, {
+ timeoutMs: opts?.requestTimeoutMs,
+ })
return res.entry!
}
async watch(
path: string,
- onEvent: (event: FilesystemEvent) => any,
+ onEvent: (event: FilesystemEvent) => void | Promise<void>,
+ opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>,
): Promise<WatchHandle> {
const params: PlainMessage<WatchRequest> = {
path,
@@ -67,12 +75,13 @@ export class Filesystem {
const req = this.service.watch(params, {
signal: controller.signal,
+ timeoutMs: opts?.requestTimeoutMs,
})
async function processStream() {
for await (const event of req) {
if (event.event) {
- onEvent?.(event.event)
+ await onEvent?.(event.event)
}
}
}
diff --git a/packages/js-sdk/src/sandbox/index.ts b/packages/js-sdk/src/sandbox/index.ts
index adb88c91a..a52b688af 100644
--- a/packages/js-sdk/src/sandbox/index.ts
+++ b/packages/js-sdk/src/sandbox/index.ts
@@ -13,7 +13,7 @@ export interface SandboxOpts extends ConnectionOpts {
*/
metadata?: Record<string, string>
logger?: Logger
- timeout?: number
+ timeoutMs?: number
}
const SANDBOX_SERVER_PORT = 49982
@@ -28,12 +28,12 @@ export class Sandbox extends SandboxApi {
private readonly connectionConfig: ConnectionConfig
private readonly files: SandboxFiles
- constructor(readonly sandboxID: string, opts: Omit<SandboxOpts, 'timeout' | 'metadata'> = {}) {
+ constructor(readonly sandboxID: string, opts: Omit<SandboxOpts, 'timeoutMs' | 'metadata'> = {}) {
super()
this.connectionConfig = new ConnectionConfig(opts)
- const sandboxServerUrl = `${this.connectionConfig.debug ? 'http' : 'https'}://${this.getHostname(SANDBOX_SERVER_PORT)}`
+ const sandboxServerUrl = `${this.connectionConfig.debug ? 'http' : 'https'}://${this.getHost(SANDBOX_SERVER_PORT)}`
this.files = new SandboxFiles(sandboxServerUrl)
@@ -54,19 +54,11 @@ export class Sandbox extends SandboxApi {
/**
* Connects to an existing Sandbox.
*/
- static async connect<S extends typeof Sandbox>(this: S, sandboxID: string, opts?: Omit<SandboxOpts, 'metadata' | 'timeout'>): Promise<InstanceType<S>> {
+ static async connect<S extends typeof Sandbox>(this: S, sandboxID: string, opts?: Omit<SandboxOpts, 'metadata' | 'timeoutMs'>): Promise<InstanceType<S>> {
return new this(sandboxID, opts) as InstanceType<S>
}
- /**
- * Get the hostname for the sandbox or for the specified sandbox's port.
- *
- * `getHostname` method requires `this` context - you may need to bind it.
- *
- * @param port Specify if you want to connect to a specific port of the sandbox
- * @returns Hostname of the sandbox or sandbox's port
- */
- getHostname(port: number) {
+ getHost(port: number) {
if (this.connectionConfig.debug) {
return `localhost:${port}`
}
diff --git a/packages/js-sdk/src/sandbox/process.ts b/packages/js-sdk/src/sandbox/process.ts
index 6d56aa7ef..c58031b92 100644
--- a/packages/js-sdk/src/sandbox/process.ts
+++ b/packages/js-sdk/src/sandbox/process.ts
@@ -9,7 +9,6 @@ import { ProcessService } from '../envd/process/v1/process_connect'
import {
ProcessConfig as PsProcessConfig,
ProcessEvent_EndEvent,
- ProcessEvent_StartEvent,
ConnectResponse,
ConnectRequest,
SendInputRequest,
@@ -21,6 +20,7 @@ import {
} from '../envd/process/v1/process_pb'
import { concatUint8Arrays } from './array'
import { createDeferredPromise } from './promise'
+import { ConnectionOpts } from '../connectionConfig'
export interface ProcessOutput {
@@ -28,11 +28,79 @@ export interface ProcessOutput {
stderr?: string
}
-export interface ProcessHandle {
- pid: number
- disconnect: () => void
- wait: () => Promise<ProcessResult>
- kill: () => Promise<void>
+class ProcessHandle {
+ private readonly end = createDeferredPromise<{
+ exit: ProcessEvent_EndEvent,
+ stdout: Uint8Array[],
+ stderr: Uint8Array[],
+ }>()
+
+ constructor(
+ readonly pid: number,
+ readonly handleDisconnect: () => void,
+ private readonly handleKill: () => Promise<void>,
+ private readonly events: AsyncIterable<ConnectResponse | StartResponse>,
+ private readonly handleStdout?: ((data: string) => void | Promise<void>),
+ private readonly handleStderr?: ((data: string) => void | Promise<void>),
+ private readonly handlePtyData?: ((data: Uint8Array) => void | Promise<void>),
+ ) {
+ this.handleEvents()
+ }
+
+ disconnect() {
+ this.handleDisconnect()
+ }
+
+ async wait(): Promise<ProcessResult> {
+ const result = await this.end.promise
+ return {
+ exitCode: result.exit.exitCode,
+ error: result.exit.error,
+ stdout: concatUint8Arrays(result.stdout).toString(),
+ stderr: concatUint8Arrays(result.stderr).toString(),
+ }
+ }
+
+ async kill() {
+ this.disconnect()
+ await this.handleKill()
+ }
+
+ private async handleEvents() {
+ const stdout: Uint8Array[] = []
+ const stderr: Uint8Array[] = []
+
+ try {
+ for await (const event of this.events) {
+ switch (event.event?.event.case) {
+ case 'data':
+ switch (event.event.event.value.output.case) {
+ case 'stdout':
+ stdout.push(event.event.event.value.output.value)
+ await this.handleStdout?.(event.event.event.value.output.value.toString())
+ break
+ case 'stderr':
+ stderr.push(event.event.event.value.output.value)
+ await this.handleStderr?.(event.event.event.value.output.value.toString())
+ break
+ case 'pty':
+ await this.handlePtyData?.(event.event.event.value.output.value)
+ break
+ }
+ break
+ case 'end':
+ this.end.resolve({
+ exit: event.event.event.value,
+ stdout,
+ stderr,
+ })
+ break
+ }
+ }
+ } catch (error) {
+ this.end.reject(error)
+ }
+ }
}
export interface ProcessResult extends ProcessOutput {
@@ -51,43 +119,46 @@ export type ProcessConfig = PlainMessage<PsProcessConfig>
// TODO: Add req timeout
// TODO: Enable using process handle as iterable
// TODO: Solve stream input
+// TODO: For watch and other stream ensure the timeout throw is handlable
+// TODO: Create class from process handler
+// TODO: Disconnect during kill
+// TODO: Add iterator returns (python too)
+// TODO: Passing timeout to envd context via metadata?
+// TODO: Finish requestTimeoutMs handling in normal API
+// TODO: Does the abort controller work when we specify the request timeout via connect rpc?
export class Process {
private readonly service: PromiseClient<typeof ProcessService> = createPromiseClient(ProcessService, this.transport)
constructor(private readonly transport: Transport) { }
- async list(): Promise<ProcessConfig[]> {
- const res = await this.service.list({})
+ async list(opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>): Promise<ProcessConfig[]> {
+ const res = await this.service.list({}, {
+ timeoutMs: opts?.requestTimeoutMs,
+ })
return res.processes
}
async start(
cmd: string,
- {
- cwd,
- user,
- envs,
- onStderr,
- onStdout,
- }: {
- cwd: string | undefined,
- user: 'root' | 'user' | undefined,
- envs: Record<string, string> | undefined,
- onStdout: ((data: string) => any) | undefined,
- onStderr: ((data: string) => any) | undefined,
- }
+ opts: {
+ cwd?: string,
+ user?: 'root' | 'user',
+ envs?: Record<string, string>,
+ onStdout?: ((data: string) => void | Promise<void>),
+ onStderr?: ((data: string) => void | Promise<void>),
+ } & Pick<ConnectionOpts, 'requestTimeoutMs'> = {}
): Promise<ProcessHandle> {
const params: PlainMessage<StartRequest> = {
owner: {
credential: {
case: 'username',
- value: user || 'user',
+ value: opts.user || 'user',
},
},
process: {
cmd: '/bin/bash',
- cwd,
- envs: envs || {},
+ cwd: opts.cwd,
+ envs: opts.envs || {},
args: ['-l', '-c', cmd],
},
}
@@ -96,35 +167,33 @@ export class Process {
const events = this.service.start(params, {
signal: controller.signal,
+ timeoutMs: opts.requestTimeoutMs,
})
- const {
- start,
- wait,
- } = await this.handleProcessEvents(events, {
- onStdout,
- onStderr,
- })
-
- const startEvent = await start
+ const startEvent: StartResponse = (await events[Symbol.asyncIterator]().next()).value
- return {
- pid: startEvent.pid,
- disconnect: () => controller.abort(),
- wait,
- kill: () => this.kill(startEvent.pid),
+ if (startEvent.event?.event.case !== 'start') {
+ throw new Error('Expected start event')
}
+
+ const pid = startEvent.event.event.value.pid
+
+ return new ProcessHandle(
+ pid,
+ () => controller.abort(),
+ () => this.kill(pid),
+ events,
+ opts.onStdout,
+ opts.onStderr,
+ )
}
async connect(
pid: number,
- {
- onStderr,
- onStdout,
- }: {
- onStdout: ((data: string) => any) | undefined,
- onStderr: ((data: string) => any) | undefined,
- }
+ opts?: {
+ onStdout?: ((data: string) => void | Promise<void>),
+ onStderr?: ((data: string) => void | Promise<void>),
+ } & Pick<ConnectionOpts, 'requestTimeoutMs'>
): Promise<ProcessHandle> {
const params: PlainMessage<ConnectRequest> = {
process: {
@@ -138,24 +207,20 @@ export class Process {
const events = this.service.connect(params, {
signal: controller.signal,
+ timeoutMs: opts?.requestTimeoutMs,
})
- const {
- wait,
- } = await this.handleProcessEvents(events, {
- onStdout,
- onStderr,
- })
-
- return {
+ return new ProcessHandle(
pid,
- disconnect: () => controller.abort(),
- wait,
- kill: () => this.kill(pid),
- }
+ () => controller.abort(),
+ () => this.kill(pid),
+ events,
+ opts?.onStdout,
+ opts?.onStderr,
+ )
}
- async kill(pid: number): Promise<void> {
+ async kill(pid: number, opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>): Promise<void> {
await this.service.sendSignal({
process: {
selector: {
@@ -164,10 +229,12 @@ export class Process {
}
},
signal: Signal.SIGKILL,
+ }, {
+ timeoutMs: opts?.requestTimeoutMs,
})
}
- async sendStdin(pid: number, data: Uint8Array): Promise<void> {
+ async sendStdin(pid: number, data: Uint8Array, opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>): Promise<void> {
const params: PlainMessage<SendInputRequest> = {
process: {
selector: {
@@ -183,14 +250,16 @@ export class Process {
},
}
- await this.service.sendInput(params)
+ await this.service.sendInput(params, {
+ timeoutMs: opts?.requestTimeoutMs,
+ })
}
async startTerminal({ cols, rows, onData }: {
cols: number,
rows: number,
- onData: (data: Uint8Array) => any,
- }): Promise<void> {
+ onData: (data: Uint8Array) => void | Promise<void>,
+ }, opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>): Promise<ProcessHandle> {
const params: PlainMessage<StartRequest> = {
owner: {
credential: {
@@ -200,7 +269,6 @@ export class Process {
},
process: {
cmd: '/bin/bash',
- cwd: '/',
envs: {},
args: ['-i', '-l'],
},
@@ -212,50 +280,40 @@ export class Process {
},
}
- const start = createDeferredPromise<ProcessEvent_StartEvent>()
- const end = createDeferredPromise<ProcessEvent_EndEvent>()
-
- const stream = this.service.start(params)
-
- async function processStream() {
- try {
- for await (const event of stream) {
- switch (event.event?.event.case) {
- case 'start':
- start.resolve(event.event.event.value)
- break
- case 'data':
- switch (event.event.event.value.output.case) {
- case 'pty':
- onData(event.event.event.value.output.value)
- break
- }
- break
- case 'end':
- end.resolve(event.event.event.value)
- break
- }
-
- }
- } catch (error) {
- start.reject(error)
- end.reject(error)
- }
- }
+ const controller = new AbortController()
- processStream()
+ const events = this.service.start(params, {
+ timeoutMs: opts?.requestTimeoutMs,
+ signal: controller.signal,
+ })
- const startEvent = await start.promise
+ const startEvent: StartResponse = (await events[Symbol.asyncIterator]().next()).value
+ if (startEvent.event?.event.case !== 'start') {
+ throw new Error('Expected start event')
+ }
- return
+ const pid = startEvent.event.event.value.pid
+ return new ProcessHandle(
+ pid,
+ () => controller.abort(),
+ () => this.kill(pid),
+ events,
+ undefined,
+ undefined,
+ onData,
+ )
}
- private async resizeTerminal(pid: number, size: {
- cols: number,
- rows: number,
- }): Promise<void> {
+ async resizeTerminal(
+ pid: number,
+ size: {
+ cols: number,
+ rows: number,
+ },
+ opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>,
+ ): Promise<void> {
const params: PlainMessage<UpdateRequest> = {
process: {
selector: {
@@ -268,98 +326,21 @@ export class Process {
},
}
- await this.service.update(params)
+ await this.service.update(params, {
+ timeoutMs: opts?.requestTimeoutMs,
+ })
}
- private async streamTerminalInput(params: AsyncIterable<PlainMessage<StreamInputRequest>>) {
+ private async streamTerminalInput(params: AsyncIterable<PlainMessage<StreamInputRequest>>, opts?: Pick<ConnectionOpts, 'requestTimeoutMs'>) {
const controller = new AbortController()
this.service.streamInput(params, {
signal: controller.signal,
+ timeoutMs: opts?.requestTimeoutMs,
})
return {
stop: () => controller.abort(),
}
}
-
- private async handleProcessEvents(
- events: AsyncIterable<StartResponse | ConnectResponse>,
- {
- onStderr,
- onStdout,
- }: {
- onStdout: ((data: string) => any) | undefined,
- onStderr: ((data: string) => any) | undefined,
- }
- ) {
- const start = createDeferredPromise<ProcessEvent_StartEvent>()
- const end = createDeferredPromise<{
- exit: ProcessEvent_EndEvent,
- stdout: Uint8Array[],
- stderr: Uint8Array[],
- }>()
-
- async function processStream() {
- const stdout: Uint8Array[] = []
- const stderr: Uint8Array[] = []
-
- try {
- for await (const event of events) {
- switch (event.event?.event.case) {
- case 'start':
- start.resolve(event.event.event.value)
- break
- case 'data':
- switch (event.event.event.value.output.case) {
- case 'stdout':
- stdout.push(event.event.event.value.output.value)
- onStdout?.(event.event.event.value.output.value.toString())
- break
- case 'stderr':
- stderr.push(event.event.event.value.output.value)
- onStderr?.(event.event.event.value.output.value.toString())
- break
- }
- break
- case 'end':
- end.resolve({
- exit: event.event.event.value,
- stdout,
- stderr,
- })
- break
- }
-
- }
- } catch (error) {
- start.reject(error)
- end.reject(error)
- }
- }
-
- processStream()
-
- return {
- start: start.promise,
- wait: async () => {
- const { exit, stdout, stderr } = await end.promise
- return {
- ...exit,
- get stdout() {
- return concatUint8Arrays(stdout).toString()
- },
- get stderr() {
- return concatUint8Arrays(stderr).toString()
- },
- }
- },
- // async *[Symbol.asyncIterator](): AsyncIterable<ProcessOutput> {
- // for (const delay of this.delays) {
- // await this.wait(delay)
- // yield `Delayed response for ${delay} milliseconds`
- // }
- // },
- }
- }
}
diff --git a/packages/js-sdk/src/sandbox/sandboxApi.ts b/packages/js-sdk/src/sandbox/sandboxApi.ts
index b52aac5c3..674ee517c 100644
--- a/packages/js-sdk/src/sandbox/sandboxApi.ts
+++ b/packages/js-sdk/src/sandbox/sandboxApi.ts
@@ -9,12 +9,10 @@ export interface RunningSandbox {
startedAt: Date
}
-// TODO: Add requestTimeout
-
export class SandboxApi {
static async kill(
sandboxID: string,
- opts: ConnectionOpts = {}
+ opts?: ConnectionOpts,
): Promise<void> {
const config = new ConnectionConfig(opts)
@@ -25,10 +23,12 @@ export class SandboxApi {
client.api.path('/sandboxes/{sandboxID}').method('delete').create(),
)
- await killSandbox(config.apiKey, { sandboxID })
+ await killSandbox(config.apiKey, { sandboxID }, {
+ signal: config.requestTimeoutMs ? AbortSignal.timeout(config.requestTimeoutMs) : undefined,
+ })
}
- static async list(opts: ConnectionOpts = {}): Promise<RunningSandbox[]> {
+ static async list(opts?: ConnectionOpts): Promise<RunningSandbox[]> {
const config = new ConnectionConfig(opts)
const client = new APIClient(config)
@@ -37,7 +37,9 @@ export class SandboxApi {
client.api.path('/sandboxes').method('get').create(),
)
- const res = await listSandboxes(config.apiKey, {})
+ const res = await listSandboxes(config.apiKey, {}, {
+ signal: config.requestTimeoutMs ? AbortSignal.timeout(config.requestTimeoutMs) : undefined,
+ })
return res.data.map((sandbox) => ({
sandboxID: this.getSandboxID(sandbox),
@@ -50,10 +52,10 @@ export class SandboxApi {
protected static async createSandbox(
template: string,
- opts: ConnectionOpts & {
+ opts?: ConnectionOpts & {
metadata?: Record<string, string>,
timeout?: number,
- } = {}): Promise<string> {
+ }): Promise<string> {
const config = new ConnectionConfig(opts)
const client = new APIClient(config)
@@ -64,8 +66,10 @@ export class SandboxApi {
const res = await createSandbox(config.apiKey, {
templateID: template,
- metadata: opts.metadata,
- timeout: opts.timeout,
+ metadata: opts?.metadata,
+ timeout: opts?.timeout,
+ }, {
+ signal: config.requestTimeoutMs ? AbortSignal.timeout(config.requestTimeoutMs) : undefined,
})
return this.getSandboxID(res.data)
@@ -74,7 +78,8 @@ export class SandboxApi {
protected static async setTimeout(
sandboxID: string,
timeout: number,
- opts: ConnectionOpts = {}): Promise<void> {
+ opts?: ConnectionOpts,
+ ): Promise<void> {
const config = new ConnectionConfig(opts)
const client = new APIClient(config)
@@ -86,10 +91,12 @@ export class SandboxApi {
await setSandboxTimeout(config.apiKey, {
sandboxID,
timeout,
+ }, {
+ signal: config.requestTimeoutMs ? AbortSignal.timeout(config.requestTimeoutMs) : undefined,
})
}
private static getSandboxID({ sandboxID, clientID }: { sandboxID: string, clientID: string }): string {
return `${sandboxID}-${clientID}`
}
-}
\ No newline at end of file
+}
diff --git a/packages/python-sdk/e2b/sandbox/main.py b/packages/python-sdk/e2b/sandbox/main.py
index b36d641aa..4cdd2ddd9 100644
--- a/packages/python-sdk/e2b/sandbox/main.py
+++ b/packages/python-sdk/e2b/sandbox/main.py
@@ -45,7 +45,7 @@ class Sandbox(SandboxApi):
self.sandbox_id = sandbox_id
sandbox_server_url = (
- f"{'http' if debug else 'https'}://{self.get_hostname(SANDBOX_SERVER_PORT)}"
+ f"{'http' if debug else 'https'}://{self.get_host(SANDBOX_SERVER_PORT)}"
)
self.filesystem = Filesystem(sandbox_server_url)
@@ -53,7 +53,7 @@ class Sandbox(SandboxApi):
self._files = SandboxFiles(sandbox_server_url)
- def get_hostname(self, port: int) -> str:
+ def get_host(self, port: int) -> str:
if self._connection_config.debug:
return f"localhost:{port}"