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): Template().copy() fails in ESM modules (#1046)

Filip Brebera37bd3e3d7cc7
diff --git a/.changeset/nine-taxes-greet.md b/.changeset/nine-taxes-greet.md
new file mode 100644
index 000000000..8fcf88418
--- /dev/null
+++ b/.changeset/nine-taxes-greet.md
@@ -0,0 +1,5 @@
+---
+'e2b': patch
+---
+
+Template().copy() fails in ESM modules
diff --git a/packages/js-sdk/src/template/utils.ts b/packages/js-sdk/src/template/utils.ts
index f6377db14..d1224b978 100644
--- a/packages/js-sdk/src/template/utils.ts
+++ b/packages/js-sdk/src/template/utils.ts
@@ -1,7 +1,7 @@
import crypto from 'node:crypto'
import fs from 'node:fs'
import path from 'node:path'
-import { dynamicGlob, dynamicTar } from '../utils'
+import { dynamicImport, dynamicRequire } from '../utils'
import { BASE_STEP_NAME, FINALIZE_STEP_NAME } from './consts'
import type { Path } from 'glob'
@@ -47,7 +47,7 @@ export async function getAllFilesInPath(
ignorePatterns: string[],
includeDirectories: boolean = true
) {
- const { glob } = await dynamicGlob()
+ const { glob } = await dynamicImport<typeof import('glob')>('glob')
const files = new Map<string, Path>()
const globFiles = await glob(src, {
@@ -219,11 +219,20 @@ export function getCallerDirectory(depth: number): string | undefined {
return undefined
}
- const fileName = callSites[0].getFileName()
+ let fileName = callSites[0].getFileName()
if (!fileName) {
return 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)
+ }
+
return path.dirname(fileName)
}
@@ -258,7 +267,7 @@ export async function tarFileStream(
ignorePatterns: string[],
resolveSymlinks: boolean
) {
- const { create } = await dynamicTar()
+ const { create } = await dynamicImport<typeof import('tar')>('tar')
const allFiles = await getAllFilesInPath(
fileName,
diff --git a/packages/js-sdk/src/utils.ts b/packages/js-sdk/src/utils.ts
index f85d4e1b9..8e5af4bc4 100644
--- a/packages/js-sdk/src/utils.ts
+++ b/packages/js-sdk/src/utils.ts
@@ -67,22 +67,21 @@ export function timeoutToSeconds(timeout: number): number {
return Math.ceil(timeout / 1000)
}
-export async function dynamicGlob(): Promise<typeof import('glob')> {
+export function dynamicRequire<T>(module: string): T {
if (runtime === 'browser') {
- throw new Error('Browser runtime is not supported for glob')
+ throw new Error('Browser runtime is not supported for require')
}
- // @ts-ignore
- return await import('glob')
+ return require(module)
}
-export async function dynamicTar(): Promise<typeof import('tar')> {
+export async function dynamicImport<T>(module: string): Promise<T> {
if (runtime === 'browser') {
- throw new Error('Browser runtime is not supported for tar')
+ throw new Error('Browser runtime is not supported for dynamic import')
}
// @ts-ignore
- return await import('tar')
+ return await import(module)
}
// Source: https://github.com/chalk/ansi-regex/blob/main/index.js
diff --git a/packages/js-sdk/tests/template/utils/getCallerDirectory.test.ts b/packages/js-sdk/tests/template/utils/getCallerDirectory.test.ts
index 143c13cff..063d43a9d 100644
--- a/packages/js-sdk/tests/template/utils/getCallerDirectory.test.ts
+++ b/packages/js-sdk/tests/template/utils/getCallerDirectory.test.ts
@@ -1,4 +1,6 @@
import { expect, test } from 'vitest'
+import path from 'node:path'
+import { fileURLToPath } from 'node:url'
import { getCallerDirectory } from '../../../src/template/utils'
test('getCallerDirectory', () => {
@@ -6,3 +8,23 @@ test('getCallerDirectory', () => {
// __dirname is the directory of the current file
expect(getCallerDirectory(1)).toBe(__dirname)
})
+
+test('getCallerDirectory handles file:// URLs from ESM modules', () => {
+ // In ESM modules, CallSite.getFileName() returns file:// URLs
+ // This test verifies that the conversion works correctly
+ const testPath = '/Users/test/project/src/template.ts'
+ const fileUrl = `file://${testPath}`
+
+ // Verify that fileURLToPath correctly converts the URL
+ // This is the same conversion used in getCallerDirectory
+ expect(fileURLToPath(fileUrl)).toBe(testPath)
+ expect(path.dirname(fileURLToPath(fileUrl))).toBe('/Users/test/project/src')
+})
+
+test('getCallerDirectory handles file:// URLs with three slashes', () => {
+ // file:///path is the standard format on Unix systems
+ const testPath = '/Users/test/project/src/template.ts'
+ const fileUrl = `file:///${testPath.slice(1)}` // file:///Users/test/...
+
+ expect(fileURLToPath(fileUrl)).toBe(testPath)
+})
diff --git a/packages/js-sdk/tests/template/utils/tarFileStream.test.ts b/packages/js-sdk/tests/template/utils/tarFileStream.test.ts
index 7628c5dd3..d08278cd2 100644
--- a/packages/js-sdk/tests/template/utils/tarFileStream.test.ts
+++ b/packages/js-sdk/tests/template/utils/tarFileStream.test.ts
@@ -5,7 +5,7 @@ import { join } from 'path'
import { tmpdir } from 'os'
import { pipeline } from 'stream/promises'
import { tarFileStream } from '../../../src/template/utils'
-import { dynamicTar } from '../../../src/utils'
+import * as tar from 'tar'
import { ReadEntry } from 'tar'
describe('tarFileStream', () => {
@@ -36,7 +36,6 @@ describe('tarFileStream', () => {
const writeStream = createWriteStream(tarFile)
await pipeline(stream, writeStream)
- const tar = await dynamicTar()
const contents = new Map<string, Buffer | null>()
// Extract files to a temp directory and collect members
@@ -86,7 +85,6 @@ describe('tarFileStream', () => {
const writeStream = createWriteStream(tarFile)
await pipeline(stream, writeStream)
- const tar = await dynamicTar()
const members = new Map<string, ReadEntry>()
const extractDir = join(tmpdir(), `tar-extract-${Date.now()}`)