Skip to content

Commit

Permalink
Initial implementation of stateless SDK in JS
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno committed Feb 16, 2024
1 parent 1726c91 commit 212d3a3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/js-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export { Process, ProcessMessage, ProcessOutput } from './sandbox/process'
export type { ProcessManager } from './sandbox/process'
export type { EnvVars } from './sandbox/envVars'
export { runCode, CodeRuntime } from './runCode' // Export CodeRuntime enum as value, not as type, so it can be actually used in consumer code
import { Sandbox } from './sandbox/index'
import { Sandbox } from './sandbox'

import { DataAnalysis } from './templates/dataAnalysis'
export { DataAnalysis as CodeInterpreter }
Expand All @@ -32,3 +32,4 @@ export type { Action } from './sandbox/index'

export { Sandbox }
export default Sandbox
export * as stateless from './sandbox/stateless'
7 changes: 4 additions & 3 deletions packages/js-sdk/src/sandbox/sandboxConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ export class SandboxConnection {
* @param apiKey API key to use for authentication. If not provided, the `E2B_API_KEY` environment variable will be used.
*/
static async kill(sandboxID: string, apiKey?: string): Promise<void> {
const id = sandboxID.split('-')[0]
apiKey = getApiKey(apiKey)
try {
await killSandbox(apiKey, {sandboxID})
await killSandbox(apiKey, {sandboxID: id})
} catch (e) {
if (e instanceof killSandbox.Error) {
const error = e.getActualType()
Expand Down Expand Up @@ -546,15 +547,15 @@ export class SandboxConnection {
try {
// eslint-disable-next-line no-constant-condition
while (true) {
await wait(SANDBOX_REFRESH_PERIOD)

if (!this.isOpen) {
this.logger.debug?.(
`Cannot refresh sandbox ${this.id} - it was closed`,
)
return
}

await wait(SANDBOX_REFRESH_PERIOD)

try {
await refreshSandbox(this.apiKey, {
sandboxID, duration: 0,
Expand Down
44 changes: 44 additions & 0 deletions packages/js-sdk/src/sandbox/stateless.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Sandbox, SandboxOpts } from './index'
import { ProcessOpts, ProcessOutput } from './process'

/**
* Creates a new sandbox.
* @param lifetime Lifetime of the sandbox in milliseconds
* @param opts Sandbox options
* @returns Sandbox ID
*/
export async function create(lifetime: number, opts: SandboxOpts ): Promise<string> {
const s = await Sandbox.create(opts)
await s.keepAlive(lifetime)
await s.close()
return s.id
}

/**
* Starts a new process.
* @overload
* @param sandboxId Sandbox ID
* @returns Process output
*/
/**
* Starts a new process and wait until it finishes.
* @param sandboxId Sandbox ID
* @param opts Process options
* @returns Process output
*/
export async function exec(sandboxId: string, opts: ProcessOpts): Promise<ProcessOutput> {
const s = await Sandbox.reconnect(sandboxId)
const result = await s.process.startAndWait(opts)
await s.close()
return result
}



/**
* Kills a sandbox.
* @param sandboxId Sandbox ID
*/
export async function kill(sandboxId: string): Promise<void> {
await Sandbox.kill(sandboxId)
}

0 comments on commit 212d3a3

Please sign in to comment.