$ show fastify/fastify-cors
This is the official Fastify CORS plugin, implementing Cross-Origin Resource Sharing (CORS) for Fastify servers. It provides comprehensive configuration for all standard CORS headers, including origin (string, regex, array, boolean, or dynamic function), methods, allowed headers, exposed headers, credentials, max age, cache control, and preflight handling. The architecture is a standard Fastify plugin that registers a configurable lifecycle hook (default onRequest) and a wildcard OPTIONS route to handle preflight requests. It supports dynamic configuration via a delegator callback or promise, and per-route CORS overrides via route config. The code is well-structured, thoroughly tested, and includes a particularly well-crafted `vary.js` module for managing the Vary header with caching. Dependencies are minimal and up to date.
The LLM identified 1 files worth highlighting for reuse:
A self-contained utility module for managing HTTP Vary headers. It provides `createAddFieldnameToVary(fieldname)` which returns a function that can be called with a reply object to add a field name to the Vary header. It uses a FIFO cache (via toad-cache) to avoid reparsing the header on every request, handles edge cases (wildcards, empty headers, arrays), and validates field names. This module is reusable independently of the CORS plugin; an engineer could extract it into any Node.js HTTP project to manage Vary headers efficiently.