Coder Social home page Coder Social logo

vardius / peer-cdn Goto Github PK

View Code? Open in Web Editor NEW
67.0 5.0 12.0 31.15 MB

Lightweight library providing peer to peer CDN functionality

Home Page: https://rafallorenz.com/peer-cdn

License: MIT License

HTML 1.70% CSS 2.06% JavaScript 96.24%
peer peer-cdn cdn cache

peer-cdn's Introduction

peer-cdn

Build Status codecov npm version npm downloads license

logo

Lightweight library providing peer to peer CDN functionality

πŸ“– ABOUT

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

πŸ“š Documentation

For documentation (including examples), visit rafallorenz.com/peer-cdn

🚏 HOW TO USE

Installation

$ npm install peer-cdn

Basic example

main.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);
    });
}

sw.js

// import peer-cdn into service worker
self.importScripts("https://github.com/vardius/peer-cdn/blob/v1.0.5-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

This package is released under the MIT license. See the complete license in the package

peer-cdn's People

Contributors

dependabot[bot] avatar vardius avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

peer-cdn's Issues

Refactor PeerCDN API

Refactor API

  • allow to register custom middlewares
  • manage middlewares for each event
  • manage event handlers for each event

1.0.0-beta

release beta after milestone steps are done
For more info chcek milestone decs

todo

  • allow to provide custom cache names
  • fix message channel closure
  • create better socket server with ability to extend
  • update docs and readme

Compatible with multiple service workers

Do you want to request a feature or report a bug?
feature

What is the current behavior?
cannot used when there is another service worker

What is the expected behavior?
when there is another service worker. this one can add before it

Implement peer middleware

Implement peer middleware

  • call signaling server for peers
  • download response from chunks
  • seed chunks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.