# swproxy

> swproxy is a library to write service workers with ease.

Latest version **0.2.3** (published 2015-12-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install swproxy
pnpm add swproxy
yarn add swproxy
bun add swproxy
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.3 |
| Published | 2015-12-05 |
| First published | 2015-11-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Author | Alexander Bartels |
| Maintainers | bartels |
| Keywords | swproxy, serviceworker, proxy |

## Links

- npm: https://www.npmjs.com/package/swproxy
- Repository: https://github.com/alexanderbartels/swproxy
- Homepage: http://www.alexanderbartels.com/swproxy-www
- Issues: https://github.com/alexanderbartels/swproxy/issues
- npm.io page: https://npm.io/package/swproxy

## Alternatives

- [cron](https://npm.io/package/cron.md) — 4.9M weekly downloads
- [@vercel/queue](https://npm.io/package/@vercel/queue.md) — 731.6K weekly downloads
- [create-sonicjs](https://npm.io/package/create-sonicjs.md) — 1.6K weekly downloads
- [@exellix/jobs-api](https://npm.io/package/@exellix/jobs-api.md) — 941 weekly downloads
- [@forwardimpact/libskill](https://npm.io/package/@forwardimpact/libskill.md) — 575 weekly downloads

## Recent versions

- 0.2.3 (latest) — 2015-12-05
- 0.0.0-beta.9 (master) — 2015-11-26
- 0.2.2 — 2015-12-05
- 0.1.1 — 2015-11-29
- 0.1.0 — 2015-11-28
- 0.0.9 — 2015-11-27
- 0.0.8 — 2015-11-27
- 0.0.7 — 2015-11-27
- 0.0.6 — 2015-11-27
- 0.0.5 — 2015-11-27
- 0.0.4 — 2015-11-26
- 0.0.3 — 2015-11-26
- 0.0.2 — 2015-11-26
- 0.0.1 — 2015-11-26
- 0.0.0-beta.8 — 2015-11-25
- … 7 more at https://npm.io/package/swproxy/versions

## README

# swproxy 
[![NPM version][npm-image]][npm-url] [![wercker status](https://app.wercker.com/status/60a2d039560adbf52b7b1467b49fdc7a/m/master "wercker status")](https://app.wercker.com/project/bykey/60a2d039560adbf52b7b1467b49fdc7a)
[![Dependency Status][daviddm-image]][daviddm-url]

# currently a work in progress

with swproxy you can develop a service worker in a declarative way.


## Usage

install swproxy via npm:
```shell
npm install --save swproxy
```

load swproxy as dependency in your serviceworker.js file:
```javascript
importScripts('./js/swproxy.js'); // swproxy.js is located under node_modules/swproxy/dist/swproxy.js
```

To define rules for the swproxy, you need to load some swproxy-mods.
You can search for them on [npm](https://www.npmjs.com/search?q=swproxy) or via the [swproxy website](http://www.alexanderbartels.com/swproxy-www)
Mods have to be loaded like the swproxy itself:

```shell
// install a swproxy-mod via npm. e.g. swproxy-mod-rewrite
npm install --save swproxy-mod-rewrite
```

```javascript
// in your serviceworker.js
importScripts('./js/swproxy-mod-rewrite.js'); // swproxy-mod-rewrite.js is located under node_modules/swproxy-mod-rewrite/dist/swproxy-mod-rewrite.js
```


Based on the mods you're loading in your ServiceWorker, new methods are populated to the swproxy (the factory method).
```javascript
// in your serviceworker.js file ...

// load the swproxy stuff
importScripts('./js/swproxy.js');
importScripts('./js/swproxy-mod-rewrite.js'); 

// initiate the swproxy, you need to pass 'self' as the constructor argument, 
// so the proxy can add event listeners to the service worker.
var proxy = new SwProxy(self);

// next you need to register the Mods, you want to use (Make sure you have loaded them via 'importScripts(...)'
proxy.registerMod(ModRewrite);
 
// after registering a mod, new methods on your swproxy instance are available. e.g. 'rewriteRule(...)'
proxy.rewriteRule(new RegExp('.*/swproxy/mods$', ''), '/some/other/path', {});
```

### Modifier
Each rule should accept an Modifier Object as last argument.
Currently available global modifier:
```
 * {boolean} stopPropagation => following defined rules won't be executed
 * {boolean} executeFetch => (hidden modifier, should set by mods) only if the event is from type 'event'. if false, no fetch request will executed at the end. 
                              This must done manually by the the mod who sets this flag to false. 
                              This mod should resolve the promise with a Response Object, not like normal with an event object
```

# writing your own swproxy mod

To make it easy to get started. You an use the [yeoman generator-swproxy-mod](https://www.npmjs.com/package/generator-swproxy-mod).

Your swproxy-mod must have the `swproxy-mod` keyword and a description in `package.json` to be listed on the swproxy website.
The generator will create two base Classes for you:
  * Mod< YOUR MOD NAME>  => e.g. ModRewrite 
  * Mod< YOUR MOD NAME>Rule => e.g. ModRewriteRule

### Mod

The Mod must implement two static methods. The `factoryMethodName` and `factoryMethod` functions.
Example (ModRewrite):
```javascript
// in your swproxy-mod-rewrite.js file

// [...]

/**
 * es6 class definition for the swproxy mod
 */
class ModRewrite {

  /**
   * @return {String} the name from the factory function, that will be populated at the swproxy isntance.
   */
   static factoryMethodName() {
      return 'rewriteRule';
   }
   
   /**
    * @return {function} factory function to create rules
    */
   static factoryMethod(swproxy) {
      return (srcUrl, destUrl, modifiers) {
         // create a new instance from your rule
         let rule = new RewriteRule(srcUrl, destUrl, modifiers);
         
         // add the rule, based on the rule type (fetch, install, activate) to the swproxy instance
         swproxy.addFetchRule(rule);
      }
   }
}

export default ModRewrite;
```

To use your Mod, it should be registered at the swproxy
Example:
```javascript
// in your service worker js file

// importScripts [...]

// initiate the swproxy
var swproxy = new SwProxy(self);

// register your mod
swproxy.registerMod(ModRewrite);

// after registering the mod, 
// the factory function with name from the `factoryMethodName` function can be used to define rules
swproxy.rewriteRule('param1', 'param2', 'param3');
```

### Rule
```javascript
// in your swproxy-mod-rewrite.js file
class RewriteRule {
   match: (event) => true,
   execute: (originalEvent, event) {
    return new Promise((resolve, reject) => {
       // execute the rule in this promise
       
       // originalEvent can't be modified and is the event that was originally fired by the service worker
        
       // event should be modified by the rules,
       // the modified event will be passed to the next rule
       // it will also used, to make the final fetch event.
        
       // modify header, url, etc. on the event object and pass it to the resolve function
        
       // if the reject function will be called, the event/request will be canceled with an Error 
       resolve(event);
    });
   }
}

// Mod Rewrite definition [...]

export {
   ModRewrite as default,
   RewriteRule
};
```

## License

MIT © [Alexander Bartels](http://www.alexanderbartels.com)

[npm-image]: https://badge.fury.io/js/swproxy.svg
[npm-url]: https://npmjs.org/package/swproxy
[daviddm-image]: https://david-dm.org/alexanderbartels/swproxy.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/alexanderbartels/swproxy

---
_Source: https://npm.io/package/swproxy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
