Skip to content

Commit

Permalink
Add stream resume
Browse files Browse the repository at this point in the history
  • Loading branch information
mitra42 committed Oct 27, 2017
1 parent 6ea6e6f commit 4858e64
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions js/Dweb.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,19 @@ 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) {
// 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 = []
let chunks = [];
stream
.on('data', (chunk)=>chunks.push(chunk))
.on('end', ()=>resolve(Buffer.concat(chunks)))
.on('data', (chunk) => chunks.push(chunk))
.once('end', () => 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'))
})
});
stream.resume();
} catch (err) {
console.log("Error thrown in p_streamToBuffer", err);
reject(err);
Expand All @@ -80,17 +83,18 @@ exports.utils.p_streamToBlob = function(stream, mimeType) {
// resolve to a promise that returns a stream - currently untested as using Buffer
return new Promise((resolve, reject) => {
try {
let chunks = []
let chunks = [];
stream
.on('data', (chunk)=>chunks.push(chunk))
.on('end', () =>
.once('end', () =>
resolve(mimeType
? new Blob(chunks, { type: mimeType })
: new Blob(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'))
})
stream.resume();
} catch(err) {
console.log("Error thrown in p_streamToBlob",err);
reject(err);
Expand Down

0 comments on commit 4858e64

Please sign in to comment.