Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Fix timeout unhandled error

Tomas Valenta188dfd30aee2
diff --git a/.changeset/shaggy-geese-search.md b/.changeset/shaggy-geese-search.md
new file mode 100644
index 000000000..d3d1555df
--- /dev/null
+++ b/.changeset/shaggy-geese-search.md
@@ -0,0 +1,5 @@
+---
+"@e2b/sdk": patch
+---
+
+Fix timeout unhandled error
diff --git a/packages/js-sdk/package.json b/packages/js-sdk/package.json
index 85c27ada6..7e790c579 100644
--- a/packages/js-sdk/package.json
+++ b/packages/js-sdk/package.json
@@ -72,6 +72,7 @@
"dependencies": {
"normalize-path": "^3.0.0",
"openapi-typescript-fetch": "^1.1.3",
+ "p-timeout": "^6.1.2",
"path-browserify": "^1.0.1",
"platform": "^1.3.6",
"rpc-websocket-client": "^1.1.4"
diff --git a/packages/js-sdk/src/utils/promise.ts b/packages/js-sdk/src/utils/promise.ts
index 8d7d4d718..bc9aa4832 100644
--- a/packages/js-sdk/src/utils/promise.ts
+++ b/packages/js-sdk/src/utils/promise.ts
@@ -1,5 +1,6 @@
+import pTimeout from 'p-timeout'
+
import { TIMEOUT } from '../constants'
-import { TimeoutError } from '../error'
export function assertFulfilled<T>(
item: PromiseSettledResult<T>,
@@ -44,19 +45,6 @@ export function withTimeout<T extends (...args: any[]) => any>(
return fn
}
- let timerId: NodeJS.Timeout
- const timer = new Promise((resolve, reject) => {
- timerId = setTimeout(
- () =>
- reject(new TimeoutError(`Calling "${fn.name}" timeouted after ${timeout}ms.`)),
- timeout,
- )
- })
-
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- return ((...args: T extends (...args: infer A) => any ? A : never) => {
- const result = Promise.race([timer, fn(...args)])
- result.finally(() => clearTimeout(timerId))
- return result
- }) as T
+ return ((...args: T extends (...args: infer A) => any ? A : never) =>
+ pTimeout(fn(...args), { milliseconds: timeout })) as T
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index a0b78ca0b..25f80fdb0 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -318,6 +318,9 @@ importers:
openapi-typescript-fetch:
specifier: ^1.1.3
version: 1.1.3
+ p-timeout:
+ specifier: ^6.1.2
+ version: 6.1.2
path-browserify:
specifier: ^1.0.1
version: 1.0.1
@@ -8526,6 +8529,11 @@ packages:
mimic-fn: 3.1.0
dev: true
+ resolution: {integrity: sha512-UbD77BuZ9Bc9aABo74gfXhNvzC9Tx7SxtHSh1fxvx3jTLLYvmVhiQZZrJzqqU0jKbN32kb5VOKiLEQI/3bIjgQ==}
+ engines: {node: '>=14.16'}
+ dev: false
+
resolution: {integrity: sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==}
engines: {node: '>=6'}