Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add node globals to context #33

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.2.0 (Dec 12, 2019)
* Add support for more node global objects
* Update Sailor to version 2.5.1

## 1.0.1 (November 13, 2019)
* Added snapshots support by passing 2 more arguments to `run` function: `cfg` and `snapshot`.
* All log statements changed from `info` level to `debug`.
Expand Down
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,20 @@ However, don't let the simple look fool you - it has a full-fledged interface wi

## Available Variables and Libraries
Here are the available variables and libraries that can be used within the context of execution. The most up-to-date list
can always be found in be used within the context of execution. The most up-to-date list can always be found in code.js
of the component. Below is a sample for the reference:

- `console` - more on [Node.js console](https://nodejs.org/dist/latest-v5.x/docs/api/console.html)
- `process` - current Node.js process
- `require` - module require
- `setTimeout` - more on [setTimeout](https://nodejs.org/dist/latest-v5.x/docs/api/timers.html)
- `clearTimeout` - more on [clearTimeout](https://nodejs.org/dist/latest-v5.x/docs/api/timers.html)
- `setInterval` - more on setInterval
- `clearInterval` - more on clearInterval
- `msg` - incoming message containing the payload from the previous step
- `cfg` - step's configuration. At the moment contains only one property: `code` (the code, being executed)
- `snapshot` - step's snapshot
- `exports` - just a plain object `{}`
- `messages` - utility for convenient message creation
- `request` - Http Client (wrapped in `co` - [this library](https://www.npmjs.com/package/co-request))
- `wait` - wait
- `emitter` user to emit messages and errors
can always be found in be used within the context of execution or in `code.js` of the component. Below is a sample for the reference.
Built-in Node.js global objects are also supported.

### Elastic.io Specific Functionality
- `msg` - incoming message containing the payload from the previous step
- `cfg` - step's configuration. At the moment contains only one property: `code` (the code, being executed)
- `snapshot` - step's snapshot
- `messages` - utility for convenient message creation
- `emitter` user to emit messages and errors

### Other Libraries/functions
- `wait(numberOfMilliscondsToSleep)` - Utility function for sleeping
- [`request`](https://github.com/request/request) - Http Client (wrapped in `co` - [this library](https://www.npmjs.com/package/co-request) so that it is pre-promisified)
- `_` - [LoDash](https://lodash.com/)

## Code component usage Examples

Expand Down
24 changes: 17 additions & 7 deletions actions/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,30 @@ function wait(timeout) {
// eslint-disable-next-line consistent-return,func-names
exports.process = async function (msg, conf, snapshot) {
const ctx = vm.createContext({
_,
// Node Globals
Buffer,
clearInterval,
clearTimeout,
console,
exports: {},
global: {},
module: { exports },
jhorbulyk marked this conversation as resolved.
Show resolved Hide resolved
process,
require,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
msg,
exports: {},
setTimeout,
URL,
URLSearchParams,

// Elasticio Specific Functionality
emitter: this,
messages,
msg,

// Other Libraries
_,
request,
wait: wait.bind(this),
emitter: this,
});
this.logger.debug('Running the code %s', conf.code);
vm.runInContext(conf.code, ctx, {
Expand Down
Loading