Skip to content

Commit

Permalink
Merge pull request #23 from ipfs/es6ify
Browse files Browse the repository at this point in the history
es6ify and fix certain err conditions that were not being returned
  • Loading branch information
daviddias committed Jan 14, 2016
2 parents a87f6f1 + a5a7eac commit 3011a4e
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 118 deletions.
42 changes: 21 additions & 21 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var stores = require('./stores')
var extend = require('xtend')
var fs = require('fs-blob-store')
const stores = require('./stores')
const extend = require('xtend')
const fs = require('fs-blob-store')

exports = module.exports = Repo

function Repo (repoPath, options) {
var self = this
var base = {
const base = {
stores: {
keys: fs,
config: fs,
Expand All @@ -17,9 +16,10 @@ function Repo (repoPath, options) {
version: fs
}
}

options = extend(base, options)

self.init = function (config, callback) {
this.init = (config, callback) => {
if (this.exists()) {
throw new Error('Repo already exists')
}
Expand All @@ -29,40 +29,40 @@ function Repo (repoPath, options) {
// 2. init all of them
}

self.locks = require('./stores').locks.setUp(repoPath, options.stores.locks)
this.locks = require('./stores').locks.setUp(repoPath, options.stores.locks)

self.exists = function (callback) {
self.version.get(function (err, version) {
this.exists = callback => {
this.version.get((err, version) => {
if (err) {
return callback(new Error('Repo does not exist'))
}
callback(null, true)
})
}

self.version = stores
this.version = stores
.version
.setUp(repoPath, options.stores.version, self.locks)
.setUp(repoPath, options.stores.version, this.locks)

self.config = stores
this.config = stores
.config
.setUp(repoPath, options.stores.config, self.locks)
.setUp(repoPath, options.stores.config, this.locks)

self.keys = stores
this.keys = stores
.keys
.setUp(repoPath, options.stores.keys, self.locks, self.config)
.setUp(repoPath, options.stores.keys, this.locks, this.config)

self.datastore = stores
this.datastore = stores
.datastore
.setUp(repoPath, options.stores.datastore, self.locks)
.setUp(repoPath, options.stores.datastore, this.locks)

// TODO: needs https://github.com/ipfs/js-ipfs-repo/issues/6#issuecomment-164650642
// self.datastoreLegacy = stores
// this.datastoreLegacy = stores
// .datastore
// .setUp(repoPath, options.stores.datastore, self.locks)
// .setUp(repoPath, options.stores.datastore, this.locks)

// TODO: Currently this was also deprecated in go-ipfs
// self.logs = stores
// this.logs = stores
// .logs
// .setUp(repoPath, options.stores.logs, self.locks)
// .setUp(repoPath, options.stores.logs, this.locks)
}
12 changes: 6 additions & 6 deletions src/stores/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ var bl = require('bl')

exports = module.exports

exports.setUp = function (basePath, blobStore, locks) {
exports.setUp = (basePath, blobStore, locks) => {
var store = blobStore(basePath)
return {
get: function (callback) {
get: callback => {
store
.createReadStream('config')
.pipe(bl(function (err, config) {
.pipe(bl((err, config) => {
if (err) {
return callback(err)
}
Expand All @@ -22,14 +22,14 @@ exports.setUp = function (basePath, blobStore, locks) {
}))
},

set: function (config, callback) {
locks.lock(function (err) {
set: (config, callback) => {
locks.lock(err => {
if (err) {
return callback(err)
}

store.createWriteStream('config')
.on('finish', function () {
.on('finish', () => {
locks.unlock(callback)
})
.end(JSON.stringify(config))
Expand Down
7 changes: 4 additions & 3 deletions src/stores/datastore-legacy.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// TODO: This may end up being configurable
// TODO: This should belong to the `fs` implementation

var PREFIX_LENGTH = 8
var multihash = require('multihashes')
var path = require('path')

module.exports = function (store) {
module.exports = store => {
function hashToPath (hash) {
var folder = hash.slice(0, PREFIX_LENGTH)
return path.join(folder, hash) + '.data'
}

return {
read: function (hash, cb) {
read: (hash, cb) => {
return store.read(hashToPath(hash), cb)
},

write: function (buf, cb) {
write: (buf, cb) => {
var mh = multihash.encode(buf, 'hex')
return store.write(hashToPath(mh), buf, cb)
}
Expand Down
6 changes: 3 additions & 3 deletions src/stores/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ const PREFIX_LENGTH = 8

exports = module.exports

exports.setUp = function (basePath, blobStore, locks) {
exports.setUp = (basePath, blobStore, locks) => {
var store = blobStore(basePath + '/blocks/')

return {
createReadStream: function (multihash) {
createReadStream: multihash => {
var path = multihashToPath(multihash)
return store.createReadStream(path)
},

createWriteStream: function (multihash, cb) {
createWriteStream: (multihash, cb) => {
var path = multihashToPath(multihash)
return store.createWriteStream(path, cb)
}
Expand Down
6 changes: 3 additions & 3 deletions src/stores/keys.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
exports = module.exports

exports.setUp = function (basePath, blobStore, locks, config) {
exports.setUp = (basePath, blobStore, locks, config) => {
return {
get: function (callback) {
config.get(function (err, config) {
get: callback => {
config.get((err, config) => {
if (err) {
return callback(err)
}
Expand Down
18 changes: 9 additions & 9 deletions src/stores/locks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
exports = module.exports

exports.setUp = function (basePath, blobStore) {
exports.setUp = (basePath, blobStore) => {
var store = blobStore(basePath)
var lockFile = 'repo.lock'

Expand All @@ -25,20 +25,20 @@ exports.setUp = function (basePath, blobStore) {
function createLock () {
store
.createWriteStream(lockFile)
.on('finish', function () {
.on('finish', () => {
cb()
})
.end()
}
},
unlock: function (cb) {
store.remove(lockFile, function (err) {
if (err) cb(err)
store.exists(lockFile, function (err, exists) {
if (err) cb(err)
unlock: cb => {
store.remove(lockFile, err => {
if (err) { return cb(err) }
store.exists(lockFile, (err, exists) => {
if (err) { return cb(err) }

store.exists(lockFile, function (err, exists) {
if (err) cb(err)
store.exists(lockFile, (err, exists) => {
if (err) { return cb(err) }
cb()
})
})
Expand Down
13 changes: 7 additions & 6 deletions src/stores/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ var bl = require('bl')

exports = module.exports

exports.setUp = function (basePath, blobStore, locks) {
exports.setUp = (basePath, blobStore, locks) => {
var store = blobStore(basePath)

return {
get: function (callback) {
get: callback => {
store
.createReadStream('version')
.pipe(bl(function (err, version) {
.pipe(bl((err, version) => {
if (err) {
return callback(err)
}
callback(null, version.toString('utf8'))
}))
},
set: function (value, callback) {
locks.lock(function (err) {
set: (value, callback) => {
locks.lock(err => {
if (err) {
return callback(err)
}

store.createWriteStream('version')
.on('finish', function () {
.on('finish', () => {
locks.unlock(callback)
})
.end(value)
Expand Down
Loading

0 comments on commit 3011a4e

Please sign in to comment.