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

fetch-robot

v1.0.5

Published

Javascript module template.

Downloads

18

Readme

Fetch Robot

Run fetch() through an iframe proxy to avoid dealing with CORS.

Uses post-robot for frame-to-frame communication.

Why?

  • CORS requires server-side changes on each endpoint
  • For non-GET requests, preflight requests must be sent to verify the origin, which affects site-speed
  • Support is limited in older browsers
  • CORS headers only allow a certain degree of granularity

Example

Let's say:

  • I'm on https://www.rorschach.com
  • I want to make a request to https://www.niteowl.com

In the parent window rorschach.com:

<script src="https://rawgit.com/krakenjs/fetch-robot/master/dist/fetch-robot.min.js"></script>

<script>
    // Create a proxy instance and open the iframe

    let proxy = fetchRobot.connect({ url: 'https://www.niteowl.com/fetch-robot-proxy' });

    // Use `proxy.fetch` in the same way as `fetch`

    proxy.fetch('https://www.niteowl.com/api/foo', { method: 'POST' })
        .then(response => response.text())
        .then(console.log);
</script>

In the child window niteowl.com/fetch-robot-proxy:

<!-- Add a fetch polyfill for older browsers -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.3/fetch.min.js"></script>
<script src="https://rawgit.com/krakenjs/fetch-robot/master/dist/fetch-robot.min.js"></script>

<script>
    // Enable requests to be passed through the current frame using fetchRobot

    fetchRobot.serve({

        allow: [
            {
                path: [
                    '/api/foo',
                    '/api/bar'
                ],

                headers: [
                    'x-csrf'
                ]
            },

            {
                origin: 'https://some-domain.com',

                path: [
                    '/api/baz',
                ],

                headers: [
                    'x-custom-header'
                ],

                credentials: 'include'
            }
        ]
    });
</script>

Rules

Pass one or more rules in allow.

  • If any of the rules pass for a given request, the request will be allowed through.
  • If no rules pass, the request will error out

Each option in a rule can be one of:

  • string (e.g. origin: 'https://foo.com')
  • array (e.g. origin: [ 'https://foo.com', 'https://bar.com' ])
  • regex (e.g. origin: new RegExp('https://(foo|bar)\.com'))
  • wildcard (e.g. origin: '*')

Available options

  • origin - The domain(s) from which the request can be sent
    • default: '*'
  • domain - The domain(s) to which the request can be sent
    • default: domain where fetchRobot.serve() was called
  • path - The path(s) to which requests can be sent
    • default: []
  • query - The query param(s) which can be sent with the request
    • default: '*'
  • credentials - The credential level which can be sent with requests
    • default: [ 'omit' ]
  • method - The method(s) with which requests can be sent
    • default: [ 'get', 'head', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'patch' ]
  • headers - The headers(s) which can be sent with the request
    • default: [ 'accept', 'accept-language', 'content-language', 'content-type' ]
  • responseHeaders - The header(s) which can be sent with the response
    • default: [ 'cache-control', 'content-language', 'content-type', 'expires', 'last-modified', 'pragma' ]

Quick Start

Getting Started

  • Run setup: ./setup.sh
  • Start editing code in ./src and writing tests in ./tests
  • gulp build

Building

npm run build

Tests

  • Edit tests in ./test/tests

  • Run the tests:

    gulp test

Testing with different/multiple browsers

npm run karma -- --browser=PhantomJS
npm run karma -- --browser=Chrome
npm run karma -- --browser=Safari
npm run karma -- --browser=Firefox
npm run karma -- --browser=PhantomJS,Chrome,Safari,Firefox

Keeping the browser open after tests

npm run karma -- --browser=Chrome --keep-browser-open

Publishing

  • Publish your code: ./publish.sh