• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ipfs-shipyard/js-mock-ipfs-pinning-service: Implementation of in-memory IPFS Pin ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

ipfs-shipyard/js-mock-ipfs-pinning-service

开源软件地址:

https://github.com/ipfs-shipyard/js-mock-ipfs-pinning-service

开源编程语言:

JavaScript 59.8%

开源软件介绍:

Mock IPFS Pinning Service

Implementation of in-memory IPFS Pinning Service API for testing purposes.

Install

npm i -D mock-ipfs-pinning-service @types/express

Usage

CLI Usage

You can start the mock server from the command line:

npx mock-ipfs-pinning-service --port 3000 --token secret

If token is not passed it will not preform authentification.

Code Usage

const { setup } = require('mock-ipfs-pinning-service')
const port = 3005

const main = async () => {
  /**
   * @type {import('express').Application}
   */
  const service = await setup({ token: 'secret' })
  const server = service.listen(port, () => {
    console.log(`server running on port ${port}`)
  })

  const cleanupEvents = ['beforeExit', 'SIGTERM', 'SIGINT', 'SIGHUP']

  // Express server cleanup handling.
  const cleanup = () => {
    // To prevent duplicated cleanup, remove the process listeners on cleanup
    cleanupEvents.forEach((event) => process.off(event, cleanup))
    server.close((err) => {
      if (err) {
        console.error(err)
        return
      }
      console.log(`server stopped listening on port ${port}`)
    })
  }
  // close the server when your process exits
  cleanupEvents.forEach((event) => process.on(event, cleanup))
}

State of the pins is exposed via service.locals.state which you can monkeypatch or replace. Each new request will be served based on that value. All requests perform that perform state updates do them in immutable style and swap the whole value, in other words references are guaranteed to not mutate.

You can see the status of the server and test individual functions from the SwaggerUI by navigating to http://localhost:${port}/docs/, switching Server to / and then entering access token under Authorize button.

Log levels

Pass:

  • --loglevel error to only print errors
  • --loglevel info to print each JSON request and response (default on CLI)
  • --loglevel debug to see low level OpenAPI validation details

Mocking specific PinStatus response

By default the mock service will respond with PinStatus.status="queued". This means ipfs pin remote add will hang forever, unless --background is passed, because it will wait for status to change to pinned or failed.

It is possible to overide this behavior per pin request by prefixing Pin.name with ${status}-, for example:

  • queued-test0PinStatus.status="queued"pin remote add hangs (needs --background)
  • pinning-test1PinStatus.status="pinning"pin remote add hangs (needs --background)
  • pinned-test2PinStatus.status="pinned"pin remote add responds instantly
  • failed-test3PinStatus.status="failed"pin remote add responds instantly

Debugging client in go-ipfs (ipfs pin remote)

One can use this mock service with client included in go-ipfs to debug its behavior:

// start mock service
$ npx mock-ipfs-pinning-service --port 5000 --token secret --loglevel info

// then in other console
$ ipfs pin remote service add mock "http://127.0.0.1:5000" secret
$ ipfs pin remote service ls --stat
mock http://127.0.0.1:5000 0/0/0/1

The first console will show what happened on the wire:

Request: GET /pins?limit=1&status=queued headers[host=127.0.0.1:5000;user-agent=go-pinning-service-http-client;accept=application/json;authorization=Bearer secret;accept-encoding=gzip] at Fri Apr 23 2021 19:58:49 GMT+0200, IP: ::ffff:127.0.0.1, User Agent: go-pinning-service-http-client
Response Body:
{
	"count": 0,
	"results": []
}
Response: 200 3.183 ms  headers[x-powered-by=Express;access-control-allow-origin=*;content-type=application/json; charset=utf-8;content-length=24;etag=W/"18-sS5FLbfK694W6H4gsKxYsIoy1Pk"]



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
jonasbostoen/nodejs-ipfs-app发布时间:2022-06-22
下一篇:
vigzmv/IPFS-CRDT-shared-editing: Decentralized Real-Time Collaborative Documents ...发布时间:2022-06-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap