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

Commit 250aed4

Browse files
committed
clean up source
1 parent d0485ef commit 250aed4

File tree

2 files changed

+45
-61
lines changed

2 files changed

+45
-61
lines changed

src/exporter.js

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
22

33
const debug = require('debug')
4-
const log = debug('exporter')
5-
log.err = debug('exporter:error')
4+
const log = debug('unixfs')
5+
log.err = debug('unixfs:error')
66
const isIPFS = require('is-ipfs')
7-
const bs58 = require('bs58')
87
const UnixFS = require('ipfs-unixfs')
98
const series = require('run-series')
109
const Readable = require('readable-stream').Readable
@@ -21,13 +20,10 @@ function Exporter (hash, dagService, options) {
2120
return new Exporter(hash, dagService, options)
2221
}
2322

24-
// Sanitize hash.
23+
// Sanitize hash
2524
if (!isIPFS.multihash(hash)) {
2625
throw new Error('not valid multihash')
2726
}
28-
if (Buffer.isBuffer(hash)) {
29-
hash = bs58.encode(hash)
30-
}
3127

3228
Readable.call(this, { objectMode: true })
3329

@@ -36,61 +32,52 @@ function Exporter (hash, dagService, options) {
3632
this._read = (n) => {}
3733

3834
let fileExporter = (node, name, done) => {
39-
let init = false
35+
if (!done) {
36+
throw new Error('done must be set')
37+
}
4038

41-
if (!done) throw new Error('done must be set')
39+
const contentRS = new Readable()
40+
contentRS._read = () => {}
4241

4342
// Logic to export a single (possibly chunked) unixfs file.
44-
var rs = new Readable()
4543
if (node.links.length === 0) {
4644
const unmarshaledData = UnixFS.unmarshal(node.data)
47-
rs._read = () => {
48-
if (init) {
49-
return
50-
}
51-
init = true
52-
rs.push(unmarshaledData.data)
53-
rs.push(null)
54-
}
55-
this.push({ content: rs, path: name })
45+
contentRS.push(unmarshaledData.data)
46+
contentRS.push(null)
47+
this.push({ content: contentRS, path: name })
5648
done()
5749
} else {
58-
rs._read = () => {
59-
if (init) {
60-
return
50+
const array = node.links.map((link) => {
51+
return (cb) => {
52+
dagService.get(link.hash, (err, res) => {
53+
if (err) {
54+
return cb(err)
55+
}
56+
var unmarshaledData = UnixFS.unmarshal(res.data)
57+
contentRS.push(unmarshaledData.data)
58+
cb()
59+
})
6160
}
62-
init = true
63-
64-
const array = node.links.map((link) => {
65-
return (cb) => {
66-
dagService.get(link.hash, (err, res) => {
67-
if (err) {
68-
return cb(err)
69-
}
70-
var unmarshaledData = UnixFS.unmarshal(res.data)
71-
rs.push(unmarshaledData.data)
72-
cb()
73-
})
74-
}
75-
})
76-
series(array, (err, res) => {
77-
if (err) {
78-
rs.emit('error', err)
79-
return
80-
}
81-
rs.push(null)
82-
return
83-
})
84-
}
85-
this.push({ content: rs, path: name })
61+
})
62+
series(array, (err) => {
63+
if (err) {
64+
return contentRS.emit('error', err)
65+
}
66+
contentRS.push(null)
67+
})
68+
this.push({ content: contentRS, path: name })
8669
done()
8770
}
8871
}
8972

9073
// Logic to export a unixfs directory.
9174
let dirExporter = (node, name, add, done) => {
92-
if (!add) throw new Error('add must be set')
93-
if (!done) throw new Error('done must be set')
75+
if (!add) {
76+
throw new Error('add must be set')
77+
}
78+
if (!done) {
79+
throw new Error('done must be set')
80+
}
9481

9582
this.push({content: null, path: name})
9683

@@ -104,32 +91,29 @@ function Exporter (hash, dagService, options) {
10491
}
10592

10693
// Traverse the DAG asynchronously
107-
var self = this
108-
fieldtrip([{ path: hash, hash: hash }], visit, (err) => {
94+
fieldtrip([{path: hash, hash: hash}], visit.bind(this), (err) => {
10995
if (err) {
110-
self.emit('error', err)
111-
return
96+
return this.emit('error', err)
11297
}
113-
self.push(null)
98+
this.push(null)
11499
})
115100

116101
// Visit function: called once per node in the exported graph
117102
function visit (item, add, done) {
118-
dagService.get(item.hash, (err, fetchedNode) => {
103+
dagService.get(item.hash, (err, node) => {
119104
if (err) {
120-
self.emit('error', err)
121-
return
105+
return this.emit('error', err)
122106
}
123107

124-
const data = UnixFS.unmarshal(fetchedNode.data)
108+
const data = UnixFS.unmarshal(node.data)
125109
const type = data.type
126110

127111
if (type === 'directory') {
128-
dirExporter(fetchedNode, item.path, add, done)
112+
dirExporter(node, item.path, add, done)
129113
}
130114

131115
if (type === 'file') {
132-
fileExporter(fetchedNode, item.path, done)
116+
fileExporter(node, item.path, done)
133117
}
134118
})
135119
}

src/importer.js

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

33
const debug = require('debug')
4-
const log = debug('importer')
5-
log.err = debug('importer:error')
4+
const log = debug('unixfs')
5+
log.err = debug('unixfs:error')
66
const fsc = require('./chunker-fixed-size')
77
const through2 = require('through2')
88
const merkleDAG = require('ipfs-merkle-dag')

0 commit comments

Comments
 (0)