Skip to content

Commit

Permalink
verbose flag on streams
Browse files Browse the repository at this point in the history
  • Loading branch information
mitra42 committed Oct 27, 2017
1 parent 4858e64 commit a866c46
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions js/Dweb.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ exports.utils.mergeTypedArraysUnsafe = function(a, b) { // Take care of inabilit
};

//TODO-STREAM, use this code and return stream from p_rawfetch that this can be applied to
exports.utils.p_streamToBuffer = function(stream, mimeType) {
exports.utils.p_streamToBuffer = function(stream, verbose) {
// resolve to a promise that returns a stream.
// Note this comes form one example ...
// There is another example https://github.com/ipfs/js-ipfs/blob/master/examples/exchange-files-in-browser/public/js/app.js#L102 very different
return new Promise((resolve, reject) => {
try {
let chunks = [];
stream
.on('data', (chunk) => chunks.push(chunk))
.once('end', () => resolve(Buffer.concat(chunks)))
.on('data', (chunk) => { if (verbose) console.log('on', chunk.length); chunks.push(chunk); })
.once('end', () => { if (verbose) console.log('end chunks', chunks.length); resolve(Buffer.concat(chunks)); })
.on('error', (err) => { // Note error behavior untested currently
console.log("Error event in p_streamToBuffer",err);
reject(new Dweb.errors.TransportError('Error in stream'))
Expand All @@ -79,7 +79,8 @@ exports.utils.p_streamToBuffer = function(stream, mimeType) {
})
}
//TODO-STREAM, use this code and return stream from p_rawfetch that this can be applied to
exports.utils.p_streamToBlob = function(stream, mimeType) {
//TODO-STREAM debugging in streamToBuffer above, copy to here when fixed above
exports.utils.p_streamToBlob = function(stream, mimeType, verbose) {
// resolve to a promise that returns a stream - currently untested as using Buffer
return new Promise((resolve, reject) => {
try {
Expand Down
2 changes: 1 addition & 1 deletion js/TransportIPFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class TransportIPFS extends Transport {
if (verbose) console.log("CID=",cid)
//return this.promisified.ipfs.block.get(cid).then((result) => result.data) // OLD way, works below 250k bytes, where files.cat doesnt !
return this.ipfs.files.cat(cid)
.then((stream) => Dweb.utils.p_streamToBuffer(stream))
.then((stream) => Dweb.utils.p_streamToBuffer(stream, verbose))
.then((data)=> { if (verbose) console.log("fetched ",data.length); return data; })
.catch((err) => {
console.log("Caught misc error in TransportIPFS.p_rawfetch", err);
Expand Down

0 comments on commit a866c46

Please sign in to comment.