@chris.troutner/ipfs-message-port-server

IPFS server library for exposing IPFS node over message port

Usage no npm install needed!

<script type="module">
  import chrisTroutnerIpfsMessagePortServer from 'https://cdn.skypack.dev/@chris.troutner/ipfs-message-port-server';
</script>

README

ipfs-message-port-server

Travis CI Codecov branch Dependency Status js-standard-style

A library for providing IPFS node over message channel. This library enables applications running in the different JS context to use IPFS API (subset) via ipfs-message-port-client.

Lead Maintainer

Alex Potsides

Table of Contentens

Install

$ npm install --save ipfs-message-port-server

Usage

This library can wrap a JS IPFS node and expose it over the message channel. It assumes ipfs-message-port-client on the other end, however it is not strictly necessary anything complying with the wire protocol will do.

It provides following API subset:

The server is designed to run in a SharedWorker (although it is possible to run it in the other JS contexts). The example below illustrates running a js-ipfs node in a SharedWorker and exposing it to all connected ports

const IPFS = require('ipfs')
const { IPFSService, Server } = require('ipfs-message-port-server')

const main = async () => {
  const connections = []
  // queue connections that occur while node was starting.
  self.onconnect = ({ports}) => connections.push(...ports)

  const ipfs = await IPFS.create()
  const service = new IPFSService(ipfs)
  const server = new Server(service)

  // connect new ports and queued ports with the server.
  self.onconnect = ({ports}) => server.connect(ports[0])
  for (const port of connections.splice(0)) {
    server.connect(port)
  }
}

main()

Notes on Performance

Since the data sent over the message channel is copied via the structured cloning algorithm it may lead to suboptimal results (especially with large binary data). In order to avoid unnecessary copying the server will transfer all passed Transferables which will be emptied on the server side. This should not be a problem in general as IPFS node itself does not retain references to returned values, but is something to keep in mind when doing something custom.

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to this repo are subject to the IPFS Code of Conduct.

License

FOSSA Status