Skip to content

Latest commit

 

History

History
107 lines (73 loc) · 4.6 KB

File metadata and controls

107 lines (73 loc) · 4.6 KB

ipfs-message-port-server

ipfs.io IRC Discord codecov CI

IPFS server library for exposing IPFS node over message port

Table of contents

Install

$ npm i 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

import { create } from 'ipfs'
import { IPFSService, Server } from '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 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.

License

Licensed under either of

Contribute

Contributions welcome! Please check out the issues.

Also see 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.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.