Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ var requireLike = require('require-like')

function merge (a, b) {
if (!a || !b) return a
// Include all non-enumerable variables, including console (v10+),
// process (v12+), URL, etc.
var keys = Object.getOwnPropertyNames(b)
var keys = Object.keys(b)
for (var k, i = 0, n = keys.length; i < n; i++) {
k = keys[i]
a[k] = b[k]
}
return a
}

var vmGlobals = new vm.Script('Object.getOwnPropertyNames(globalThis)')
.runInNewContext()

// Return the exports/module.exports variable set in the content
// content (String|VmScript): required
module.exports = function (content, filename, scope, includeGlobals) {
Expand All @@ -37,7 +38,19 @@ module.exports = function (content, filename, scope, includeGlobals) {
var _filename = filename || module.parent.filename;

if (includeGlobals) {
// Merge enumerable variables on `global`
merge(sandbox, global)
// Merge all non-enumerable variables on `global`, including console (v10+),
// process (v12+), URL, etc. We first filter out anything that's already in
// the VM scope (i.e. those in the ES standard) so we don't have two copies
Object.getOwnPropertyNames(global).forEach((name) => {
if (!vmGlobals.includes(name)) {
sandbox[name] = global[name]
}
})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review hint

New globals automatically forwarded:

[
  "global",
  "process",
  "Buffer",
  "atob",
  "btoa",
  "URL",
  "URLSearchParams",
  "TextEncoder",
  "TextDecoder",
  "AbortController",
  "AbortSignal",
  "EventTarget",
  "Event",
  "MessageChannel",
  "MessagePort",
  "MessageEvent",
  "clearInterval",
  "clearTimeout",
  "setInterval",
  "setTimeout",
  "queueMicrotask",
  "performance",
  "clearImmediate",
  "setImmediate",
  "__extends",
  "__assign",
  "__rest",
  "__decorate",
  "__param",
  "__metadata",
  "__awaiter",
  "__generator",
  "__exportStar",
  "__createBinding",
  "__values",
  "__read",
  "__spread",
  "__spreadArrays",
  "__spreadArray",
  "__await",
  "__asyncGenerator",
  "__asyncDelegator",
  "__asyncValues",
  "__makeTemplateObject",
  "__importStar",
  "__importDefault",
  "__classPrivateFieldGet",
  "__classPrivateFieldSet",
  "consola",
  "regeneratorRuntime",
];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of these globals are injected by the transpiler. Only stuff like URL process TextDecoder etc. are of our interest.

// `console` exists in VM scope, but we want to pipe the output to the
// process'
sandbox.console = console
sandbox.require = requireLike(_filename)
}

Expand Down