objects on ipfs made fun
Last updated 5 years ago by krlkrl .
MIT · Original npm · Tarball · package.json
$ cnpm install ipfs-obj 
SYNC missed versions from official npm registry.

Ipfs-obj Duck-typed Javascript Objects on IPFS

Docs for 4.0.0


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.


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


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.



Current Tags

  • 4.2.3                                ...           latest (5 years ago)

13 Versions

  • 4.2.3                                ...           5 years ago
  • 4.2.2                                ...           5 years ago
  • 4.2.1                                ...           5 years ago
  • 4.2.0                                ...           5 years ago
  • 4.0.0                                ...           5 years ago
  • 3.1.1                                ...           5 years ago
  • 3.1.0                                ...           5 years ago
  • 3.0.0                                ...           5 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (8)
Dev Dependencies (4)

Copyright 2014 - 2016 © taobao.org |