Skip to content

armgit / E2B

Public Imported from GitHub
0

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

feat(js-sdk): Add an option to upload and download binary files via writeBytes and readBytes (#151)

Pavel 'Strajk' Dolecekc04b4452eb6b
diff --git a/.changeset/gentle-cameras-build.md b/.changeset/gentle-cameras-build.md
new file mode 100644
index 000000000..192252de9
--- /dev/null
+++ b/.changeset/gentle-cameras-build.md
@@ -0,0 +1,5 @@
+---
+"@e2b/sdk": minor
+---
+
+Add an option to upload and download binary files via writeBytes and readBytes
diff --git a/packages/js-sdk/rollup.config.js b/packages/js-sdk/rollup.config.js
index 77d268046..5b5999e96 100644
--- a/packages/js-sdk/rollup.config.js
+++ b/packages/js-sdk/rollup.config.js
@@ -34,6 +34,7 @@ export default {
autoExternal({ builtins: false }),
typescript({
clean: false,
+ // check: false, uncomment when TS gives you weird erros when running vitest
}),
nodePolyfills(),
nodeResolve({
diff --git a/packages/js-sdk/src/session/filesystem.ts b/packages/js-sdk/src/session/filesystem.ts
index b56ef0efd..559dde31b 100644
--- a/packages/js-sdk/src/session/filesystem.ts
+++ b/packages/js-sdk/src/session/filesystem.ts
@@ -10,9 +10,9 @@ export interface FileInfo {
export interface FilesystemManager {
readonly write: (path: string, content: string, opts?: CallOpts) => Promise<void>
- // readonly writeBytes: (path: string, content: Uint8Array) => Promise<void>
+ readonly writeBytes: (path: string, content: Uint8Array) => Promise<void>
readonly read: (path: string, opts?: CallOpts) => Promise<string>
- // readonly readBytes: (path: string) => Promise<Uint8Array>
+ readonly readBytes: (path: string) => Promise<Uint8Array>
readonly remove: (path: string, opts?: CallOpts) => Promise<void>
readonly list: (path: string, opts?: CallOpts) => Promise<FileInfo[]>
readonly makeDir: (path: string, opts?: CallOpts) => Promise<void>
diff --git a/packages/js-sdk/src/session/index.ts b/packages/js-sdk/src/session/index.ts
index 5e798df64..436102080 100644
--- a/packages/js-sdk/src/session/index.ts
+++ b/packages/js-sdk/src/session/index.ts
@@ -85,32 +85,32 @@ export class Session extends SessionConnection {
write: async (path, content, opts?: CallOpts) => {
await this.call(filesystemService, 'write', [path, content], opts)
},
- // /**
- // * Write array of bytes to a file.
- // * This can be used when you cannot represent the data as an UTF-8 string.
- // *
- // * @param path path to a file
- // * @param content byte array representing the content to write
- // */
- // writeBytes: async (path, content) => {
- // // We need to convert the byte array to base64 string without using browser or node specific APIs.
- // // This should be achieved by the node polyfills.
- // const base64Content = Buffer.from(content).toString('base64')
- // await this.call(filesystemService, 'writeBase64', [path, base64Content])
- // },
- // /**
- // * Reads the whole content of a file as an array of bytes.
- // * @param path path to a file
- // * @returns byte array representing the content of a file
- // */
- // readBytes: async path => {
- // const base64Content = (await this.call(filesystemService, 'readBase64', [
- // path,
- // ])) as string
- // // We need to convert the byte array to base64 string without using browser or node specific APIs.
- // // This should be achieved by the node polyfills.
- // return Buffer.from(base64Content, 'base64')
- // },
+ /**
+ * Write array of bytes to a file.
+ * This can be used when you cannot represent the data as an UTF-8 string.
+ *
+ * @param path path to a file
+ * @param content byte array representing the content to write
+ */
+ writeBytes: async (path: string, content: Uint8Array) => {
+ // We need to convert the byte array to base64 string without using browser or node specific APIs.
+ // This should be achieved by the node polyfills.
+ const base64Content = Buffer.from(content).toString('base64')
+ await this.call(filesystemService, 'writeBase64', [path, base64Content])
+ },
+ /**
+ * Reads the whole content of a file as an array of bytes.
+ * @param path path to a file
+ * @returns byte array representing the content of a file
+ */
+ readBytes: async (path: string) => {
+ const base64Content = (await this.call(filesystemService, 'readBase64', [
+ path,
+ ])) as string
+ // We need to convert the byte array to base64 string without using browser or node specific APIs.
+ // This should be achieved by the node polyfills.
+ return Buffer.from(base64Content, 'base64')
+ },
/**
* Creates a new directory and all directories along the way if needed on the specified pth.
* @param path Path to a new directory. For example '/dirA/dirB' when creating 'dirB'.
diff --git a/packages/js-sdk/test/assets/video.webm b/packages/js-sdk/test/assets/video.webm
new file mode 100644
index 000000000..77f488a57
Binary files /dev/null and b/packages/js-sdk/test/assets/video.webm differ
diff --git a/packages/js-sdk/test/filesystem.test.mjs b/packages/js-sdk/test/filesystem.test.mjs
index cc0f2a390..099c54255 100644
--- a/packages/js-sdk/test/filesystem.test.mjs
+++ b/packages/js-sdk/test/filesystem.test.mjs
@@ -1,5 +1,7 @@
import { Session } from '../src'
import { expect, test } from 'vitest'
+import * as fs from "node:fs"
+import * as path from 'node:path';
const E2B_API_KEY = process.env.E2B_API_KEY
@@ -26,11 +28,17 @@ test('create file', async () => {
test('read and write', async () => {
const session = await Session.create({ id: 'Nodejs', apiKey: E2B_API_KEY })
-
+
+ // String
await session.filesystem.write('/tmp/test.txt', 'Hello World!')
-
const content = await session.filesystem.read('/tmp/test.txt')
expect(content).toEqual('Hello World!')
+
+ // Binary file
+ const binaryFile = fs.readFileSync(path.join(__dirname, '/assets/video.webm'))
+ await session.filesystem.writeBytes('/tmp/video.webm', binaryFile)
+ const binaryContent = await session.filesystem.readBytes('/tmp/video.webm')
+ expect(binaryContent).toEqual(binaryFile)
await session.close()
})