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

@goa/cors

v1.2.1

Published

Cross-Origin Resource Sharing (CORS) For Goa.

Downloads

16

Readme

@goa/cors

npm version

@goa/cors is Cross-Origin Resource Sharing (CORS) For Goa.

yarn add @goa/cors

Table Of Contents

API

The package is available by importing its default function:

import cors from '@goa/cors'

cors(  config=: !CorsConfig,): !Middleware

Cross-Origin Resource Sharing (CORS) For Goa.

  • config !CorsConfig (optional): The config.

CorsConfig: Options for the program.

There are 3 main use cases:

1. Accept any origin form the client

import Goa from '@goa/koa'
import { aqt } from 'rqt'
import cors from '@goa/cors'

const goa = new Goa()
goa.use(cors())

goa.listen(async function() {
  const { port } = this.address()
  const url = `http://localhost:${port}`

  // 1. accept origin from the host
  const { headers } = await aqt(url, {
    headers: {
      origin: 'www.example.com',
    },
  })
  console.log(headers)
  this.close()
})
{ vary: 'Origin',
  'access-control-allow-origin': 'www.example.com',
  'content-type': 'text/plain; charset=utf-8',
  'content-length': '9',
  date: 'Thu, 09 Jan 2020 14:43:05 GMT',
  connection: 'close' }

2. Send out only specific origin

import Goa from '@goa/koa'
import { aqt } from 'rqt'
import cors from '@goa/cors'

const goa = new Goa()
goa.use(cors({
  origin: 'www.hello-world.com',
}))

goa.listen(async function() {
  const { port } = this.address()
  const url = `http://localhost:${port}`

  // 2. only serve specific origin
  const { headers } = await aqt(url, {
    headers: {
      origin: 'www.example.com',
    },
  })
  console.log(headers)
  this.close()
})
{ vary: 'Origin',
  'access-control-allow-origin': 'www.hello-world.com',
  'content-type': 'text/plain; charset=utf-8',
  'content-length': '9',
  date: 'Thu, 09 Jan 2020 14:43:05 GMT',
  connection: 'close' }

3. Pre-flight Requests Via OPTIONS (both above apply)

import Goa from '@goa/koa'
import { aqt } from 'rqt'
import cors from '@goa/cors'

const goa = new Goa()
goa.use(cors({
  origin: 'www.hello-world.com',
  credentials: true,
  maxAge: 1000,
  allowMethods: ['POST', 'PUT'],
}))

goa.listen(async function() {
  const { port } = this.address()
  const url = `http://localhost:${port}`

  // 3. respond to pre-flight request
  const { statusCode, headers } = await aqt(url, {
    method: 'OPTIONS',
    headers: {
      'Access-Control-Request-Method': 'POST',
      origin: 'www.example.com',
    },
  })
  console.log(statusCode, headers)
  this.close()
})
204 { vary: 'Origin',
  'access-control-allow-origin': 'www.hello-world.com',
  'access-control-allow-credentials': 'true',
  'access-control-max-age': '1000',
  'access-control-allow-methods': 'POST,PUT',
  date: 'Thu, 09 Jan 2020 14:43:05 GMT',
  connection: 'close' }

Usage Events

This middleware integrates with Idio that collects middleware usage statistics to reward package maintainers. It will emit certain events to bill its usage:

  1. headers: When setting the headers if origin was present.
  2. options: When responding to pre-flight requests via the OPTIONS http method.

The usage is recorded via the ctx.neoluddite context property set by a server such as Idio. In future, more fine-grained usage events might appear.

Copyright & License

GNU Affero General Public License v3.0

Affero GPL means that you're not allowed to use this middleware on the web unless you release the source code for your application. This is a restrictive license which has the purpose of defending Open Source work and its creators.

Please refer to the Idio license agreement for more info on dual-licensing. You're allowed to use this middleware without disclosing the source code if you sign up on neoluddite.dev package reward scheme.

Original Work by dead-horse & contributors licensed under MIT found in COPYING.