Skip to content

armgit / E2B

Public Imported from GitHub
0

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

(docs) Add docs for reading and writing bytes

Vasek Mlejnskye3a9c9bde822
diff --git a/apps/docs/src/code/js/basics/fs_read_bytes.js b/apps/docs/src/code/js/basics/fs_read_bytes.js
new file mode 100644
index 000000000..82836dd07
--- /dev/null
+++ b/apps/docs/src/code/js/basics/fs_read_bytes.js
@@ -0,0 +1,20 @@
+import fs from 'fs'
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// File bytes will read file's content as bytes
+// `fileBytes` as a Uint8Array
+const fileBytes = await session.filesystem.readBytes('/etc/hosts') // $HighlightLine
+
+// The output will look similar to this:
+// <Buffer 31 32 37 2e 30 2e 30 2e 31 09 6c 6f 63 61 6c 68 6f 73 ...
+console.log(fileBytes)
+
+// We can save those bytes to a file locally like this:
+fs.writeFileSync('hosts', fileBytes)
+
+await session.close()
diff --git a/apps/docs/src/code/js/basics/fs_write_bytes.js b/apps/docs/src/code/js/basics/fs_write_bytes.js
new file mode 100644
index 000000000..542202556
--- /dev/null
+++ b/apps/docs/src/code/js/basics/fs_write_bytes.js
@@ -0,0 +1,21 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// Let's convert string to bytes for testing purposes
+const encoder = new TextEncoder('utf-8')
+const contentInBytes = encoder.encode('Hello World!')
+
+// `writeBytes` accepts a Uint8Array and saves it to a file inside the playground
+await session.filesystem.writeBytes('/file', contentInBytes) // $HighlightLine
+
+// We can read the file back to verify the content
+const fileContent = await session.filesystem.read('/file')
+
+// This will print 'Hello World!'
+console.log(fileContent)
+
+await session.close()
diff --git a/apps/docs/src/code/js/package-lock.json b/apps/docs/src/code/js/package-lock.json
index 39d91cc46..dafa52682 100644
--- a/apps/docs/src/code/js/package-lock.json
+++ b/apps/docs/src/code/js/package-lock.json
@@ -9,18 +9,33 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
- "@e2b/sdk": "^0.2.1"
+ "@e2b/sdk": "^0.3.0"
}
},
"node_modules/@e2b/sdk": {
- "version": "0.2.1",
- "resolved": "https://registry.npmjs.org/@e2b/sdk/-/sdk-0.2.1.tgz",
- "integrity": "sha512-DxMHCeMIoizDsFwkfvQeKkD9u6p7tMmQIf2ijBMSACrARv2q3El3fSguWzZDLwH9t46HSDCHQOmIc5nY6eNglw==",
+ "version": "0.3.0",
+ "resolved": "https://registry.npmjs.org/@e2b/sdk/-/sdk-0.3.0.tgz",
+ "integrity": "sha512-80eC923ebOBOQHByNY9ZoILrh0PnN4AzDWv5vJJ1DKJ/Qrw7h0lFqHuogrwSpTN/tr/ZLR5jtDOJL8zM/RkFaA==",
"dependencies": {
+ "bufferutil": "^4.0.7",
"cross-fetch": "^3.1.5",
"normalize-path": "^3.0.0",
"openapi-typescript-fetch": "^1.1.3",
- "rpc-websocket-client": "^1.1.4"
+ "platform": "^1.3.6",
+ "rpc-websocket-client": "^1.1.4",
+ "utf-8-validate": "^5.0.10"
+ }
+ },
+ "node_modules/bufferutil": {
+ "version": "4.0.7",
+ "resolved": "https://registry.npmjs.org/bufferutil/-/bufferutil-4.0.7.tgz",
+ "integrity": "sha512-kukuqc39WOHtdxtw4UScxF/WVnMFVSQVKhtx3AjZJzhd0RGZZldcrfSEbVsWWe6KNH253574cq5F+wpv0G9pJw==",
+ "hasInstallScript": true,
+ "dependencies": {
+ "node-gyp-build": "^4.3.0"
+ },
+ "engines": {
+ "node": ">=6.14.2"
}
},
"node_modules/cross-fetch": {
@@ -58,6 +73,16 @@
}
}
},
+ "node_modules/node-gyp-build": {
+ "version": "4.6.1",
+ "resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.6.1.tgz",
+ "integrity": "sha512-24vnklJmyRS8ViBNI8KbtK/r/DmXQMRiOMXTNz2nrTnAYUwjmEEbnnpB/+kt+yWRv73bPsSPRFddrcIbAxSiMQ==",
+ "bin": {
+ "node-gyp-build": "bin.js",
+ "node-gyp-build-optional": "optional.js",
+ "node-gyp-build-test": "build-test.js"
+ }
+ },
"node_modules/normalize-path": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz",
@@ -75,6 +100,11 @@
"npm": ">= 7.0.0"
}
},
+ "node_modules/platform": {
+ "version": "1.3.6",
+ "resolved": "https://registry.npmjs.org/platform/-/platform-1.3.6.tgz",
+ "integrity": "sha512-fnWVljUchTro6RiCFvCXBbNhJc2NijN7oIQxbwsyL0buWJPG85v81ehlHI9fXrJsMNgTofEoWIQeClKpgxFLrg=="
+ },
"node_modules/rpc-websocket-client": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/rpc-websocket-client/-/rpc-websocket-client-1.1.4.tgz",
@@ -93,6 +123,18 @@
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw=="
},
+ "node_modules/utf-8-validate": {
+ "version": "5.0.10",
+ "resolved": "https://registry.npmjs.org/utf-8-validate/-/utf-8-validate-5.0.10.tgz",
+ "integrity": "sha512-Z6czzLq4u8fPOyx7TU6X3dvUZVvoJmxSQ+IcrlmagKhilxlhZgxPK6C5Jqbkw1IDUmFTM+cz9QDnnLTwDz/2gQ==",
+ "hasInstallScript": true,
+ "dependencies": {
+ "node-gyp-build": "^4.3.0"
+ },
+ "engines": {
+ "node": ">=6.14.2"
+ }
+ },
"node_modules/uuid": {
"version": "3.4.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz",
diff --git a/apps/docs/src/code/js/package.json b/apps/docs/src/code/js/package.json
index ea030a10a..a3156f0f9 100644
--- a/apps/docs/src/code/js/package.json
+++ b/apps/docs/src/code/js/package.json
@@ -12,6 +12,6 @@
"author": "",
"license": "ISC",
"dependencies": {
- "@e2b/sdk": "^0.2.2"
+ "@e2b/sdk": "^0.3.0"
}
}