Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 25a5465

Browse files
committed
chore: handle adding data in same way as go
License: MIT Signed-off-by: achingbrain <[email protected]>
1 parent c4206cd commit 25a5465

20 files changed

+218
-173
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ Loading this module through a script tag will make the `mfs` obj available in th
5858
<script src="https://npmcdn.com/ipfs-mfs/dist/index.js"></script>
5959
```
6060

61+
### A note on concurrency
62+
63+
The mfs works by storing a reference to the root node's CID in LevelDB. LevelDB does not support concurrent access so there are read/write locks around bits of the code that modify the the root node's CID.
64+
65+
A lock is kept on the main thread and any requests to read/write from workers or the main thread itself are queued pending release of the lock by the existing holder.
66+
67+
Reads are executed together, writes are executed sequentially and prevent any reads from starting.
68+
69+
If you are using IPFS in a single process or with the node [cluster](https://nodejs.org/api/cluster.html) module this should be completely transparent.
70+
71+
If you are using [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API) there is no way to globally listen to messages sent between workers and the main thread so you will need to also use the [observable-webworkers](https://www.npmjs.com/package/observable-webworkers) module to ensure the right message transports are set up to allow requesting/releasing the locks.
72+
6173
## Contribute
6274

6375
All are welcome, please join in!

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"scripts": {
1010
"test": "cross-env aegir test -f test/browser.js",
1111
"test:node": "aegir test -t node",
12-
"test:browser": "aegir test -t browser -t webworker -f test/browser.js",
12+
"test:browser": "aegir test -t browser -f test/browser.js",
13+
"test:webworker": "aegir test -t webworker -f test/browser.js",
1314
"build": "aegir build",
1415
"lint": "aegir lint",
1516
"release": "aegir release",
@@ -41,8 +42,9 @@
4142
"aegir": "^13.1.0",
4243
"chai": "^4.1.2",
4344
"cross-env": "^5.1.4",
45+
"detect-webworker": "^1.0.0",
4446
"dirty-chai": "^2.0.1",
45-
"ipfs": "~0.28.2",
47+
"ipfs": "~0.29.0",
4648
"pre-commit": "^1.2.2",
4749
"pre-push": "~0.1.1",
4850
"safe-buffer": "^5.1.1",
@@ -63,7 +65,7 @@
6365
"ipfs-unixfs-engine": "~0.29.0",
6466
"is-pull-stream": "~0.0.0",
6567
"is-stream": "^1.1.0",
66-
"key_mutex": "achingbrain/key_mutex",
68+
"mortice": "^1.0.0",
6769
"promisify-es6": "^1.0.3",
6870
"pull-cat": "^1.1.11",
6971
"pull-paramap": "^1.2.2",

src/cli/cp.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,10 @@ module.exports = {
4141
hash
4242
} = argv
4343

44-
ipfs.files.cp(source, dest, {
44+
return ipfs.files.cp(source, dest, {
4545
parents,
4646
format,
4747
hashAlg: hash
48-
}, (error) => {
49-
if (error) {
50-
throw error
51-
}
5248
})
5349
}
5450
}

src/cli/flush.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ module.exports = {
1717
ipfs
1818
} = argv
1919

20-
ipfs.files.flush(path || FILE_SEPARATOR, {}, (error) => {
21-
if (error) {
22-
throw error
23-
}
24-
})
20+
return ipfs.files.flush(path || FILE_SEPARATOR, {})
2521
}
2622
}

src/cli/ls.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,45 @@ module.exports = {
3030
long
3131
} = argv
3232

33-
ipfs.files.ls(path || FILE_SEPARATOR, {
34-
long
35-
}, (error, files) => {
36-
if (error) {
37-
throw error
38-
}
39-
40-
if (long) {
41-
const table = []
42-
const lengths = {}
43-
44-
files.forEach(link => {
45-
const row = {
46-
name: `${link.name}`,
47-
hash: `${link.hash}`,
48-
size: `${link.size}`
49-
}
50-
51-
Object.keys(row).forEach(key => {
52-
const value = row[key]
53-
54-
lengths[key] = lengths[key] > value.length ? lengths[key] : value.length
33+
return ipfs.files.ls(path || FILE_SEPARATOR)
34+
.then(files => {
35+
if (long) {
36+
const table = []
37+
const lengths = {}
38+
39+
files.forEach(link => {
40+
const row = {
41+
name: `${link.name}`,
42+
hash: `${link.hash}`,
43+
size: `${link.size}`
44+
}
45+
46+
Object.keys(row).forEach(key => {
47+
const value = row[key]
48+
49+
lengths[key] = lengths[key] > value.length ? lengths[key] : value.length
50+
})
51+
52+
table.push(row)
5553
})
5654

57-
table.push(row)
58-
})
55+
table.forEach(row => {
56+
let line = ''
5957

60-
table.forEach(row => {
61-
let line = ''
58+
Object.keys(row).forEach(key => {
59+
const value = row[key]
6260

63-
Object.keys(row).forEach(key => {
64-
const value = row[key]
61+
line += value.padEnd(lengths[key])
62+
line += '\t'
63+
})
6564

66-
line += value.padEnd(lengths[key] - value.length)
67-
line += '\t'
65+
print(line)
6866
})
6967

70-
print(line)
71-
})
72-
73-
return
74-
}
68+
return
69+
}
7570

76-
files.forEach(link => print(link.name))
77-
})
71+
files.forEach(link => print(link.name))
72+
})
7873
}
7974
}

src/cli/mkdir.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const {
4-
print,
54
asBoolean
65
} = require('./utils')
76

@@ -45,17 +44,11 @@ module.exports = {
4544
flush
4645
} = argv
4746

48-
ipfs.files.mkdir(path, {
47+
return ipfs.files.mkdir(path, {
4948
parents,
5049
cidVersion,
5150
hash,
5251
flush
53-
}, (error, result) => {
54-
if (error) {
55-
throw error
56-
}
57-
58-
print(result)
5952
})
6053
}
6154
}

src/cli/mv.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,9 @@ module.exports = {
3535
recursive
3636
} = argv
3737

38-
ipfs.files.mv(source, dest, {
38+
return ipfs.files.mv(source, dest, {
3939
parents,
4040
recursive
41-
}, (error) => {
42-
if (error) {
43-
throw error
44-
}
4541
})
4642
}
4743
}

src/cli/read.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,28 @@ module.exports = {
3434
length
3535
} = argv
3636

37-
waterfall([
38-
(cb) => ipfs.files.readPullStream(path, {
39-
offset,
40-
length
41-
}, cb),
42-
(stream, cb) => {
43-
pull(
44-
stream,
45-
through(buffer => {
46-
print(buffer, false)
47-
}),
48-
collect(cb)
49-
)
50-
}
51-
], (error) => {
52-
if (error) {
53-
throw error
54-
}
37+
return new Promise((resolve, reject) => {
38+
waterfall([
39+
(cb) => ipfs.files.readPullStream(path, {
40+
offset,
41+
length
42+
}, cb),
43+
(stream, cb) => {
44+
pull(
45+
stream,
46+
through(buffer => {
47+
print(buffer, false)
48+
}),
49+
collect(cb)
50+
)
51+
}
52+
], (error) => {
53+
if (error) {
54+
return reject(error)
55+
}
56+
57+
resolve()
58+
})
5559
})
5660
}
5761
}

src/cli/rm.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ module.exports = {
2626
recursive
2727
} = argv
2828

29-
ipfs.files.rm(path, {
29+
return ipfs.files.rm(path, {
3030
recursive
31-
}, (error) => {
32-
if (error) {
33-
throw error
34-
}
3531
})
3632
}
3733
}

src/cli/stat.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

33
const {
4-
print,
5-
asBoolean
4+
asBoolean,
5+
print
66
} = require('./utils')
77

88
module.exports = {
@@ -54,28 +54,25 @@ Type: <type>`,
5454
withLocal
5555
} = argv
5656

57-
ipfs.files.stat(path, {
57+
return ipfs.files.stat(path, {
5858
withLocal
59-
}, (error, stats) => {
60-
if (error) {
61-
throw error
62-
}
63-
64-
if (hash) {
65-
return print(stats.hash)
66-
}
59+
})
60+
.then((stats) => {
61+
if (hash) {
62+
return print(stats.hash)
63+
}
6764

68-
if (size) {
69-
return print(stats.size)
70-
}
65+
if (size) {
66+
return print(stats.size)
67+
}
7168

72-
print(format
73-
.replace('<hash>', stats.hash)
74-
.replace('<size>', stats.size)
75-
.replace('<cumulsize>', stats.cumulativeSize)
76-
.replace('<childs>', stats.blocks)
77-
.replace('<type>', stats.type)
78-
)
79-
})
69+
print(format
70+
.replace('<hash>', stats.hash)
71+
.replace('<size>', stats.size)
72+
.replace('<cumulsize>', stats.cumulativeSize)
73+
.replace('<childs>', stats.blocks)
74+
.replace('<type>', stats.type)
75+
)
76+
})
8077
}
8178
}

0 commit comments

Comments
 (0)