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.

Running in the current context of an app with a module.

See original GitHub issue

I have been looking into the source for bytenode and was able to modify the runBytecode to support running a module. When I run in the global context I can not access the variables in the module. I would like to be able to call functions inside of the file even when it is compiled as a module.

Ex.

const hello = "https://www.google.com/";

function testfunc(paramater) {
	paramater = paramater + 'hello';
	return paramater;
}

function testfunc2(number, number2) {
	return number * number2;
}

console.log(testfunc(hello))

That compiles using:

const bytenode = require('bytenode');

bytenode.compileFile({
    filename: 'encode.js',
    compileAsModule: true
});

I sorta hacked something together for the implementation, this is NOT going into production and simply for personal experiments ONLY, please understand this:

const runBytecode = function (bytecodeBuffer) {

  if (!Buffer.isBuffer(bytecodeBuffer)) {
    throw new Error(`bytecodeBuffer must be a buffer object.`);
  }

  fixBytecode(bytecodeBuffer);

  let length = readSourceHash(bytecodeBuffer);

  let dummyCode = "";

  if (length > 1) {
    dummyCode = '"' + "\u200b".repeat(length - 2) + '"'; // "\u200b" Zero width space
  }

  const c = new vm.Script(dummyCode, {
    cachedData: bytecodeBuffer
  })
  c.runInNewContext(global)(module.exports, require, module, __filename, __dirname, process, global);
  console.log(global) // Does not log the functions 'testfunc' and 'testfunc2'
  // Successfully runs and logs the output, though.

  /*if (script.cachedDataRejected) {
    throw new Error('Invalid or incompatible cached data (cachedDataRejected)');
  }*/

  return //script.runInThisContext();
};

Any input would help!

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

1reaction
aarock1234commented, Apr 15, 2021

I apologize for the late response. I will look into it and get back to you.

Any updates? 😄

Haven’t had a chance yet, sorry. I’m assuming it is not urgent, is it?

Not urgent, take your time, just curious.

1reaction
OsamaAbbascommented, Apr 6, 2021

I apologize for the late response. I will look into it and get back to you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Application Context — Flask Documentation (1.1.x)
The application context keeps track of the application-level data during a request, CLI command, or other activity. Rather than passing the application around ......
Read more >
How to access application context from another module of ...
I have two modules of same application, having different Manifest files, Main App is in first Module, I want to access to Main...
Read more >
Understanding the Application and Request Contexts in ...
First, let's look at how to work with the current_app object to access the Application context. Within the Python shell, if you try...
Read more >
Configure the app module | Android Developers
This page describes useful app settings in the module-level build.gradle ... Safely adjust the namespace independent of the application ID.
Read more >
Using current context | LoopBack Documentation
Running and debugging apps ... LoopBack 2.x introduced current-context APIs using the module continuation-local-storage to provide a context object ...
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