Skip to content

Commit

Permalink
feat(js-sdk): Add an option to upload and download binary files via w…
Browse files Browse the repository at this point in the history
…riteBytes and readBytes (#151)
  • Loading branch information
Strajk authored Sep 20, 2023
1 parent 1fc210b commit c04b445
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-cameras-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@e2b/sdk": minor
---

Add an option to upload and download binary files via writeBytes and readBytes
1 change: 1 addition & 0 deletions packages/js-sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 2 additions & 2 deletions packages/js-sdk/src/session/filesystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
52 changes: 26 additions & 26 deletions packages/js-sdk/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down
Binary file added packages/js-sdk/test/assets/video.webm
Binary file not shown.
12 changes: 10 additions & 2 deletions packages/js-sdk/test/filesystem.test.mjs
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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()
})
Expand Down

0 comments on commit c04b445

Please sign in to comment.