@chris.troutner/ipfs-message-port-client
Version:
IPFS client library for accessing IPFS node over message port
57 lines (51 loc) • 1.56 kB
JavaScript
/* eslint-env browser */
const MessageTransport = require('./client/transport')
const BlockClient = require('./block')
const DAGClient = require('./dag')
const CoreClient = require('./core')
const FilesClient = require('./files')
class IPFSClient extends CoreClient {
/**
* @param {MessageTransport} transport
*/
constructor (transport) {
super(transport)
this.transport = transport
this.dag = new DAGClient(this.transport)
this.files = new FilesClient(this.transport)
this.block = new BlockClient(this.transport)
}
/**
* Attaches IPFS client to the given message port. Throws
* exception if client is already attached.
*
* @param {IPFSClient} self
* @param {MessagePort} port
*/
static attach (self, port) {
self.transport.connect(port)
}
/**
* Creates IPFS client that is detached from the `ipfs-message-port-service`.
* This can be useful when in a scenario where obtaining message port happens
* later on in the application logic. Datached IPFS client will queue all the
* API calls and flush them once client is attached.
*
* @returns {IPFSClient}
*/
static detached () {
return new IPFSClient(new MessageTransport(undefined))
}
/**
* Creates IPFS client from the message port (assumes that
* `ipfs-message-port-service` is instantiated on the other end)
*
* @param {MessagePort} port
* @returns {IPFSClient}
*/
static from (port) {
return new IPFSClient(new MessageTransport(port))
}
}
module.exports = IPFSClient