question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RangeError: Invalid string length

See original GitHub issue

Nice idea, but for some reason it doesn’t want to work out of the box.

This example:

const bytenode = require('bytenode');

let helloWorldBytecode = bytenode.compileCode(`console.log('Hi there')`)
bytenode.runBytecode(helloWorldBytecode);

Produce

let dummyCode = ' '.repeat(length);
                      ^

RangeError: Invalid string length
let length = bytecodeBuffer.slice(8, 12).reduce((sum, number, power) => sum += number * 256 ** power, 0);
let dummyCode = ' '.repeat(length); // length is too big

I wonder, what’s the magic behind this line?

Node version: v8.9.3, Mac

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
OsamaAbbascommented, Jan 18, 2019

Yes, wherever you can use Node.js require function, you can use bytenode. Just do require('bytenode') somewhere in your code, after that you can require your ‘.jsc’ files as usual.

But of course you can’t use ‘.jsc’ as src attribute in <script> tags.

So, this would work:

<script>
  const bytenode = require('bytenode');

  let app;

  try {
    app = require('./app.jsc');
  } catch (error) {
    bytenode.compileFile('./app.js'); // this file should not exist in production, if you want to protect your source code
    app = require('./app.jsc');
  }

// use your app functions and classes here
</script>

but this will NOT work:

<script src="./app.jsc"></script>
1reaction
cyphercodes96commented, Nov 16, 2020

Awesome and amazing work on this package - Looking forward to put it into good use

JSYK whenever I try to bytenode an obfuscated file using JavaScriptObfuscator I get the same rangeError invalid string length exception

Would be awesome if we can somehow resolve this - would add more security whenever the .js file will exist prior to .jsc on production environment ( prior to compilation / deletion of file )

const obfJs = (src, options = {}) => {
  fs.readFile(src, 'utf8', function (err, data) {
    if (err) return console.log('err obfuscation3 to file', err);
    var obfuscationResult = JavaScriptObfuscator.obfuscate(data, Object.assign({}, obfOptions, options));

    fs.writeFile(src, obfuscationResult.getObfuscatedCode(), 'utf8', function (err) {
      if (err) return console.log('err obfuscation4 to file', err);
    });
  });
};
obfJs('./libraries/secured.js');

Then on my electron main.js

const appPath = require('electron-root-path').rootPath;
const obfJs2Byte = (src, options = {}) => {
  console.log('obfuscating js 2 bye');
  try {

    let compiledFilename = bytenode.compileFile({
      filename: appPath + src,
      compileAsModule: true
    });
    console.log(compiledFilename)
  } catch (err) {
    console.log('ERR CAUGHT', err)
  }
  try {
    fs.unlink(appPath + src, (err) => {
      if (err) {
        console.error(err);
        return
      }
    })
  } catch (err) {
    console.error(err)
  }
};
console.log('LOGGING FILE RN');
const bytenode = require('bytenode');
if (!fs.existsSync(appPath + '/secured.jsc')) {
  obfJs2Byte('/secured.js');
}


const Security = require(appPath + '/secured.jsc');

Remove javascript obfuscation and it works like a charm

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught RangeError: Invalid string length when appending ...
Your 2-D array accesses are incorrect, but the main problem is that you're re-using the variable i in an inner loop: for (i...
Read more >
RangeError: Invalid string length with large files. · Issue #35973
When i'm trying to read a large Json File (700MB - 26.640.222 lines) using ReadStream i'm getting the error: "RangeError: Invalid string length" ......
Read more >
RangeError: invalid array length - JavaScript - MDN Web Docs
The JavaScript exception "Invalid array length" occurs when specifying an array length that is either negative, a floating number or exceeds ...
Read more >
HTML : Uncaught RangeError: Invalid string length ... - YouTube
HTML : Uncaught RangeError : Invalid string length when appending to a string in JavaScript [ Beautify Your Computer ...
Read more >
Simplest solution to JSON.stringify RangeError: Invalid string ...
We often come across this issue where the object or value passed to JSON.stringify is more than it can handle and the most...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found