Skip to content

Commit

Permalink
Add timeouts to JS SDK (#104)
Browse files Browse the repository at this point in the history
Co-authored-by: Pavel 'Strajk' Dolecek <strajk@me.com>
  • Loading branch information
jakubno and Strajk committed Sep 6, 2023
1 parent e894b49 commit 4ce867c
Show file tree
Hide file tree
Showing 11 changed files with 464 additions and 289 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-wolves-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@e2b/sdk": patch
---

Add the possibility to timeout calls
2 changes: 2 additions & 0 deletions packages/js-sdk/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export const SESSION_REFRESH_PERIOD = 5_000 // 5s
export const WS_RECONNECT_INTERVAL = 600 // 600ms

export const TIMEOUT = 60_000 // 60s

export const SESSION_DOMAIN = 'ondevbook.com'
export const WS_PORT = 49982
export const WS_ROUTE = '/ws'
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export class TimeoutError extends Error {
constructor(message: any) {
super(message)
this.name = 'TimeoutError'
}
}

export class AuthenticationError extends Error {
constructor(message: any) {
super(message)
Expand Down
11 changes: 6 additions & 5 deletions packages/js-sdk/src/session/filesystem.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FilesystemWatcher from './filesystemWatcher'
import { CallOpts } from './sessionConnection'

export const filesystemService = 'filesystem'

Expand All @@ -8,12 +9,12 @@ export interface FileInfo {
}

export interface FilesystemManager {
readonly write: (path: string, content: string) => Promise<void>
readonly write: (path: string, content: string, opts?: CallOpts) => Promise<void>
// readonly writeBytes: (path: string, content: Uint8Array) => Promise<void>
readonly read: (path: string) => Promise<string>
readonly read: (path: string, opts?: CallOpts) => Promise<string>
// readonly readBytes: (path: string) => Promise<Uint8Array>
readonly remove: (path: string) => Promise<void>
readonly list: (path: string) => Promise<FileInfo[]>
readonly makeDir: (path: string) => Promise<void>
readonly remove: (path: string, opts?: CallOpts) => Promise<void>
readonly list: (path: string, opts?: CallOpts) => Promise<FileInfo[]>
readonly makeDir: (path: string, opts?: CallOpts) => Promise<void>
readonly watchDir: (path: string) => FilesystemWatcher
}
26 changes: 15 additions & 11 deletions packages/js-sdk/src/session/filesystemWatcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { withTimeout } from '../utils/promise'
import { filesystemService } from './filesystem'
import { SessionConnection } from './sessionConnection'
import { CallOpts, SessionConnection } from './sessionConnection'

export enum FilesystemOperation {
Create = 'Create',
Expand Down Expand Up @@ -32,18 +33,21 @@ class FilesystemWatcher {
}

// Starts watching the path that was passed to the contructor
async start() {
// Already started.
if (this.rpcSubscriptionID) return
async start(opts: CallOpts) {
const start = async () => {
// Already started.
if (this.rpcSubscriptionID) return

this.handleFilesystemEvents = this.handleFilesystemEvents.bind(this)
this.handleFilesystemEvents = this.handleFilesystemEvents.bind(this)

this.rpcSubscriptionID = await this.sessConn.subscribe(
filesystemService,
this.handleFilesystemEvents,
'watchDir',
this.path,
)
this.rpcSubscriptionID = await this.sessConn.subscribe(
filesystemService,
this.handleFilesystemEvents,
'watchDir',
this.path,
)
}
return await withTimeout(start, opts?.timeout)()
}

// Stops watching the path and removes all listeners.
Expand Down
Loading

0 comments on commit 4ce867c

Please sign in to comment.