项目作者: ipfs

项目描述 :
Control an IPFS daemon (go-ipfs or js-ipfs) using JavaScript!
高级语言: JavaScript
项目地址: git://github.com/ipfs/js-ipfsd-ctl.git
创建时间: 2015-04-25T04:40:11Z
项目社区:https://github.com/ipfs/js-ipfsd-ctl

开源协议:MIT License

下载


ipfsd-ctl

ipfs.tech
Discuss
codecov
CI

Spawn IPFS Daemons, Kubo or…

About

This module allows you to spawn long-lived IPFS implementations from any JS environment and interact with the as is they were in the local process.

It is designed mostly for testing interoperability and is not suitable for production use.

Spawning a single noder: createNode

Example - Spawning a Kubo node

  1. import { createNode } from 'ipfsd-ctl'
  2. import { path } from 'kubo'
  3. import { create } from 'kubo-rpc-client'
  4. const node = await createNode({
  5. type: 'kubo',
  6. rpc: create,
  7. bin: path()
  8. })
  9. console.info(await node.api.id())

Manage multiple nodes: createFactory

Use a factory to spawn multiple nodes based on some common template.

Example - Spawning multiple Kubo nodes

  1. import { createFactory } from 'ipfsd-ctl'
  2. import { path } from 'kubo'
  3. import { create } from 'kubo-rpc-client'
  4. const factory = createFactory({
  5. type: 'kubo',
  6. rpc: create,
  7. bin: path()
  8. })
  9. const node1 = await factory.spawn()
  10. const node2 = await factory.spawn()
  11. //...etc
  12. // later stop all nodes
  13. await factory.clean()

Override config based on implementation type

createFactory takes a second argument that can be used to pass default options to an implementation based on the type field.

  1. import { createFactory } from 'ipfsd-ctl'
  2. import { path } from 'kubo'
  3. import { create } from 'kubo-rpc-client'
  4. const factory = createFactory({
  5. type: 'kubo',
  6. test: true
  7. }, {
  8. otherImpl: {
  9. //...other impl args
  10. }
  11. })
  12. const kuboNode = await factory.spawn()
  13. const otherImplNode = await factory.spawn({
  14. type: 'otherImpl'
  15. })

Spawning nodes from browsers

To spawn nodes from browsers, first start an ipfsd-ctl server from node.js and make the address known to the browser (the default way is to set process.env.IPFSD_CTL_SERVER in your bundle):

Example - Create server

In node.js:

  1. // Start a remote disposable node, and get access to the api
  2. // print the node id, and stop the temporary daemon
  3. import { createServer } from 'ipfsd-ctl'
  4. const port = 9090
  5. const server = createServer(port, {
  6. type: 'kubo',
  7. test: true
  8. }, {
  9. // overrides
  10. })
  11. await server.start()

In a browser:

  1. import { createFactory } from 'ipfsd-ctl'
  2. const factory = createFactory({
  3. // or you can set process.env.IPFSD_CTL_SERVER to http://localhost:9090
  4. endpoint: `http://localhost:${port}`
  5. })
  6. const node = await factory.createNode({
  7. type: 'kubo'
  8. })
  9. console.info(await node.api.id())

Disposable vs non Disposable nodes

ipfsd-ctl can spawn disposable and non-disposable nodes.

  • disposable- Disposable nodes are useful for tests or other temporary use cases, they create a temporary repo which is deleted automatically when the node is stopped
  • non-disposable - Disposable nodes will not delete their repo when stopped

Install

  1. $ npm i ipfsd-ctl

Browser <script> tag

Loading this module through a script tag will make it’s exports available as IpfsdCtl in the global namespace.

  1. <script src="https://unpkg.com/ipfsd-ctl/dist/index.min.js"></script>

API Docs

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.