
Security News
Happy Birthday, Shai-Hulud
It has been one year since Shai-Hulud made its first appearance on npm.
@device-router/middleware-fastify
Advanced tools
Fastify plugin for [DeviceRouter](https://github.com/SimplyLiz/DeviceRouter). Adds device classification and rendering hints to every request.
Fastify plugin for DeviceRouter. Adds device classification and rendering hints to every request.
pnpm add @device-router/middleware-fastify @device-router/storage @fastify/cookie
For automatic probe injection:
pnpm add @device-router/probe
import Fastify from 'fastify';
import cookie from '@fastify/cookie';
import { createDeviceRouter } from '@device-router/middleware-fastify';
import { MemoryStorageAdapter } from '@device-router/storage';
const app = Fastify();
const { plugin, probeEndpoint } = createDeviceRouter({
storage: new MemoryStorageAdapter(),
});
await app.register(cookie);
await app.register(plugin);
app.post('/device-router/probe', probeEndpoint);
app.get('/', (req, reply) => {
const profile = req.deviceProfile;
if (profile?.hints.preferServerRendering) {
return reply.send(renderSSR());
}
if (profile?.hints.deferHeavyComponents) {
return reply.send(renderLite());
}
reply.send(renderFull());
});
app.listen({ port: 3000 });
preHandler hook that reads the session cookie, loads the profile from storage, and attaches it to req.deviceProfilereq.deviceProfile.hints and req.deviceProfile.tiers to adapt responsesAutomatically inject the probe <script> into HTML responses:
const { plugin, probeEndpoint, injectionHook } = createDeviceRouter({
storage: new MemoryStorageAdapter(),
injectProbe: true,
probeNonce: 'my-csp-nonce', // optional
});
When injectProbe is enabled, the plugin registers an onSend hook that injects the script before </head>.
const { plugin, 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 | 'dr_session' | Session cookie name |
cookiePath | string | '/' | Cookie path |
ttl | number | 86400 (24h) | Profile TTL in seconds |
thresholds | TierThresholds | Built-in defaults | Custom tier thresholds |
injectProbe | boolean | false | Auto-inject probe into HTML |
probePath | string | — | Custom probe endpoint path |
probeNonce | string | ((req: FastifyRequest) => string) | — | CSP nonce for injected script |
createDeviceRouter(options) — All-in-one setup returning { plugin, probeEndpoint, injectionHook? }createMiddleware(options) — Standalone preHandler hookcreateProbeEndpoint(options) — Standalone probe endpoint handlercreateInjectionHook(options) — Standalone onSend injection hook@fastify/cookie 11.xMIT
FAQs
Fastify middleware for DeviceRouter — device classification and rendering hints per request
The npm package @device-router/middleware-fastify receives a total of 7 weekly downloads. As such, @device-router/middleware-fastify popularity was classified as not popular.
We found that @device-router/middleware-fastify 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
It has been one year since Shai-Hulud made its first appearance on npm.

Research
/Security News
Operators behind PolinRider used a compromised GitHub account to plant malware in four development versions of a Packagist package with 700,000+ downloads.

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.