> ## Documentation Index
> Fetch the complete documentation index at: https://docs.napps.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Runtime APIs

Expresso provides a JavaScript runtime with selected web-like APIs. It is not a browser: DOM, `window`, cookies, Web Workers, Canvas, and WebGL are not available.

## Runtime globals

```ts theme={null}
setTimeout(callback: Function, delayMs?: number, ...args: unknown[]): number;
clearTimeout(id: number): void;
setInterval(callback: Function, delayMs?: number, ...args: unknown[]): number;
clearInterval(id: number): void;
queueMicrotask(callback: Function): void;
```

`console` is available with the usual `log`, `info`, `warn`, `error`, `debug`, `trace`, `dir`, `table`, `assert`, timing, grouping, and clearing methods.

## Fetch

Expresso supports `fetch`, `Headers`, `FormData`, `Request`, `Response`, `Blob`, and `AbortController`.

```ts theme={null}
const controller = new AbortController();
const response = await fetch("https://example.com/data.json", {
  headers: { Accept: "application/json" },
  signal: controller.signal
});

if (response.ok) {
  const data = await response.json();
}
```

The request and response objects support `text()`, `json()`, `arrayBuffer()`, `bytes()`, `blob()`, and `clone()`. Use `FormData` for string form fields and `Headers` for case-insensitive request headers.

Fetch does not provide browser cookies, browser cache, DOM events, or browser CORS behavior. Check `response.ok` and `response.status` explicitly.

## Web Streams

Fetch response bodies are exposed as WHATWG-style `ReadableStream` objects.

```ts theme={null}
const response = await fetch(url);
const reader = response.body?.getReader();

if (reader) {
  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    console.log(value); // Uint8Array
  }
  reader.releaseLock();
}
```

`ReadableStream` supports `locked`, `getReader()`, and `cancel()`. A reader supports `read()`, `cancel()`, and `releaseLock()`. Network response bodies stream incrementally; JavaScript-supplied underlying sources are not supported.

## WebSocket

Expresso exposes a WHATWG-style `WebSocket` for text and binary messaging.

```ts theme={null}
const socket = new WebSocket("wss://example.com/events");

socket.addEventListener("open", () => socket.send("hello"));
socket.addEventListener("message", (event) => console.log(event.data));
socket.addEventListener("error", () => console.error("socket failed"));
socket.addEventListener("close", (event) => console.log(event.code));
```

The API exposes `url`, `protocol`, `extensions`, `binaryType`, `readyState`, and `bufferedAmount`, plus `send()` and `close()`. Supported events are `open`, `message`, `error`, and `close`. Connections do not automatically reconnect.

## Abort APIs

```ts theme={null}
const controller = new AbortController();
const request = fetch(url, { signal: controller.signal });

controller.abort("no longer needed");
```

`AbortSignal` exposes `aborted`, `reason`, `onabort`, `addEventListener`, `removeEventListener`, and `throwIfAborted()`.

## Storage

### `sessionStorage`

`sessionStorage` is synchronous and shared across runtime contexts in the current app process. It is cleared when the app logs out or the process ends.

```ts theme={null}
sessionStorage.setItem("selectedVariant", "123");
const value = sessionStorage.getItem("selectedVariant");
```

Use `length`, `key(index)`, `removeItem(key)`, and `clear()` to manage values. The store has a 5 MiB total limit and throws `QuotaExceededError` when exceeded.

### `AppStorage`

`AppStorage` is persistent, asynchronous, and organized into named scopes. Availability depends on the host providing a storage directory.

```ts theme={null}
if (AppStorage.available) {
  const scope = AppStorage.scope("cart");
  const values = scope.kv("preferences");
  await values.set("key", "value");
  const value = await values.get("key");
}
```

The API also supports tables, bulk operations, expiration, usage inspection, clearing scopes, and dropping scopes. Calls reject when storage is unavailable.

## URL APIs

```ts theme={null}
const url = new URL("https://example.com/products?q=shoes");
url.searchParams.set("page", "2");

URL.canParse(url.toString());
URL.parse(url.toString());
```

`URLSearchParams` supports `append`, `delete`, `get`, `getAll`, `has`, `set`, `sort`, `entries`, `keys`, `values`, and `forEach`.

## Performance

The runtime provides `performance.now()`, `timeOrigin`, marks, measures, entry queries, and `PerformanceObserver` for `mark` and `measure` entries.

```ts theme={null}
performance.mark("load-start");
// work
performance.mark("load-end");
const entries = performance.getEntriesByName("load-end");
```
