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

swopr

v0.0.1

Published

Service Worker based proxy server

Downloads

2

Readme

swopr

swopr is a really tiny proxy server which utilizes a service worker. A service worker sits between web applications and the network (when available).

Service workers are able to intercept requests and take an action, like serving a custom response.

This enables you to use swopr as a proxy server inside your browser or as a mock server to test a REST API without having to run a local backend.

Installation

Run npm install swopr --save-dev

Usage

The Service Worker file needed by swopr needs to be in the root folder of your application, so you need to copy node_modules/swopr/swopr.js to the root folder.

Then simply add swopr to your web app using a script tag:

<script src="node_modules/swopr/index.js></script>

Then list the requests you want to intercept in an array of objects and save it to swopr-responses.js in the root folder of your application.

For example:

const responses = [
    {
      url: 'http://api.example.com/json',
      headers: {
       'Content-Type': 'application/json'
      },
      body: {
       message: 'a json response'
      },
      status: 200,
      statusText: 'OK'
    },
    {
      ...
    }
]
  • url: String (required), fully qualified URL of the request to be proxied
  • method: String (required), HTTP method (GET, POST, PUT or DELETE)
  • headers: Object (optional), key/value pairs of HTTP headers
  • body: Object/String/Function (optional): response body, when a function is given it will receive as argument an object containing any request parameters
  • file: String (optional), path to file to be served, relative to root folder of the application. Must be accessible to the web server and will be ignored if body is present
  • status: Number (optional), HTTP status code
  • statusText: String (optional), HTTP status text

The file swopr-responses.example.js contains an example for each option.

Whenever a change is made to the responses, the page must be reloaded.

Since this will not reload the Service Worker, swopr will add a 'beforeunload' event listener to the window to re-register the Service Worker when the page is reloaded. This ensures that swopr will always use the correct responses whenever these are changed, without having to change the Service Worker file itself.

Running the demo

Run npm install.

Rename swopr-responses.example.js to swopr-responses.js.

Run npm start.

The demo can now be viewed on http://localhost:8080/demo.

Click the buttons on the demo page to generate the requests and view the results in the dev tools of your browser of choice.

To view the console logs in Safari you need to open a separate Web Inspector window via Develop > Service Workers > localhost.

Browser support

Tested in:

  • Chrome 67+
  • Safari 11.1+
  • Firefox 60+
  • Edge 17+

swopr should run in all browsers that support Service Worker.

Inspecting and debugging the service worker

For a great explanation of how to inspect and debug service workers in Chrome, Firefox, Safari and Edge check Tools for PWA developers.