Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-retrieve-globals

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-retrieve-globals

Execute a string of JavaScript using Node.js and return the global variable values and functions.

  • 3.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16K
decreased by-41.03%
Maintainers
1
Weekly downloads
 
Created
Source

node-retrieve-globals

Execute a string of JavaScript using Node.js and return the global variable values and functions.

  • Supported on Node.js 14 and newer.

  • Uses var, let, const, function, Array and Object destructuring assignment.

  • Async-friendly but synchronous methods are available.

  • Can return any valid JS data type (including functions).

  • Can provide an external data object as context to the local execution scope

  • Uses Node’s vm module to execute JavaScript

    • ⚠️ The node:vm module is not a security mechanism. Do not use it to run untrusted code.
    • codeGeneration (e.g. eval) is disabled by default; use setCreateContextOptions({codeGeneration: { strings: true, wasm: true } }) to re-enable.
  • In use on:

Installation

Available on npm

npm install node-retrieve-globals

Usage

Works from Node.js with ESM and CommonJS:

import { RetrieveGlobals } from "node-retrieve-globals";
// const { RetrieveGlobals } = require("node-retrieve-globals");

And then:

let code = `var a = 1;
const b = "hello";

function hello() {}`;

let vm = new RetrieveGlobals(code);

await vm.getGlobalContext();

// or sync:
// vm.getGlobalContextSync();

Returns:

{ a: 1, b: "hello", hello: function hello() {} }

Pass in your own Data and reference it in the JavaScript code

let code = `let ref = myData;`;

let vm = new RetrieveGlobals(code);

await vm.getGlobalContext({ myData: "hello" });

// or sync:
// vm.getGlobalContextSync({ myData: "hello" });

Returns:

{ ref: "hello" }

Advanced options

// Defaults shown
let options = {
	reuseGlobal: false, // re-use Node.js `global`, important if you want `console.log` to log to your console as expected.
	dynamicImport: false, // allows `import()`
};

await vm.getGlobalContext({}, options);

FAQs

Package last updated on 03 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc