The following APIs are the same as the native Web APIS you already know. We also follow the WinterCG (opens in a new tab) conventions. Lagon's Runtime uses the V8 engine and is written in both Rust and TypeScript.
Handler
The only required code to make your Function runnable is to export a handler
function, that accepts a Request
and returns a Response
(or a promise returning a Response):
export function handler(request: Request) {
return new Response('Hello World!');
}
Starting from this simple code, you can do whatever you wish, using the Web APIs you already know.
Additional Headers
The Request
object coming from the handler
function also contains additional headers:
X-Lagon-Region
: the region where this Function is executingX-Forwarded-For
: the IP address of the client that made the request
You can access them the same as any other header:
export function handler(request: Request) {
const region = request.headers.get('x-lagon-region');
const ip = request.headers.get('x-forwarded-for');
return new Response(`Region: ${region}, User IP: ${ip}`);
}
The X-Lagon-Region
header is also automatically added to each response, making it easy to identify which Region served the request.
When developing locally using lagon dev
, the X-Lagon-Region
header will be set to local
.
NPM support
Lagon's Runtime supports any NPM package. The only requirement is that the package must not use Node.js-specific APIs (e.g Buffer
, fs
, path
, etc.). This is because Lagon's Runtime is not Node.js, but a browser-like environment.
Global objects
AbortController
The standard AbortController
object. See the documentation on MDN (opens in a new tab).
AbortSignal
The standard AbortSignal
object. See the documentation on MDN (opens in a new tab).
AsyncContext
An early implementation of the Async Context proposal (opens in a new tab). You shouldn't use this API yet, as it is still experimental and subject to change.
AsyncLocalStorage
A minimal implementation of Node.js's AsyncLocalStorage (opens in a new tab). The following methods are supported:
getStore()
run(store, callback, ...args)
Blob
The standard Blob
object. See the documentation on MDN (opens in a new tab).
console
Similar to the standard console
object on the browser and Node.js, except that it only supports the following methods:
log
info
debug
warn
error
You can log multiple objects, and use string substitution. See the documentation on MDN (opens in a new tab).
crypto
The standard crypto
object.
crypto.randomUUID()
The standard randomUUID()
method. See the documentation on MDN (opens in a new tab).
crypto.getRandomValues()
The standard getRandomValues()
method. See the documentation on MDN (opens in a new tab).
crypto.subtle
The standard CryptoSubtle
object. See the documentation on MDN (opens in a new tab).
The following table summarizes the supported algorithms on each method:
sign() , verify() | encrypt() , decrypt() | digest() | deriveBits() , deriveKey() | wrapKey() , unwrapKey() | |
---|---|---|---|---|---|
HMAC | ✅ | ||||
SHA-1 | ✅ | ||||
SHA-256 | ✅ | ||||
SHA-384 | ✅ | ||||
SHA-512 | ✅ | ||||
AES-GCM | ✅ | ✅ |
CustomEvent
The standard CustomEvent
object. See the documentation on MDN (opens in a new tab).
Event
The standard Event
object. See the documentation on MDN (opens in a new tab).
EventTarget
The standard EventTarget
object. See the documentation on MDN (opens in a new tab).
Fetch APIs
fetch()
method? Jump to fetch().Headers
The standard Headers
object. It also supports the getSetCookie()
method (opens in a new tab). See the documentation on MDN (opens in a new tab).
Request
The standard Request
object. See the documentation on MDN (opens in a new tab).
Response
The standard Response
object. See the documentation on MDN (opens in a new tab).
Streaming:
You can pass a ReadableStream
object as the body
of a Response
to stream the response as more data becomes available. Often, you won't need to implement the logic yourself as it is implemented by the frameworks and libraries you use.
URL
The standard URL
object. See the documentation on MDN (opens in a new tab).
URLSearchParams
The standard URLSearchParams
object. See the documentation on MDN (opens in a new tab).
File
The standard File
object. See the documentation on MDN (opens in a new tab).
FileReader
The standard FileReader
object. See the documentation on MDN (opens in a new tab).
FormData
The standard FormData
object. See the documentation on MDN (opens in a new tab).
navigator.userAgent
navigator.userAgent
is a fixed string that can be used to detect the current runtime. Its value is always Lagon/VERSION
, where VERSION
is the current version of the Lagon Runtime.
process.env
The only usage of process
is to access environment variables. By default, it will only contain the NODE_ENV
variable, which is set to "production"
when deployed, and to "development"
when using lagon dev
.
Learn more about environment variables.
ProgressEvent
The standard ProgressEvent
object. See the documentation on MDN (opens in a new tab).
Stream APIs
ReadableStream
The standard ReadableStream
object. See the documentation on MDN (opens in a new tab).
ReadableStreamDefaultReader
The standard ReadableStreamDefaultReader
object. See the documentation on MDN (opens in a new tab).
TransformStream
The standard TransformStream
object. See the documentation on MDN (opens in a new tab).
WritableStream
The standard WritableStream
object. See the documentation on MDN (opens in a new tab).
WritableStreamDefaultWriter
The standard WritableStreamDefaultWriter
object. See the documentation on MDN (opens in a new tab).
TextEncoder
The standard TextEncoder
object. See the documentation on MDN (opens in a new tab).
TextDecoder
The standard TextDecoder
object. See the documentation on MDN (opens in a new tab).
Global methods
atob()
The standard atob
method. See the documentation on MDN (opens in a new tab).
btoa()
The standard btoa
method. See the documentation on MDN (opens in a new tab).
clearInterval()
The standard clearInterval
method. See the documentation on MDN (opens in a new tab).
clearTimeout()
The standard clearTimeout
method. See the documentation on MDN (opens in a new tab).
fetch()
The standard fetch
method. See the documentation on MDN (opens in a new tab).
queueMicrotask()
The standard queueMicrotask
method. See the documentation on MDN (opens in a new tab).
setInterval()
The standard setInterval
method. See the documentation on MDN (opens in a new tab).
setTimeout()
The standard setTimeout
method. See the documentation on MDN (opens in a new tab).