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

Commit

Permalink
feat: convert to async/await
Browse files Browse the repository at this point in the history
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
  • Loading branch information
achingbrain committed Apr 16, 2019
1 parent 25bf86b commit 82079db
Show file tree
Hide file tree
Showing 67 changed files with 2,096 additions and 3,150 deletions.
26 changes: 10 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"release": "aegir release",
"release-minor": "aegir release --type minor",
"release-major": "aegir release --type major",
"coverage": "aegir coverage"
"coverage": "aegir coverage",
"dep-check": "aegir dep-check"
},
"repository": {
"type": "git",
Expand All @@ -42,37 +43,30 @@
"detect-node": "^2.0.4",
"detect-webworker": "^1.0.0",
"dirty-chai": "^2.0.1",
"ipld": "~0.21.1",
"ipld-in-memory": "^2.0.0",
"multihashes": "~0.4.14",
"pull-buffer-stream": "^1.0.1",
"pull-traverse": "^1.0.3",
"ipfs-block-service": "~0.15.2",
"ipfs-repo": "~0.26.4",
"ipld": "~0.22.0",
"memdown": "^4.0.0",
"temp-write": "^3.4.0"
},
"dependencies": {
"async": "^2.6.1",
"cids": "~0.5.5",
"debug": "^4.1.0",
"filereader-stream": "^2.0.0",
"err-code": "^1.1.2",
"hamt-sharding": "~0.0.2",
"interface-datastore": "~0.6.0",
"ipfs-multipart": "~0.1.0",
"ipfs-unixfs": "~0.1.16",
"ipfs-unixfs-exporter": "~0.36.1",
"ipfs-unixfs-importer": "~0.38.5",
"ipld-dag-pb": "~0.15.2",
"is-pull-stream": "~0.0.0",
"is-stream": "^1.1.0",
"joi": "^14.3.0",
"joi-browser": "^13.4.0",
"mortice": "^1.2.1",
"multicodec": "~0.5.0",
"multihashes": "~0.4.14",
"once": "^1.4.0",
"promisify-es6": "^1.0.3",
"pull-cat": "^1.1.11",
"pull-defer": "~0.2.3",
"pull-stream": "^3.6.9",
"pull-stream-to-stream": "^1.3.4",
"stream-to-pull-stream": "^1.7.2"
"promisify-es6": "^1.0.3"
},
"contributors": [
"Alan Shaw <alan.shaw@protocol.ai>",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/flush.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const {
FILE_SEPARATOR
} = require('../core/utils')
} = require('../core/utils/constants')

module.exports = {
command: 'flush [path]',
Expand Down
77 changes: 27 additions & 50 deletions src/cli/ls.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
'use strict'

const pull = require('pull-stream/pull')
const onEnd = require('pull-stream/sinks/on-end')
const through = require('pull-stream/throughs/through')
const {
print,
asBoolean
asBoolean,
formatCid
} = require('./utils')
const {
FILE_SEPARATOR
} = require('../core/utils')
} = require('../core/utils/constants')

module.exports = {
command: 'ls [path]',
Expand Down Expand Up @@ -48,57 +46,36 @@ module.exports = {

argv.resolve((async () => {
const ipfs = await getIpfs()
return new Promise((resolve, reject) => {
if (sort) {
ipfs.files.ls(path || FILE_SEPARATOR, {
long,
sort,
cidBase
})
.then(files => {
// https://github.com/ipfs/go-ipfs/issues/5181
if (sort) {
files = files.sort((a, b) => {
return a.name.localeCompare(b.name)
})
}

if (long) {
files.forEach(link => {
print(`${link.name}\t${link.hash}\t${link.size}`)
})
} else {
files.forEach(link => print(link.name))
}

resolve()
})
.catch(reject)
if (sort) {
const files = []

return
for await (const file of ipfs.files.ls(path || FILE_SEPARATOR)) {
files.push(file)
}

pull(
ipfs.files.lsPullStream(path, {
long,
cidBase
}),
through(file => {
if (long) {
print(`${file.name}\t${file.hash}\t${file.size}`)
} else {
print(file.name)
}
}),
onEnd((error) => {
if (error) {
return reject(error)
}
if (sort) {
files = files.sort((a, b) => {
return a.name.localeCompare(b.name)
})
}

resolve()
if (long) {
files.forEach(link => {
print(`${link.name}\t${formatCid(link.cid, cidBase)}\t${link.size}`)
})
)
})
} else {
files.forEach(link => print(link.name))
}
}

for await (const file of ipfs.files.ls(path || FILE_SEPARATOR)) {
if (long) {
print(`${file.name}\t${formatCid(link.cid, cidBase)}\t${file.size}`)
} else {
print(file.name)
}
}
})())
}
}
31 changes: 8 additions & 23 deletions src/cli/read.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict'

const pull = require('pull-stream/pull')
const through = require('pull-stream/throughs/through')
const onEnd = require('pull-stream/sinks/on-end')
const {
print
} = require('./utils')
Expand All @@ -16,12 +13,12 @@ module.exports = {
offset: {
alias: 'o',
type: 'number',
describe: 'Start writing at this offset'
describe: 'Start reading at this offset'
},
length: {
alias: 'l',
type: 'number',
describe: 'Write only this number of bytes'
describe: 'Read only this number of bytes'
}
},

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

return new Promise((resolve, reject) => {
pull(
ipfs.files.readPullStream(path, {
offset,
length
}),
through(buffer => {
print(buffer, false)
}),
onEnd((error) => {
if (error) {
return reject(error)
}

resolve()
})
)
})
for await (const buf of ipfs.files.read(path, {
offset,
length
})) {
print(buf)
}
})())
}
}
10 changes: 6 additions & 4 deletions src/cli/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const {
asBoolean,
print
print,
formatCid
} = require('./utils')

module.exports = {
Expand Down Expand Up @@ -55,7 +56,8 @@ Type: <type>`,
format,
hash,
size,
withLocal
withLocal,
cidBase
} = argv

argv.resolve((async () => {
Expand All @@ -66,15 +68,15 @@ Type: <type>`,
})
.then((stats) => {
if (hash) {
return print(stats.hash)
return print(formatCid(stats.cid, cidBase))
}

if (size) {
return print(stats.size)
}

print(format
.replace('<hash>', stats.hash)
.replace('<hash>', formatCid(stats.cid, cidBase))
.replace('<size>', stats.size)
.replace('<cumulsize>', stats.cumulativeSize)
.replace('<childs>', stats.blocks)
Expand Down
11 changes: 10 additions & 1 deletion src/cli/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ const asBoolean = (value) => {
return false
}

const formatCid = (cid, base) => {
if (base === 'base58btc') {
return cid.toBaseEncodedString(base)
}

return cid.toV1().toBaseEncodedString(base)
}

module.exports = {
disablePrinting,
print,
asBoolean
asBoolean,
formatCid
}
Loading

0 comments on commit 82079db

Please sign in to comment.