bytenode/bytenode

Debugging non-encoded javascript code

jurepetrovic opened this issue · 11 comments

Hello,

I am trying to use bytenode in such way that some of the application files will be compiled
and some will stay in plain source code. I would like to have the ability to debug the
non-compiled files.

The compiled file loads the non-compiled one and this works ok, but the
ndb debugger will not break at the debugger statement if it is being launched
through compiled file.

Any idea on that?

Can you provide a minimal setup that shows your issue?

I have tried a small project with index.js that requires module1.jsc which will eventually require module2.js.

module1.jsc was compiled using bytenode -c module1.js as usual.

I tried both debugger; statement and manual breakpoints inside module2.js, both works just fine with me. The ndb debugger will break as expected.

I'm using Node v14.16.1.

Thank you for the quick help!
Let me prepare the minimum example, we might discover something while preparing it.
I'm a bit suspicious - there are some eval() calls in the code...

Thanks,
Jure

Hello @OsamaAbbas,

I have prepared small setup with the code that I found suspicious, however it works fine.
https://github.com/jurepetrovic/encoding

You can do:
ndb simplay.loader.js
and the debugger statements are already inside.

However, I still have problems in the complete code. The points I noticed:

  • if there is no debugger statement, the code will execute correctly to the end.
  • if there is debugger statement the code will hang there, but the ndb debugger will not show anything.

Any clues?
Thanks a million,
jure

Hello @OsamaAbbas,

okay, now I have a working example. And I cannot figure it out.
Please check here: (https://github.com/jurepetrovic/encoding)

Running:

  • ndb simplay.loader.js works like a charm, the debugger will stop in simulworker.js, line 11.
  • ndb slotsimplay.loader.js will start and the debugger will hang on the same line, not showing anything.

My concern was there is an issue in this Thread loading logic. However, in that case simplay would not work properly.
What does slotsimplay.js contain to prevent proper debugging?

I'm using node v14.15.4

Thanks,
Jure

Ok, I changed line 124 to change anonymous function to the classic declaration.
this. simulate = function() {}
https://github.com/jurepetrovic/encoding/blob/master/slotsimplay.js

However, I don't understand why anonymous functions can be a problem?

Thanks,
Jure

Arrow functions have cause a few issues and bugs before. I don't know the exact reason, but I believe it is related to the way v8 handles them in the C++ source code. So there is nothing that can be done to fix them from bytenode or node itself.

I will add your case to the known issues and limitations in the README file soon.

Thank you for reporting and finding out the cause of the issue.

Thank YOU for bytenode actually.

Is there any way to fix the case where the code does:
var code = fs.readFileSync("./file.js").toString();
eval(code);

yes, I know - but people still do this :)

Thanks,
Jure

This code:

var code = fs.readFileSync("./file.js").toString();
eval(code);

Should work with bytenode. So if that code is in a file named script.js and you compiled it to script.jsc: it will work as usual.

The only place where toString() does not work is with function objects. If you have a function and your code depends on its string content (for analyzing purposes, or transpiling, or to send it to another context like in Puppeteer), then it won't work with bytenode and you will have to find another way. However, all these purposes are typical in tools and utilities, not in your everyday code.

No, not this case :-)
The problem is, when you need "file.js" encoded

No, not this case :-)
The problem is, when you need "file.js" encoded

This can't be done. If you want to protect file.js too, you have to change the previous code.