npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fluvial/cors

v0.5.0

Published

A fluvial-compatible Cross-Origin Resource Sharing middleware

Downloads

11

Readme

@fluvial/cors

Middleware for Cross-Origin-Resource-Sharing (CORS) headers. CORS explains to the browser what is and is not allowed in HTTP requests. This includes loading assets (such as JavaScript, CSS, and images) and API calls. These CORS policies are often provided in response to preflight requests the browser sends prior to

CORS-related headers include the following:

  • Access-Control-Allow-Origin, which governs what origins the server will accept
  • Access-Control-Allow-Credentials, which allows or denies JavaScript to access the credentials-related parts of the response
  • Access-Control-Allow-Methods, which specifies what HTTP methods are permitted (e.g., GET, POST, etc.)
  • Access-Control-Allow-Headers, which specifies what headers are permitted
  • Access-Control-Max-Age, which tells the browser how long the results of preflight requests are cached
  • Access-Control-Expose-Headers, which tells the browser what headers are permitted to be accessed by JavaScript

More documentation about CORS can be found on MDN:

  • CORS guide: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
  • CORS glossary and headers: https://developer.mozilla.org/en-US/docs/Glossary/CORS

Basic usage

// in a router or application file
import { cors } from '@fluvial/cors';

app.use(cors());

API documentation

cors(options?: CorsOptions)

Allows you to configure the way the CORS middleware behaves and what it includes in its headers.

Arguments:

  • options (optional) - CorsOptions, which includes the following properties:
    • continuePreflight (optional) - boolean, which indicates if you want the CORS middleware to not short-circuit in the case of a preflight request; default is false
    • continueOnFailure (optional) - boolean, which indicates if requests that fail the CORS checks will continue past the CORS middleware; default is false
    • preflightStatusCode (optional) - number, which is the status code that returned preflight requests should have; default is 204
    • failureStatusCode (optional) - number, which is the status code that failed requests are given; default is 405 for invalid/disallowed methods and 406 for anything else
    • allowedOrigins (optional) - string[], which contains all of the origins that are allowed; default is [ '*' ]
    • credentialsAllowed (optional) - boolean, which controls the Access-Control-Allow-Credentials header; default is true
    • allowedMethods (optional) - string[] containing the methods that are allowed; default includes GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS
    • allowedHeaders (optional) - string[] specifying the header names allowed; defaults to the headers in the current request
    • maxAge (optional) - number representing the number of seconds that the CORS options are cached in the request
    • exposedHeaders (optional) - string[], which contains the headers that are allowed to be exposed to the code running on the page; defaults to all headers allowed

Returns a middleware function that can be used in an Application or Router.

Contributing

See something you want this middleware to do? Find a bug? Feel free to open an issue or a PR with the fix or feature you want to add.