Variable does not call function, Webworker and Scope functions

Asked

Viewed 45 times

1

I have serious problems, I have a Webworker that imports a file, but when calling the function with the variable does not work. See:

importScripts(
  './libs/RSA.js'
);

self.onmessage = function (e) {
        JSEncrypt.getKey(function () {
          privateKey = RSA.getPrivateKey();
          publicKey = RSA.getPublicKey();
        });
}

The RSA file is nothing more than: http://travistidwell.com/jsencrypt/bin/jsencrypt.js

The error returned is: Uncaught Typeerror: RSA.getKey is not a Function

  • Where did you define RSA, and what is your type? I couldn’t find any variables called RSA in the linked file...

  • Lost linked file has variable JSEncrypt and mine was changed to RSA, but I got it right. @mgibsonbr

  • You used this jsencrypt.js as it is, or adapted it in some way? I tested here, and got Uncaught ReferenceError: window is not defined - which is natural, since within the web worker no access to window. That is, he did not even run the line where defines the onmessage, not responding to events sent from the main page.

  • i defined var window = self; @mgibsonbr according to a question on Soen

  • 1

    Okay, I’ll repeat the test. Please mention this kind of thing in the question, otherwise it’s hard to know what’s going on. I suggest you also read this: http://answall.com/help/mcve. (P.S. Now yes I got the same error; it is already a progress... : P)

1 answer

3


The function getKey does not belong to JSEncrypt, and yes to JSEncrypt.prototype. That means that all instance of JSEncrypt - maid new JSEncrypt(...) - is who will have this method.

Create an instance the way you think best (with or without options), and call the method in it:

var window = self;
importScripts(
  'jsencrypt.js'
);

var meuRSA = new JSEncrypt();

self.onmessage = function (e) {
        meuRSA.getKey(function () {
          privateKey = meuRSA.getPrivateKey();
          publicKey = meuRSA.getPublicKey();

          // Para visualização
          console.log(privateKey);
          console.log('');
          console.log(publicKey);
        });
}
  • how could I miss it ?? My God.. I waste hours sometimes by silly things that I’m ashamed of..

  • Hehe if he was that silly I wouldn’t have taken so long to find the problem... : P JS prototypes are unnecessarily confusing.

  • You’re nice, but I think there should be something that helps you review the code to find the error.. like "patterns," but I don’t think there’s any way..

  • 1

    @Elaine Historically it has been very difficult to do this kind of analysis in dynamically typed languages, as is done in those with static typing. The most that can be achieved is some analysis of the script itself (see Jslint), but without deepening the relations between one archive and another (and still without type checking). I read long ago that some tools to help in this were being developed, but it’s been seven years and I haven’t heard from her since... :(

  • Interesting the idea proposed, but I think it is something very complex, and as not all program equal with defined "patterns" becomes even more difficult :/ Who knows until 2050 we do not see something aimed at this area haha

Browser other questions tagged

You are not signed in. Login or sign up in order to post.