Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Add JS code snippets

Vasek Mlejnskyfde6f1cd5919
diff --git a/apps/docs/src/code/js/basics/fs_ls.js b/apps/docs/src/code/js/basics/fs_ls.js
new file mode 100644
index 000000000..5cf9fd20a
--- /dev/null
+++ b/apps/docs/src/code/js/basics/fs_ls.js
@@ -0,0 +1,13 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+const dirContent = await session.filesystem.list('/')
+dirContent.forEach((item) => {
+ console.log(item.name)
+})
+
+await session.close()
diff --git a/apps/docs/src/code/js/basics/fs_mkdir.js b/apps/docs/src/code/js/basics/fs_mkdir.js
new file mode 100644
index 000000000..c07ec5865
--- /dev/null
+++ b/apps/docs/src/code/js/basics/fs_mkdir.js
@@ -0,0 +1,12 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// `filesystem.make_dir()` will fail if any directory in the path doesn't exist
+// Create a new directory '/dir'
+await session.filesystem.makeDir('/dir')
+
+await session.close()
diff --git a/apps/docs/src/code/js/basics/fs_read.js b/apps/docs/src/code/js/basics/fs_read.js
new file mode 100644
index 000000000..7b6950ed3
--- /dev/null
+++ b/apps/docs/src/code/js/basics/fs_read.js
@@ -0,0 +1,11 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+const fileContent = await session.filesystem.read('/etc/hosts')
+console.log(fileContent)
+
+await session.close()
diff --git a/apps/docs/src/code/js/basics/fs_watch.js b/apps/docs/src/code/js/basics/fs_watch.js
new file mode 100644
index 000000000..1fda7be40
--- /dev/null
+++ b/apps/docs/src/code/js/basics/fs_watch.js
@@ -0,0 +1,26 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// Start filesystem watcher for the /home directory
+const watcher = session.filesystem.watchDir('/home')
+watcher.addEventListener(event => {
+ console.log('Filesystem event', event)
+})
+await watcher.start()
+
+
+// Create files in the /home directory inside the playground
+// We'll receive notifications for these events through the watcher we created above.
+for (let i = 0; i < 10; i++) {
+ console.log('Creating file', i)
+ // `filesystem.write()` will trigger two events:
+ // 1. 'Create' when the file is created
+ // 2. 'Write' when the file is written to
+ await session.filesystem.write(`/home/${i}.txt`, `Hello World ${i}!`)
+}
+
+await session.close()
diff --git a/apps/docs/src/code/js/basics/fs_write.js b/apps/docs/src/code/js/basics/fs_write.js
new file mode 100644
index 000000000..517981843
--- /dev/null
+++ b/apps/docs/src/code/js/basics/fs_write.js
@@ -0,0 +1,19 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// `filesystem.write()` will:
+// - create the file if it doesn't exist
+// - fail if any directory in the path doesn't exist
+// - overwrite the file if it exists
+
+// Write the content of the file '/hello.txt'
+await session.filesystem.write("/hello.txt", "Hello World!")
+
+// The following would fail because '/dir' doesn't exist
+// await session.filesystem.write("/dir/hello.txt", "Hello World!")
+
+await session.close()
diff --git a/apps/docs/src/code/js/basics/init.js b/apps/docs/src/code/js/basics/init.js
index 842b71ad7..5b17c7510 100644
--- a/apps/docs/src/code/js/basics/init.js
+++ b/apps/docs/src/code/js/basics/init.js
@@ -1,6 +1,11 @@
import { Session } from '@e2b/sdk'
+// `id` can also be one of:
+// 'Nodejs', 'Bash', 'Python3', 'Java', 'Go', 'Rust', 'PHP', 'Perl', 'DotNET'
+// We're working on custom environments.
const session = await Session.create({
id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
})
-await session.close()
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/basics/process_start.js b/apps/docs/src/code/js/basics/process_start.js
new file mode 100644
index 000000000..f69d27392
--- /dev/null
+++ b/apps/docs/src/code/js/basics/process_start.js
@@ -0,0 +1,15 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+const npmInit = await session.process.start({
+ cmd: 'npm init -y'
+})
+await npmInit.finished
+
+console.log(npmInit.output.stdout)
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/basics/process_stop.js b/apps/docs/src/code/js/basics/process_stop.js
new file mode 100644
index 000000000..88f00673a
--- /dev/null
+++ b/apps/docs/src/code/js/basics/process_stop.js
@@ -0,0 +1,15 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+const npmInit = await session.process.start({
+ cmd: 'npm init -y'
+})
+await npmInit.kill()
+// There will be no output because we immediatelly kill the `npm_init` process
+console.log(npmInit.output.stdout)
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/basics/process_stream_stderr.js b/apps/docs/src/code/js/basics/process_stream_stderr.js
new file mode 100644
index 000000000..9e7e8b53e
--- /dev/null
+++ b/apps/docs/src/code/js/basics/process_stream_stderr.js
@@ -0,0 +1,15 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// This command will fail and output to stderr because Golang isn't installed in the cloud playground
+const golangVersion = await session.process.start({
+ cmd: 'go version',
+ onStderr: output => console.log(output), // Print stderr to console
+})
+await golangVersion.finished
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/basics/process_stream_stdout.js b/apps/docs/src/code/js/basics/process_stream_stdout.js
new file mode 100644
index 000000000..8ed9fbc6b
--- /dev/null
+++ b/apps/docs/src/code/js/basics/process_stream_stdout.js
@@ -0,0 +1,14 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+const npmInit = await session.process.start({
+ cmd: 'npm init -y',
+ onStdout: output => console.log(output),
+})
+await npmInit.finished
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/basics/process_write_stdin.js b/apps/docs/src/code/js/basics/process_write_stdin.js
new file mode 100644
index 000000000..94b0cd06e
--- /dev/null
+++ b/apps/docs/src/code/js/basics/process_write_stdin.js
@@ -0,0 +1,17 @@
+import { Session } from '@e2b/sdk'
+
+const session = await Session.create({
+ id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
+})
+
+// This example will print back the string we send to the process using `sendStdin()`
+
+const proc = await session.process.start({
+ cmd: 'while IFS= read -r line; do echo \"$line\"; sleep 1; done',
+ onStdout: output => console.log(output),
+})
+await proc.sendStdin("AI Playground\n")
+await proc.kill()
+
+await session.close()
\ No newline at end of file
diff --git a/apps/docs/src/code/js/index.js b/apps/docs/src/code/js/index.js
new file mode 100644
index 000000000..e69de29bb
diff --git a/apps/docs/src/code/js/package-lock.json b/apps/docs/src/code/js/package-lock.json
index 7b8c9cd34..39d91cc46 100644
--- a/apps/docs/src/code/js/package-lock.json
+++ b/apps/docs/src/code/js/package-lock.json
@@ -9,13 +9,13 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
- "@e2b/sdk": "^0.1.0"
+ "@e2b/sdk": "^0.2.1"
}
},
"node_modules/@e2b/sdk": {
- "version": "0.1.0",
- "resolved": "https://registry.npmjs.org/@e2b/sdk/-/sdk-0.1.0.tgz",
- "integrity": "sha512-FtJ3s2uzIfXAxn81WJCM7/3paN3I7jGubwaiTQxED4eYNLEnhY+pCe6AGoAmdfGoZHahsX2ZcIRRxQXCAExrJg==",
+ "version": "0.2.1",
+ "resolved": "https://registry.npmjs.org/@e2b/sdk/-/sdk-0.2.1.tgz",
+ "integrity": "sha512-DxMHCeMIoizDsFwkfvQeKkD9u6p7tMmQIf2ijBMSACrARv2q3El3fSguWzZDLwH9t46HSDCHQOmIc5nY6eNglw==",
"dependencies": {
"cross-fetch": "^3.1.5",
"normalize-path": "^3.0.0",
@@ -40,9 +40,9 @@
}
},
"node_modules/node-fetch": {
- "version": "2.6.13",
- "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.13.tgz",
- "integrity": "sha512-StxNAxh15zr77QvvkmveSQ8uCQ4+v5FkvNTj0OESmiHu+VRi/gXArXtkWMElOsOUNLtUEvI4yS+rdtOHZTwlQA==",
+ "version": "2.7.0",
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
+ "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"dependencies": {
"whatwg-url": "^5.0.0"
},
diff --git a/apps/docs/src/code/js/package.json b/apps/docs/src/code/js/package.json
index be89f3cbc..15be418f0 100644
--- a/apps/docs/src/code/js/package.json
+++ b/apps/docs/src/code/js/package.json
@@ -3,13 +3,15 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
+ "type": "module",
"scripts": {
+ "all": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
- "@e2b/sdk": "^0.1.0"
+ "@e2b/sdk": "^0.2.1"
}
}
diff --git a/apps/docs/src/code/js/quickstart.js b/apps/docs/src/code/js/quickstart.js
index 310332b16..d67879ad9 100644
--- a/apps/docs/src/code/js/quickstart.js
+++ b/apps/docs/src/code/js/quickstart.js
@@ -5,5 +5,6 @@ import { Session } from '@e2b/sdk'
// We're working on custom environments.
const session = await Session.create({
id: 'Nodejs',
+ apiKey: process.env.E2B_API_KEY,
})
await session.close()