JavaScript Support

js-cgi provides full ES2023+ JavaScript support in a lightweight, embeddable runtime.

Supported Features

All standard ES2023+ language features are available:

  • let, const, block scoping
  • Arrow functions
  • Template literals
  • Destructuring (arrays and objects)
  • Spread/rest operators
  • Classes and inheritance
  • Promises, async/await
  • Generators and iterators
  • Symbols
  • Map, Set, WeakMap, WeakSet
  • Proxy and Reflect
  • BigInt
  • Optional chaining (?.)
  • Nullish coalescing (??)
  • Array methods (.at(), .findLast(), .toSorted(), etc.)
  • Object methods (Object.groupBy(), Object.hasOwn(), etc.)
  • Regular expression improvements (lookbehind, named groups, /d flag)
  • ES Modules (import/export)
  • Top-level await (in module mode)

What's Not Available

js-cgi is a server-side CGI environment, not a browser or Node.js. The following are not available:

Browser APIs

  • window, document, DOM
  • fetch() — use the http extension instead
  • localStorage, sessionStorage
  • XMLHttpRequest
  • Web Workers
  • Canvas, WebGL

Node.js APIs

Timers

  • setTimeout(), setInterval(), setImmediate()

Scripts execute synchronously from top to bottom. There is no event loop.

Available Built-in Objects

All standard JavaScript built-in objects work as expected:

  • JSON — parse and stringify
  • Math — mathematical operations
  • Date — date and time
  • RegExp — regular expressions
  • Array, Object, String, Number, Boolean
  • Map, Set, WeakMap, WeakSet
  • Promise
  • Error and subtypes
  • ArrayBuffer, DataView, typed arrays

js-cgi Globals

In addition to standard JavaScript, js-cgi provides these globals:

GlobalDescription
requestIncoming HTTP request (method, headers, query, body, cookies)
responseHTTP response controls (setHeader, setStatus, setCookie)
print()Output to response body
include()Execute another .js file in the same context

Extensions add additional globals (sqlite, session, file, crypto, http) when loaded.

Was this page helpful?