Guides
Using srvx
The srvx adapter is the right boundary when your server hands middleware a
ServerRequestwith host metadata.
Mount a Chain
Import toFetchHandler from alien-middleware/srvx when passing a chain to
srvx serve.
import { serve } from 'srvx'
import { chain } from 'alien-middleware'
import { toFetchHandler } from 'alien-middleware/srvx'
const app = chain().use(context => {
return new Response(`path: ${context.url.pathname}`)
})
serve({
port: 3000,
fetch: toFetchHandler(app),
})
The adapter creates an alien-middleware context for each srvx request and calls
the chain. The returned function has the shape srvx expects for fetch.
Read srvx Host Metadata
The srvx adapter copies ServerRequest metadata into context.host.
const app = chain().use(context => {
return new Response(
JSON.stringify({
ip: context.host.ip,
runtime: context.host.runtime?.name,
})
)
})
If the srvx request provides ip, runtime, or waitUntil, the adapter maps
them to context.host.ip, context.host.runtime, and context.waitUntil().
Override Host Metadata
Pass static host metadata when a deployment needs known overrides.
import { toFetchHandler } from 'alien-middleware/srvx'
serve({
fetch: toFetchHandler(app, {
host: {
ip: '127.0.0.1',
runtime: { name: 'local' },
},
}),
})
Static host values take precedence over srvx request values for the same field.
Compute Host Metadata per Request
Pass a host factory when metadata depends on the request.
serve({
fetch: toFetchHandler(app, {
host: request => ({
env: name => {
if (name === 'PATHNAME') {
return new URL(request.url).pathname
}
},
runtime: { name: request.runtime?.name ?? 'unknown' },
}),
}),
})
The factory runs for each request before the chain receives its context.
Create a Context Manually
Use createContextFromServerRequest in tests or custom srvx glue code when you
need a context object without creating a fetch handler.
import { createContextFromServerRequest } from 'alien-middleware/srvx'
const context = createContextFromServerRequest(request)
context.host.ip
context.host.runtime
When you already have a plain Web Request, use createContext from the root
entrypoint instead.
The srvx adapter treats host metadata like Agent J's neuralyzer: it keeps the useful flash and leaves the rest at the boundary.