Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Max body size for template build (#292)

Jakub Novákf53b00c8d438
diff --git a/.changeset/rich-papayas-design.md b/.changeset/rich-papayas-design.md
new file mode 100644
index 000000000..88561dabc
--- /dev/null
+++ b/.changeset/rich-papayas-design.md
@@ -0,0 +1,5 @@
+---
+"@e2b/cli": patch
+---
+
+Add check for max build body size in CLI
diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts
index 7a9b1374b..402f4648f 100644
--- a/packages/cli/src/commands/build.ts
+++ b/packages/cli/src/commands/build.ts
@@ -22,7 +22,8 @@ import {
import { pathOption } from 'src/options'
import { createBlobFromFiles } from 'src/docker/archive'
import { defaultDockerfileName, fallbackDockerfileName } from 'src/docker/constants'
-import { configName, getConfigPath, loadConfig, saveConfig } from 'src/config'
+import { configName, getConfigPath, loadConfig, maxContentSize, saveConfig } from 'src/config'
+import { estimateContentLength } from '../utils/form'
const templateCheckInterval = 1_000 // 1 sec
@@ -175,6 +176,15 @@ export const buildCommand = new commander.Command('build')
body.append('startCmd', startCmd)
}
+ const estimatedSize = estimateContentLength(body)
+ if (estimatedSize > maxContentSize) {
+ console.error(
+ `The sandbox template build context is too large ${asLocal(`${Math.round(estimatedSize / 1024 / 1024 * 100) /100} MiB`)}. The maximum size is ${asLocal(
+ `${maxContentSize / 1024 / 1024} MiB.`)}\n\nCheck if you are not including unnecessary files in the build context (e.g. node_modules)`,
+ )
+ process.exit(1)
+ }
+
const build = await buildTemplate(accessToken, body, !!config, configPath, envID)
console.log(
diff --git a/packages/cli/src/config/index.ts b/packages/cli/src/config/index.ts
index 3e16c349b..70d963647 100644
--- a/packages/cli/src/config/index.ts
+++ b/packages/cli/src/config/index.ts
@@ -6,6 +6,7 @@ import * as path from 'path'
import { asFormattedSandboxTemplate, asLocalRelative } from 'src/utils/format'
+export const maxContentSize = 256 * 1024 * 1024 // 256Mi
export const configName = 'e2b.toml'
function getConfigHeader(config: E2BConfig) {
diff --git a/packages/cli/src/utils/form.ts b/packages/cli/src/utils/form.ts
new file mode 100644
index 000000000..993177842
--- /dev/null
+++ b/packages/cli/src/utils/form.ts
@@ -0,0 +1,15 @@
+export function estimateContentLength(formData: FormData) {
+ const baseLength = 50; // estimated max value
+ const separatorLength = 115; // estimated max value
+ let length = baseLength;
+ const entries = formData.entries();
+ for (const [key, value] of entries) {
+ length += key.length + separatorLength;
+ if (typeof value === 'object') {
+ length += value.size;
+ } else {
+ length += String(value).length;
+ }
+ }
+ return length;
+}
diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json
index 5677c5819..da4ebec7b 100644
--- a/packages/cli/tsconfig.json
+++ b/packages/cli/tsconfig.json
@@ -1,11 +1,12 @@
{
"compilerOptions": {
- "target": "ES2020",
+ "target": "es2022",
+ "module": "es2022",
"lib": [
- "ES2020",
- "webworker"
+ "es2022",
+ "dom",
+ "dom.iterable",
],
- "module": "CommonJS",
"allowJs": true,
"declaration": true,
"declarationMap": true,