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,
/dflag) - 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,DOMfetch()— use the http extension insteadlocalStorage,sessionStorageXMLHttpRequest- Web Workers
- Canvas, WebGL
Node.js APIs
require()— use ES modules orinclude()process,Bufferfs,path,os— use the file extensionhttp/httpsmodules — use the http extensioncryptomodule — use the crypto extension- npm packages
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 stringifyMath— mathematical operationsDate— date and timeRegExp— regular expressionsArray,Object,String,Number,BooleanMap,Set,WeakMap,WeakSetPromiseErrorand subtypesArrayBuffer,DataView, typed arrays
js-cgi Globals
In addition to standard JavaScript, js-cgi provides these globals:
| Global | Description |
|---|---|
request | Incoming HTTP request (method, headers, query, body, cookies) |
response | HTTP 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.