Skip to content

Commit

Permalink
change streamsaver WritableStream to be a polyfilled version instead …
Browse files Browse the repository at this point in the history
  • Loading branch information
ibudisteanu committed Aug 1, 2020
1 parent da89fde commit 853d66a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"@transcend-io/conflux": "^2.0.2",
"path-parse": "^1.0.6",
"rimraf": "^3.0.2",
"streamsaver": "^2.0.4"
"streamsaver": "^2.0.4",
"web-streams-polyfill": "^2.1.1"
},
"devDependencies": {
"@babel/core": "^7.1.0",
Expand Down
16 changes: 13 additions & 3 deletions src/pandora-locations/browser/browser-pandora-locations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const {createHash} = require('crypto')

const { WritableStream, ReadableStream, TransformStream } = require('web-streams-polyfill/ponyfill')
const streamsaver = require('streamsaver')
const stream = require('stream')

// change streamsaver WritableStream to be a polyfilled version instead
// see https://github.com/Pandora-Protocol/pandora-protocol-reference/issues/6 @jimmywarting
streamsaver.WritableStream = WritableStream

const InterfacePandoraLocations = require('../interface-pandora-locations')
const Storage = require('pandora-protocol-kad-reference').storage.Storage;
const PandoraStreamType = require('../../pandora-box/stream/pandora-box-stream-type')
Expand Down Expand Up @@ -104,6 +110,8 @@ module.exports = class BrowserPandoraLocations extends InterfacePandoraLocations

savePandoraBoxAs(pandoraBox, name, cb){

const self = this;

if (!pandoraBox || !(pandoraBox instanceof PandoraBox) ) return cb(new Error('PandoraBox is invalid'))
if (!pandoraBox.isDone ) return cb(new Error('PandoraBox is not ready!'));

Expand All @@ -120,7 +128,9 @@ module.exports = class BrowserPandoraLocations extends InterfacePandoraLocations
// - conflux: wait a sec i pull data from the parent readableStream and then forwards it to you.
async pull (ctrl) {

const { pandoraBoxStream, done } = iterator.next()
const { value, done } = iterator.next()
const pandoraBoxStream = value;

if (done) return ctrl.close()

// do something with value
Expand All @@ -145,8 +155,8 @@ module.exports = class BrowserPandoraLocations extends InterfacePandoraLocations
// or remove this altogether
},
pull (ctrl) {
return new Promise(resolve, reject => {
this.getLocationStreamChunk(absolutePath, i, chunkSize, pandoraBoxStream.chunkRealSize(i), (err, buffer) => {
return new Promise( (resolve, reject) => {
self.getLocationStreamChunk(absolutePath, i, chunkSize, pandoraBoxStream.chunkRealSize(i), (err, buffer) => {
if (err) return reject(err)
if (stopped) return reject(new Error('stopped'))
ctrl.enqueue(buffer)
Expand Down

1 comment on commit 853d66a

@jimmywarting
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now a tiny small thing i would have done would be to have some utility function that takes a pandoraBoxStream and converts it to a ReadableStream so you can use it in both savePandoraBoxAs and savePandoraStreamAs to make it look nicer

function pandoraStreamToReadable (pandoraBoxStream) {
  return new ReadableStream(...)
}

Please sign in to comment.