-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bring @masylum PR and @diasdavid PR to be a PR from a ipfs/js-ipfs-repo branch to master #2
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a5f3f22
Initial implementation
masylum 5b9a4df
Added api and repo
masylum 993c46c
Implemented blocks; Added stream API; Started memory-repo
masylum c7e4b13
wip, adding tests, repo exists, repo load
daviddias e2e70bf
avoid needing to call each api level as a function
daviddias 8b68155
update tests
daviddias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,113 @@ | ||
# js-ipfs-repo | ||
Implementation of the IPFS repo spec (https://github.com/ipfs/specs/tree/master/repo) in JavaScript | ||
js-ipfs-repo | ||
============ | ||
|
||
|
||
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) | ||
|
||
> Implementation of the IPFS repo spec (https://github.com/ipfs/specs/tree/master/repo) in JavaScript | ||
|
||
## API | ||
|
||
### `Repo` | ||
|
||
Constructor, accepts a path and options: | ||
|
||
```js | ||
var Repo = require('js-ipfs-repo') | ||
var repo = new Repo('/Users/someone/.ipfs', {adaptor: 'fs'}) | ||
``` | ||
|
||
Options: | ||
|
||
- `adaptor`: String with the adaptor. Defaults to `fs` | ||
|
||
### `#version` | ||
|
||
Read/Write the version number of that repository. | ||
|
||
```js | ||
repo.version().read(function (err, num) { | ||
console.log(err, num) // => 2 | ||
}) | ||
|
||
repo.version().write(3, function (err) { | ||
console.log(err) | ||
}) | ||
``` | ||
|
||
### `#api` | ||
|
||
Read/Write the JSON configuration for that repository. | ||
|
||
```js | ||
repo.api().read(function (err, multiaddr) { | ||
console.log(err, multiaddr) | ||
}) | ||
|
||
repo.api().write('/ip4/127.0.0.1/tcp/5001', function (err) { | ||
console.log(err) | ||
}) | ||
``` | ||
|
||
### `#config` | ||
|
||
Read/Write the JSON configuration for that repository. | ||
|
||
```js | ||
repo.config().read(function (err, json) { | ||
console.log(err, json) | ||
}) | ||
|
||
repo.config().write({foo: 'bar'}, function (err) { | ||
console.log(err) | ||
}) | ||
``` | ||
|
||
### `#blocks` | ||
|
||
Store data on the block store. | ||
|
||
```js | ||
repo.blocks().read('12200007d4e3a319cd8c7c9979280e150fc5dbaae1ce54e790f84ae5fd3c3c1a0475', function (buff, err) { | ||
console.log(err) | ||
}) | ||
``` | ||
|
||
```js | ||
repo.blocks().write(buff, function (buff, err) { | ||
console.log(buff.toString('utf-8'), err) | ||
}) | ||
``` | ||
|
||
### `#repo` | ||
|
||
Read/Write the `repo.lock` file. | ||
|
||
```js | ||
repo.repo().read(function (err, content) { | ||
console.log(err, content) | ||
}) | ||
|
||
repo.repo().write('foo', function (err) { | ||
console.log(err) | ||
}) | ||
``` | ||
|
||
## Adaptors | ||
|
||
By default it will use the `fs-repo` adaptor. Eventually we can write other adaptors | ||
and make those available on configuration. | ||
|
||
### `fs-repo` | ||
|
||
The default adaptor. Uses the `repo.lock` file to ensure there are no simultaneous reads | ||
nor writes. Uses the `fs-blob-store`. | ||
|
||
### `memory-repo` | ||
|
||
Ideal for testing purposes. Uses the `abstract-blob-store`. | ||
|
||
## Tests | ||
|
||
Not there yet! Should ran both in node and in Phantom with compatible | ||
adaptors. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "js-ipfs-repo", | ||
"version": "0.0.1", | ||
"description": "IPFS Repo implementation", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "mocha tests/*-test.js", | ||
"coverage": "istanbul cover --print both -- _mocha tests/*-test.js", | ||
"lint": "standard" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/diasdavid/js-ipfs-repo.git" | ||
}, | ||
"keywords": [ | ||
"IPFS" | ||
], | ||
"pre-commit": [ | ||
"lint", | ||
"test" | ||
], | ||
"homepage": "https://github.com/ipfs/js-ipfs-record", | ||
"devDependencies": { | ||
"chai": "^3.4.1", | ||
"istanbul": "^0.4.1", | ||
"mocha": "^2.3.4", | ||
"ncp": "^2.0.0", | ||
"pre-commit": "^1.1.1", | ||
"rimraf": "^2.4.4", | ||
"standard": "^5.1.1" | ||
}, | ||
"dependencies": { | ||
"abstract-blob-store": "^3.2.0", | ||
"concat-stream": "^1.5.1", | ||
"fs-blob-store": "^5.2.0", | ||
"level-js": "^2.2.2", | ||
"lockfile": "^1.0.1", | ||
"multihashes": "^0.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
var concat = require('concat-stream') | ||
var fs = require('fs-blob-store') | ||
var path = require('path') | ||
var lockFile = require('lockfile') | ||
|
||
function BlobStore (base_path) { | ||
this.store = fs(base_path) | ||
this.LOCK_PATH = path.join(base_path, 'repo.lock') | ||
} | ||
|
||
BlobStore.prototype = { | ||
|
||
/** | ||
* Read a blob on a given path | ||
* It holds the repo.lock while reading | ||
* | ||
* @param {String} key | ||
* @param {Function} cb | ||
* @return {ReadableStream} | ||
*/ | ||
read: function (key, cb) { | ||
var store = this.store | ||
var LOCK_PATH = this.LOCK_PATH | ||
var rs = store.createReadStream(key) | ||
|
||
function onFinish (buff) { | ||
lockFile.unlock(LOCK_PATH, function (err) { | ||
if (err) return cb(err) | ||
|
||
cb(null, buff.toString('utf8')) | ||
}) | ||
} | ||
|
||
function onLock (err) { | ||
if (err) return cb(err) | ||
|
||
rs.on('error', cb) | ||
rs.pipe(concat(onFinish)) | ||
} | ||
|
||
lockFile.lock(LOCK_PATH, {}, onLock) | ||
|
||
return rs | ||
}, | ||
|
||
/** | ||
* Read a blob on a given path | ||
* It does not lock | ||
* | ||
* @param {String} key | ||
* @param {Function} cb | ||
* @return {ReadableStream} | ||
*/ | ||
readWithoutLock: function (key, cb) { | ||
var rs = this.store.createReadStream(key) | ||
|
||
rs.on('error', cb) | ||
rs.pipe(concat(function (buff) { | ||
cb(null, buff.toString('utf8')) | ||
})) | ||
|
||
return rs | ||
}, | ||
|
||
/** | ||
* Write the contents to the blob in the given path | ||
* It holds the repo.lock while reading | ||
* | ||
* @param {String} key | ||
* @param {Function} cb | ||
* @return {WritableStream} | ||
*/ | ||
write: function (key, content, cb) { | ||
var store = this.store | ||
var LOCK_PATH = this.LOCK_PATH | ||
var ws = store.createWriteStream(key) | ||
|
||
function onFinish (err) { | ||
if (err) return cb(err) | ||
|
||
lockFile.unlock(LOCK_PATH, cb) | ||
} | ||
|
||
function onLock (err) { | ||
if (err) return cb(err) | ||
|
||
ws.on('error', cb) | ||
ws.on('finish', onFinish) | ||
|
||
ws.write(content) | ||
ws.end() | ||
} | ||
|
||
lockFile.lock(LOCK_PATH, {}, onLock) | ||
|
||
return ws | ||
}, | ||
|
||
/** | ||
* Writes content to a blob on a given path | ||
* It does not lock | ||
* | ||
* @param {String} key | ||
* @param {String} content | ||
* @param {Function} cb | ||
* @return {WritableStream} | ||
*/ | ||
writeWithoutLock: function (key, content, cb) { | ||
var ws = this.store.createWriteStream(key) | ||
|
||
ws.on('error', cb) | ||
ws.on('finish', cb) | ||
|
||
ws.write(content) | ||
ws.end() | ||
|
||
return ws | ||
} | ||
} | ||
|
||
module.exports = BlobStore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
'fs-repo': require('./fs-repo'), | ||
'memory-repo': require('./memory-repo') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
var concat = require('concat-stream') | ||
var ms = require('abstract-blob-store') | ||
|
||
function BlobStore () { | ||
this.store = ms() | ||
} | ||
|
||
BlobStore.prototype = { | ||
|
||
/** | ||
* Read a blob on a given path | ||
* | ||
* @param {String} key | ||
* @param {Function} cb | ||
* @return {ReadableStream} | ||
*/ | ||
read: function (key, cb) { | ||
var store = this.store | ||
var rs = store.createReadStream(key) | ||
|
||
function onFinish (buff) { | ||
cb(null, buff.toString('utf8')) | ||
} | ||
|
||
rs.on('error', cb) | ||
rs.pipe(concat(onFinish)) | ||
|
||
return rs | ||
}, | ||
|
||
/** | ||
* Read a blob on a given path | ||
* | ||
* @param {String} key | ||
* @param {Function} cb | ||
* @return {ReadableStream} | ||
*/ | ||
readWithoutLock: function (key, cb) { | ||
return this.read(key, cb) | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is something that I'm not fully sure about. Should the locks be on the API? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we will want to use it directly, but won't hurt having them available/thought through :) |
||
|
||
/** | ||
* Write the contents to the blob in the given path | ||
* | ||
* @param {String} key | ||
* @param {Function} cb | ||
* @return {WritableStream} | ||
*/ | ||
write: function (key, content, cb) { | ||
var store = this.store | ||
var ws = store.createWriteStream(key) | ||
|
||
ws.on('error', cb) | ||
ws.on('finish', cb) | ||
|
||
ws.write(content) | ||
ws.end() | ||
|
||
return ws | ||
}, | ||
|
||
/** | ||
* Write the contents to the blob in the given path | ||
* | ||
* @param {String} key | ||
* @param {Function} cb | ||
* @return {WritableStream} | ||
*/ | ||
writeWithoutLock: function (key, cb) { | ||
return this.write(key, cb) | ||
} | ||
} | ||
|
||
module.exports = BlobStore |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any specific reason why
version()
has to be a func?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it was easier to develop but we can indeed have a better API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm "injecting" the store on each of those (https://github.com/ipfs/js-ipfs-repo/pull/2/files#diff-1fdf421c05c1140f6d71444ea2b27638R36). I can improve that I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added a
load
method, so that we can have a pre state where we can check if a repo exists, but without loading some of the variables to memory (things like version or config that can be read once), and also start the remaining stores, avoiding the need to have inject the 'store' for each call :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better. Any reason why is this not in the constructor?