Skip to content

Commit 6b08993

Browse files
committed
Add website, bump version
1 parent 3ce852e commit 6b08993

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1230
-84
lines changed

README.md

Lines changed: 112 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,12 @@
33
[![Build Status](https://travis-ci.org/vardius/peer-cdn.svg?branch=master)](https://travis-ci.org/vardius/peer-cdn)
44
[![codecov](https://codecov.io/gh/vardius/peer-cdn/branch/master/graph/badge.svg)](https://codecov.io/gh/vardius/peer-cdn)
55
[![npm version](https://img.shields.io/npm/v/peer-cdn.svg)](https://www.npmjs.com/package/peer-cdn)
6+
[![npm downloads](https://img.shields.io/npm/dm/peer-cdn.svg)](https://www.npmjs.com/package/peer-cdn)
67
[![license](https://img.shields.io/github/license/vardius/peer-cdn.svg)](LICENSE.md)
78

89
Lightweight library providing peer to peer CDN functionality
910

10-
### Bundle size
11-
```bash
12-
┌───────────────────────────────────┐
13-
│ │
14-
│ Destination: dist/index.es.js │
15-
│ Bundle Size: 232.06 KB │
16-
│ Minified Size: 103.46 KB │
17-
│ Gzipped Size: 28.94 KB │
18-
│ │
19-
└───────────────────────────────────┘
20-
┌────────────────────────────────┐
21-
│ │
22-
│ Destination: dist/index.js │
23-
│ Bundle Size: 247.91 KB │
24-
│ Minified Size: 95.23 KB │
25-
│ Gzipped Size: 27.78 KB │
26-
│ │
27-
└────────────────────────────────┘
28-
```
29-
30-
# **This is work in progress!**
11+
## **This is work in progress!**
3112

3213
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)
3314

@@ -41,26 +22,123 @@ For now I know there might be some issues with:
4122
- [`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)**
4223
- [range requests](https://github.com/vardius/peer-cdn/issues/7)
4324

44-
### Next steps:
45-
- [ ] add more tests
46-
- [ ] resolve browser support
47-
- [ ] create web pack plugin
48-
- [ ] improve signalling server
25+
📖 ABOUT
26+
==================================================
27+
Contributors:
4928

50-
## Contribution
29+
* [Rafał Lorenz](https://rafallorenz.com)
5130

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

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

56-
## Installation
36+
## 📚 Documentation
5737

38+
For **documentation** (_including examples_), **visit [rafallorenz.com/peer-cdn](https://rafallorenz.com/peer-cdn)**
39+
40+
🚏 HOW TO USE
41+
==================================================
42+
43+
## Installation
5844
```bash
59-
npm install --save peer-cdn
45+
$ npm install peer-cdn
46+
```
47+
48+
## Basic example
49+
50+
### main.js
51+
52+
```js
53+
"use strict";
54+
55+
import { PeerPlugin } from "peer-cdn";
56+
57+
if ("serviceWorker" in navigator) {
58+
// since sw does not support WebRTC yet
59+
// this is workaround to use it
60+
// we use PeerPlugin on client side
61+
const peerPlugin = new PeerPlugin({
62+
cacheName: CachePlugin.peerFetch + 1,
63+
timeoutAfter: 3000,
64+
servers: {
65+
iceServers: [
66+
{
67+
url: "stun:74.125.142.127:19302",
68+
},
69+
],
70+
},
71+
constraints: {
72+
ordered: true,
73+
},
74+
});
75+
76+
// Set up a listener for messages posted from the service worker.
77+
// The service worker is set to post a message to specific client only
78+
// so you should see this message event fire once.
79+
// You can force it to fire again by visiting this page in an Incognito window.
80+
navigator.serviceWorker.addEventListener("message", function (event) {
81+
const request = new Request(event.data.url);
82+
// mock sw event wrapping request with object
83+
const middleware = peerPlugin.getMiddleware({ request });
84+
85+
// run get method of a created middleware
86+
middleware
87+
.get()
88+
.then(function (response) {
89+
// return response to a service worker
90+
event.ports[0].postMessage(response);
91+
})
92+
.catch(function (error) {
93+
// return response to a service worker
94+
event.ports[0].postMessage(null);
95+
});
96+
});
97+
98+
navigator.serviceWorker
99+
.register("sw.js")
100+
.then(function (registration) {
101+
// Registration was successful
102+
console.log(
103+
"ServiceWorker registration successful with scope: ",
104+
registration.scope
105+
);
106+
})
107+
.catch(function (error) {
108+
console.error("Service Worker Error", error);
109+
});
110+
}
60111
```
61112

62-
## [Documentation](https://github.com/vardius/peer-cdn/wiki)
113+
### sw.js
114+
115+
```js
116+
// import peer-cdn into service worker
117+
self.importScripts("https://github.com/vardius/peer-cdn/blob/v1.0.4-beta/dist/index.js");
118+
119+
const { CachePlugin, DelegatePlugin, NetworkPlugin, strategies: { ordered }} = PeerCDN;
120+
121+
const cachePlugin = new CachePlugin({ version: 1 });
122+
// since sw does not support WebRTC yet we use PeerPlugin on client side
123+
// and we delegate request to it with DelegatePlugin
124+
const delegatePlugin = new DelegatePlugin({ timeoutAfter: 5000 });
125+
const networkPlugin = new NetworkPlugin();
126+
127+
const cdn = new PeerCDN();
128+
129+
cdn.GET("/css/main.css", ordered,
130+
cachePlugin.getMiddleware,
131+
delegatePlugin.getMiddleware,
132+
networkPlugin.getMiddleware
133+
);
134+
135+
// We need to register service worker events
136+
// cdn.register() will add listeners for install, activate and fetch
137+
// gaining required control
138+
cdn.register();
139+
```
63140

64-
## License
141+
📜 [License](LICENSE.md)
142+
-------
65143

66-
The code is available under the [MIT license](LICENSE.md).
144+
This package is released under the MIT license. See the complete license in the package

dist/index.es.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,7 +1293,7 @@ var PeerCDN = /*#__PURE__*/function () {
12931293
this.router = new Router();
12941294
this.register = this.register.bind(this);
12951295
this.GET = this.GET.bind(this);
1296-
} // Register middlewares for a all methods and given route path with one of stategies
1296+
} // Register middleware for a GET method and given route path with one of strategies
12971297

12981298

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

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

13111311
}, {
@@ -9434,16 +9434,18 @@ var Network = /*#__PURE__*/function () {
94349434
}();
94359435

94369436
var middleware = new Middleware();
9437-
var STRATEGIES = {
9438-
fastest: middleware.applyFastest,
9439-
ordered: middleware.applyOrdered
9440-
};
9437+
var exportObj = {
9438+
CachePlugin: Cache,
9439+
PeerPlugin: Peer,
9440+
DelegatePlugin: Delegate,
9441+
NetworkPlugin: Network,
9442+
strategies: {
9443+
fastest: middleware.applyFastest,
9444+
ordered: middleware.applyOrdered
9445+
}
9446+
}; // Merge object for easy access
9447+
9448+
Object.assign(PeerCDN, exportObj);
94419449
self.PeerCDN = PeerCDN;
9442-
self.STRATEGIES = STRATEGIES;
9443-
self.CachePlugin = Cache;
9444-
self.PeerPlugin = Peer;
9445-
self.DelegatePlugin = Delegate;
9446-
self.NetworkPlugin = Network;
9447-
9448-
export default PeerCDN;
9449-
export { Cache as CachePlugin, Delegate as DelegatePlugin, Network as NetworkPlugin, Peer as PeerPlugin, STRATEGIES };
9450+
9451+
export { Cache as CachePlugin, Delegate as DelegatePlugin, Network as NetworkPlugin, Peer as PeerPlugin };

dist/index.es.js.gz

40 Bytes
Binary file not shown.

dist/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,7 @@
12991299
this.router = new Router();
13001300
this.register = this.register.bind(this);
13011301
this.GET = this.GET.bind(this);
1302-
} // Register middlewares for a all methods and given route path with one of stategies
1302+
} // Register middleware for a GET method and given route path with one of strategies
13031303

13041304

13051305
createClass(PeerCDN, [{
@@ -1311,7 +1311,7 @@
13111311
middleware[_key - 2] = arguments[_key];
13121312
}
13131313

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

13171317
}, {
@@ -9440,23 +9440,24 @@
94409440
}();
94419441

94429442
var middleware = new Middleware();
9443-
var STRATEGIES = {
9444-
fastest: middleware.applyFastest,
9445-
ordered: middleware.applyOrdered
9446-
};
9443+
var exportObj = {
9444+
CachePlugin: Cache,
9445+
PeerPlugin: Peer,
9446+
DelegatePlugin: Delegate,
9447+
NetworkPlugin: Network,
9448+
strategies: {
9449+
fastest: middleware.applyFastest,
9450+
ordered: middleware.applyOrdered
9451+
}
9452+
}; // Merge object for easy access
9453+
9454+
Object.assign(PeerCDN, exportObj);
94479455
self.PeerCDN = PeerCDN;
9448-
self.STRATEGIES = STRATEGIES;
9449-
self.CachePlugin = Cache;
9450-
self.PeerPlugin = Peer;
9451-
self.DelegatePlugin = Delegate;
9452-
self.NetworkPlugin = Network;
94539456

94549457
exports.CachePlugin = Cache;
94559458
exports.DelegatePlugin = Delegate;
94569459
exports.NetworkPlugin = Network;
94579460
exports.PeerPlugin = Peer;
9458-
exports.STRATEGIES = STRATEGIES;
9459-
exports.default = PeerCDN;
94609461

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

dist/index.js.gz

61 Bytes
Binary file not shown.

example/js/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
22

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

55
if ("serviceWorker" in navigator) {
6-
// since sw does not support webrtc yet
6+
// since sw does not support WebRTC yet
77
// this is workaround to use it
88
// we use PeerPlugin on client side
99
const peerPlugin = new PeerPlugin({

example/server.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const fspath = require("path");
33
const cookieParser = require("cookie-parser");
44
const http = require("http");
55
const fs = require("fs");
6-
// const PeerCdnServer = require("peer-cdn/src/server");
7-
const PeerCdnServer = require("../src/server");
6+
const PeerCdnServer = require("../src/server"); // require("peer-cdn/src/server")
87

98
const PeerEventType = { PEER: "PEER" };
109
const port = process.env.PORT || 3000;

example/sw.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33

44
// import peer-cdn into service worker
55
// this path is exposed with server
6-
self.importScripts("/peer-cdn/index.js");
6+
self.importScripts("/peer-cdn/index.js"); // self.importScripts("https://github.com/vardius/peer-cdn/blob/v1.0.4-beta/dist/index.js");
7+
8+
const { CachePlugin, DelegatePlugin, NetworkPlugin, strategies: { ordered }} = PeerCDN;
79

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

1416
function run() {
1517
const cdn = new PeerCDN();
16-
cdn.GET("/css/main.css", STRATEGIES.ordered,
18+
cdn.GET("/css/main.css", ordered,
1719
cachePlugin.getMiddleware,
1820
delegatePlugin.getMiddleware,
1921
networkPlugin.getMiddleware

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "peer-cdn",
3-
"version": "1.0.3-beta",
3+
"version": "1.0.4-beta",
44
"description": "Lightweight library providing peer to peer CDN functionality",
55
"main": "dist/index.js",
66
"module": "dist/index.es.js",

src/PeerCDN.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ export default class PeerCDN {
1212
this.GET = this.GET.bind(this);
1313
}
1414

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

2020
// Register handlers for given service worker instance
2121
register() {
22-
[getInstall()].forEach(h => self.addEventListener("install", h));
23-
[getActivate()].forEach(h => self.addEventListener("activate", h));
22+
[getInstall()].forEach((h) => self.addEventListener("install", h));
23+
[getActivate()].forEach((h) => self.addEventListener("activate", h));
2424
// Register fetch events from array.
2525
// When an event occurs, they're invoked one at a time, in the order that they're registered.
2626
// As soon as one handler calls event.respondWith(), none of the other registered handlers will be run.
27-
[getFetch(this.router)].forEach(h => self.addEventListener("fetch", h));
27+
[getFetch(this.router)].forEach((h) => self.addEventListener("fetch", h));
2828
}
2929
}

0 commit comments

Comments
 (0)