node-retrieve-globals
Advanced tools
Comparing version 2.0.2 to 2.0.3
{ | ||
"name": "node-retrieve-globals", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Execute a string of JavaScript using Node.js and return the global variable values and functions.", | ||
@@ -21,2 +21,3 @@ "type": "module", | ||
"devDependencies": { | ||
"@zachleat/noop": "^1.0.2", | ||
"ava": "^5.2.0" | ||
@@ -23,0 +24,0 @@ }, |
@@ -5,5 +5,8 @@ # node-retrieve-globals | ||
* Sync and async methods for synchronous or asynchronous JavaScript code respectively. | ||
* Can return any JavaScript data types | ||
* Can provide external variable values as context to the local scope | ||
* 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](https://nodejs.org/api/vm.html#vmruninthiscontextcode-options) | ||
* ⚠️ 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. | ||
@@ -20,5 +23,7 @@ ## Installation | ||
Works from Node.js with ESM and CommonJS: | ||
```js | ||
import { RetrieveGlobals } from "node-retrieve-globals"; | ||
// const { RetrieveGlobals } = require("node-retrieve-globals"); | ||
import { RetrieveGlobals } from "node-retrieve-globals"; | ||
``` | ||
@@ -36,4 +41,6 @@ | ||
vm.getGlobalContextSync(); | ||
// or await vm.getGlobalContext(); | ||
await vm.getGlobalContext(); | ||
// or sync: | ||
// vm.getGlobalContextSync(); | ||
``` | ||
@@ -54,4 +61,6 @@ | ||
vm.getGlobalContextSync({ myData: "hello" }); | ||
// or await vm.getGlobalContext({ myData: "hello" }); | ||
await vm.getGlobalContext({ myData: "hello" }); | ||
// or sync: | ||
// vm.getGlobalContextSync({ myData: "hello" }); | ||
``` | ||
@@ -58,0 +67,0 @@ |
@@ -51,2 +51,29 @@ import test from "ava"; | ||
// TODO code that parses fine but throws an error | ||
test("import", async t => { | ||
let vm = new RetrieveGlobals(`const { noop } = await import("@zachleat/noop");`); | ||
let ret = await vm.getGlobalContext(undefined, { | ||
dynamicImport: true | ||
}); | ||
t.is(typeof ret.noop, "function"); | ||
}); | ||
test("global: same console.log", async t => { | ||
let vm = new RetrieveGlobals(`const b = console.log`); | ||
let ret = await vm.getGlobalContext(undefined, { | ||
reuseGlobal: false | ||
}); | ||
t.not(ret.b, console.log); | ||
let ret2 = await vm.getGlobalContext(undefined, { | ||
reuseGlobal: true | ||
}); | ||
t.is(ret2.b, console.log); | ||
}); | ||
test("global: Same URL", async t => { | ||
let vm = new RetrieveGlobals(`const b = URL`); | ||
let ret = await vm.getGlobalContext(undefined, { | ||
reuseGlobal: true | ||
}); | ||
t.is(ret.b, URL); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9240
5
218
69
2