npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mock-ipfs-pinning-service

v0.4.2

Published

mock ipfs pinning service

Downloads

119

Readme

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"]