Skip to content

zcstarr/js-ipfs-repo

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

IPFS Repo JavaScript Implementation

standard-readme compliant Travis CI codecov Dependency Status js-standard-style

Implementation of the IPFS repo spec (https://github.com/ipfs/specs/tree/master/repo) in JavaScript

This is the implementation of the IPFS repo spec in JavaScript.

Lead Maintainer

Jacob Heun

Table of Contents

Background

Here is the architectural reasoning for this repo:

                          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                          β”‚                IPFSRepo                β”‚
                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                      β”‚        /        β”‚
                                      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                                      β”‚    Datastore    β”‚
                                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                          β”‚     /blocks     β”‚     β”‚   /datastore    β”‚
                          β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                          β”‚    Datastore    β”‚     β”‚ LevelDatastore  β”‚
                          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚       IPFSRepo - Default Node.js       β”‚          β”‚       IPFSRepo - Default Browser       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚        /        β”‚                                 β”‚        /        β”‚
            β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                                 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
            β”‚   FsDatastore   β”‚                                 β”‚LevelJSDatastore β”‚
            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     /blocks     β”‚     β”‚   /datastore    β”‚         β”‚     /blocks     β”‚     β”‚   /datastore    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€         β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ FlatfsDatastore β”‚     β”‚LevelDBDatastore β”‚         β”‚LevelJSDatastore β”‚     β”‚LevelJSDatastore β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

This provides a well defined interface for creating and interacting with an IPFS repo.

Install

npm

> npm install ipfs-repo

Use in Node.js

var IPFSRepo = require('ipfs-repo')

Use in a browser with browserify, webpack or any other bundler

var IPFSRepo = require('ipfs-repo')

Use in a browser Using a script tag

Loading this module through a script tag will make the IpfsRepo obj available in the global namespace.

<script src="https://unpkg.com/ipfs-repo/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/ipfs-repo/dist/index.js"></script>

Usage

Example:

const Repo = require('ipfs-repo')
const repo = new Repo('/tmp/ipfs-repo')

await repo.init({ cool: 'config' })
await repo.open()
console.log('repo is ready')

This now has created the following structure, either on disk or as an in memory representation:

β”œβ”€β”€ blocks
β”‚Β Β  β”œβ”€β”€ SHARDING
β”‚   └── _README
β”œβ”€β”€ config
β”œβ”€β”€ datastore
β”œβ”€β”€ keys
└── version

API

Setup

new Repo(path[, options])

Creates an IPFS Repo.

Arguments:

  • path (string, mandatory): the path for this repo
  • options (object, optional): may contain the following values
    • lock (Lock or string Deprecated): what type of lock to use. Lock has to be acquired when opening. string can be "fs" or "memory".
    • storageBackends (object, optional): may contain the following values, which should each be a class implementing the datastore interface:
      • root (defaults to datastore-fs in Node.js and datastore-level in the browser). Defines the back-end type used for gets and puts of values at the root (repo.set(), repo.get())
      • blocks (defaults to datastore-fs in Node.js and datastore-level in the browser). Defines the back-end type used for gets and puts of values at repo.blocks.
      • keys (defaults to datastore-fs in Node.js and datastore-level in the browser). Defines the back-end type used for gets and puts of encrypted keys at repo.keys
      • datastore (defaults to datastore-level). Defines the back-end type used as the key-valye store used for gets and puts of values at repo.datastore.
const repo = new Repo('path/to/repo')

Promise repo.init ()

Creates the necessary folder structure inside the repo.

Promise repo.open ()

Locks the repo to prevent conflicts arising from simultaneous access.

Promise repo.close ()

Unlocks the repo.

Promise<boolean> repo.exists ()

Tells whether this repo exists or not. Returns a boolean.

Repos

Root repo:

Promise repo.put (key, value:Buffer)

Put a value at the root of the repo.

  • key can be a buffer, a string or a Key.

Promise<value> repo.get (key)

Get a value at the root of the repo.

  • key can be a buffer, a string or a Key.

Blocks:

Promise repo.blocks.put (block:Block)

  • block should be of type Block.

Promise repo.blocks.putMany (blocks)

Put many blocks.

  • block should be an array of type Block.

Promise<Buffer> repo.blocks.get (cid)

Get block.

Datastore:

repo.datastore

This contains a full implementation of the interface-datastore API.

Utils

repo.config

Instead of using repo.set('config') this exposes an API that allows you to set and get a decoded config object, as well as, in a safe manner, change any of the config values individually.

Promise repo.config.set(key:string, value)

Set a config value. value can be any object that is serializable to JSON.

  • key is a string specifying the object path. Example:
await repo.config.set('a.b.c', 'c value')
const config = await repo.config.get()
assert.equal(config.a.b.c, 'c value')
Promise repo.config.set(value)

Set the whole config value. value can be any object that is serializable to JSON.

Promise<string> repo.config.get(key:string)

Get a config value. Returns the same type that was set before.

  • key is a string specifying the object path. Example:
const value = await repo.config.get('a.b.c')
console.log('config.a.b.c = ', value)
Promise<Object> repo.config.get()

Get the entire config value.

Promise<boolean> repo.config.exists()

Whether the config sub-repo exists.

repo.version

Promise<number> repo.version.get ()

Gets the repo version (an integer).

Promise repo.version.set (version:number)

Sets the repo version

repo.apiAddr

Promise<string> repo.apiAddr.get ()

Gets the API address.

Promise repo.apiAddr.set (value)

Sets the API address.

  • value should be a Multiaddr or a String representing a valid one.

Promise<Object> repo.stat ([options])

Gets the repo status.

options is an object which might contain the key human, which is a boolean indicating whether or not the repoSize should be displayed in MiB or not.

Returns an Object with the following keys:

  • numObjects
  • repoPath
  • repoSize
  • version
  • storageMax

Lock

IPFS Repo comes with two built in locks: memory and fs. These can be imported via the following:

const fsLock = require('ipfs-repo/src/lock')  // Default in Node.js
const memoryLock = require('ipfs-repo/src/lock-memory')  // Default in browser

You can also provide your own custom Lock. It must be an object with the following interface:

Promise lock.lock (dir)

Sets the lock if one does not already exist. If a lock already exists, should throw an error.

dir is a string to the directory the lock should be created at. The repo typically creates the lock at its root.

Returns closer, where closer has a close method for removing the lock.

Promise closer.close ()

Closes the lock created by lock.open

If no error was thrown, the lock was successfully removed.

Promise<boolean> lock.locked (dir)

Checks the existence of the lock.

dir is a string to the directory to check for the lock. The repo typically checks for the lock at its root.

Returns a boolean indicating the existence of the lock.

Notes

Contribute

There are some ways you can make this module better:

  • Consult our open issues and take on one of them
  • Help our tests reach 100% coverage!

This repository falls under the IPFS Code of Conduct.

License

MIT

About

Implementation of the IPFS Repo spec in JavaScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%