Skip to content

Instantly share code, notes, and snippets.

@sgammon
Last active July 3, 2024 06:03
Show Gist options
  • Save sgammon/b04f1f31628f3557e6ed5e9d7d3dbba7 to your computer and use it in GitHub Desktop.
Save sgammon/b04f1f31628f3557e6ed5e9d7d3dbba7 to your computer and use it in GitHub Desktop.
stuff to try in elide

Stuff to try

Thanks for trying Elide :)

elide --help
elide --version

Basic commands

To see runtime info:

elide info

JavaScript

To run a JavaScript terminal:

elide

Or:

elide repl

Encode some JSON:

JSON.stringify({ x: 5 });

Generate a UUID:

crypto.randomUUID()

Parse a URL and print the host:

new URL("https://google.com").host;

List the machine CPUs

const { cpus } = require("node:os")
cpus()

To run a JavaScript server:

(see server.js below)

elide serve server.js

Python

To run a Python terminal:

elide repl --python

To see process + .env environment:

import os; print(os.environ)

Note

Elide isolates host environment, I/O, and other sensitive operations from runtime code by default.

To see all environment, including host env:

elide repl --python --host:allow-env
import os; print(os.environ)

Ruby

To run a Ruby terminal:

elide repl --ruby

Say hello:

puts "Hello, Elide!"

Tooling integrations

Install JavaScript packages:

mkdir tmp && cd tmp \
  && echo '{"name":"hello","dependencies":{"glob":"10.4.2"}}' > package.json \
  && elide install

This embeds Orogene.

Note

Package installation is only available for JavaScript for now, but will soon include Python, Ruby, and JVM.

Lint Python files:

mkdir tmp && cd tmp \
  && echo 'invalid lol' > sample.py \
  && touch requirements.txt \
  && elide check sample.py

This embeds Ruff.

Note

Linting/checking/formatting will soon ship in JavaScript, TypeScript, and Python, with Ruby, Kotlin, and Java to follow. Elide detects the languages in use for a project when files like package.json or requirements.txt are present.

Benchmarks

macOS JS server benchmarks are run with:

hyperfine --warmup 1000 --runs 1000 --shell none "curl http://localhost:3000/plaintext" \
  && oha -z 30s http://localhost:3000/plaintext
// access the built-in HTTP server engine
const app = Elide.http
// register basic handler
app.router.handle("GET", "/", (request, response) => {
response.send(200, "Hello, Elide!")
})
// register plaintext bench handler
app.router.handle("GET", "/plaintext", (request, response) => {
response.send(200, "Hello, World!")
})
// register json bench handler
app.router.handle("GET", "/json", (request, response) => {
response.header("Content-Type", "application/json")
response.send(200, JSON.stringify({ message: "Hello, World!" }))
})
// register a route handler
app.router.handle("GET", "/hello/:name", (request, response, context) => {
// respond using the captured path variables
response.send(200, `Hello, ${context.params.name}`)
})
// configure the server binding options
app.config.port = 3000
// receive a callback when the server starts
app.config.onBind(() => {
console.log(`Server listening at "http://localhost:${app.config.port}"! 🚀`)
})
// start the server
app.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment