Skip to content

Commit

Permalink
Add configurable extension
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Apr 8, 2016
1 parent 77700c5 commit 582f513
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Repo (repoPath, options) {

// If options.stores is an abstract-blob-store instead of a map, use it for
// all stores.
if (options.stores.prototype && options.stores.prototype.createWriteSteam) {
if (options.stores.prototype && options.stores.prototype.createWriteStream) {
const store = options.stores
options.stores = {
keys: store,
Expand Down
36 changes: 26 additions & 10 deletions src/stores/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ const PREFIX_LENGTH = 8

exports = module.exports

function multihashToPath (multihash) {
const filename = multihash.toString('hex') + '.data'
function multihashToPath (multihash, extension) {
extension = extension || 'data'
const filename = `${multihash.toString('hex')}.${extension}`
const folder = filename.slice(0, PREFIX_LENGTH)
const path = folder + '/' + filename

Expand All @@ -16,21 +17,36 @@ exports.setUp = (basePath, blobStore, locks) => {
const store = blobStore(basePath + '/blocks')

return {
createReadStream: (multihash) => {
const path = multihashToPath(multihash)
createReadStream: (multihash, extension) => {
const path = multihashToPath(multihash, extension)
return store.createReadStream(path)
},

createWriteStream: (multihash, cb) => {
const path = multihashToPath(multihash)
createWriteStream: (multihash, extension, cb) => {
if (typeof extension === 'function') {
cb = extension
extension = undefined
}

const path = multihashToPath(multihash, extension)
return store.createWriteStream(path, cb)
},
exists: (multihash, cb) => {
const path = multihashToPath(multihash)
exists: (multihash, extension, cb) => {
if (typeof extension === 'function') {
cb = extension
extension = undefined
}

const path = multihashToPath(multihash, extension)
return store.exists(path, cb)
},
remove: (multihash, cb) => {
const path = multihashToPath(multihash)
remove: (multihash, extension, cb) => {
if (typeof extension === 'function') {
cb = extension
extension = undefined
}

const path = multihashToPath(multihash, extension)
return store.remove(path, cb)
}
}
Expand Down

0 comments on commit 582f513

Please sign in to comment.