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
End export stream on completion. #47
Merged
daviddias
merged 4 commits into
ipfs-inactive:master
from
hackergrrl:end-exporter-stream
Jun 28, 2016
Merged
Changes from 3 commits
Commits
Show all changes
4 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 |
---|---|---|
|
@@ -3,12 +3,14 @@ | |
const debug = require('debug') | ||
const log = debug('exporter') | ||
log.err = debug('exporter:error') | ||
const isIPFS = require('is-ipfs') | ||
const bs58 = require('bs58') | ||
const UnixFS = require('ipfs-unixfs') | ||
const series = require('run-series') | ||
const async = require('async') | ||
const Readable = require('readable-stream').Readable | ||
const pathj = require('path') | ||
const util = require('util') | ||
const fieldtrip = require('field-trip') | ||
|
||
exports = module.exports = Exporter | ||
|
||
|
@@ -19,17 +21,26 @@ function Exporter (hash, dagService, options) { | |
return new Exporter(hash, dagService, options) | ||
} | ||
|
||
// 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 }) | ||
|
||
this.options = options || {} | ||
|
||
this._read = (n) => {} | ||
|
||
let fileExporter = (node, name, callback) => { | ||
let fileExporter = (node, name, done) => { | ||
let init | ||
|
||
if (!callback) { callback = function noop () {} } | ||
if (!done) throw new Error('done must be set') | ||
|
||
// Logic to export a single (possibly chunked) unixfs file. | ||
var rs = new Readable() | ||
if (node.links.length === 0) { | ||
const unmarshaledData = UnixFS.unmarshal(node.data) | ||
|
@@ -43,8 +54,7 @@ function Exporter (hash, dagService, options) { | |
rs.push(null) | ||
} | ||
this.push({ content: rs, path: name }) | ||
callback() | ||
return | ||
done() | ||
} else { | ||
init = false | ||
rs._read = () => { | ||
|
@@ -57,7 +67,7 @@ function Exporter (hash, dagService, options) { | |
return (cb) => { | ||
dagService.get(link.hash, (err, res) => { | ||
if (err) { | ||
cb(err) | ||
return cb(err) | ||
} | ||
var unmarshaledData = UnixFS.unmarshal(res.data) | ||
rs.push(unmarshaledData.data) | ||
|
@@ -67,80 +77,64 @@ function Exporter (hash, dagService, options) { | |
}) | ||
series(array, (err, res) => { | ||
if (err) { | ||
callback() | ||
rs.emit('error', err) | ||
return | ||
} | ||
rs.push(null) | ||
callback() | ||
return | ||
}) | ||
} | ||
this.push({ content: rs, path: name }) | ||
callback() | ||
return | ||
done() | ||
} | ||
} | ||
|
||
let dirExporter = (node, name, callback) => { | ||
let init | ||
// Logic to export a unixfs directory. | ||
let dirExporter = (node, name, add, done) => { | ||
if (!add) throw new Error('add must be set') | ||
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.
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. Right now: yes, it is. I'm being defensive here and explicit with my expectations for the function. I thoroughly expect myself or someone else to goof this up when rearranging things in the future and it'll be nice to have a nice obvious error message. :) |
||
if (!done) throw new Error('done must be set') | ||
|
||
if (!callback) { callback = function noop () {} } | ||
this.push({content: null, path: name}) | ||
|
||
var rs = new Readable() | ||
if (node.links.length === 0) { | ||
init = false | ||
rs._read = () => { | ||
if (init) { | ||
return | ||
} | ||
init = true | ||
rs.push(node.data) | ||
rs.push(null) | ||
} | ||
this.push({content: null, path: name}) | ||
callback() | ||
return | ||
} else { | ||
async.forEachSeries(node.links, (link, callback) => { | ||
dagService.get(link.hash, (err, res) => { | ||
if (err) { | ||
callback(err) | ||
} | ||
var unmarshaledData = UnixFS.unmarshal(res.data) | ||
if (unmarshaledData.type === 'file') { | ||
return (fileExporter(res, pathj.join(name, link.name), callback)) | ||
} | ||
if (unmarshaledData.type === 'directory') { | ||
return (dirExporter(res, pathj.join(name, link.name), callback)) | ||
} | ||
callback() | ||
}) | ||
}, (err) => { | ||
if (err) { | ||
callback() | ||
return | ||
} | ||
callback() | ||
return | ||
// Directory has links | ||
if (node.links.length > 0) { | ||
node.links.forEach((link) => { | ||
add({ path: pathj.join(name, link.name), hash: link.hash }) | ||
}) | ||
} | ||
done() | ||
} | ||
|
||
dagService.get(hash, (err, fetchedNode) => { | ||
// Traverse the DAG asynchronously | ||
var self = this | ||
fieldtrip([{ path: hash, hash: hash }], visit, (err) => { | ||
if (err) { | ||
this.emit('error', err) | ||
self.emit('error', err) | ||
return | ||
} | ||
const data = UnixFS.unmarshal(fetchedNode.data) | ||
const type = data.type | ||
|
||
if (type === 'directory') { | ||
dirExporter(fetchedNode, hash) | ||
} | ||
if (type === 'file') { | ||
fileExporter(fetchedNode, hash) | ||
} | ||
self.push(null) | ||
}) | ||
|
||
// Visit function: called once per node in the exported graph | ||
function visit (item, add, done) { | ||
dagService.get(item.hash, (err, fetchedNode) => { | ||
if (err) { | ||
self.emit('error', err) | ||
return | ||
} | ||
|
||
const data = UnixFS.unmarshal(fetchedNode.data) | ||
const type = data.type | ||
|
||
if (type === 'directory') { | ||
dirExporter(fetchedNode, item.path, add, done) | ||
} | ||
|
||
if (type === 'file') { | ||
fileExporter(fetchedNode, item.path, done) | ||
} | ||
}) | ||
} | ||
|
||
return this | ||
} |
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.
Could we initialize this to false so that L47 and L59 can be removed?
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.
Sure! Done.