This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
update/files add interface #306
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d704742
Make ipfs.files.add return DAGNodes.
hackergrrl 3e1f563
Rename unixfs output from 'stream' to 'content'.
hackergrrl b083649
Mitigate test timing issues.
hackergrrl f5e6761
Update ipfs-unixfs-engine to 0.9.0
hackergrrl dd344e4
update ipfs-api and interface-ipfs-core
daviddias 903022c
add createAddStream feature
daviddias 20d880c
fix port conflict
daviddias 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 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 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,22 +1,57 @@ | ||
'use strict' | ||
|
||
const Importer = require('ipfs-unixfs-engine').importer | ||
const Exporter = require('ipfs-unixfs-engine').exporter | ||
const unixfsEngine = require('ipfs-unixfs-engine') | ||
const Importer = unixfsEngine.Importer | ||
const Exporter = unixfsEngine.Exporter | ||
const UnixFS = require('ipfs-unixfs') | ||
const bs58 = require('bs58') | ||
const through = require('through2') | ||
const isStream = require('isstream') | ||
const promisify = require('promisify-es6') | ||
const Duplex = require('stream').Duplex | ||
const multihashes = require('multihashes') | ||
|
||
module.exports = function files (self) { | ||
return { | ||
createAddStream: promisify((callback) => { | ||
// TODO: wip | ||
if (data === undefined) { | ||
return new Importer(self._dagS) | ||
createAddStream: (callback) => { | ||
const i = new Importer(self._dagS) | ||
const ds = new Duplex({ objectMode: true }) | ||
|
||
ds._read = (n) => {} | ||
ds._write = (file, enc, next) => { | ||
i.write(file) | ||
next() | ||
} | ||
}), | ||
|
||
ds.end = () => { | ||
i.end() | ||
} | ||
|
||
let counter = 0 | ||
|
||
i.on('data', (file) => { | ||
counter++ | ||
self.object.get(file.multihash, (err, node) => { | ||
if (err) { | ||
return ds.emit('error', err) | ||
} | ||
ds.push({path: file.path, node: node}) | ||
counter-- | ||
}) | ||
}) | ||
|
||
i.on('end', () => { | ||
function canFinish () { | ||
if (counter === 0) { | ||
ds.push(null) | ||
} else { | ||
setTimeout(canFinish, 100) | ||
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 do we need this 100ms timeout loop? 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. Cause getting the object is an Async operation and the underlying stream from the importer won't care to wait that we have pushed all the objects to the upper stream and emit them all and then the 'end' event. This avoid a racing condition. |
||
} | ||
} | ||
canFinish() | ||
}) | ||
|
||
callback(null, ds) | ||
}, | ||
add: promisify((data, callback) => { | ||
// Buffer input | ||
if (Buffer.isBuffer(data)) { | ||
|
@@ -33,7 +68,7 @@ module.exports = function files (self) { | |
}] | ||
} | ||
if (!callback || typeof callback !== 'function') { | ||
callback = function oop () {} | ||
callback = function noop () {} | ||
} | ||
if (!Array.isArray(data)) { | ||
return callback(new Error('"data" must be an array of { path: string, content: Buffer|Readable } or Buffer or Readable')) | ||
|
@@ -43,8 +78,8 @@ module.exports = function files (self) { | |
const res = [] | ||
|
||
// Transform file info tuples to DAGNodes | ||
i.pipe(through.obj(function transform (info, enc, next) { | ||
const mh = bs58.encode(info.multihash).toString() | ||
i.pipe(through.obj((info, enc, next) => { | ||
const mh = multihashes.toB58String(info.multihash) | ||
self._dagS.get(mh, (err, node) => { | ||
if (err) return callback(err) | ||
var obj = { | ||
|
@@ -54,7 +89,7 @@ module.exports = function files (self) { | |
res.push(obj) | ||
next() | ||
}) | ||
}, function end (done) { | ||
}, (done) => { | ||
callback(null, res) | ||
})) | ||
|
||
|
This file contains 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 |
---|---|---|
|
@@ -9,9 +9,8 @@ module.exports = (httpAPI) => { | |
describe('api', () => { | ||
let api | ||
|
||
before((done) => { | ||
before(() => { | ||
api = httpAPI.server.select('API') | ||
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. no need for done |
||
|
||
describe('/files/cat', () => { | ||
|
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.
Why is this passthrough needed?
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.
It is a transform, on the way up