【IPFS更新追踪】js-ipfs v0.31.1版本发布

【IPFS更新追踪】js-ipfs v0.31.1版本发布

  更新时间:2018年7月31日

  更新出处:GitHub

  消息发布:Alan·Shaw

  更新概述:消息发布人Alan·Shaw在Twitter中发布js-ipfs v0.31.1版本。并表示项目已经进行了很久,但是这个项目仍然在测试期,正在进行大量的开发,API可能会改变,注意风险

 


以下为IPFS原力区追踪代码译文及本次更新内容原文,供各位爱好者参考

 

译文

项目状态

我们已经进行了很久,但是这个项目仍然在测试期,正在进行大量的开发,API可能会改变,注意风险 🐉..

想要开始?查看我们的examples文件夹,了解如何在Node.js和Browser中生成IPFS节点。

您可以在聊天板块查看开发状态

请阅读: DHT(自动内容发现)和电路中继(穿过NAT并在网络中的任何节点之间拨号)是两个尚未最终确定的基本部分。有两个应用程序可以在没有这两个服务的情况下构建,但它们是获得神奇IPFS体验的基础。如果您想跟踪进度或贡献,请按以下步骤操作:

  • DHT:https//github.com/ipfs/js-ipfs/pull/856

  • 接力:https//github.com/ipfs/js-ipfs/pull/1063

Weekly Core Dev Calls

技术主管

大卫迪亚斯

主要维护者

艾伦肖

目录

  • 安装

    • NPM

    • 在Node.js中使用

    • 通过命令行工具

    • 在浏览器中使用

  • 用法

    • 构造函数

    • 活动

    • 开始

    • 停止

    • 核心API

    • 图形

    • 加密和密钥管理

    • 网络

    • 节点管理

    • 域数据类型

    • UTIL

    • IPFS CLI

    • IPFS守护进程

    • IPFS模块(在Node.js或浏览器中使用IPFS作为模块)

    • 教程和示例

    • API文档

  • 常问问题

  • 用Docker运行js-ipfs

  • 发展

    • 克隆并安装依赖项

    • 运行测试

    • 皮棉

    • 构建dist版本

  • 有助于

    • 想破解IPFS?

  • 执照

安装

NPM

该项目可通过npm获得安装运行

> npm install ipfs --save

需要npm @ 3和node @ 6或更高版本,在OSX和Linux上测试,预计可在Windows上运行。

在Node.js中使用

要以编程方式包含此项目:

const  IPFS  =  require' ipfs 'const  node  =  new  IPFS()

通过命令行工具

要将js-ipfs用作CLI,必须使用该global标志安装它运行以下命令(即使您在本地安装了ipfs):

> npm install ipfs --global

可以使用jsipfs终端中的命令来使用CLI 这是别名而不是使用ipfs,以确保它不与Go实现冲突

在浏览器中使用

了解如何在examples文件夹中捆绑browserify和webpack 

您也可以<script>使用unpkg CDN或jsDelivr CDN 加载它插入以下行之一将使Ipfs对象在全局命名空间中可用。

<! -加载缩小版本- > 
<
script src = https://unpkg.com/ipfs/dist/index.min.js > < / script >
< script src = https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js > < / script >

<! -加载人类可读(未缩小)的版本- >
<
script src = https://unpkg.com/ipfs/dist/index.js > < / script >
< script src = https://cdn.jsdelivr.net/npm/ipfs/dist/index.js > < / script >

插入上述行之一将使Ipfs对象在全局命名空间中可用。

用法

IPFS CLI

jsipfsCLI,如果可用js-ipfs在全球的装机量,如下(应该,这是一个WIP)的定义相同的接口go-ipfs,可以随时使用该help命令的帮助菜单。

全局安装js-ipfs > npm install ipfs --global > jsipfs --help命令:
  bitswap一用于操作bitswap代理的命令。
  block操作原始IPFS块。
  bootstrap显示或编辑引导对等体列表。
  命令列出所有可用命令
  config < key > [value]获取并设置 IPFS配置值
  守护程序启动长时运行的守护程序进程 ...

js-ipfs使用一些不同的默认配置值,这样它们就不会直接与在同一台机器上运行的go-ipfs节点发生冲突。这些是:

  • 默认仓库位置:( ~/.jsipfs可以用env变量更改IPFS_PATH

  • 默认群集端口: 4002

  • 默认API端口: 5002

  • 默认Bootstrap已关闭,以启用它设置 IPFS_BOOTSTRAP=1

IPFS守护进程

IPFS守护程序公开了定义的API http-api-spec您可以使用任何IPFS HTTP-API客户端库,例如:js-ipfs-api

如果您想要一种使用JavaScript生成IPFS守护程序的编程方法,请查看ipfsd-ctl模块

IPFS模块

使用IPFS模块作为项目的依赖项,以在IPFS的流程实例中生成通过调用new IPFS()并等待其ready事件来创建实例

//创建IPFS节点实例

const node = new IPFS()节点

on' ready ',()=> {   //您的节点现在可以使用 o / //停止节点 的节点stop(() => {     //节点现在'离线'  })})

教程和示例

您可以在示例文件夹中找到一些示例和教程,这些示例和教程可帮助您开始使用js-ipfs

API

IPFS构造函数

const  node  =  new  IPFS([options])

创建并返回IPFS节点的实例。使用options参数指定高级配置。它是具有以下任何属性的对象:

repo(字符串或ipfs.Repo实例):存储IPFS节点数据的文件路径。或者,您可以通过提供ipfs.Repo实例来设置自定义存储系统(默认值:'~/.jsipfs'在Node.js中,'ipfs'在浏览器中。)

例:

//将数据存储在用户目录之外
const node = new IPFS({repo ' / var / ipfs / data ' })
  • init(boolean或object):在创建IPFS节点时初始化repo。(默认值:true

    如果您在创建IPFS节点之前已经初始化了一个repo(例如,您正在加载从以前的程序运行中保存到磁盘的repo),则必须确保将其设置为false请注意,初始化 repo与创建实例不同ipfs.RepoIPFS构造函数在初始化repo时设置了许多特殊属性,因此通常不应该尝试repoInstance.init()自己调用

    您可以使用自定义初始化选项提供对象,而不是布尔值。所有属性都是可选的:

    • init.emptyRepo(boolean)是否从repo中删除内置资源,如教学游览和空可变文件系统。(默认值:false

    • init.bits(number)生成的密钥对中使用的位数。(默认值:2048

    • init.pass(字符串)用于加密密钥的密码。您通常应使用顶级pass选项而不是init.pass选项(如果未设置,此选项将从顶级选项中获取其值)。

  • start(布尔值):如果false,不自动启动IPFS节点。相反,你需要手动打电话给node.start()自己。(默认值:true

  • pass (字符串):用于加密/解密密钥的密码。

  • relay(对象):配置电路继电器(参见电路继电器教程了解更多信息)。

    • enabled(布尔值):使此节点成为中继(其他节点可以通过连接)。(默认值:false

    • active(布尔值):使其成为活动的中继节点。即使该对等体尚未连接到中继,活动中继节点也将尝试拨打目标对等体。(默认值:false

    • enabled(布尔值):启用电路中继拨号器和监听器。(默认值:false

    • hop (目的)

  • preload (object):配置将预加载添加到此节点的内容的外部节点

    • enabled(布尔值):启用内容预加载(缺省:true

    • addresses(array):应预加载内容的节点的Multiaddr API地址。注意:此处指定的节点也应添加到节点的引导程序地址列表中config.Boostrap

  • EXPERIMENTAL (对象):启用和配置实验功能。

    • pubsub(布尔值):启用libp2p pub-sub。(默认值:false

    • sharding(布尔值):启用目录分片。具有多个子对象的目录将由多个DAG节点表示,而不是仅由一个。当目录有几千个或更多文件时,它可以提高查找性能。(默认值:false

    • dht(布尔值):启用KadDHT。目前这不能与之互动go-ipfs

  • config(对象)修改默认IPFS节点配置。找到Node.js默认值为src/core/runtime/config-nodejs.js,浏览器默认值为src/core/runtime/config-browser.js此对象将与默认配置合并 ; 它不会取代它。

  • libp2p (对象)将自定义模块添加到节点的libp2p堆栈中

    • peerDiscovery (目的):

    • enabled (布尔值):是启用还是禁用此模块

    • [custom config] (任何):其他键是特定于模块的

    • [PeerDiscovery.tag] (object):对等发现模块的配置

    • transport(Array < libp2p.Transport >):要使用的Libp2p传输类/实例数组,而不是默认值。有关详细信息,请参阅libp2p / interface-transport

    • peerDiscovery(Array < libp2p.PeerDiscovery >):要使用的Libp2p对等方发现类/实例的数组,而不是默认值。有关详细信息,请参阅libp2p / peer-discovery如果传递一个类,可以使用下面的config部分传递配置,该键对应于您模块的唯一键tag(类的静态属性)

    • modules (目的):

    • config (目的):

  • connectionManager(对象):配置libp2p连接管理器,请参阅可用选项文档

活动

IPFS实例是Node.js EventEmitters您可以通过以下方式收听事件node.on('event', handler)

const  node  =  new  IPFS({repo  ' / var / ipfs / data ' }) 节点'错误'errorObject  =>  控制台误差(errorObject))
  • error总是伴随着一个Error对象,其中包含有关错误的信息。

    节点'错误'误差 => {   控制台错误错误消息})
  • init在新的repo初始化之后发出。如果init: false在构造函数上设置选项,则不会发出它

  • ready在节点准备好使用时发出。这是您在创建节点时(在init之后start将收到的最终事件

    在创建新的IPFS节点时,您应该几乎总是ready在调用方法或与节点交互之前等待事件。

  • start在节点开始侦听连接时发出。如果start: false在构造函数上设置选项,则不会发出它

  • stop当节点关闭所有连接并释放对其repo的访问权限时,将发出此消息。这通常是打电话的结果node.stop()

node.start([callback])

开始侦听与网络上其他IPFS节点的连接。在大多数情况下,您无需调用此方法 - new IPFS()将自动为您执行此操作。

此方法是异步的。节点启动完成后,有几种方法可以通知:

  1. 如果不node.start()带参数调用,则返回一个promise。

  2. 如果将函数作为最终参数传递,则在启动节点时将调用该函数。(注意:如果使用回调函数,此方法不会返回承诺。)

  3. 你可以听听这个start事件

const  node  =  new  IPFS({start  false })//使用promise:节点开始()
  。然后(()=>  控制台日志'节点开始吧!'))
  。catch错误 =>  控制台错误'节点无法启动!',错误))//或者使用回调:节点启动错误 => {  如果(错误){    控制台错误节点启动失败!,错误)    回报
  }
  控制台log' Node started!'})// OR使用事件:nodeon ' error ' error  =>  console错误 '事情发生了错误!',错误))节点 '开始',() =>  控制台日志 '节点开始吧!'))节点开始()

node.stop([callback])

关闭并停止侦听与其他IPFS节点的连接,然后释放对节点repo的访问权限。

此方法是异步的。当节点完全停止时,有几种方法可以通知:

  1. 如果不node.stop()带参数调用,则返回一个promise。

  2. 如果将函数作为最终参数传递,则在节点停止时将调用它。(注意:如果使用回调函数,此方法不会返回承诺。)

  3. 你可以听听这个stop事件

const  node  =  新的 IPFS() 节点'准备好',()=> {   控制台日志'节点就可以使用了!'
  //使用promise:
  节点停止停止()
    。然后(()=>  控制台登录'节点停!'))
    。catch错误 =>  控制台错误'节点未能完全停止!',错误))
  //或者使用回调:
  节点停止错误 => {     if(错误){       console错误 '节点未能完全停止!',错误)      返回
    }
    控制台log' Node stopped!'
  })
  // OR使用事件:
  nodeon ' error ' error  =>  console错误 '事情发生了错误!',错误))  节点停止()})

核心API

IPFS核心API提供的所有功能并非特定于设置和启动或停止节点。此API可直接在IPFS实例上,命令行上(使用CLI界面时)和HTTP REST API上使用。有关完整参考,请参阅

核心API分为几个方面:

Files

    • ipfs.files.cp([from, to], [callback])

    • ipfs.files.flush([path], [callback])

    • ipfs.files.ls([path], [options], [callback])

    • ipfs.files.mkdir(path, [options], [callback])

    • ipfs.files.mv([from, to], [callback])

    • ipfs.files.read(path, [options], [callback])

    • ipfs.files.readPullStream(path, [options])

    • ipfs.files.readReadableStream(path, [options])

    • ipfs.files.rm(path, [options], [callback])

    • ipfs.files.stat(path, [options], [callback])

    • ipfs.files.write(path, content, [options], [callback])

    • ipfs.files.add(data, [options], [callback])别名来ipfs.add

    • ipfs.files.addPullStream([options])

    • ipfs.files.addReadableStream([options])

    • ipfs.files.cat(ipfsPath, [options], [callback])别名来ipfs.cat

    • ipfs.files.catPullStream(ipfsPath, [options])

    • ipfs.files.catReadableStream(ipfsPath, [options])

    • ipfs.files.get(ipfsPath, [options], [callback])别名来ipfs.get

    • ipfs.files.getPullStream(ipfsPath, [options])

    • ipfs.files.getReadableStream(ipfsPath, [options])

    • ipfs.ls(ipfsPath, [callback])

    • ipfs.lsPullStream(ipfsPath)

    • ipfs.lsReadableStream(ipfsPath)

    • MFS(可变文件系统)具体

    • ipfs.block.get(cid, [options, callback])

    • ipfs.block.put(block, cid, [callback])

    • ipfs.block.stat(cid, [callback])

Graph

  • DAG

    • ipfs.dag.put(dagNode, options, callback)

    • ipfs.dag.get(cid [, path, options], callback)

    • ipfs.dag.tree(cid [, path, options], callback)

  • 对象

    • ipfs.object.new([template][, callback])

    • ipfs.object.put(obj, [options, callback])

    • ipfs.object.get(multihash, [options, callback])

    • ipfs.object.data(multihash, [options, callback])

    • ipfs.object.links(multihash, [options, callback])

    • ipfs.object.stat(multihash, [options, callback])

    • ipfs.object.patch.addLink(multihash, DAGLink, [options, callback])

    • ipfs.object.patch.rmLink(multihash, DAGLink, [options, callback])

    • ipfs.object.patch.appendData(multihash, data, [options, callback])

    • ipfs.object.patch.setData(multihash, data, [options, callback])

  • pin(尚未实现!)

Crypto and Key Management

    • ipfs.key.export(name, password, [callback])

    • ipfs.key.gen(name, options, [callback])

    • ipfs.key.import(name, pem, password, [callback])

    • ipfs.key.list([callback])

    • ipfs.key.rename(oldName, newName, [callback])

    • ipfs.key.rm(name, [callback])

  • 加密(尚未实施)

Network

  • 引导

    • ipfs.bootstrap.list

    • ipfs.bootstrap.add

    • ipfs.bootstrap.rm

  • 比特交换

    • ipfs.bitswap.wantlist()

    • ipfs.bitswap.stat()

    • ipfs.bitswap.unwant()

  • dht(尚未实施!)

  • 发布订阅

    • ipfs.pubsub.subscribe(topic, handler, options, callback)

    • ipfs.pubsub.unsubscribe(topic, handler, callback)

    • ipfs.pubsub.publish(topic, data, callback)

    • ipfs.pubsub.ls(topic, callback)

    • ipfs.pubsub.peers(topic, callback)

  • libp2p每个IPFS实例也公开了libp2p SPEC ipfs.libp2p您尚未定义此SPEC的正式接口,您可以在其实现中找到文档:

    • Node.js包

    • 浏览器套装

    • libp2p基类

  • 一群

    • ipfs.swarm.addrs([callback])

    • ipfs.swarm.connect(addr, [callback])

    • ipfs.swarm.disconnect(addr, [callback])

    • ipfs.swarm.peers([opts] [, callback])

Node Management

  • 杂项业务

    • ipfs.id([callback])

    • ipfs.version([callback])

    • ipfs.ping()

    • ipfs.init([options], callback)

    • ipfs.start([callback])

    • ipfs.stop([callback])

    • ipfs.isOnline()

  • 回购

    • ipfs.repo.init

    • ipfs.repo.stat([options, callback])

    • ipfs.repo.version([callback])

    • ipfs.repo.gc([options, callback]) (尚未实施!)

  • 统计

    • ipfs.stats.bitswap([callback])

    • ipfs.stats.bw([options, callback])

    • ipfs.stats.bwPullStream([options]) -> Pull Stream

    • ipfs.stats.bwReadableStream([options]) -> Readable Stream

    • ipfs.stats.repo([options, callback])

  • 配置

    • ipfs.config.get([key, callback])

    • ipfs.config.set(key, value, [callback])

    • ipfs.config.replace(config, [callback])

Domain data types

直接从IPFS实例下公开一组数据类型ipfs.types这样您就不需要导入/要求以下内容。

  • ipfs.types.Buffer

  • ipfs.types.PeerId

  • ipfs.types.PeerInfo

  • ipfs.types.multiaddr

  • ipfs.types.multibase

  • ipfs.types.multihash

  • ipfs.types.CID

  • ipfs.types.dagPB

  • ipfs.types.dagCBOR

Util

一组utils直接从IPFS实例下公开ipfs.util这样您就不需要导入/要求以下内容:

  • ipfs.util.crypto

  • ipfs.util.isIPFS

常问问题

如何在浏览器中启用对js-ipfs的WebRTC支持

要将WebRTC传输添加到js-ipfs节点,必须添加WebRTC multiaddr。为此,简单地覆盖config.Addresses.Swarm数组,该数组包含IPFS节点将使用的所有多重加载器。见下文:

const  node  =  new  IPFS({
  config  {
    地址 {
      Swarm  [         '/ dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star '
      ]
    }
  }})节点on' ready ',()=> {   //您的WebRTC实例已准备就绪 })

重要提示:此传输使用情况有点不稳定,有几个用户遇到崩溃问题。通过https://github.com/ipfs/js-ipfs/issues/1088跟踪解决方案的开发

WebRTC是否支持带有Node.js的js-ipfs?

是的,但是,请记住,在Node.js中使用WebRTC没有100%稳定的解决方案,使用它需要您自担风险。测试最多的选项是:

  • wrtc - 按照安装说明进行操作。

  • 电子的WebRTC

要在IPFS节点实例中添加WebRTC支持,请执行以下操作:

const  wrtc  =  require' wrtc '//或require('electron- webrtc '//()const  WStar  =  require' libp2p-webrtc-star 'const  wstar  =  new  WStar({wrtc  wrtc})const  node  =  new  IPFS({
  repo  ' your-repo-path ',   // start:false, 
  config  {
    地址 {
      Swarm  [         “/ ip4 / 0.0.0.0 / tcp / 4002 ,         “/ ip4 / 127.0.0.1 / tcp / 4003 / ww ,         “/ dns4 / wrtc-star.discovery.libp2p.io / tcp / 443 / wss / p2p-webrtc-star 
      ]
    }
  },
  libp2p  {
    模块 {
      运输 [wstar],
      peerDiscovery  [ wstar发现 ]
    }
  }})节点on' ready ',()=> {   //您的WebRTC实例已准备就绪 })

要向IPFS守护程序添加WebRTC支持,您只需要在全局安装一个WebRTC模块:

npm install wrtc --global  npm install electron-webrtc --global

然后,更新您的IPFS守护程序配置,以在Addresses.Swarm阵列上包含此新传输的multiaddr 加:"/dns4/wrtc-star.discovery.libp2p.io/wss/p2p-webrtc-star"

如何配置IPFS节点以使用自定义signaling endpointWebRTC传输?

您需要执行兼容signaling serverlibp2p-webrtc-star工作)并为您的IPFS节点包含正确的配置参数:

  • 提供multiaddrsignaling server

const  node  =  new  IPFS({
  回购 '你的回购路径'
  config  {
    地址 {
      Swarm  [         '/ ip4/127.0.0.1/tcp/9090/ws/p2p-webrtc-star '
      ]
    }
  }})

上面的代码假设您正在运行本地signaling server端口9090相应地提供正确的值。

是否有更稳定的替代webrtc-star提供类似的功能?

是的,websocket-star!基于WebSocket的传输,使用中继路由消息。要启用它,只需执行以下操作:

const  node  =  new  IPFS({
  config  {
    地址 {
      Swarm  [         '/ dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star '
      ]
    }
  }})节点on' ready ',()=> {   //你的websocket-star实例准备就绪 })

我看到在Chrome与IPFS节点之间跳转时有些缓慢,有什么原因吗?

是的,不幸的是,由于Chrome积极的资源限制策略,它会冻结任何后台选项卡的执行,将在该网页上运行的IPFS节点变为蔬菜状态。

在Chrome中缓解此问题的一种方法是在Service Worker中运行IPFS节点,以便IPFS实例在后台进程中运行。您可以在repo ipfs-service-worker中学习如何将IPFS节点作为服务工作者进行安装

我可以在Electron App中使用IPFS吗?

是的,你可以在很多方面。有关多个选项,请阅读https://github.com/ipfs/notes/issues/256

如果您的电子重建步骤失败,您需要做的就是:

电子版的。出口 npm_config_target = 2.0.0

电子的结构,可以是IA32或x64。

export npm_config_arch = x64
export
npm_config_target_arch = x64

下载电子标题。
出口 npm_config_disturl = HTTPS://atom.io/download/electron

告诉我们正在为电子节点预GYP。
export npm_config_runtime = electron

告诉node-pre- gyp从源代码构建模块。
export npm_config_build_from_source = true

安装所有依赖项,并将缓存存储到〜/ .electron-gyp。
HOME =/.electron-gyp npm install

如果您发现任何其他问题,请检查Electron Support问题

有更多问题吗?

在我们的论坛https://discuss.ipfs.io或IRC(Freenode上的#ipfs)寻求帮助

用Docker运行js-ipfs

我们使用Docker Hub进行自动Docker构建设置:https//hub.docker.com/r/ipfs/js-ipfs/

Github存储库中的所有分支都映射到Docker Hub中的标记,masterGit分支除外,该分支映射到latestDocker标记。

你可以像这样运行js-ipfs:

$ docker run -it -p 4002:4002 -p 4003:4003 -p 5002:5002 -p 9090:9090 ipfs/js-ipfs:latest
initializing ipfs node at /root/.jsipfs
generating 2048-bit RSA keypair...done
peer identity: Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
to get started, enter:         jsipfs files cat /ipfs/QmfGBRT6BbWJd7yUc2uYdaUZJBbnEFvTqehPFoSMQ6wgdr/readme
Initializing daemon...
Using wrtc for webrtc support
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
Swarm listening on /ip4/172.17.0.2/tcp/4003/ws/ipfs/Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
Swarm listening on /ip4/172.17.0.2/tcp/4002/ipfs/Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
API is listening on: /ip4/0.0.0.0/tcp/5002
Gateway (readonly) is listening on: /ip4/0.0.0.0/tcp/9090
Daemon is ready
$ curl --silent localhost:5002/api/v0/id | jq .ID
"Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS"

列出IPFS生态系统中使用的主要包。这里还有三个值得链接的规范:

  • interface-ipfs-core

  • http-api-spec

  • cli spec



克隆并安装依赖项发展

> git clone https://github.com/ipfs/js-ipfs.git 
> cd js-ipfs
> npm install

运行单元测试

运行所有单元测试
> npm测试

在Node.js中运行IPFS测试
> npm run test:node

运行IPFS核心测试
> npm run test:node:core

运行IPFS HTTP-API测试
> npm run test:node:http

运行只是IPFS CLI测试
> NPM运行测试:节点:CLI

在浏览器(Chrome)中运行IPFS核心测试
> npm run test:browser

运行互操作测试

使用https://github.com/ipfs/interop运行互操作测试

运行基准测试

运行所有基准测试
> npm运行基准测试
在Node.js中运行IPFS基准测试
> npm运行基准测试:节点
在Node.js中为IPFS实例运行IPFS基准测试
> npm运行基准测试:node:core
在Node.js中运行IPFS基准测试以获取IPFS守护程序
> npm run benchmark:node:http
在浏览器中运行IPFS基准测试(Chrome)
> npm运行基准测试:浏览器

皮棉

符合linting规则是提交js-ipfs的先决条件。

> npm run lint

构建dist版本

> npm run build

运行时支持

代码体系结构和文件夹结构

源代码
>树src -L 2src                  主要源代码文件夹 ├──cli              # IPFS CLI的实现│└──......├──HTTP-API         由http-API规范中定义IPFS的HTTP-API实现 ├──核心              IPFS实施,核心(什么获取浏览器加载) │├──组件   每个IPFS子的│└──......└──......

监控

使用js-ipfs公开的HTTP API也可用于公开有关正在运行的js-ipfs节点和其他Node.js指标的指标。

要启用它,您需要设置环境变量IPFS_MONITORING(任何值)

设置环境变量并运行js-ipfs守护程序后,您可以通过向以下端点发出GET请求来获取指标(以prometheus格式):

http://localhost:5002/debug/metrics/prometheus

IPFS核心架构

这个图像解释了什么?

  • IPFS使用ipfs-repo哪个选择fsindexeddb作为其存储驱动程序,具体取决于它是在Node.js还是在浏览器中运行。

  • 交换协议bitswap使用块服务,块服务又使用Repo,为IPFS实现提供块的获取和放置。

  • DAG API(以前称为Object)来自IPLD解析器,它可以支持多种IPLD格式(即:dag-pb,dag-cbor等)。

  • Files API用于ipfs-unixfs-engine从IPFS导入和导出文件。

  • Swarm是提供网络API的组件,它使用libp2p来拨号和侦听连接,使用DHT,发现机制等等。在Node.js中运行时使用libp2p-ipfs-nodejs,在浏览器中运行时使用libp2p-ipfs-browser。

有助于

JavaScript中的IPFS实现正在进行中。因此,您现在可以做一些事情来帮助:

  • 浏览下面的模块并查看现有问题这对于积极开发中的模块尤其有用。可能需要一些IPFS知识,以及它背后的基础设施 - 例如,您可能需要阅读p2p以及更复杂的操作(如多路复用)以便能够在技术上提供帮助。

  • 执行代码审查更多的目光将有助于(a)加快项目进度,(b)确保质量,(c)减少可能的未来错误。

  • 看一下go-ipfs和一些规划存储库或问题:例如,libp2p规范这里最有帮助的贡献是基于我们理解的关于它应该如何看待的顶级评论再一次,眼睛越多越好。

  • 添加测试永远不会有足够的测试。

 

原文

Project status

We've come a long way, but this project is still in Alpha, lots of development is happening, API might change, beware of the Dragons 🐉..

Want to get started? Check our examples folder to learn how to spawn an IPFS node in Node.js and in the Browser.

You can check the development status at the Waffle Board.Please read this: DHT (automatic content discovery) and Circuit Relay (pierce through NATs and dial between any node in the network) are two fundamental pieces that are not finalized yet. There are multiple applications that can be built without these two services but nevertheless they are fundamental to get that magic IPFS experience. If you want to track progress or contribute, please follow:

  • DHT: https://github.com/ipfs/js-ipfs/pull/856

  •  Relay: https://github.com/ipfs/js-ipfs/pull/1063

Weekly Core Dev Calls

Tech Lead

David Dias

Lead Maintainer

Alan Shaw

Table of Contents

  • Install

    • npm

    • Use in Node.js

    • Through command line tool

    • Use in the browser

  • Usage

    • Constructor

    • Events

    • start

    • stop

    • Core API

    • Files

    • Graph

    • Crypto and Key Management

    • Network

    • Node Management

    • Domain data types

    • Util

    • IPFS CLI

    • IPFS Daemon

    • IPFS Module (use IPFS as a module in Node.js or in the Browser)

    • Tutorials and Examples

    • API Docs

  • FAQ

  • Running js-ipfs with Docker

  • Packages

  • Development

    • Clone and install dependencies

    • Run Tests

    • Lint

    • Build a dist version

  • Contribute

    • Want to hack on IPFS?

  • License

Install

npm

This project is available through npm. To install run

> npm install ipfs --save

Requires [email protected] and [email protected] or above, tested on OSX & Linux, expected to work on Windows.

Use in Node.js

To include this project programmatically:

const IPFS = require('ipfs')const node = new IPFS()

Through command line tool

In order to use js-ipfs as a CLI, you must install it with the global flag. Run the following (even if you have ipfs installed locally):

> npm install ipfs --global

The CLI is available by using the command jsipfs in your terminal. This is aliased, instead of using ipfs, to make sure it does not conflict with the Go implementation.

Use in the browser

Learn how to bundle with browserify and webpack in the examples folder.

You can also load it using a <script> using the unpkg CDN or the jsDelivr CDN. Inserting one of the following lines will make a Ipfs object available in the global namespace.

<!-- loading the minified version --><script src="https://unpkg.com/ipfs/dist/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script><!-- loading the human-readable (not minified) version --><script src="https://unpkg.com/ipfs/dist/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.js"></script>

Inserting one of the above lines will make an Ipfs object available in the global namespace.

Usage

IPFS CLI

The jsipfs CLI, available when js-ipfs is installed globally, follows(should, it is a WIP) the same interface defined by go-ipfs, you can always use the help command for help menus.

# Install js-ipfs globally> npm install ipfs --global> jsipfs --help
Commands:
  bitswap               A set of commands to manipulate the bitswap agent.
  block                 Manipulate raw IPFS blocks.
  bootstrap             Show or edit the list of bootstrap peers.
  commands              List all available commands
  config <key> [value]  Get and set IPFS config values
  daemon                Start a long-running daemon process# ...

js-ipfs uses some different default config values, so that they don't clash directly with a go-ipfs node running in the same machine. These are:

  • default repo location: ~/.jsipfs (can be changed with env variable IPFS_PATH)

  • default swarm port: 4002

  • default API port: 5002

  • default Bootstrap is off, to enable it set IPFS_BOOTSTRAP=1

IPFS Daemon

The IPFS Daemon exposes the API defined http-api-spec. You can use any of the IPFS HTTP-API client libraries with it, such as: js-ipfs-api.

If you want a programmatic way to spawn a IPFS Daemon using JavaScript, check out ipfsd-ctl module

IPFS Module

Use the IPFS Module as a dependency of a project to spawn in process instances of IPFS. Create an instance by calling new IPFS() and waiting for its ready event:

// Create the IPFS node instanceconst node = new IPFS()node.on('ready', () => {  // Your node is now ready to use o/

  // stopping a node
  node.stop(() => {    // node is now 'offline'
  })
})

Tutorials and Examples

You can find some examples and tutorials in the examples folder, these exist to help you get started using js-ipfs.

API

IPFS Constructor

const node = new IPFS([options])

Creates and returns an instance of an IPFS node. Use the options argument to specify advanced configuration. It is an object with any of these properties:

  • repo (string or ipfs.Repo instance): The file path at which to store the IPFS node’s data. Alternatively, you can set up a customized storage system by providing an ipfs.Repo instance. (Default: '~/.jsipfs' in Node.js, 'ipfs' in browsers.)

    Example:

    // Store data outside your user directoryconst node = new IPFS({ repo: '/var/ipfs/data' })
  • init (boolean or object): Initialize the repo when creating the IPFS node. (Default: true)

    If you have already initialized a repo before creating your IPFS node (e.g. you are loading a repo that was saved to disk from a previous run of your program), you must make sure to set this to false. Note that initializing a repo is different from creating an instance of ipfs.Repo. The IPFS constructor sets many special properties when initializing a repo, so you should usually not try and call repoInstance.init() yourself.

    Instead of a boolean, you may provide an object with custom initialization options. All properties are optional:

    • init.emptyRepo (boolean) Whether to remove built-in assets, like the instructional tour and empty mutable file system, from the repo. (Default: false)

    • init.bits (number) Number of bits to use in the generated key pair. (Default: 2048)

    • init.pass (string) A passphrase to encrypt keys. You should generally use the top-level pass option instead of the init.pass option (this one will take its value from the top-level option if not set).

  • start (boolean): If false, do not automatically start the IPFS node. Instead, you’ll need to manually call node.start()yourself. (Default: true)

  • pass (string): A passphrase to encrypt/decrypt your keys.

  • relay (object): Configure circuit relay (see the circuit relay tutorial to learn more).

    • enabled (boolean): Make this node a relay (other nodes can connect through it). (Default: false)

    • active (boolean): Make this an active relay node. Active relay nodes will attempt to dial a destination peer even if that peer is not yet connected to the relay. (Default: false)

    • enabled (boolean): Enable circuit relay dialer and listener. (Default: false)

    • hop (object)

  • preload (object): Configure external nodes that will preload content added to this node

    • enabled (boolean): Enable content preloading (Default: true)

    • addresses (array): Multiaddr API addresses of nodes that should preload content. NOTE: nodes specified here should also be added to your node's bootstrap address list at config.Boostrap

  • EXPERIMENTAL (object): Enable and configure experimental features.

    • pubsub (boolean): Enable libp2p pub-sub. (Default: false)

    • sharding (boolean): Enable directory sharding. Directories that have many child objects will be represented by multiple DAG nodes instead of just one. It can improve lookup performance when a directory has several thousand files or more. (Default: false)

    • dht (boolean): Enable KadDHT. This is currently not interopable with go-ipfs.

  • config (object) Modify the default IPFS node config. Find the Node.js defaults at src/core/runtime/config-nodejs.jsand the browser defaults at src/core/runtime/config-browser.js. This object will be merged with the default config; it will not replace it.

  • libp2p (object) add custom modules to the libp2p stack of your node

    • peerDiscovery (object):

    • enabled (boolean): whether this module is enabled or disabled

    • [custom config] (any): other keys are specific to the module

    • [PeerDiscovery.tag] (object): configuration for a peer discovery module

    • transport (Array<libp2p.Transport>): An array of Libp2p transport classes/instances to use instead of the defaults. See libp2p/interface-transport for details.

    • peerDiscovery (Array<libp2p.PeerDiscovery>): An array of Libp2p peer discovery classes/instances to use insteadof the defaults. See libp2p/peer-discovery for details. If passing a class, configuration can be passed using the config section below under the key corresponding to you module's unique tag (a static property on the class)

    • modules (object):

    • config (object):

  • connectionManager (object): Configure the libp2p connection manager, see the documentation for available options.

Events

IPFS instances are Node.js EventEmitters. You can listen for events by calling node.on('event', handler):

const node = new IPFS({ repo: '/var/ipfs/data' })node.on('error', errorObject => console.error(errorObject))
  • error is always accompanied by an Error object with information about the error that ocurred.

    node.on('error', error => {  console.error(error.message)
    })
  • init is emitted after a new repo has been initialized. It will not be emitted if you set the init: false option on the constructor.

  • ready is emitted when a node is ready to use. This is the final event you will receive when creating a node (after init and start).

    When creating a new IPFS node, you should almost always wait for the ready event before calling methods or interacting with the node.

  • start is emitted when a node has started listening for connections. It will not be emitted if you set the start: falseoption on the constructor.

  • stop is emitted when a node has closed all connections and released access to its repo. This is usually the result of calling node.stop().

node.start([callback])

Start listening for connections with other IPFS nodes on the network. In most cases, you do not need to call this method — new IPFS() will automatically do it for you.

This method is asynchronous. There are several ways to be notified when the node has finished starting:

  1. If you call node.start() with no arguments, it returns a promise.

  2. If you pass a function as the final argument, it will be called when the node is started. (Note: this method will not return a promise if you use a callback function.)

  3. You can listen for the start event.

const node = new IPFS({ start: false })// Use a promise:node.start()
  .then(() => console.log('Node started!'))
  .catch(error => console.error('Node failed to start!', error))// OR use a callback:node.start(error => {  if (error) {    console.error('Node failed to start!', error)    return
  }  console.log('Node started!')
})// OR use events:node.on('error', error => console.error('Something went terribly wrong!', error))node.on('start', () => console.log('Node started!'))node.start()

node.stop([callback])

Close and stop listening for connections with other IPFS nodes, then release access to the node’s repo.

This method is asynchronous. There are several ways to be notified when the node has completely stopped:

  1. If you call node.stop() with no arguments, it returns a promise.

  2. If you pass a function as the final argument, it will be called when the node is stopped. (Note: this method will not return a promise if you use a callback function.)

  3. You can listen for the stop event.

const node = new IPFS()node.on('ready', () => {  console.log('Node is ready to use!')  // Stop with a promise:
  node.stop()
    .then(() => console.log('Node stopped!'))
    .catch(error => console.error('Node failed to stop cleanly!', error))  // OR use a callback:
  node.stop(error => {    if (error) {      console.error('Node failed to stop cleanly!', error)      return
    }    console.log('Node stopped!')
  })  // OR use events:
  node.on('error', error => console.error('Something went terribly wrong!', error))  node.stop()
})

Core APIThe IPFS core API provides all functionality that is not specific to setting up and starting or stopping a node. This API is available directly on an IPFS instance, on the command line (when using the CLI interface), and as an HTTP REST API. For a complete reference, see.

The core API is grouped into several areas:

Files

  • files

    • ipfs.files.cp([from, to], [callback])

    • ipfs.files.flush([path], [callback])

    • ipfs.files.ls([path], [options], [callback])

    • ipfs.files.mkdir(path, [options], [callback])

    • ipfs.files.mv([from, to], [callback])

    • ipfs.files.read(path, [options], [callback])

    • ipfs.files.readPullStream(path, [options])

    • ipfs.files.readReadableStream(path, [options])

    • ipfs.files.rm(path, [options], [callback])

    • ipfs.files.stat(path, [options], [callback])

    • ipfs.files.write(path, content, [options], [callback])

    • ipfs.files.add(data, [options], [callback]). Alias to ipfs.add.

    • ipfs.files.addPullStream([options])

    • ipfs.files.addReadableStream([options])

    • ipfs.files.cat(ipfsPath, [options], [callback]). Alias to ipfs.cat.

    • ipfs.files.catPullStream(ipfsPath, [options])

    • ipfs.files.catReadableStream(ipfsPath, [options])

    • ipfs.files.get(ipfsPath, [options], [callback]). Alias to ipfs.get.

    • ipfs.files.getPullStream(ipfsPath, [options])

    • ipfs.files.getReadableStream(ipfsPath, [options])

    • ipfs.ls(ipfsPath, [callback])

    • ipfs.lsPullStream(ipfsPath)

    • ipfs.lsReadableStream(ipfsPath)

    • MFS (mutable file system) specific

  • block

    • ipfs.block.get(cid, [options, callback])

    • ipfs.block.put(block, cid, [callback])

    • ipfs.block.stat(cid, [callback])

Graph

  • dag

    • ipfs.dag.put(dagNode, options, callback)

    • ipfs.dag.get(cid [, path, options], callback)

    • ipfs.dag.tree(cid [, path, options], callback)

  • object.

    • ipfs.object.new([template][, callback])

    • ipfs.object.put(obj, [options, callback])

    • ipfs.object.get(multihash, [options, callback])

    • ipfs.object.data(multihash, [options, callback])

    • ipfs.object.links(multihash, [options, callback])

    • ipfs.object.stat(multihash, [options, callback])

    • ipfs.object.patch.addLink(multihash, DAGLink, [options, callback])

    • ipfs.object.patch.rmLink(multihash, DAGLink, [options, callback])

    • ipfs.object.patch.appendData(multihash, data, [options, callback])

    • ipfs.object.patch.setData(multihash, data, [options, callback])

  • pin (not implemented, yet!)

Crypto and Key Management

  • key

    • ipfs.key.export(name, password, [callback])

    • ipfs.key.gen(name, options, [callback])

    • ipfs.key.import(name, pem, password, [callback])

    • ipfs.key.list([callback])

    • ipfs.key.rename(oldName, newName, [callback])

    • ipfs.key.rm(name, [callback])

  • crypto (not yet implemented)

Network

  • bootstrap

    • ipfs.bootstrap.list

    • ipfs.bootstrap.add

    • ipfs.bootstrap.rm

  • bitswap

    • ipfs.bitswap.wantlist()

    • ipfs.bitswap.stat()

    • ipfs.bitswap.unwant()

  • dht (not implemented, yet!)

  • pubsub

    • ipfs.pubsub.subscribe(topic, handler, options, callback)

    • ipfs.pubsub.unsubscribe(topic, handler, callback)

    • ipfs.pubsub.publish(topic, data, callback)

    • ipfs.pubsub.ls(topic, callback)

    • ipfs.pubsub.peers(topic, callback)

  • libp2p. Every IPFS instance also exposes the libp2p SPEC at ipfs.libp2p. The formal interface for this SPEC hasn't been defined by you can find documentation at its implementations:

    • Node.js bundle

    • Browser Bundle

    • libp2p baseclass

  • swarm

    • ipfs.swarm.addrs([callback])

    • ipfs.swarm.connect(addr, [callback])

    • ipfs.swarm.disconnect(addr, [callback])

    • ipfs.swarm.peers([opts] [, callback])

Node Management

  • miscellaneous operations

    • ipfs.id([callback])

    • ipfs.version([callback])

    • ipfs.ping()

    • ipfs.init([options], callback)

    • ipfs.start([callback])

    • ipfs.stop([callback])

    • ipfs.isOnline()

  • repo

    • ipfs.repo.init

    • ipfs.repo.stat([options, callback])

    • ipfs.repo.version([callback])

    • ipfs.repo.gc([options, callback]) (not implemented, yet!)

  • stats

    • ipfs.stats.bitswap([callback])

    • ipfs.stats.bw([options, callback])

    • ipfs.stats.bwPullStream([options]) -> Pull Stream

    • ipfs.stats.bwReadableStream([options]) -> Readable Stream

    • ipfs.stats.repo([options, callback])

  • config

    • ipfs.config.get([key, callback])

    • ipfs.config.set(key, value, [callback])

    • ipfs.config.replace(config, [callback])

Domain data types

A set of data types are exposed directly from the IPFS instance under ipfs.types. That way you're not required to import/require the following.

  • ipfs.types.Buffer

  • ipfs.types.PeerId

  • ipfs.types.PeerInfo

  • ipfs.types.multiaddr

  • ipfs.types.multibase

  • ipfs.types.multihash

  • ipfs.types.CID

  • ipfs.types.dagPB

  • ipfs.types.dagCBOR

Util

A set of utils are exposed directly from the IPFS instance under ipfs.util. That way you're not required to import/require the following:

  • ipfs.util.crypto

  • ipfs.util.isIPFS

FAQ

How to enable WebRTC support for js-ipfs in the Browser

To add a WebRTC transport to your js-ipfs node, you must add a WebRTC multiaddr. To do that, simple override the config.Addresses.Swarm array which contains all the multiaddrs which the IPFS node will use. See below:

const node = new IPFS({
  config: {
    Addresses: {
      Swarm: [        '/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star'
      ]
    }
  }
})node.on('ready', () => {  // your instance with WebRTC is ready})

Important: This transport usage is kind of unstable and several users have experienced crashes. Track development of a solution at https://github.com/ipfs/js-ipfs/issues/1088.

Is there WebRTC support for js-ipfs with Node.js?

Yes, however, bare in mind that there isn't a 100% stable solution to use WebRTC in Node.js, use it at your own risk. The most tested options are:

  • wrtc - Follow the install instructions.

  • electron-webrtc

To add WebRTC support in a IPFS node instance, do:

const wrtc = require('wrtc') // or require('electron-webrtc')()const WStar = require('libp2p-webrtc-star')const wstar = new WStar({ wrtc: wrtc })const node = new IPFS({
  repo: 'your-repo-path',  // start: false,
  config: {
    Addresses: {
      Swarm: [        "/ip4/0.0.0.0/tcp/4002",        "/ip4/127.0.0.1/tcp/4003/ws",        "/dns4/wrtc-star.discovery.libp2p.io/tcp/443/wss/p2p-webrtc-star"
      ]
    }
  },
  libp2p: {
    modules: {
      transport: [wstar],
      peerDiscovery: [wstar.discovery]
    }
  }
})node.on('ready', () => {  // your instance with WebRTC is ready})

To add WebRTC support to the IPFS daemon, you only need to install one of the WebRTC modules globally:

npm install wrtc --global# ornpm install electron-webrtc --global

Then, update your IPFS Daemon config to include the multiaddr for this new transport on the Addresses.Swarm array. Add: "/dns4/wrtc-star.discovery.libp2p.io/wss/p2p-webrtc-star"

How can I configure an IPFS node to use a custom signaling endpoint for my WebRTC transport?

You'll need to execute a compatible signaling server (libp2p-webrtc-star works) and include the correct configuration param for your IPFS node:

  • provide the multiaddr for the signaling server

const node = new IPFS({
  repo: 'your-repo-path',
  config: {
    Addresses: {
      Swarm: [        '/ip4/127.0.0.1/tcp/9090/ws/p2p-webrtc-star'
      ]
    }
  }
})

The code above assumes you are running a local signaling server on port 9090. Provide the correct values accordingly.

Is there a more stable alternative to webrtc-star that offers a similar functionality?

Yes, websocket-star! A WebSockets based transport that uses a Relay to route the messages. To enable it, just do:

const node = new IPFS({
  config: {
    Addresses: {
      Swarm: [        '/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
      ]
    }
  }
})node.on('ready', () => {  // your instance with websocket-star is ready})

I see some slowness when hopping between tabs Chrome with IPFS nodes, is there a reason why?

Yes, unfortunately, due to Chrome aggressive resource throttling policy, it cuts freezes the execution of any background tab, turning an IPFS node that was running on that webpage into a vegetable state.

A way to mitigate this in Chrome, is to run your IPFS node inside a Service Worker, so that the IPFS instance runs in a background process. You can learn how to install an IPFS node as a service worker in here the repo ipfs-service-worker

Can I use IPFS in my Electron App?

Yes you can and in many ways. Read https://github.com/ipfs/notes/issues/256 for the multiple options.

If your electron-rebuild step is failing, all you need to do is:

# Electron's version.export npm_config_target=2.0.0# The architecture of Electron, can be ia32 or x64.export npm_config_arch=x64export npm_config_target_arch=x64# Download headers for Electron.export npm_config_disturl=https://atom.io/download/electron# Tell node-pre-gyp that we are building for Electron.export npm_config_runtime=electron# Tell node-pre-gyp to build module from source code.export npm_config_build_from_source=true# Install all dependencies, and store cache to ~/.electron-gyp.HOME=~/.electron-gyp npm install

If you find any other issue, please check the Electron Support issue.

Have more questions?

Ask for help in our forum at https://discuss.ipfs.io or in IRC (#ipfs on Freenode).

Running js-ipfs with Docker

We have automatic Docker builds setup with Docker Hub: https://hub.docker.com/r/ipfs/js-ipfs/

All branches in the Github repository maps to a tag in Docker Hub, except master Git branch which is mapped to latestDocker tag.

You can run js-ipfs like this:

$ docker run -it -p 4002:4002 -p 4003:4003 -p 5002:5002 -p 9090:9090 ipfs/js-ipfs:latest

initializing ipfs node at /root/.jsipfs
generating 2048-bit RSA keypair...done
peer identity: Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
to get started, enter:

         jsipfs files cat /ipfs/QmfGBRT6BbWJd7yUc2uYdaUZJBbnEFvTqehPFoSMQ6wgdr/readme

Initializing daemon...
Using wrtc for webrtc support
Swarm listening on /ip4/127.0.0.1/tcp/4003/ws/ipfs/Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
Swarm listening on /ip4/172.17.0.2/tcp/4003/ws/ipfs/Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
Swarm listening on /ip4/127.0.0.1/tcp/4002/ipfs/Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
Swarm listening on /ip4/172.17.0.2/tcp/4002/ipfs/Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS
API is listening on: /ip4/0.0.0.0/tcp/5002
Gateway (readonly) is listening on: /ip4/0.0.0.0/tcp/9090
Daemon is ready

$ curl --silent localhost:5002/api/v0/id | jq .ID
"Qmbd5jx8YF1QLhvwfLbCTWXGyZLyEJHrPbtbpRESvYs4FS"

Packages

Listing of the main packages used in the IPFS ecosystem. There are also three specifications worth linking here:

  • interface-ipfs-core

  • http-api-spec

  • cli spec

     


Clone and install dependencies
Development

> git clone https://github.com/ipfs/js-ipfs.git> cd js-ipfs> npm install

Run unit tests

# run all the unit tsts> npm test# run just IPFS tests in Node.js> npm run test:node# run just IPFS core tests> npm run test:node:core# run just IPFS HTTP-API tests> npm run test:node:http# run just IPFS CLI tests> npm run test:node:cli# run just IPFS core tests in the Browser (Chrome)> npm run test:browser

Run interop tests

Run the interop tests with https://github.com/ipfs/interop

Run benchmark tests

# run all the benchmark tests> npm run benchmark# run just IPFS benchmarks in Node.js> npm run benchmark:node# run just IPFS benchmarks in Node.js for an IPFS instance> npm run benchmark:node:core# run just IPFS benchmarks in Node.js for an IPFS daemon> npm run benchmark:node:http# run just IPFS benchmarks in the browser (Chrome)> npm run benchmark:browser

Lint

Conforming to linting rules is a prerequisite to commit to js-ipfs.

> npm run lint

Build a dist version

> npm run build

Runtime Support

Code Architecture and folder Structure

Source code
> tree src -L 2
src                 # Main source code folder├── cli             # Implementation of the IPFS CLI│   └── ...
├── http-api        # The HTTP-API implementation of IPFS as defined by http-api-spec├── core            # IPFS implementation, the core (what gets loaded in browser)│   ├── components  # Each of IPFS subcomponent│   └── ...
└── ...

Monitoring

The HTTP API exposed with js-ipfs can also be used for exposing metrics about the running js-ipfs node and other Node.js metrics.

To enable it, you need to set the environment variable IPFS_MONITORING (any value)

Once the environment variable is set and the js-ipfs daemon is running, you can get the metrics (in prometheus format) by making a GET request to the following endpoint:

http://localhost:5002/debug/metrics/prometheus

IPFS Core Architecture

What does this image explain?

  • IPFS uses ipfs-repo which picks fs or indexeddb as its storage drivers, depending if it is running in Node.js or in the Browser.

  • The exchange protocol, bitswap, uses the Block Service which in turn uses the Repo, offering a get and put of blocks to the IPFS implementation.

  • The DAG API (previously Object) comes from the IPLD Resolver, it can support several IPLD Formats (i.e: dag-pb, dag-cbor, etc).

  • The Files API uses ipfs-unixfs-engine to import and export files to and from IPFS.

  • Swarm, the component that offers a network API, uses libp2p to dial and listen for connections, to use the DHT, for discovery mechanisms, and more. libp2p-ipfs-nodejs is used when running in Node.js and libp2p-ipfs-browser is used when running in the browser.

Contribute

IPFS implementation in JavaScript is a work in progress. As such, there's a few things you can do right now to help out:

  • Go through the modules below and check out existing issues. This would be especially useful for modules in active development. Some knowledge of IPFS may be required, as well as the infrastructure behind it - for instance, you may need to read up on p2p and more complex operations like muxing to be able to help technically.

  • Perform code reviews. More eyes will help (a) speed the project along, (b) ensure quality, and (c) reduce possible future bugs.

  • Take a look at go-ipfs and some of the planning repositories or issues: for instance, the libp2p spec. Contributions here that would be most helpful are top-level comments about how it should look based on our understanding. Again, the more eyes the better.

  • Add tests. There can never be enough tests.


 

ipfs原创,作者:IPFSforce,转载请注明出处:http://ipfser.org/2018/08/01/js-ipfs-v0-31-1/

0

扫一扫,分享到微信

猜你喜欢

文章评论

电子邮件地址不会被公开。 必填项已用*标注

获取验证码
后发表评论

微信公众号

知识星球