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
Rename "stream" to "content" in tuples. #43
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b5acb24
Rename "stream" to "content" in tuples.
hackergrrl b177a08
Support content as a Buffer.
hackergrrl 462d2f4
End import stream after all writes.
hackergrrl 96a8aa2
README <3
hackergrrl f65abd6
more readme tweaks; squash me
hackergrrl 3f07067
Rename "base" to "path" in traverse.
hackergrrl 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
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 |
---|---|---|
|
@@ -10,6 +10,8 @@ const UnixFS = require('ipfs-unixfs') | |
const util = require('util') | ||
const bs58 = require('bs58') | ||
const Duplex = require('readable-stream').Duplex | ||
const isStream = require('isstream') | ||
const streamifier = require('streamifier') | ||
|
||
exports = module.exports = Importer | ||
|
||
|
@@ -36,7 +38,7 @@ function Importer (dagService, options) { | |
this._write = (fl, enc, next) => { | ||
this.read() | ||
counter++ | ||
if (!fl.stream) { | ||
if (!fl.content) { | ||
// 1. create the empty dir dag node | ||
// 2. write it to the dag store | ||
// 3. add to the files array {path: <>, hash: <>} | ||
|
@@ -63,8 +65,20 @@ function Importer (dagService, options) { | |
return | ||
} | ||
|
||
// Convert a buffer to a readable stream | ||
if (Buffer.isBuffer(fl.content)) { | ||
const r = streamifier.createReadStream(fl.content) | ||
fl.content = r | ||
} | ||
|
||
// Bail if 'content' is not readable | ||
if (!isStream.isReadable(fl.content)) { | ||
this.emit('error', new Error('"content" is not a Buffer nor Readable stream')) | ||
return | ||
} | ||
|
||
const leaves = [] | ||
fl.stream | ||
fl.content | ||
.pipe(fsc(CHUNK_SIZE)) | ||
.pipe(through2((chunk, enc, cb) => { | ||
// 1. create the unixfs merkledag node | ||
|
@@ -224,13 +238,15 @@ function Importer (dagService, options) { | |
// If the value is not an object | ||
// add as a link to the dirNode | ||
|
||
function traverse (tree, base) { | ||
let pendingWrites = 0 | ||
|
||
function traverse (tree, base, done) { | ||
const keys = Object.keys(tree) | ||
let tmpTree = tree | ||
keys.map((key) => { | ||
if (typeof tmpTree[key] === 'object' && | ||
!Buffer.isBuffer(tmpTree[key])) { | ||
tmpTree[key] = traverse.call(this, tmpTree[key], base ? base + '/' + key : key) | ||
tmpTree[key] = traverse.call(this, tmpTree[key], base ? base + '/' + key : key, done) | ||
} | ||
}) | ||
|
||
|
@@ -250,28 +266,39 @@ function Importer (dagService, options) { | |
}) | ||
|
||
n.data = d.marshal() | ||
|
||
pendingWrites++ | ||
dagService.add(n, (err) => { | ||
pendingWrites-- | ||
if (err) { | ||
this.push({error: 'failed to store dirNode'}) | ||
} else if (base) { | ||
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. what is this 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. 'base' is the current path of the DAG Node that's being visited as the function traverses the file hierarchy tree. On the first iteration it will be 'null' (it hasn't entered a node yet?). I've renamed this to 'path'.
|
||
const el = { | ||
path: base, | ||
multihash: n.multihash(), | ||
yes: 'no', | ||
size: n.size() | ||
} | ||
this.push(el) | ||
} | ||
|
||
if (pendingWrites <= 0) { | ||
done() | ||
} | ||
}) | ||
|
||
if (!base) { | ||
return | ||
} | ||
|
||
const el = { | ||
path: base, | ||
multihash: n.multihash(), | ||
size: n.size() | ||
} | ||
this.push(el) | ||
|
||
mhIndex[bs58.encode(n.multihash())] = { size: n.size() } | ||
return n.multihash() | ||
} | ||
/* const rootHash = */ traverse.call(this, fileTree) | ||
this.push(null) | ||
|
||
let self = this | ||
/* const rootHash = */ traverse.call(this, fileTree, null, function () { | ||
self.push(null) | ||
}) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -29,6 +29,17 @@ module.exports = function (repo) { | |
done() | ||
}) | ||
|
||
it('bad input', (done) => { | ||
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. what about a good banana (inside a buffer) ? :) 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. Did you see 'small file as buffer (smaller than a chunk)'? It tests a Buffer that's <= 1 chunk.
|
||
const r = 'banana' | ||
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. |
||
const i = new Importer(ds) | ||
i.on('error', (err) => { | ||
expect(err).to.exist | ||
done() | ||
}) | ||
i.write({path: '200Bytes.txt', content: r}) | ||
i.end() | ||
}) | ||
|
||
it('small file (smaller than a chunk)', (done) => { | ||
const buffered = smallFile | ||
const r = streamifier.createReadStream(buffered) | ||
|
@@ -38,7 +49,22 @@ module.exports = function (repo) { | |
expect(bs58.encode(obj.multihash)).to.equal('QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8') | ||
expect(obj.size).to.equal(211) | ||
}) | ||
i.write({path: '200Bytes.txt', stream: r}) | ||
i.write({path: '200Bytes.txt', content: r}) | ||
i.end() | ||
i.on('end', () => { | ||
done() | ||
}) | ||
}) | ||
|
||
it('small file as buffer (smaller than a chunk)', (done) => { | ||
const buffered = smallFile | ||
const i = new Importer(ds) | ||
i.on('data', (obj) => { | ||
expect(obj.path).to.equal('200Bytes.txt') | ||
expect(bs58.encode(obj.multihash)).to.equal('QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8') | ||
expect(obj.size).to.equal(211) | ||
}) | ||
i.write({path: '200Bytes.txt', content: buffered}) | ||
i.end() | ||
i.on('end', () => { | ||
done() | ||
|
@@ -69,7 +95,7 @@ module.exports = function (repo) { | |
i.on('end', () => { | ||
done() | ||
}) | ||
i.write({path: 'foo/bar/200Bytes.txt', stream: r}) | ||
i.write({path: 'foo/bar/200Bytes.txt', content: r}) | ||
i.end() | ||
}) | ||
|
||
|
@@ -85,7 +111,7 @@ module.exports = function (repo) { | |
i.on('end', () => { | ||
done() | ||
}) | ||
i.write({path: '1.2MiB.txt', stream: r}) | ||
i.write({path: '1.2MiB.txt', content: r}) | ||
i.end() | ||
}) | ||
|
||
|
@@ -106,7 +132,7 @@ module.exports = function (repo) { | |
i.on('end', () => { | ||
done() | ||
}) | ||
i.write({path: 'foo-big/1.2MiB.txt', stream: r}) | ||
i.write({path: 'foo-big/1.2MiB.txt', content: r}) | ||
i.end() | ||
}) | ||
|
||
|
@@ -156,8 +182,8 @@ module.exports = function (repo) { | |
i.on('end', () => { | ||
done() | ||
}) | ||
i.write({path: 'pim/200Bytes.txt', stream: r1}) | ||
i.write({path: 'pim/1.2MiB.txt', stream: r2}) | ||
i.write({path: 'pim/200Bytes.txt', content: r1}) | ||
i.write({path: 'pim/1.2MiB.txt', content: r2}) | ||
i.end() | ||
}) | ||
|
||
|
@@ -195,9 +221,9 @@ module.exports = function (repo) { | |
i.on('end', () => { | ||
done() | ||
}) | ||
i.write({path: 'pam/pum/200Bytes.txt', stream: r1}) | ||
i.write({path: 'pam/pum/1.2MiB.txt', stream: r2}) | ||
i.write({path: 'pam/1.2MiB.txt', stream: r3}) | ||
i.write({path: 'pam/pum/200Bytes.txt', content: r1}) | ||
i.write({path: 'pam/pum/1.2MiB.txt', content: r2}) | ||
i.write({path: 'pam/1.2MiB.txt', content: r3}) | ||
i.end() | ||
}) | ||
}) | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
const Exporter = require('ipfs-unixfs-engine').exporter