RangerMauve / hyper-sdk

Make your own hyper apps!
https://www.youtube.com/watch?v=HyHk4aImd_I&list=PL7sG5SCUNyeYx8wnfMOUpsh7rM_g0w_cu&index=20
MIT License
292 stars 45 forks source link

Error in remote readFile: Uncaught TypeError: Cannot read property 'update' of null #79

Closed 4c656554 closed 2 years ago

4c656554 commented 3 years ago

My setup.

Two chrome based browsers (1. Ubuntu 18.04.5, 2. Win10)

A website that loads browserified hyper-sdk (from this repo)

A Node server running hyperswarm-web proxy and signal servers (used by hyper-sdk)

I can set up and read small text snippets:

e.g. Computer 1:

let hypersdk = await window.hyperSDK({swarmOpts:{preferredPort:42420,wsProxy:'wss:/my.url/proxy'}}
let c1Drive = await hypersdk.Hyperdrive('c1Drive')
await myDrive.writeFile('test','This is some text')
await myDrive.writeFile('blob',<1.3MB base64 encoded data (as string)>)

Computer 2:

let hypersdk = await window.hyperSDK({swarmOpts:{preferredPort:42420,wsProxy:'wss:/my.url/proxy'}}
let c1Drive = await hypersdk.Hyperdrive("hyper://"+<key of c1Drive>)
let txt = await c1Drive.readFile('test')  // works :-)
let vid = await c1Drive.readFile('blob')  // doesn't work, see error below:

I attempting to read the large file I get the following error, on the console of computer 1.

Uncaught TypeError: Cannot read property 'update' of null
    at XSalsa20.update (hyper-sdk-bundle.js:72718)
    at XORJS.update (hyper-sdk-bundle.js:72686)
    at XOR.encrypt (hyper-sdk-bundle.js:62572)
    at SimpleProtocol._sendNow (hyper-sdk-bundle.js:62330)
    at SimpleProtocol._send (hyper-sdk-bundle.js:62318)
    at SimpleProtocol.data (hyper-sdk-bundle.js:62256)
    at Channel.data (hyper-sdk-bundle.js:33587)
    at onvalue (hyper-sdk-bundle.js:36342)
    at Request._callback (hyper-sdk-bundle.js:37014)
    at Request.callback (hyper-sdk-bundle.js:55488)

I will continue to investigate, but posting here in case anyone can recognise what is going wrong.

Thanks

RangerMauve commented 3 years ago

Thank you for reporting this, I haven't come across it before. 😅

Would you be able to create a minimal repo that reproduces this issue to help debug?

4c656554 commented 3 years ago

Will do

4c656554 commented 3 years ago

Finally done :-)

https://github.com/4c656554/hypersdk_debug

4c656554 commented 3 years ago

I've found that editing the XSalsa20 code as follows (line ~72712 in hypersdk bundle) removes the error. However it still seems to wheel spin and not transfer the larger files:

,{"xsalsa20":573}],573:[function(require,module,exports){
var xsalsa20 = require("./xsalsa20")();

var SIGMA = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
var head = 144;
var top = head;
var free = [];
module.exports = XSalsa20;
XSalsa20.NONCEBYTES = 24;
XSalsa20.KEYBYTES = 32;
XSalsa20.core_hsalsa20 = core_hsalsa20;
XSalsa20.SIGMA = SIGMA;
let salsaNonce = null;
let salsaKey = null;

function XSalsa20(nonce, key) {
  if (!(this instanceof XSalsa20)) return new XSalsa20(nonce, key);
  if (!nonce || nonce.length < 24) throw new Error('nonce must be at least 24 bytes');
  if (!key || key.length < 32) throw new Error('key must be at least 32 bytes');
  salsaNonce = nonce;
  salsaKey = key
  this._xor = xsalsa20 && xsalsa20.exports ? new WASM(nonce, key) : new Fallback(nonce, key);
  console.log('XSalsa20 called',"this:",this,"this._xor:",this._xor)
}

XSalsa20.prototype.update = function (input, output) {
  if (!input) throw new Error('input must be Uint8Array or Buffer');
  if (!output) output = new Uint8Array(input.length);
  if(!this._xor) this._xor = xsalsa20 && xsalsa20.exports ? new WASM(salsaNonce, salsaKey) : new Fallback(salsaNonce, salsaKey)
  console.log('_xor?',"this:",this,"this._xor:",this._xor,"input:",input,"output:", output,"nonce,key",salsaNonce, salsaKey)
  if (input.length) this._xor.update(input, output);
  return output;
};
4c656554 commented 3 years ago

The maximum file size I'm able to transfer is <450kB

RangerMauve commented 2 years ago

Is this still an issue? I haven't done any large file tests lately.

4c656554 commented 2 years ago

I don't know, I plan to come back to it at some point but have no time estimate atm.

RangerMauve commented 2 years ago

The changes here might have also helped.

4c656554 commented 2 years ago

I just re checked this, open the above debug check from links below, if you'd like to verify:

https://ooda.space/hypersdk_debug/hypersdk_debug_A.html

and

https://ooda.space/hypersdk_debug/hypersdk_debug_B.html

The error doesn't appear, so I'd go ahead and close it - Thank you!

RangerMauve commented 2 years ago

Glad that's sorted out! 😁 Thank you for the follow up.