Skip to content

armgit / E2B

Public Imported from GitHub
0

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

fix(js-sdk): drop bare require calls that crash edge runtimes at import (#1583)

Mish Ushakovf10989813c6a
diff --git a/.changeset/spotty-pianos-shake.md b/.changeset/spotty-pianos-shake.md
new file mode 100644
index 000000000..f2dd21240
--- /dev/null
+++ b/.changeset/spotty-pianos-shake.md
@@ -0,0 +1,5 @@
+---
+'e2b': patch
+---
+
+Fix ESM bundle crashing at import time in Cloudflare Workers (and other edge runtimes) with `The argument 'path' must be a file URL object, a file URL string, or an absolute path string. Received 'undefined'`. Bare `require` calls in the SDK source made the bundler emit an eager `createRequire(import.meta.url)` shim at module scope, which throws in workerd where `import.meta.url` is undefined. SHA-256 hashing now uses WebCrypto directly, `node:url` is imported statically like the other Node.js built-ins, and a bundle test fails if a `require` shim ever reappears in the ESM bundle.
diff --git a/packages/js-sdk/src/template/utils.ts b/packages/js-sdk/src/template/utils.ts
index 933cdd177..3a953d180 100644
--- a/packages/js-sdk/src/template/utils.ts
+++ b/packages/js-sdk/src/template/utils.ts
@@ -2,7 +2,8 @@ import crypto from 'node:crypto'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
-import { dynamicImport, dynamicRequire } from '../utils'
+import url from 'node:url'
+import { dynamicImport } from '../utils'
import { TemplateError } from '../errors'
import { BASE_STEP_NAME, FINALIZE_STEP_NAME } from './consts'
import type { Readable } from 'node:stream'
@@ -329,11 +330,7 @@ export function getCallerDirectory(depth: number): string | undefined {
// Handle file:// URLs returned by getFileName() in ESM modules
if (fileName.startsWith('file:')) {
- // we use the dynamic import to avoid bundling node:url for browser compatibility
- // getCallerDirectory method is not called in the browser
- const { fileURLToPath } =
- dynamicRequire<typeof import('node:url')>('node:url')
- fileName = fileURLToPath(fileName)
+ fileName = url.fileURLToPath(fileName)
}
return path.dirname(fileName)
diff --git a/packages/js-sdk/src/utils.ts b/packages/js-sdk/src/utils.ts
index 245b4e411..6deba47e6 100644
--- a/packages/js-sdk/src/utils.ts
+++ b/packages/js-sdk/src/utils.ts
@@ -47,34 +47,18 @@ function getRuntime(): { runtime: Runtime; version: string } {
export const { runtime, version: runtimeVersion } = getRuntime()
export async function sha256(data: string): Promise<string> {
- // Use WebCrypto API if available
- if (typeof crypto !== 'undefined') {
- const encoder = new TextEncoder()
- const dataBuffer = encoder.encode(data)
- const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer)
- const hashArray = new Uint8Array(hashBuffer)
- return btoa(String.fromCharCode(...hashArray))
- }
-
- // Use Node.js crypto if WebCrypto is not available
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const { createHash } = require('node:crypto')
- const hash = createHash('sha256').update(data, 'utf8').digest()
- return hash.toString('base64')
+ // WebCrypto is available in all supported runtimes (Node >= 20, browsers, edge)
+ const encoder = new TextEncoder()
+ const dataBuffer = encoder.encode(data)
+ const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer)
+ const hashArray = new Uint8Array(hashBuffer)
+ return btoa(String.fromCharCode(...hashArray))
}
export function timeoutToSeconds(timeout: number): number {
return Math.ceil(timeout / 1000)
}
-export function dynamicRequire<T>(module: string): T {
- if (runtime === 'browser') {
- throw new Error('Browser runtime is not supported for require')
- }
-
- return require(module)
-}
-
export async function dynamicImport<T>(module: string): Promise<T> {
if (runtime === 'browser') {
throw new Error('Browser runtime is not supported for dynamic import')
diff --git a/packages/js-sdk/tests/bundle/edgeCompat.test.ts b/packages/js-sdk/tests/bundle/edgeCompat.test.ts
new file mode 100644
index 000000000..675cd16c8
--- /dev/null
+++ b/packages/js-sdk/tests/bundle/edgeCompat.test.ts
@@ -0,0 +1,38 @@
+import { existsSync, readFileSync } from 'node:fs'
+import { fileURLToPath } from 'node:url'
+import { expect, test } from 'vitest'
+
+// Bundlers turn bare `require` references in ESM source into a
+// `createRequire(import.meta.url)` helper that is evaluated eagerly when the
+// module loads. In edge runtimes (e.g. Cloudflare Workers) `import.meta.url`
+// is undefined in bundled code, so importing the SDK crashes before any API
+// call is made. See https://github.com/e2b-dev/E2B/issues/1579
+//
+// Instead of `require`, use a static default import (`import fs from
+// 'node:fs'`) or `dynamicImport` from src/utils.ts — neither makes the
+// bundler emit the shim.
+
+const bundlePath = fileURLToPath(
+ new URL('../../dist/index.mjs', import.meta.url)
+)
+
+// CI always builds before testing (see .github/workflows/js_sdk_tests.yml),
+// so a missing bundle there is a real failure; locally it just means the
+// build hasn't been run.
+const bundleExists = existsSync(bundlePath)
+if (!bundleExists && process.env.CI) {
+ throw new Error(
+ `dist/index.mjs not found at ${bundlePath} — run \`pnpm build\` before testing`
+ )
+}
+
+test.skipIf(!bundleExists)(
+ 'ESM bundle contains no require shim (edge-runtime safe)',
+ () => {
+ const bundle = readFileSync(bundlePath, 'utf8')
+
+ expect(bundle).not.toMatch(/from\s*['"]node:module['"]/)
+ expect(bundle).not.toMatch(/\bcreateRequire\s*\(/)
+ expect(bundle).not.toMatch(/\b__require\b/)
+ }
+)
diff --git a/packages/js-sdk/tests/utils.test.ts b/packages/js-sdk/tests/utils.test.ts
new file mode 100644
index 000000000..5bebb6622
--- /dev/null
+++ b/packages/js-sdk/tests/utils.test.ts
@@ -0,0 +1,9 @@
+import { expect, test } from 'vitest'
+
+import { sha256 } from '../src/utils'
+
+test('sha256 hashes with WebCrypto', async () => {
+ expect(await sha256('hello')).toBe(
+ 'LPJNul+wow4m6DsqxbninhsWHlwfp0JecwQzYpOLmCQ='
+ )
+})