Execution Context
Zelt can run without AsyncLocalStorage (ALS). Request accessors and middleware values use the same APIs in both context modes.
| Operation inside an HTTP entry | sync | async |
|---|---|---|
Read request(), response(), or middlewareValue(M) in parameter defaults | Yes | Yes |
Use the captured accessor or value after await | Yes | Yes |
Call next(value) after await | Yes | Yes |
Call an ambient context helper directly after await | Throws | Yes |
In sync mode, ambient helpers are available only during the synchronous part of a framework invocation. A timer or .then() callback is outside that interval too. Each downstream middleware and handler receives a fresh invocation boundary, even when its upstream middleware awaited before calling next.
Choose a mode
The app declares its context mode. createApp defaults to async; declare sync for apps that do not rely on ambient context after await:
import { createApp, http } from '@zeltjs/core';
const app = createApp([http({ controllers: [] })], { contextMode: 'sync' });
app.contextMode; // 'sync'
The adapter or runtime does not change the declared mode. A sync app stays sync even on Node with ALS enabled; its context does not become ambient after suspension.
Running an async app requires ALS, which is a separate capability provided by its own entry. Core itself does not import any Node built-ins. Enable it before creating the runtime:
import { createApp, http } from '@zeltjs/core';
import { enableAsyncContext } from '@zeltjs/core/async-context';
enableAsyncContext();
const app = createApp([http({ controllers: [] })]);
const runtime = await app.createRuntime();
enableAsyncContext() is idempotent and applies to every app created from the same @zeltjs/core installation. It only makes async apps runnable; it does not change any app's mode.
Node, Bun, Lambda, and Electron adapters call enableAsyncContext() for you. On Cloudflare Workers, enable nodejs_compat (or nodejs_als) and call enableAsyncContext() in the worker entry before creating the runtime; the adapter itself does not import ALS, so an app declared { contextMode: 'sync' } runs without those flags. txiki.js and tinyjs provide no ALS, so apps on those runtimes declare { contextMode: 'sync' }.
Creating a runtime for an async app without ALS enabled throws a TypeError. There is no automatic fallback to synchronous mode.
Portable handler signatures
Capture framework inputs at the invocation boundary:
import { Controller, Get, request, response } from '@zeltjs/core';
@Controller('/users')
class UsersController {
@Get('/:id')
async find(req = request(), res = response()) {
await Promise.resolve();
return res.json({ id: req.pathParam('id') });
}
}
Use value = middlewareValue(MyMiddleware) and opts = middlewareOptions(MyMiddleware) the same way. Captured request accessors retain their body cache, path parameters, and Hono context. Middleware instances remain shared singletons; each invocation's arguments and next retain its own request state. A saved next cannot be used after that middleware's use() has completed.
If an operation needs to read or update context later, capture an explicit execution handle before suspension:
import { captureContext, Controller, Get, request } from '@zeltjs/core';
@Controller('/example')
class ExampleController {
@Get('/')
async handle(req = request()) {
const context = captureContext();
await Promise.resolve();
return context.run(() => ({ path: request().path(), method: req.method() }));
}
}
In sync mode, context.run(async () => ...) restores ambient access only until that callback first suspends. Capture its inputs before awaiting as usual. setUser, session mutation functions, and platform-binding reads follow this same rule.
Other context-dependent features
- Logging:
logger.child(bindings)retains its bindings across awaits in both modes.withLogContextonly propagates implicitly across awaits inasyncmode. - Tasks:
run()andrunAndWait()work in both modes. Insyncmode,shutdown()rejects withTaskRunnerSynchronousShutdownErrorwhile tasks are active, because an external caller cannot be distinguished from a task waiting on itself. AwaitrunAndWait()before shutdown, or useasyncmode for task draining. afterResponse()tasks run in the completed request's flush scope. Temporary storage scopes from registration, including database transactions and log bindings, are not retained. Capture any data or child logger your task needs before returning from the handler.- Database: automatic transaction propagation requires
asyncmode. This change does not redesign database transactions. - CLI and GraphQL: capture
args(...)in method parameter defaults. Resolver invocation restores its execution context after asynchronous resolver lookup.
onTest and createTestTarget from @zeltjs/testing enable ALS automatically for async apps, so tests run in the same mode as production. A portable test uses an app declared with { contextMode: 'sync' }.