Skip to content

Commit

Permalink
fix console global object in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
davideicardi committed Aug 5, 2020
1 parent 6c74ecd commit b30cf37
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 30 deletions.
12 changes: 9 additions & 3 deletions dist/src/PluginVm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/src/PluginVm.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions samples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# live-plugin-manager samples

## How to run samples

The easiest way to run these samples is usint `ts-node`.

```
npm install -g ts-node
ts-node ./samples/basic.ts
```
13 changes: 0 additions & 13 deletions samples/complex.ts

This file was deleted.

12 changes: 9 additions & 3 deletions src/PluginVm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,17 @@ export class PluginVm {
Uint8ClampedArray: srcGlobal.Uint8ClampedArray,
WeakMap: srcGlobal.WeakMap,
WeakSet: srcGlobal.WeakSet,

// create a new instance, but if process is undefined just use null, because undefined is not permitted
process: Object.create(srcGlobal.process || null)
};

// copy properties that are not copied automatically (don't know why..)
// https://stackoverflow.com/questions/59009214/some-properties-of-the-global-instance-are-not-copied-by-spread-operator-or-by-o
if (!sandbox.process) {
sandbox.process = Object.create(srcGlobal.process || null);
}
if (!sandbox.console) {
sandbox.console = new console.Console({ stdout: process.stdout, stderr: process.stderr });
}

// override the global obj to "unlink" it from the original global obj
// and make it unique for each sandbox
sandbox.global = sandbox;
Expand Down
8 changes: 4 additions & 4 deletions test/PluginManagerSuite.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/PluginManagerSuite.js.map

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions test/PluginManagerSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,18 +603,18 @@ describe("PluginManager:", function() {
path.join(manager.options.pluginsPath, "my-test-plugin", "index.js"));
assert.equal(pluginInstance.myGlobals.__dirname, path.join(manager.options.pluginsPath, "my-test-plugin"));

// NOTE: process is not equal because I copy it to override vars
// assert.equal(pluginInstance.myGlobals.process, process);

// assert.equal(pluginInstance.myGlobals.console, console); // TODO Check why this check doesn't work
assert.isDefined(pluginInstance.myGlobals.console);
assert.equal(pluginInstance.myGlobals.clearImmediate, clearImmediate);
assert.equal(pluginInstance.myGlobals.clearInterval, clearInterval);
assert.equal(pluginInstance.myGlobals.clearTimeout, clearTimeout);
assert.equal(pluginInstance.myGlobals.setImmediate, setImmediate);
assert.equal(pluginInstance.myGlobals.setInterval, setInterval);
assert.equal(pluginInstance.myGlobals.setTimeout, setTimeout);
assert.equal(pluginInstance.myGlobals.Buffer, Buffer);
assert.equal(pluginInstance.myGlobals.Function, Function);

// NOTE: process and console are not the same but they should be available
assert.isDefined(pluginInstance.myGlobals.process);
assert.isDefined(pluginInstance.myGlobals.console);
});

it("require absolute files", async function() {
Expand Down
1 change: 1 addition & 0 deletions test/my-test-plugin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports.myGlobals = {
process,
Buffer,
console,
Function,

clearImmediate,
clearInterval,
Expand Down

0 comments on commit b30cf37

Please sign in to comment.