Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Merge branch 'master' into feat/better-errors-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias authored May 4, 2018
2 parents d6f4a35 + 3019fc0 commit 48b07e4
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[*]
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
end_of_line = lf
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Let us know if you find any issue or if you want to contribute and add a new tut
- [js-ipfs in the browser with a `<script>` tag](./browser-script-tag)
- [js-ipfs in electron](./run-in-electron)
- [Using streams to add a directory of files to ipfs](./browser-add-readable-stream)
- [Customizing the ipfs repository](./custom-ipfs-repo)

## Understanding the IPFS Stack

Expand Down
32 changes: 32 additions & 0 deletions examples/custom-ipfs-repo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Customizing the IPFS Repo

This example shows you how to customize your repository, including where your data is stored and how the repo locking is managed. Customizing your repo makes it easier to extend IPFS for your particular needs. You may want to customize your repo if:

* If you want to store data somewhere that’s not on your local disk, like S3, a Redis instance, a different machine on your local network, or in your own database system, like MongoDB or Postgres, you might use a custom datastore.
* If you have multiple browser windows or workers sharing the same IPFS storage, you might want to use a custom lock to coordinate between them. (Locking is currently only used to ensure a single IPFS instance can access a repo at a time. This check is done on `repo.open()`. A more complex lock, coupled with a custom datastore, could allow for safe writes to a single datastore from multiple IPFS nodes.)

You can find full details on customization in the [IPFS Repo Docs](https://github.com/ipfs/js-ipfs-repo#setup).

## Run this example

```
> npm install
> npm start
```

## Other Options

### Custom `storageBackends`
This example leverages [datastore-fs](https://github.com/ipfs/js-datastore-fs) to store all data in the IPFS Repo. You can customize each of the 4 `storageBackends` to meet the needs of your project. For an example on how to manage your entire IPFS REPO on S3, you can see the [datastore-s3 example](https://github.com/ipfs/js-datastore-s3/tree/master/examples/full-s3-repo).

### Custom Repo Lock
This example uses one of the locks that comes with IPFS Repo. If you would like to control how locking happens, such as with a centralized S3 IPFS Repo, you can pass in your own custom lock. See [custom-lock.js](./custom-lock.js) for an example of a custom lock that can be used for [datastore-s3](https://github.com/ipfs/js-datastore-s3). This is also being used in the [full S3 example](https://github.com/ipfs/js-datastore-s3/tree/master/examples/full-s3-repo).

```js
const S3Lock = require('./custom-lock')

const repo = new Repo('/tmp/.ipfs', {
...
lock: new S3Lock(s3DatastoreInstance)
})
```
99 changes: 99 additions & 0 deletions examples/custom-ipfs-repo/custom-lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict'

const PATH = require('path')

/**
* Uses an object in an S3 bucket as a lock to signal that an IPFS repo is in use.
* When the object exists, the repo is in use. You would normally use this to make
* sure multiple IPFS nodes don’t use the same S3 bucket as a datastore at the same time.
*/
class S3Lock {
constructor (s3Datastore) {
this.s3 = s3Datastore
}

/**
* Returns the location of the lock file given the path it should be located at
*
* @private
* @param {string} dir
* @returns {string}
*/
getLockfilePath (dir) {
return PATH.join(dir, 'repo.lock')
}

/**
* Creates the lock. This can be overriden to customize where the lock should be created
*
* @param {string} dir
* @param {function(Error, LockCloser)} callback
* @returns {void}
*/
lock (dir, callback) {
const lockPath = this.getLockfilePath(dir)

this.locked(dir, (err, alreadyLocked) => {
if (err || alreadyLocked) {
return callback(new Error('The repo is already locked'))
}

// There's no lock yet, create one
this.s3.put(lockPath, Buffer.from(''), (err, data) => {
if (err) {
return callback(err, null)
}

callback(null, this.getCloser(lockPath))
})
})
}

/**
* Returns a LockCloser, which has a `close` method for removing the lock located at `lockPath`
*
* @param {string} lockPath
* @returns {LockCloser}
*/
getCloser (lockPath) {
return {
/**
* Removes the lock. This can be overriden to customize how the lock is removed. This
* is important for removing any created locks.
*
* @param {function(Error)} callback
* @returns {void}
*/
close: (callback) => {
this.s3.delete(lockPath, (err) => {
if (err && err.statusCode !== 404) {
return callback(err)
}

callback(null)
})
}
}
}

/**
* Calls back on whether or not a lock exists. Override this method to customize how the check is made.
*
* @param {string} dir
* @param {function(Error, boolean)} callback
* @returns {void}
*/
locked (dir, callback) {
this.s3.get(this.getLockfilePath(dir), (err, data) => {
if (err && err.message.match(/not found/)) {
return callback(null, false)
} else if (err) {
return callback(err)
}

callback(null, true)
})
}
}

module.exports = S3Lock
107 changes: 107 additions & 0 deletions examples/custom-ipfs-repo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict'

const IPFS = require('ipfs')
const Repo = require('ipfs-repo')
const fsLock = require('ipfs-repo/src/lock')

// Create our custom options
const customRepositoryOptions = {

/**
* IPFS nodes store different information in separate storageBackends, or datastores.
* Each storage backend can use the same type of datastore or a different one — you
* could store your keys in a levelDB database while everything else is in files,
* for example. (See https://github.com/ipfs/interface-datastore for more about datastores.)
*/
storageBackends: {
root: require('datastore-fs'), // version and config data will be saved here
blocks: require('datastore-fs'),
keys: require('datastore-fs'),
datastore: require('datastore-fs')
},

/**
* Storage Backend Options will get passed into the instantiation of their counterpart
* in `storageBackends`. If you create a custom datastore, this is where you can pass in
* custom constructor arguments. You can see an S3 datastore example at:
* https://github.com/ipfs/js-datastore-s3/tree/master/examples/full-s3-repo
*
* NOTE: The following options are being overriden for demonstration purposes only.
* In most instances you can simply use the default options, by not passing in any
* overrides, which is recommended if you have no need to override.
*/
storageBackendOptions: {
root: {
extension: '.ipfsroot', // Defaults to ''. Used by datastore-fs; Appended to all files
errorIfExists: false, // Used by datastore-fs; If the datastore exists, don't throw an error
createIfMissing: true // Used by datastore-fs; If the datastore doesn't exist yet, create it
},
blocks: {
sharding: false, // Used by IPFSRepo Blockstore to determine sharding; Ignored by datastore-fs
extension: '.ipfsblock', // Defaults to '.data'.
errorIfExists: false,
createIfMissing: true
},
keys: {
extension: '.ipfskey', // No extension by default
errorIfExists: false,
createIfMissing: true
},
datastore: {
extension: '.ipfsds', // No extension by default
errorIfExists: false,
createIfMissing: true
}
},

/**
* A custom lock can be added here. Or the build in Repo `fs` or `memory` locks can be used.
* See https://github.com/ipfs/js-ipfs-repo for more details on setting the lock.
*/
lock: fsLock
}

// Initialize our IPFS node with the custom repo options
const node = new IPFS({
repo: new Repo('/tmp/custom-repo/.ipfs', customRepositoryOptions)
})

// Test the new repo by adding and fetching some data
node.on('ready', () => {
console.log('Ready')
node.version()
.then((version) => {
console.log('Version:', version.version)
})
// Once we have the version, let's add a file to IPFS
.then(() => {
return node.files.add({
path: 'test-data.txt',
content: Buffer.from('We are using a customized repo!')
})
})
// Log out the added files metadata and cat the file from IPFS
.then((filesAdded) => {
console.log('\nAdded file:', filesAdded[0].path, filesAdded[0].hash)
return node.files.cat(filesAdded[0].hash)
})
// Print out the files contents to console
.then((data) => {
console.log('\nFetched file content:')
process.stdout.write(data)
})
// Log out the error, if there is one
.catch((err) => {
console.log('File Processing Error:', err)
})
// After everything is done, shut the node down
// We don't need to worry about catching errors here
.then(() => {
console.log('\n\nStopping the node')
return node.stop()
})
// Let users know where they can inspect the repo
.then(() => {
console.log('Check "/tmp/custom-repo/.ipfs" to see what your customized repository looks like on disk.')
})
})
16 changes: 16 additions & 0 deletions examples/custom-ipfs-repo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "custom-ipfs-repo",
"version": "0.1.0",
"description": "Customizing your ipfs repo",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"license": "MIT",
"dependencies": {
"datastore-fs": "~0.4.2",
"ipfs": "file:../../",
"ipfs-repo": "^0.20.0"
}
}
42 changes: 21 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"homepage": "https://github.com/ipfs/js-ipfs#readme",
"devDependencies": {
"aegir": "^13.0.6",
"aegir": "^13.1.0",
"buffer-loader": "0.0.1",
"chai": "^4.1.2",
"delay": "^2.0.0",
Expand All @@ -75,8 +75,8 @@
"hat": "0.0.3",
"interface-ipfs-core": "^0.61.0",
"ipfsd-ctl": "^0.32.1",
"lodash": "^4.17.5",
"mocha": "^5.0.5",
"lodash": "^4.17.10",
"mocha": "^5.1.1",
"ncp": "^2.0.0",
"nexpect": "^0.5.0",
"pre-commit": "^1.2.2",
Expand All @@ -97,7 +97,7 @@
"byteman": "^1.3.5",
"cids": "~0.5.3",
"debug": "^3.1.0",
"file-type": "^7.6.0",
"file-type": "^7.7.1",
"filesize": "^3.6.1",
"fsm-event": "^2.1.0",
"get-folder-size": "^1.0.1",
Expand All @@ -106,41 +106,41 @@
"hapi-set-header": "^1.0.2",
"hoek": "^5.0.3",
"human-to-milliseconds": "^1.0.0",
"ipfs-api": "^20.0.1",
"ipfs-api": "^20.2.0",
"ipfs-bitswap": "~0.20.0",
"ipfs-block": "^0.7.1",
"ipfs-block": "~0.7.1",
"ipfs-block-service": "~0.14.0",
"ipfs-multipart": "~0.1.0",
"ipfs-repo": "^0.19.0",
"ipfs-repo": "~0.20.0",
"ipfs-unixfs": "~0.1.14",
"ipfs-unixfs-engine": "~0.28.1",
"ipfs-unixfs-engine": "~0.29.0",
"ipld": "^0.17.0",
"is-ipfs": "^0.3.2",
"is-stream": "^1.1.0",
"joi": "^13.1.2",
"joi": "^13.2.0",
"joi-browser": "^13.0.1",
"joi-multiaddr": "^2.0.0",
"libp2p": "^0.20.2",
"libp2p": "^0.20.4",
"libp2p-circuit": "^0.2.0",
"libp2p-floodsub": "~0.15.0",
"libp2p-kad-dht": "~0.10.0",
"libp2p-keychain": "~0.3.1",
"libp2p-mdns": "~0.11.0",
"libp2p-mplex": "^0.7.0",
"libp2p-railing": "~0.8.0",
"libp2p-railing": "~0.8.1",
"libp2p-secio": "~0.10.0",
"libp2p-tcp": "^0.12.0",
"libp2p-webrtc-star": "0.14.0",
"libp2p-websocket-star": "^0.8.0",
"libp2p-websockets": "^0.11.0",
"libp2p-tcp": "~0.12.0",
"libp2p-webrtc-star": "~0.14.0",
"libp2p-websocket-star": "~0.8.0",
"libp2p-websockets": "~0.12.0",
"lodash.flatmap": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.sortby": "^4.7.0",
"lodash.values": "^4.3.0",
"mafmt": "^6.0.0",
"mime-types": "^2.1.18",
"mkdirp": "~0.5.1",
"multiaddr": "^4.0.0",
"multiaddr": "^5.0.0",
"multihashes": "~0.4.13",
"once": "^1.4.0",
"path-exists": "^3.0.0",
Expand All @@ -160,15 +160,15 @@
"pull-stream-to-stream": "^1.3.4",
"pull-zip": "^2.0.1",
"read-pkg-up": "^3.0.0",
"readable-stream": "2.3.5",
"safe-buffer": "^5.1.1",
"readable-stream": "2.3.6",
"safe-buffer": "^5.1.2",
"stream-to-pull-stream": "^1.7.2",
"tar-stream": "^1.5.5",
"tar-stream": "^1.6.0",
"temp": "~0.8.3",
"through2": "^2.0.3",
"update-notifier": "^2.4.0",
"update-notifier": "^2.5.0",
"yargs": "^11.0.0",
"yargs-parser": "^9.0.2"
"yargs-parser": "^10.0.0"
},
"optionalDependencies": {
"prom-client": "^11.0.0",
Expand Down
3 changes: 1 addition & 2 deletions test/core/create-node.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,7 @@ describe('create node', function () {
API: `/ip4/127.0.0.1/tcp/${5010 + _nodeNumber}`,
Gateway: `/ip4/127.0.0.1/tcp/${9090 + _nodeNumber}`,
Swarm: [
`/ip4/0.0.0.0/tcp/${4010 + _nodeNumber * 2}`,
`/ip4/127.0.0.1/tcp/${4011 + _nodeNumber * 2}/ws`
`/ip4/0.0.0.0/tcp/${4010 + _nodeNumber * 2}`
]
},
Bootstrap: []
Expand Down

0 comments on commit 48b07e4

Please sign in to comment.