
Security News
GitHub Actions Adds cache-mode to Limit Cache Poisoning Risk
GitHub Actions now supports cache-mode, a least-privilege control on the Actions cache aimed at the cache poisoning technique behind recent compromises.
@device-router/middleware-koa
Advanced tools
Koa middleware for DeviceRouter — device classification and rendering hints per request
Koa middleware for DeviceRouter. Adds device classification and rendering hints to every request.
pnpm add @device-router/middleware-koa @device-router/storage
For automatic probe injection:
pnpm add @device-router/probe
import Koa from 'koa';
import { createDeviceRouter } from '@device-router/middleware-koa';
import { MemoryStorageAdapter } from '@device-router/storage';
const app = new Koa();
const { middleware, probeEndpoint } = createDeviceRouter({
storage: new MemoryStorageAdapter(),
});
app.use(middleware);
app.use(async (ctx) => {
if (ctx.method === 'POST' && ctx.path === '/device-router/probe') {
return probeEndpoint(ctx);
}
const profile = ctx.state.deviceProfile;
if (profile?.hints.preferServerRendering) {
ctx.body = renderSSR();
} else if (profile?.hints.deferHeavyComponents) {
ctx.body = renderLite();
} else {
ctx.body = renderFull();
}
});
app.listen(3000);
ctx.state.deviceProfilectx.state.deviceProfile.hints and ctx.state.deviceProfile.tiers to adapt responsesAutomatically inject the probe <script> into HTML responses:
const { middleware, probeEndpoint, injectionMiddleware } = createDeviceRouter({
storage: new MemoryStorageAdapter(),
injectProbe: true,
probeNonce: 'my-csp-nonce', // optional
});
app.use(injectionMiddleware); // before routes
app.use(middleware);
Streaming responses: Injection only runs when
ctx.bodyis a string. If you setctx.bodyto aStream, injection is silently skipped. Add the probe<script>tag to your HTML shell manually instead.
const { middleware, probeEndpoint } = createDeviceRouter({
storage,
thresholds: {
cpu: { lowUpperBound: 4, midUpperBound: 8 },
memory: { midUpperBound: 8 },
},
});
| Option | Type | Default | Description |
|---|---|---|---|
storage | StorageAdapter | (required) | Storage backend for profiles |
cookieName | string | 'device-router-session' | Session cookie name |
cookiePath | string | '/' | Cookie path |
cookieSecure | boolean | false | Set Secure flag on the session cookie |
ttl | number | 86400 (24h) | Profile TTL in seconds |
rejectBots | boolean | true | Reject bot/crawler probe submissions |
thresholds | TierThresholds | Built-in defaults | Custom tier thresholds (validated at startup) |
injectProbe | boolean | false | Auto-inject probe into HTML |
probePath | string | — | Custom probe endpoint path |
probeNonce | string | ((ctx: Context) => string) | — | CSP nonce for injected script |
fallbackProfile | FallbackProfile | — | Fallback profile for first requests |
classifyFromHeaders | boolean | false | Classify from UA/Client Hints |
onEvent | OnEventCallback | — | Observability callback for logging/metrics |
Pass an onEvent callback to receive events for classification, storage, bot rejection, and errors:
const { middleware, probeEndpoint } = createDeviceRouter({
storage,
onEvent: (event) => {
console.log(`[device-router] ${event.type}`, event);
},
});
See the Observability guide for details.
Use the individual pieces when you need fine-grained control over each component:
import {
createMiddleware,
createProbeEndpoint,
createInjectionMiddleware,
loadProbeScript,
} from '@device-router/middleware-koa';
// Use only what you need
const middleware = createMiddleware({ storage, thresholds });
const endpoint = createProbeEndpoint({ storage, ttl: 3600 });
const injection = createInjectionMiddleware({
probeScript: loadProbeScript(),
});
app.use(injection);
app.use(middleware);
loadProbeScript() reads the @device-router/probe bundle and optionally rewrites the endpoint URL via { probePath }. Thresholds passed to createMiddleware() are validated at creation time.
createDeviceRouter(options) — All-in-one setup returning { middleware, probeEndpoint, injectionMiddleware? }createMiddleware(options) — Standalone middleware (validates thresholds)createProbeEndpoint(options) — Standalone probe endpoint handlercreateInjectionMiddleware(options) — Standalone injection middlewareloadProbeScript(options?) — Load the minified probe script for use with createInjectionMiddleware()MIT
FAQs
Koa middleware for DeviceRouter — device classification and rendering hints per request
We found that @device-router/middleware-koa demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.

Security News
GitHub Actions now supports cache-mode, a least-privilege control on the Actions cache aimed at the cache poisoning technique behind recent compromises.

Company News
Allow myself to introduce... myself.

Research
/Security News
A Twitch browser extension on Chrome and Firefox forwards users’ live OAuth session tokens through proxies controlled by a Russian bot service.