1
1
'use strict'
2
2
3
+ const Lock = require ( 'lock' )
4
+ const stream = require ( 'stream' )
5
+
3
6
const PREFIX_LENGTH = 8
4
7
5
8
exports = module . exports
@@ -15,6 +18,7 @@ function multihashToPath (multihash, extension) {
15
18
16
19
exports . setUp = ( basePath , blobStore , locks ) => {
17
20
const store = blobStore ( basePath + '/blocks' )
21
+ const lock = new Lock ( )
18
22
19
23
return {
20
24
createReadStream : ( multihash , extension ) => {
@@ -29,8 +33,16 @@ exports.setUp = (basePath, blobStore, locks) => {
29
33
}
30
34
31
35
const path = multihashToPath ( multihash , extension )
32
- return store . createWriteStream ( path , cb )
36
+ const through = stream . PassThrough ( )
37
+
38
+ lock ( path , ( release ) => {
39
+ const ws = store . createWriteStream ( path , release ( cb ) )
40
+ through . pipe ( ws )
41
+ } )
42
+
43
+ return through
33
44
} ,
45
+
34
46
exists : ( multihash , extension , cb ) => {
35
47
if ( typeof extension === 'function' ) {
36
48
cb = extension
@@ -40,6 +52,7 @@ exports.setUp = (basePath, blobStore, locks) => {
40
52
const path = multihashToPath ( multihash , extension )
41
53
return store . exists ( path , cb )
42
54
} ,
55
+
43
56
remove : ( multihash , extension , cb ) => {
44
57
if ( typeof extension === 'function' ) {
45
58
cb = extension
0 commit comments