oysterprotocol / oyster-streamable

Streamable implementation of the Oyster protocol. WIP!
4 stars 0 forks source link

TypeError: targetStream is not a constructor #10

Open mgpeng opened 6 years ago

mgpeng commented 6 years ago

I clone this repo on local and write simple js file to test download file which I send through oyster storage website, I got error when run this file:

(node:25235) UnhandledPromiseRejectionWarning: TypeError: targetStream is not a constructor
    at Download.startDownload (/Users/apple/Documents/penggangfiles/learn-oyster/oyster-streamable/dist/download.js:117:27)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:25235) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:25235) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are nothandled will terminate the Node.js process with a non-zero exit code.

check download.js found to seems "targetStream" is actually not constructor:

......
 var _options = this.options,
          targetStream = _options.targetStream,
          targetOptions = _options.targetOptions;

      this.downloadStream = new _downloadStream2.default(this.genesisHash, metadata, {
        iota: iota
      });
      this.decryptStream = new _decryptStream2.default(this.key);
      this.targetStream = new targetStream(metadata, targetOptions || {});
.......

do I clone old version? I clone master

nullpilot commented 6 years ago

Hi again!

No worries, it's not an old version, the README is just not up to date. As mentioned in the other issue, this is more of a preview of what is to come for third party devs, not yet meant as a ready to use library. There are still going to be changes in how this lib will be used. The goal is to get something similar to what axios is doing.

For now, to use the download in the browser, use Download.toBlob(handle, options), for Uploads it's Upload.fromFile(file, options) where file is a File object.

For node there's Download.toBuffer(handle, options) and Upload.fromData(buffer, filename, options) (which also works in the browser with Uint8Array if you don't want to use File objects)

The rest still mostly applies.
Keep in mind these methods are most likely temporary.

Glad to see you got this far! Keep the questions coming if you get stuck.

mgpeng commented 6 years ago

Hi, nullpilot download work fine, but upload have error do: let upload = new Upload.fromData(fileBuffer, upfilename); error:

  ~/learn-oyster/oyster-streamable/dist/upload.js:182
        throw "Missing required keys: " + invalidKeys.join(",");
        ^
Missing required keys: alpha,beta,epochs

nullpoint has any advice ?

nullpilot commented 6 years ago

Heya, sorry for not seeing this earlier.

One thing is that you can skip the new keyword, fromData and the rest are static functions, not constructors. For the missing options, Aaron needs to chime in, I didn't have a chance to have a closer look at these changes yet.

@AaronVasquez can you have a look at this? I couldn't really get the correct syntax for the call from the webinterface implementation.

AaronVasquez commented 6 years ago

That error is thrown when the call to fromData/fromFile doesn't have the required config properties set. See below as an example.

Stream.Upload.fromFile(file, {
    alpha: "https://broker-1.oysternodes.com",
    beta: "https://broker-2.oysternodes.com",
    epochs: 1,
    iotaProvider: new IOTA({ provider: "https://poll.oysternodes.com" }) // please use this for just reads to IOTA.
});
mgpeng commented 6 years ago

Thanks nullpilot and AaronVasquez, now upload and download work fine on nodejs for download:

import Download from "./download";
const fs = require('fs');

let pathprefix = 'updownfiles/';

var handle ='****************************b608dc*****************';
let download =  Download.toBuffer(handle);

download.on('meta', metadata => {
  // {fileName: "oyster.txt", ext: "txt", numberOfChunks: 2}
});
download.on('finish', filedata => {
  let path = pathprefix + filedata.metadata.fileName;
  let bufferFile = filedata.result;
  fs.writeFile(path,bufferFile,(err) => {
      if (err){
          throw 'could make this file' + err;
      }
      console.log('Saved download file');
  });
  // {file: Blob(), metadata: {…}, target: Download}
});

for upload:

import Upload from "./upload";
import IOTA from "iota.lib.js";
const fs = require('fs');
let uppathprefix = 'updownfiles/';
let upfilename = 'learnSome.js';
fs.readFile(uppathprefix+upfilename , function(err,bufferFile){
    if (err) {
        console.log('Error:- ' + err);
        throw err;
    }
    let upload = Upload.fromData(bufferFile, upfilename, {
        alpha: "https://broker-1.oysternodes.com",
        beta: "https://broker-2.oysternodes.com",
        epochs: 1,
        iotaProvider: new IOTA({ provider: "https://poll.oysternodes.com" }) // please use this for just reads to IOTA.
    });
    upload.on('invoice', invoice => {
        console.log(invoice)
        // {address: "<ETH_ADDRESS>", cost: 20}
    });
    upload.on('finish', filedata => {
        console.log(filedata)
        // {handle: "<OYSTER_HANDLE>", metadata: {…}, target: Upload}
    });
});
mgpeng commented 6 years ago

I think we need more complex download and upload features, like upload folder, upload one file to specific folder position, download folder structure, download whole folder, one folder with only one handle (not every file has one handle), and few different person share one file or one folder, delete file limitation( maybe can set never be deleted or after few years can be deleted). Do you have plan for those features? or have you better idea?