Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

myelnet/js-dcdn

Repository files navigation

js-dcdn

JS Client for interacting with the Myel CDN.

Retrieve content addressed data from a decentralized network of cache providers. This client is still highly experimental and API may still change at any moment.

Install

As an NPM package

npm install @dcdn/client

As a service worker

  • Install @dcdn/service-worker in your project and server @dcdn/service-worker/dist/index.min.js from your origin.
  • Register the service worker:
if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/sw.js')
  .then((reg) => {
    // registration worked
    console.log('Registration succeeded');
  }).catch((error) => {
    // registration failed
    console.log('Registration failed with ' + error);
  });
}

As a Cloudflare worker

npm install -g @cloudflare/wrangler
wrangler login
  • Deploy to a *.workers.dev subdomain
cd dists
wrangler publish

Usage

When using from a worker you can directly request the content using IPFS paths:

  • /bafyreiaemos3x3k5fmycs64ry3otobineo4wdz73ccrqxrlzt7gmtzhbmm/red-frog.jpg if running in a service worker
  • https://yourdomain.workers.dev/bafyreiaemos3x3k5fmycs64ry3otobineo4wdz73ccrqxrlzt7gmtzhbmm/red-frog.jpg if using a Cloudflare worker

When using the client directly in your application (We recommend using in a worker so as not to block the main thread).

import {create} from '@dcdn/client'

(async () => {
  const client = await create()

  const myImage = document.querySelector('img');

  const resp = await client.fetch('bafyreiaemos3x3k5fmycs64ry3otobineo4wdz73ccrqxrlzt7gmtzhbmm/red-frog.jpg')

  myImage.src = URL.createObjectURL(await resp.blob());
})();