Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Package and dump function in one process

Tomas Valentae8a3fdefdec8
diff --git a/api-service/playground/src/deployments/packaging.ts b/api-service/playground/src/deployments/packaging.ts
index f0aad0012..b7987a11f 100644
--- a/api-service/playground/src/deployments/packaging.ts
+++ b/api-service/playground/src/deployments/packaging.ts
@@ -1,7 +1,5 @@
import {
OutStderrResponse,
- OutStdoutResponse,
- ProcessManager,
Session,
createSessionProcess
} from '@devbookhq/sdk'
@@ -21,15 +19,20 @@ const dumpPath = path.join(rootdir, bundleDir, 'base')
*
* TODO: Configure esbuild to use Nodejs 18 and to minify the output.
* TODO: Explore .js/.mjs bundling differences.
+ * TODO: Implement binary file reading in devbookd so we don't have to send data via base64 (cca 25% overhead) and process stdout.
*
*/
export async function packageFunction(session: Session, code: string) {
+ if (!session.filesystem || !session.process) {
+ throw new Error('Session is not active')
+ }
const stderr: OutStderrResponse[] = []
await session.filesystem?.write(rootfilePath, code)
+ // Bundle the code and dependencies via https://github.com/netlify/zip-it-and-ship-it and dump the base64 string representationto a separate file.
const bundling = await createSessionProcess({
manager: session.process,
- cmd: `zip-it-and-ship-it ${functionsDir} ${bundleDir}`,
+ cmd: `zip-it-and-ship-it ${functionsDir} ${bundleDir} && base64 -w 0 ${bundlePath} > ${dumpPath}`,
rootdir,
onStderr: o => stderr.push(o),
})
@@ -39,30 +42,7 @@ export async function packageFunction(session: Session, code: string) {
console.error(stderr.map(o => o.line).join('\n'))
}
- const zip = await readFileAsBase64(session, bundlePath)
+ // Read the base64 representation as string. Because our filesystem.read reads file as UTF-8, we cannot read binary files this way without corrupting them.
+ const zip = await session.filesystem.read(dumpPath)
return Buffer.from(zip, 'base64')
}
-
-/**
- * TODO: Implement binary file reading in devbookd so we don't have to send data via base64 (cca 25% overhead) and process stdout.
- */
-async function readFileAsBase64(session: Session, filepath: string) {
- if (!session.filesystem || !session.process) {
- throw new Error('Session is not active')
- }
-
- const stderr: OutStderrResponse[] = []
- const dumpToBase64 = await createSessionProcess({
- manager: session.process,
- onStderr: o => stderr.push(o),
- cmd: `base64 -w 0 ${filepath} > ${dumpPath}`,
- })
-
- await dumpToBase64.exited
-
- if (stderr.length > 0) {
- throw new Error(`Error reading file ${stderr.map(o => o.line).join('\n')}`)
- }
-
- return await session.filesystem.read(dumpPath)
-}