2.1.0 • Published 5 years ago

cordial v2.1.0

Weekly downloads
4
License
GPL-3.0
Repository
github
Last release
5 years ago

Cordial

npm version npm downloads

Minimal Cross-Origin Resource Sharing (CORS) for Google Cloud Functions.

const cordial = require('cordial')

const config = {
  origins: ['https://qkly.app', 'http://localhost:3034'],
  headers: ['Content-Type', 'Content-Length'],
  methods: ['GET', 'POST']
}

exports.cloudFunction = (req, res) => {
  // Handle CORS
  if (cordial(req, res, config)) {
    return
  }

  // Continue Normal Request...
}

If the cordial function returns true you should probably terminate the cloud function, as cordial has ended the res.

The config is entirely optional, as cordial comes with practical defaults which will handle CORS on your behalf.

Installation

Node.js 8 or higher is required.

$ npm install cordial

# Or, via yarn
$ yarn add cordial

Config

origins

(Optional) Array of URLs which are allowed to access the Cloud Function.

  • Defaults to the * wildcard origin

headers

(Optional) Array of headers which must be present during the request to the Cloud Function.

  • Defaults to the CORS preflight request's Access-Control-Request-Headers

methods

(Optional) Array of HTTP methods which are allowed to access the Cloud Function.

  • Defaults to the CORS preflight request's Access-Control-Request-Method

age

(Optional) How long (in seconds) the results of the preflight request can be cached.

  • Ignored by default, must be supplied by the user's config

expose

(Optional) Array of whitelisted server headers that browsers are allowed to access.

  • Ignored by default, must be supplied by the user's config

credentials

(Optional) Indicates whether or not the actual request can be made using credentials.

  • Ignored by default, must be supplied by the user's config

Development

# Clone the repo
$ git clone https://github.com/mitchmalig/cordial.git
$ cd cordial

# Run the unit tests
$ yarn test

# Format the source code
$ yarn lint

Todo

  • Config Validation
  • Regex Origins & Headers
  • CORS Specification Compliance

License

GNU General Public License v3.0

Copyright (c) 2018, Mitchell Maligeorges