CacheP2P



How does it work?

Technical details

These are the internals

99.99% made of Webtorrent

WebTorrent is what makes CacheP2P possible, (my public thanks Feross Aboukhadijeh for making WebTorrent).

CacheP2P uses a slightly modified version of WebTorrent that allows hashes to be defined manually, CacheP2P needs this to create torrents from webpages and define it's Hashes based on each website's URLs, instead of the metadata. So that the URL is all that is needed to get a webpage's content from the Swarm.

This removes a layer or security/verification from the torrent protocol (PEP9), but it is later added again by CacheP2P, together with the mechanism to define when any content in the cache has expired.

Anatomy of a Highly Distributed Cache

The first page the user arrives, loads from the server

Any webpage that has CacheP2P deployed, will do the following steps as soon as it arrives to a browser:

Step 1

Take that page's HTML content and make it available to any other peer browsing the same website, use the page's URL to generate it's TorrentID .

Step 2

Search for all the links to other pages and filter the ones that are in the same domain.

Step 3

Generate the TorrentID of each link's URL and try to find them from other Peers.

Step 4

Wait for peers to send the cached contents.

Step 5

As soon as a cache is received check the contents sha1 hash with the one defined in document security_sha1 variable (explained in the documentation).

Step 6

If the cache received is valid, save it and use it, so that when the user clicks on the corresponding link, the content shown is from the already received cache, instead than from the website's server.

Step 7

All received and created caches are made available to any other Peer browsing the same website.

API

cachep2p = new CacheP2P(opts)

Create a new CacheP2P instance.

If opts is specified, then the default options (shown below) will be overridden.
{
  announceList: Array,                     // List of Trackers to use instead of default ones
  include_css: Boolean (default: false),   // Include aditional css stylesheets in the Cache
}


cachep2p.on('ready', [function] )

Called when the page's content has been saved as a Cache and can be sent to other Peers.

IMPORTANT If the page is dynamically modified before this event, the resulting verification hash will be different and the cache will not be accepted by other peers.

cachep2p.on('message|alert|success', function(message){} )

Informational messages.

cachep2p.on('webtorrent', function(message){} )

Important message from WebTorrent.

cachep2p.on('cache', function (click_event) {} )

Cache is being used to display the currently clicked link.

cachep2p.on('onpopstate', function(click_event){} )

User is using 'Back' or 'Next' browser buttons.

Fork me on GitHub