Skip to content

Commit

Permalink
Add website, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
vardius committed Jun 27, 2020
1 parent 3ce852e commit 6b08993
Show file tree
Hide file tree
Showing 60 changed files with 1,230 additions and 84 deletions.
146 changes: 112 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,12 @@
[![Build Status](https://travis-ci.org/vardius/peer-cdn.svg?branch=master)](https://travis-ci.org/vardius/peer-cdn)
[![codecov](https://codecov.io/gh/vardius/peer-cdn/branch/master/graph/badge.svg)](https://codecov.io/gh/vardius/peer-cdn)
[![npm version](https://img.shields.io/npm/v/peer-cdn.svg)](https://www.npmjs.com/package/peer-cdn)
[![npm downloads](https://img.shields.io/npm/dm/peer-cdn.svg)](https://www.npmjs.com/package/peer-cdn)
[![license](https://img.shields.io/github/license/vardius/peer-cdn.svg)](LICENSE.md)

Lightweight library providing peer to peer CDN functionality

### Bundle size
```bash
┌───────────────────────────────────┐
│ │
│ Destination: dist/index.es.js │
│ Bundle Size: 232.06 KB │
│ Minified Size: 103.46 KB │
│ Gzipped Size: 28.94 KB │
│ │
└───────────────────────────────────┘
┌────────────────────────────────┐
│ │
│ Destination: dist/index.js │
│ Bundle Size: 247.91 KB │
│ Minified Size: 95.23 KB │
│ Gzipped Size: 27.78 KB │
│ │
└────────────────────────────────┘
```

# **This is work in progress!**
## **This is work in progress!**

You can speed up the process of development. Check [help wanted](https://github.com/vardius/peer-cdn/issues?q=is%3Aissue+is%3Aopen+label%3A%22help+wanted%22) issues and [contribute](https://github.com/vardius/peer-cdn/blob/master/CONTRIBUTING.md#development)

Expand All @@ -41,26 +22,123 @@ For now I know there might be some issues with:
- [`client.postMessage()`](https://developer.mozilla.org/en-US/docs/Web/API/Client/postMessage#Browser_compatibility) problems on **Google Chrome Version 64.0.3282.167 (Official Build) (64-bit)** however works on **Mozilla Firefox Quantum 58.0.2 (64-bit)**
- [range requests](https://github.com/vardius/peer-cdn/issues/7)

### Next steps:
- [ ] add more tests
- [ ] resolve browser support
- [ ] create web pack plugin
- [ ] improve signalling server
📖 ABOUT
==================================================
Contributors:

## Contribution
* [Rafał Lorenz](https://rafallorenz.com)

Is *peer-cdn* library missing something ?
Want to contribute ? Feel free to send pull requests!

No problem! Simply [fork](https://github.com/vardius/peer-cdn/network#fork-destination-box) this repository and create pull request.
Have problems, bugs, feature ideas?
We are using the github [issue tracker](https://github.com/vardius/peer-cdn/issues) to manage them.

## Installation
## 📚 Documentation

For **documentation** (_including examples_), **visit [rafallorenz.com/peer-cdn](https://rafallorenz.com/peer-cdn)**

🚏 HOW TO USE
==================================================

## Installation
```bash
npm install --save peer-cdn
$ npm install peer-cdn
```

## Basic example

### main.js

```js
"use strict";

import { PeerPlugin } from "peer-cdn";

if ("serviceWorker" in navigator) {
// since sw does not support WebRTC yet
// this is workaround to use it
// we use PeerPlugin on client side
const peerPlugin = new PeerPlugin({
cacheName: CachePlugin.peerFetch + 1,
timeoutAfter: 3000,
servers: {
iceServers: [
{
url: "stun:74.125.142.127:19302",
},
],
},
constraints: {
ordered: true,
},
});

// Set up a listener for messages posted from the service worker.
// The service worker is set to post a message to specific client only
// so you should see this message event fire once.
// You can force it to fire again by visiting this page in an Incognito window.
navigator.serviceWorker.addEventListener("message", function (event) {
const request = new Request(event.data.url);
// mock sw event wrapping request with object
const middleware = peerPlugin.getMiddleware({ request });

// run get method of a created middleware
middleware
.get()
.then(function (response) {
// return response to a service worker
event.ports[0].postMessage(response);
})
.catch(function (error) {
// return response to a service worker
event.ports[0].postMessage(null);
});
});

navigator.serviceWorker
.register("sw.js")
.then(function (registration) {
// Registration was successful
console.log(
"ServiceWorker registration successful with scope: ",
registration.scope
);
})
.catch(function (error) {
console.error("Service Worker Error", error);
});
}
```

## [Documentation](https://github.com/vardius/peer-cdn/wiki)
### sw.js

```js
// import peer-cdn into service worker
self.importScripts("https://github.com/vardius/peer-cdn/blob/v1.0.4-beta/dist/index.js");

const { CachePlugin, DelegatePlugin, NetworkPlugin, strategies: { ordered }} = PeerCDN;

const cachePlugin = new CachePlugin({ version: 1 });
// since sw does not support WebRTC yet we use PeerPlugin on client side
// and we delegate request to it with DelegatePlugin
const delegatePlugin = new DelegatePlugin({ timeoutAfter: 5000 });
const networkPlugin = new NetworkPlugin();

const cdn = new PeerCDN();

cdn.GET("/css/main.css", ordered,
cachePlugin.getMiddleware,
delegatePlugin.getMiddleware,
networkPlugin.getMiddleware
);

// We need to register service worker events
// cdn.register() will add listeners for install, activate and fetch
// gaining required control
cdn.register();
```

## License
📜 [License](LICENSE.md)
-------

The code is available under the [MIT license](LICENSE.md).
This package is released under the MIT license. See the complete license in the package
30 changes: 16 additions & 14 deletions dist/index.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,7 @@ var PeerCDN = /*#__PURE__*/function () {
this.router = new Router();
this.register = this.register.bind(this);
this.GET = this.GET.bind(this);
} // Register middlewares for a all methods and given route path with one of stategies
} // Register middleware for a GET method and given route path with one of strategies


createClass(PeerCDN, [{
Expand All @@ -1305,7 +1305,7 @@ var PeerCDN = /*#__PURE__*/function () {
middleware[_key - 2] = arguments[_key];
}

(_this$router = this.router).use.apply(_this$router, ['GET', path, strategy].concat(middleware));
(_this$router = this.router).use.apply(_this$router, ["GET", path, strategy].concat(middleware));
} // Register handlers for given service worker instance

}, {
Expand Down Expand Up @@ -9434,16 +9434,18 @@ var Network = /*#__PURE__*/function () {
}();

var middleware = new Middleware();
var STRATEGIES = {
fastest: middleware.applyFastest,
ordered: middleware.applyOrdered
};
var exportObj = {
CachePlugin: Cache,
PeerPlugin: Peer,
DelegatePlugin: Delegate,
NetworkPlugin: Network,
strategies: {
fastest: middleware.applyFastest,
ordered: middleware.applyOrdered
}
}; // Merge object for easy access

Object.assign(PeerCDN, exportObj);
self.PeerCDN = PeerCDN;
self.STRATEGIES = STRATEGIES;
self.CachePlugin = Cache;
self.PeerPlugin = Peer;
self.DelegatePlugin = Delegate;
self.NetworkPlugin = Network;

export default PeerCDN;
export { Cache as CachePlugin, Delegate as DelegatePlugin, Network as NetworkPlugin, Peer as PeerPlugin, STRATEGIES };

export { Cache as CachePlugin, Delegate as DelegatePlugin, Network as NetworkPlugin, Peer as PeerPlugin };
Binary file modified dist/index.es.js.gz
Binary file not shown.
27 changes: 14 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@
this.router = new Router();
this.register = this.register.bind(this);
this.GET = this.GET.bind(this);
} // Register middlewares for a all methods and given route path with one of stategies
} // Register middleware for a GET method and given route path with one of strategies


createClass(PeerCDN, [{
Expand All @@ -1311,7 +1311,7 @@
middleware[_key - 2] = arguments[_key];
}

(_this$router = this.router).use.apply(_this$router, ['GET', path, strategy].concat(middleware));
(_this$router = this.router).use.apply(_this$router, ["GET", path, strategy].concat(middleware));
} // Register handlers for given service worker instance

}, {
Expand Down Expand Up @@ -9440,23 +9440,24 @@
}();

var middleware = new Middleware();
var STRATEGIES = {
fastest: middleware.applyFastest,
ordered: middleware.applyOrdered
};
var exportObj = {
CachePlugin: Cache,
PeerPlugin: Peer,
DelegatePlugin: Delegate,
NetworkPlugin: Network,
strategies: {
fastest: middleware.applyFastest,
ordered: middleware.applyOrdered
}
}; // Merge object for easy access

Object.assign(PeerCDN, exportObj);
self.PeerCDN = PeerCDN;
self.STRATEGIES = STRATEGIES;
self.CachePlugin = Cache;
self.PeerPlugin = Peer;
self.DelegatePlugin = Delegate;
self.NetworkPlugin = Network;

exports.CachePlugin = Cache;
exports.DelegatePlugin = Delegate;
exports.NetworkPlugin = Network;
exports.PeerPlugin = Peer;
exports.STRATEGIES = STRATEGIES;
exports.default = PeerCDN;

Object.defineProperty(exports, '__esModule', { value: true });

Expand Down
Binary file modified dist/index.js.gz
Binary file not shown.
4 changes: 2 additions & 2 deletions example/js/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";

import { PeerPlugin } from "/peer-cdn/index.es.js";
import { PeerPlugin, CachePlugin } from "/peer-cdn/index.es.js"; // from "peer-cdn";

if ("serviceWorker" in navigator) {
// since sw does not support webrtc yet
// since sw does not support WebRTC yet
// this is workaround to use it
// we use PeerPlugin on client side
const peerPlugin = new PeerPlugin({
Expand Down
3 changes: 1 addition & 2 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const fspath = require("path");
const cookieParser = require("cookie-parser");
const http = require("http");
const fs = require("fs");
// const PeerCdnServer = require("peer-cdn/src/server");
const PeerCdnServer = require("../src/server");
const PeerCdnServer = require("../src/server"); // require("peer-cdn/src/server")

const PeerEventType = { PEER: "PEER" };
const port = process.env.PORT || 3000;
Expand Down
8 changes: 5 additions & 3 deletions example/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

// import peer-cdn into service worker
// this path is exposed with server
self.importScripts("/peer-cdn/index.js");
self.importScripts("/peer-cdn/index.js"); // self.importScripts("https://github.com/vardius/peer-cdn/blob/v1.0.4-beta/dist/index.js");

const { CachePlugin, DelegatePlugin, NetworkPlugin, strategies: { ordered }} = PeerCDN;

const cachePlugin = new CachePlugin({ version: 1 });
// since sw does not support webrtc yet we use PeerPlugin on client side
// since sw does not support WebRTC yet we use PeerPlugin on client side
// and we delegate request to it with DelegatePlugin
const delegatePlugin = new DelegatePlugin({ timeoutAfter: 5000 });
const networkPlugin = new NetworkPlugin();

function run() {
const cdn = new PeerCDN();
cdn.GET("/css/main.css", STRATEGIES.ordered,
cdn.GET("/css/main.css", ordered,
cachePlugin.getMiddleware,
delegatePlugin.getMiddleware,
networkPlugin.getMiddleware
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "peer-cdn",
"version": "1.0.3-beta",
"version": "1.0.4-beta",
"description": "Lightweight library providing peer to peer CDN functionality",
"main": "dist/index.js",
"module": "dist/index.es.js",
Expand Down
10 changes: 5 additions & 5 deletions src/PeerCDN.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ export default class PeerCDN {
this.GET = this.GET.bind(this);
}

// Register middlewares for a all methods and given route path with one of stategies
// Register middleware for a GET method and given route path with one of strategies
GET(path, strategy, ...middleware) {
this.router.use('GET', path, strategy, ...middleware)
this.router.use("GET", path, strategy, ...middleware);
}

// Register handlers for given service worker instance
register() {
[getInstall()].forEach(h => self.addEventListener("install", h));
[getActivate()].forEach(h => self.addEventListener("activate", h));
[getInstall()].forEach((h) => self.addEventListener("install", h));
[getActivate()].forEach((h) => self.addEventListener("activate", h));
// Register fetch events from array.
// When an event occurs, they're invoked one at a time, in the order that they're registered.
// As soon as one handler calls event.respondWith(), none of the other registered handlers will be run.
[getFetch(this.router)].forEach(h => self.addEventListener("fetch", h));
[getFetch(this.router)].forEach((h) => self.addEventListener("fetch", h));
}
}
22 changes: 12 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ import Middleware from "./router/Middleware";
import { Cache, Peer, Network, Delegate } from "./plugins";

const middleware = new Middleware();
const STRATEGIES = {
fastest: middleware.applyFastest,
ordered: middleware.applyOrdered,
const exportObj = {
CachePlugin: Cache,
PeerPlugin: Peer,
DelegatePlugin: Delegate,
NetworkPlugin: Network,
strategies: {
fastest: middleware.applyFastest,
ordered: middleware.applyOrdered,
},
};

// Merge object for easy access
Object.assign(PeerCDN, exportObj);

self.PeerCDN = PeerCDN;
self.STRATEGIES = STRATEGIES;
self.CachePlugin = Cache;
self.PeerPlugin = Peer;
self.DelegatePlugin = Delegate;
self.NetworkPlugin = Network;

export default PeerCDN;
export {
STRATEGIES,
Cache as CachePlugin,
Peer as PeerPlugin,
Delegate as DelegatePlugin,
Expand Down
9 changes: 9 additions & 0 deletions website/docs/cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
id: cache
title: Cache Plugin
sidebar_label: Cache Plugin
---

## Source code

Please see source: [Cache.js](https://github.com/vardius/peer-cdn/blob/master/src/plugins/Cache.js)
Loading

0 comments on commit 6b08993

Please sign in to comment.