← All Modules

assay.shell

WebSocket ↔ PTY bridge for hosting in-browser terminals (xterm.js etc.) from inside an http.serve handler. Combines process.spawn_pty and the new {ws = ...} server-upgrade in http.serve into a single 2-line bridge call.

local shell = require("assay.shell")

shell.bridge(conn, opts)

Bridge a WebSocket server connection to a fresh PTY child until either side closes. Returns when the child exits or the peer disconnects.

FieldTypeDefaultNotes
opts.cmdstringRequired. argv[0]: binary or PATH name.
opts.argsstring[]{}argv[1..].
opts.cwdstringinheritChild working directory.
opts.envtableinheritExtra env vars (key=value).
opts.colsinteger80Initial PTY columns.
opts.rowsinteger24Initial PTY rows.

Wire format with the browser

Resize is best-effort: malformed JSON, missing keys, or non-positive dimensions are ignored silently.

Example

A complete browser shell endpoint:

local shell = require("assay.shell")

http.serve(8080, {
  GET = {
    ["/shell"] = function(req)
      return {
        ws = function(conn)
          shell.bridge(conn, {
            cmd  = "bash",
            args = { "-l" },
            env  = { TERM = "xterm-256color" },
          })
        end,
      }
    end,
  },
})

Pair with xterm.js on the browser side; see examples/shell-server.lua for a runnable end-to-end demo with HTML and the resize protocol wired up.

Caveats