Skip to content

Commit

Permalink
merge beta
Browse files Browse the repository at this point in the history
  • Loading branch information
0div committed Oct 8, 2024
2 parents c7e79a5 + f2bec05 commit 6a2203d
Show file tree
Hide file tree
Showing 38 changed files with 1,573 additions and 444 deletions.
19 changes: 12 additions & 7 deletions packages/js-sdk/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import { ConnectionConfig } from '../connectionConfig'
import { AuthenticationError, SandboxError } from '../errors'
import { createApiLogger } from '../logs'

export function handleApiError(err?: { code: number; message: string; }) {
export function handleApiError(err?: { code: number; message: string }) {
if (!err) {
return
}

return new SandboxError(`${err.code}: ${err.message}`)
}

/**
* Client for interacting with the E2B API.
*/
class ApiClient {
readonly api: ReturnType<typeof createClient<paths>>

Expand All @@ -22,20 +25,20 @@ class ApiClient {
opts: {
requireAccessToken?: boolean
requireApiKey?: boolean
} = { requireAccessToken: false, requireApiKey: true },
} = { requireAccessToken: false, requireApiKey: true }
) {
if (!opts?.requireApiKey && !config.apiKey) {
throw new AuthenticationError(
'API key is required, please visit https://e2b.dev/docs to get your API key. ' +
'You can either set the environment variable `E2B_API_KEY` ' +
"or you can pass it directly to the sandbox like Sandbox.create({ apiKey: 'e2b_...' })",
'You can either set the environment variable `E2B_API_KEY` ' +
"or you can pass it directly to the sandbox like Sandbox.create({ apiKey: 'e2b_...' })"
)
}

if (opts?.requireAccessToken && !config.accessToken) {
throw new AuthenticationError(
'Access token is required, please visit https://e2b.dev/docs to get your access token. ' +
'You can set the environment variable `E2B_ACCESS_TOKEN` or pass the `accessToken` in options.',
'You can set the environment variable `E2B_ACCESS_TOKEN` or pass the `accessToken` in options.'
)
}

Expand All @@ -44,8 +47,10 @@ class ApiClient {
// keepalive: true, // TODO: Return keepalive
headers: {
...defaultHeaders,
...config.apiKey && { 'X-API-KEY': config.apiKey },
...config.accessToken && { Authorization: `Bearer ${config.accessToken}` },
...(config.apiKey && { 'X-API-KEY': config.apiKey }),
...(config.accessToken && {
Authorization: `Bearer ${config.accessToken}`,
}),
},
})

Expand Down
15 changes: 13 additions & 2 deletions packages/js-sdk/src/connectionConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Logger } from './logs'
import { getEnvVar } from './api/metadata'

const REQUEST_TIMEOUT_MS = 30_000 // 30s
const REQUEST_TIMEOUT_MS = 30_000 // 30s
export const KEEPALIVE_PING_INTERVAL_SEC = 50 // 50s

export const KEEPALIVE_PING_HEADER = 'Keepalive-Ping-Interval'

/**
* Connection options for requests to the API.
*/
export interface ConnectionOpts {
apiKey?: string
accessToken?: string
Expand All @@ -15,6 +18,9 @@ export interface ConnectionOpts {
logger?: Logger
}

/**
* Configuration for connecting to the API.
*/
export class ConnectionConfig {
readonly debug: boolean
readonly domain: string
Expand All @@ -34,7 +40,9 @@ export class ConnectionConfig {
this.requestTimeoutMs = opts?.requestTimeoutMs ?? REQUEST_TIMEOUT_MS
this.logger = opts?.logger

this.apiUrl = this.debug ? 'http://localhost:3000' : `https://api.${this.domain}`
this.apiUrl = this.debug
? 'http://localhost:3000'
: `https://api.${this.domain}`
}

private static get domain() {
Expand All @@ -60,6 +68,9 @@ export class ConnectionConfig {
}
}

/**
* Username for the user in the sandbox.
*/
export type Username = 'root' | 'user'

export const defaultUsername: Username = 'user'
23 changes: 8 additions & 15 deletions packages/js-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export {
ApiClient,
} from './api'
export { ApiClient } from './api'
export type { components, paths } from './api'

export {
Expand All @@ -12,21 +10,17 @@ export {
InvalidArgumentError,
TemplateError,
} from './errors'
export {
ConnectionConfig,
} from './connectionConfig'
export { ConnectionConfig } from './connectionConfig'
export type { Logger } from './logs'
export type { ConnectionOpts, Username } from './connectionConfig'

export { FilesystemEventType } from './sandbox/filesystem/watchHandle'
export type { FilesystemEvent, WatchHandle } from './sandbox/filesystem/watchHandle'
export type {
EntryInfo,
Filesystem,
} from './sandbox/filesystem'
export {
FileType,
} from './sandbox/filesystem'
FilesystemEvent,
WatchHandle,
} from './sandbox/filesystem/watchHandle'
export type { EntryInfo, Filesystem, WatchOpts } from './sandbox/filesystem'
export { FileType } from './sandbox/filesystem'

export { ProcessExitError } from './sandbox/process/processHandle'
export type {
Expand All @@ -44,10 +38,9 @@ export type {
ProcessConnectOpts,
ProcessStartOpts,
Process,
Pty,
} from './sandbox/process'

export type { Pty } from './sandbox/pty'

export type { SandboxInfo } from './sandbox/sandboxApi'
export type { SandboxOpts } from './sandbox'
import { Sandbox } from './sandbox'
Expand Down
Loading

0 comments on commit 6a2203d

Please sign in to comment.