This repository was archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Merged
Clean up #49
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
'use strict' | ||
|
||
const debug = require('debug') | ||
const log = debug('exporter') | ||
log.err = debug('exporter:error') | ||
const log = debug('unixfs') | ||
log.err = debug('unixfs:error') | ||
const isIPFS = require('is-ipfs') | ||
const bs58 = require('bs58') | ||
const UnixFS = require('ipfs-unixfs') | ||
const series = require('run-series') | ||
const Readable = require('readable-stream').Readable | ||
|
@@ -21,13 +20,10 @@ function Exporter (hash, dagService, options) { | |
return new Exporter(hash, dagService, options) | ||
} | ||
|
||
// Sanitize hash. | ||
// Sanitize hash | ||
if (!isIPFS.multihash(hash)) { | ||
throw new Error('not valid multihash') | ||
} | ||
if (Buffer.isBuffer(hash)) { | ||
hash = bs58.encode(hash) | ||
} | ||
|
||
Readable.call(this, { objectMode: true }) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was this removed? It breaks functionality. :( Fixed in #52 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for catching it, tests didn't detect it |
||
|
@@ -36,61 +32,52 @@ function Exporter (hash, dagService, options) { | |
this._read = (n) => {} | ||
|
||
let fileExporter = (node, name, done) => { | ||
let init = false | ||
if (!done) { | ||
throw new Error('done must be set') | ||
} | ||
|
||
if (!done) throw new Error('done must be set') | ||
const contentRS = new Readable() | ||
contentRS._read = () => {} | ||
|
||
// Logic to export a single (possibly chunked) unixfs file. | ||
var rs = new Readable() | ||
if (node.links.length === 0) { | ||
const unmarshaledData = UnixFS.unmarshal(node.data) | ||
rs._read = () => { | ||
if (init) { | ||
return | ||
} | ||
init = true | ||
rs.push(unmarshaledData.data) | ||
rs.push(null) | ||
} | ||
this.push({ content: rs, path: name }) | ||
contentRS.push(unmarshaledData.data) | ||
contentRS.push(null) | ||
this.push({ content: contentRS, path: name }) | ||
done() | ||
} else { | ||
rs._read = () => { | ||
if (init) { | ||
return | ||
const array = node.links.map((link) => { | ||
return (cb) => { | ||
dagService.get(link.hash, (err, res) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
var unmarshaledData = UnixFS.unmarshal(res.data) | ||
contentRS.push(unmarshaledData.data) | ||
cb() | ||
}) | ||
} | ||
init = true | ||
|
||
const array = node.links.map((link) => { | ||
return (cb) => { | ||
dagService.get(link.hash, (err, res) => { | ||
if (err) { | ||
return cb(err) | ||
} | ||
var unmarshaledData = UnixFS.unmarshal(res.data) | ||
rs.push(unmarshaledData.data) | ||
cb() | ||
}) | ||
} | ||
}) | ||
series(array, (err, res) => { | ||
if (err) { | ||
rs.emit('error', err) | ||
return | ||
} | ||
rs.push(null) | ||
return | ||
}) | ||
} | ||
this.push({ content: rs, path: name }) | ||
}) | ||
series(array, (err) => { | ||
if (err) { | ||
return contentRS.emit('error', err) | ||
} | ||
contentRS.push(null) | ||
}) | ||
this.push({ content: contentRS, path: name }) | ||
done() | ||
} | ||
} | ||
|
||
// Logic to export a unixfs directory. | ||
let dirExporter = (node, name, add, done) => { | ||
if (!add) throw new Error('add must be set') | ||
if (!done) throw new Error('done must be set') | ||
if (!add) { | ||
throw new Error('add must be set') | ||
} | ||
if (!done) { | ||
throw new Error('done must be set') | ||
} | ||
|
||
this.push({content: null, path: name}) | ||
|
||
|
@@ -104,32 +91,29 @@ function Exporter (hash, dagService, options) { | |
} | ||
|
||
// Traverse the DAG asynchronously | ||
var self = this | ||
fieldtrip([{ path: hash, hash: hash }], visit, (err) => { | ||
fieldtrip([{path: hash, hash: hash}], visit.bind(this), (err) => { | ||
if (err) { | ||
self.emit('error', err) | ||
return | ||
return this.emit('error', err) | ||
} | ||
self.push(null) | ||
this.push(null) | ||
}) | ||
|
||
// Visit function: called once per node in the exported graph | ||
function visit (item, add, done) { | ||
dagService.get(item.hash, (err, fetchedNode) => { | ||
dagService.get(item.hash, (err, node) => { | ||
if (err) { | ||
self.emit('error', err) | ||
return | ||
return this.emit('error', err) | ||
} | ||
|
||
const data = UnixFS.unmarshal(fetchedNode.data) | ||
const data = UnixFS.unmarshal(node.data) | ||
const type = data.type | ||
|
||
if (type === 'directory') { | ||
dirExporter(fetchedNode, item.path, add, done) | ||
dirExporter(node, item.path, add, done) | ||
} | ||
|
||
if (type === 'file') { | ||
fileExporter(fetchedNode, item.path, done) | ||
fileExporter(node, item.path, done) | ||
} | ||
}) | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's wrong with #? It's the same, in rendering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been (since forever) doing
====
for the title, and now it is just autopilot.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Let's not care.