Skip to content

Commit 582f513

Browse files
Add configurable extension
1 parent 77700c5 commit 582f513

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function Repo (repoPath, options) {
88

99
// If options.stores is an abstract-blob-store instead of a map, use it for
1010
// all stores.
11-
if (options.stores.prototype && options.stores.prototype.createWriteSteam) {
11+
if (options.stores.prototype && options.stores.prototype.createWriteStream) {
1212
const store = options.stores
1313
options.stores = {
1414
keys: store,

src/stores/datastore.js

+26-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const PREFIX_LENGTH = 8
44

55
exports = module.exports
66

7-
function multihashToPath (multihash) {
8-
const filename = multihash.toString('hex') + '.data'
7+
function multihashToPath (multihash, extension) {
8+
extension = extension || 'data'
9+
const filename = `${multihash.toString('hex')}.${extension}`
910
const folder = filename.slice(0, PREFIX_LENGTH)
1011
const path = folder + '/' + filename
1112

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

1819
return {
19-
createReadStream: (multihash) => {
20-
const path = multihashToPath(multihash)
20+
createReadStream: (multihash, extension) => {
21+
const path = multihashToPath(multihash, extension)
2122
return store.createReadStream(path)
2223
},
2324

24-
createWriteStream: (multihash, cb) => {
25-
const path = multihashToPath(multihash)
25+
createWriteStream: (multihash, extension, cb) => {
26+
if (typeof extension === 'function') {
27+
cb = extension
28+
extension = undefined
29+
}
30+
31+
const path = multihashToPath(multihash, extension)
2632
return store.createWriteStream(path, cb)
2733
},
28-
exists: (multihash, cb) => {
29-
const path = multihashToPath(multihash)
34+
exists: (multihash, extension, cb) => {
35+
if (typeof extension === 'function') {
36+
cb = extension
37+
extension = undefined
38+
}
39+
40+
const path = multihashToPath(multihash, extension)
3041
return store.exists(path, cb)
3142
},
32-
remove: (multihash, cb) => {
33-
const path = multihashToPath(multihash)
43+
remove: (multihash, extension, cb) => {
44+
if (typeof extension === 'function') {
45+
cb = extension
46+
extension = undefined
47+
}
48+
49+
const path = multihashToPath(multihash, extension)
3450
return store.remove(path, cb)
3551
}
3652
}

0 commit comments

Comments
 (0)