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

Commit 82079db

Browse files
committed
feat: convert to async/await
BREAKING CHANGES: 1. Everything is now async/await 2. No more callbacks, Readable Streams or Pull Streams 3. `stat` and `ls` commands return `cid` objects instead of string hashes 4. `stat` and `ls` commands return all fields, `hash`, `long` etc options are now ignored
1 parent 25bf86b commit 82079db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2096
-3150
lines changed

package.json

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"release": "aegir release",
1919
"release-minor": "aegir release --type minor",
2020
"release-major": "aegir release --type major",
21-
"coverage": "aegir coverage"
21+
"coverage": "aegir coverage",
22+
"dep-check": "aegir dep-check"
2223
},
2324
"repository": {
2425
"type": "git",
@@ -42,37 +43,30 @@
4243
"detect-node": "^2.0.4",
4344
"detect-webworker": "^1.0.0",
4445
"dirty-chai": "^2.0.1",
45-
"ipld": "~0.21.1",
46-
"ipld-in-memory": "^2.0.0",
47-
"multihashes": "~0.4.14",
48-
"pull-buffer-stream": "^1.0.1",
49-
"pull-traverse": "^1.0.3",
46+
"ipfs-block-service": "~0.15.2",
47+
"ipfs-repo": "~0.26.4",
48+
"ipld": "~0.22.0",
49+
"memdown": "^4.0.0",
5050
"temp-write": "^3.4.0"
5151
},
5252
"dependencies": {
53-
"async": "^2.6.1",
5453
"cids": "~0.5.5",
5554
"debug": "^4.1.0",
56-
"filereader-stream": "^2.0.0",
55+
"err-code": "^1.1.2",
5756
"hamt-sharding": "~0.0.2",
5857
"interface-datastore": "~0.6.0",
5958
"ipfs-multipart": "~0.1.0",
6059
"ipfs-unixfs": "~0.1.16",
6160
"ipfs-unixfs-exporter": "~0.36.1",
6261
"ipfs-unixfs-importer": "~0.38.5",
6362
"ipld-dag-pb": "~0.15.2",
64-
"is-pull-stream": "~0.0.0",
65-
"is-stream": "^1.1.0",
6663
"joi": "^14.3.0",
6764
"joi-browser": "^13.4.0",
6865
"mortice": "^1.2.1",
66+
"multicodec": "~0.5.0",
67+
"multihashes": "~0.4.14",
6968
"once": "^1.4.0",
70-
"promisify-es6": "^1.0.3",
71-
"pull-cat": "^1.1.11",
72-
"pull-defer": "~0.2.3",
73-
"pull-stream": "^3.6.9",
74-
"pull-stream-to-stream": "^1.3.4",
75-
"stream-to-pull-stream": "^1.7.2"
69+
"promisify-es6": "^1.0.3"
7670
},
7771
"contributors": [
7872
"Alan Shaw <[email protected]>",

src/cli/flush.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const {
44
FILE_SEPARATOR
5-
} = require('../core/utils')
5+
} = require('../core/utils/constants')
66

77
module.exports = {
88
command: 'flush [path]',

src/cli/ls.js

Lines changed: 27 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
'use strict'
22

3-
const pull = require('pull-stream/pull')
4-
const onEnd = require('pull-stream/sinks/on-end')
5-
const through = require('pull-stream/throughs/through')
63
const {
74
print,
8-
asBoolean
5+
asBoolean,
6+
formatCid
97
} = require('./utils')
108
const {
119
FILE_SEPARATOR
12-
} = require('../core/utils')
10+
} = require('../core/utils/constants')
1311

1412
module.exports = {
1513
command: 'ls [path]',
@@ -48,57 +46,36 @@ module.exports = {
4846

4947
argv.resolve((async () => {
5048
const ipfs = await getIpfs()
51-
return new Promise((resolve, reject) => {
52-
if (sort) {
53-
ipfs.files.ls(path || FILE_SEPARATOR, {
54-
long,
55-
sort,
56-
cidBase
57-
})
58-
.then(files => {
59-
// https://github.com/ipfs/go-ipfs/issues/5181
60-
if (sort) {
61-
files = files.sort((a, b) => {
62-
return a.name.localeCompare(b.name)
63-
})
64-
}
65-
66-
if (long) {
67-
files.forEach(link => {
68-
print(`${link.name}\t${link.hash}\t${link.size}`)
69-
})
70-
} else {
71-
files.forEach(link => print(link.name))
72-
}
7349

74-
resolve()
75-
})
76-
.catch(reject)
50+
if (sort) {
51+
const files = []
7752

78-
return
53+
for await (const file of ipfs.files.ls(path || FILE_SEPARATOR)) {
54+
files.push(file)
7955
}
8056

81-
pull(
82-
ipfs.files.lsPullStream(path, {
83-
long,
84-
cidBase
85-
}),
86-
through(file => {
87-
if (long) {
88-
print(`${file.name}\t${file.hash}\t${file.size}`)
89-
} else {
90-
print(file.name)
91-
}
92-
}),
93-
onEnd((error) => {
94-
if (error) {
95-
return reject(error)
96-
}
57+
if (sort) {
58+
files = files.sort((a, b) => {
59+
return a.name.localeCompare(b.name)
60+
})
61+
}
9762

98-
resolve()
63+
if (long) {
64+
files.forEach(link => {
65+
print(`${link.name}\t${formatCid(link.cid, cidBase)}\t${link.size}`)
9966
})
100-
)
101-
})
67+
} else {
68+
files.forEach(link => print(link.name))
69+
}
70+
}
71+
72+
for await (const file of ipfs.files.ls(path || FILE_SEPARATOR)) {
73+
if (long) {
74+
print(`${file.name}\t${formatCid(link.cid, cidBase)}\t${file.size}`)
75+
} else {
76+
print(file.name)
77+
}
78+
}
10279
})())
10380
}
10481
}

src/cli/read.js

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

3-
const pull = require('pull-stream/pull')
4-
const through = require('pull-stream/throughs/through')
5-
const onEnd = require('pull-stream/sinks/on-end')
63
const {
74
print
85
} = require('./utils')
@@ -16,12 +13,12 @@ module.exports = {
1613
offset: {
1714
alias: 'o',
1815
type: 'number',
19-
describe: 'Start writing at this offset'
16+
describe: 'Start reading at this offset'
2017
},
2118
length: {
2219
alias: 'l',
2320
type: 'number',
24-
describe: 'Write only this number of bytes'
21+
describe: 'Read only this number of bytes'
2522
}
2623
},
2724

@@ -36,24 +33,12 @@ module.exports = {
3633
argv.resolve((async () => {
3734
const ipfs = await getIpfs()
3835

39-
return new Promise((resolve, reject) => {
40-
pull(
41-
ipfs.files.readPullStream(path, {
42-
offset,
43-
length
44-
}),
45-
through(buffer => {
46-
print(buffer, false)
47-
}),
48-
onEnd((error) => {
49-
if (error) {
50-
return reject(error)
51-
}
52-
53-
resolve()
54-
})
55-
)
56-
})
36+
for await (const buf of ipfs.files.read(path, {
37+
offset,
38+
length
39+
})) {
40+
print(buf)
41+
}
5742
})())
5843
}
5944
}

src/cli/stat.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const {
44
asBoolean,
5-
print
5+
print,
6+
formatCid
67
} = require('./utils')
78

89
module.exports = {
@@ -55,7 +56,8 @@ Type: <type>`,
5556
format,
5657
hash,
5758
size,
58-
withLocal
59+
withLocal,
60+
cidBase
5961
} = argv
6062

6163
argv.resolve((async () => {
@@ -66,15 +68,15 @@ Type: <type>`,
6668
})
6769
.then((stats) => {
6870
if (hash) {
69-
return print(stats.hash)
71+
return print(formatCid(stats.cid, cidBase))
7072
}
7173

7274
if (size) {
7375
return print(stats.size)
7476
}
7577

7678
print(format
77-
.replace('<hash>', stats.hash)
79+
.replace('<hash>', formatCid(stats.cid, cidBase))
7880
.replace('<size>', stats.size)
7981
.replace('<cumulsize>', stats.cumulativeSize)
8082
.replace('<childs>', stats.blocks)

src/cli/utils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,17 @@ const asBoolean = (value) => {
3131
return false
3232
}
3333

34+
const formatCid = (cid, base) => {
35+
if (base === 'base58btc') {
36+
return cid.toBaseEncodedString(base)
37+
}
38+
39+
return cid.toV1().toBaseEncodedString(base)
40+
}
41+
3442
module.exports = {
3543
disablePrinting,
3644
print,
37-
asBoolean
45+
asBoolean,
46+
formatCid
3847
}

0 commit comments

Comments
 (0)