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

ipfs-obj

v4.2.3

Published

objects on ipfs made fun

Downloads

29

Readme

Ipfs-obj Duck-typed Javascript Objects on IPFS

Docs for 4.0.0

motivation

This is a side-project that fell out of the frustration manually working with ipfs objects.

Instead of having to manually persist and restore your objects, ipfs-obj takes care of the heavy lifting, and lets you use javascript objects directly.

I made this to make constructing datastructures on top of ipfs a breeze.

example

object definition

module.exports = function (ipo) {

  var Duck = ipo.obj(function (loudness) {
    this.data = loudness
  })

  Duck.prototype.quack = function (cb) {
    if (this.data > 10) return cb(null, 'QUACK')
    cb(null, 'Quack')
  }

  return Duck
}

Using:


var ipfs = require('ipfs-api')('localhost', 5001)
var ipo = require('ipfs-obj')(ipfs)

var Duck = require('./duck.js')(ipo)

var ling = new Duck(2)

ling.quack(function (err, res) {
  console.log('duck says', res)
})

ling.persist(function (err, hash) {

  // we now have a hash representing the instance of our duckling

  ipo.fetch(hash, function (err, res) {

    // res is now the restored duckling
    // and we can call the same method on it...

    res.quack(function (err, res) {
      console.log('duck says', res)
    })
  })
})

Limitations, features

An ipfs object is expected to have certain properties, that you need to keep in mind.

The general structure is :

{ data: '<Any JSON-serializable value + other ipfs objects>',
  meta: '<generated metadata>' }

The data portion can be arbitrary Json, but also including other IPFS-objects, these will be packed up into links when the object is persisted, and appended to the data in the right place when restored.

The meta property gets appended to link metadata in other objects pointing to this. The perfect place to keep your monoids! And in general, metadata on your links you will want to look at without loading the child.

NB: ipfs-objects should be concidered immutable, and you should not be changing any of these fields after instatiating the object, otherwise undefined behavior awaits.

Methods on objects also have to be asynchronous.

Special methods

If you want ta use metadata on your objects, you need to provide a special initMeta method that returns an object, mapping metadata names to values, like so:

MonoidAdd.prototype.initMeta = function () {
  return {
    count: _.reduce(this.data.children, function (m, n) {
      return m + n.meta.count
    }, this.data.count)
  }
}

Memory model

The memory model is lazy, if you fetch an object (say a tree root) that refers to lots of other objects, only the first one will be loaded into memory, its links table will be populated by special Reference objects, when a method is called on a reference, this object is lazy-loaded into memory, in-place overwriting the ref, and executing the method on the newly instanciated object.

This way, you can have a huge set referenced, and find one element in it, without having to load the whole thing into memory.

License

MIT