From 7ed516a6033052ff2215b5a9af8f495efe5ff46b Mon Sep 17 00:00:00 2001 From: Jacob Horbulyk Date: Tue, 26 Nov 2019 15:15:41 +0100 Subject: [PATCH 1/6] Fix package.json. --- package-lock.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 160e2a8..137a58c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "eio-code-component", - "version": "0.0.4", + "version": "1.1.0", "lockfileVersion": 1, "requires": true, "dependencies": { From 271ee01c324f9bc163e63b693978b5090d44393e Mon Sep 17 00:00:00 2001 From: Jacob Horbulyk Date: Tue, 26 Nov 2019 15:26:35 +0100 Subject: [PATCH 2/6] Add breaking test. --- spec/code.spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/spec/code.spec.js b/spec/code.spec.js index 81ce016..0f2d14f 100644 --- a/spec/code.spec.js +++ b/spec/code.spec.js @@ -155,4 +155,26 @@ describe('code test', () => { expect(result.body).equal(200); }); }); + + + it('Validate Node Globals are available', async () => { + code = ` + async function run(msg, cfg, snapshot) { + console.log('Hello code, incoming message is msg=%j', msg); + let s = "abc"; + let buff = Buffer.from(s); + let base64data = buff.toString('base64'); + // Create message to be emitted + var data = messages.newMessageWithBody({message: base64data}); + // Emit the data event + emitter.emit('data', data); + // No need to emit end + console.log('Finished execution'); + } + `; + + await action.process.call(self, { body: {} }, { code }, {}); + const result = emitter.emit.getCall(0).args[1]; + expect(result.body).deep.equal({ message: 'YWJj' }); + }); }); From b82dc9bce84c27e150763f94e68ff2437ca01024 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 6 Dec 2019 15:33:09 +0000 Subject: [PATCH 3/6] Bump eslint-utils from 1.4.0 to 1.4.3 Bumps [eslint-utils](https://github.com/mysticatea/eslint-utils) from 1.4.0 to 1.4.3. - [Release notes](https://github.com/mysticatea/eslint-utils/releases) - [Commits](https://github.com/mysticatea/eslint-utils/compare/v1.4.0...v1.4.3) Signed-off-by: dependabot[bot] --- package-lock.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 198e8ed..eb21383 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1054,12 +1054,12 @@ } }, "eslint-utils": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.4.0.tgz", - "integrity": "sha512-7ehnzPaP5IIEh1r1tkjuIrxqhNkzUJa9z3R92tLJdZIVdWaczEhr3EbhGtsMrVxi1KeR8qA7Off6SWc5WNQqyQ==", + "version": "1.4.3", + "resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.4.3.tgz", + "integrity": "sha512-fbBN5W2xdY45KulGXmLHZ3c3FHfVYmKg0IrAKGOkT/464PQsx2UeIzfz1RmEci+KLm1bBaAzZAh8+/E+XAeZ8Q==", "dev": true, "requires": { - "eslint-visitor-keys": "^1.0.0" + "eslint-visitor-keys": "^1.1.0" } }, "eslint-visitor-keys": { From 3ac556ac298f0c047ca7fee14900f7b653497191 Mon Sep 17 00:00:00 2001 From: Jacob Horbulyk Date: Wed, 4 Dec 2019 14:51:25 +0100 Subject: [PATCH 4/6] Add node globals and refactor. --- CHANGELOG.md | 4 ++++ README.md | 31 +++++++++++++------------------ actions/code.js | 24 +++++++++++++++++------- package-lock.json | 2 +- package.json | 4 ++-- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26f1b8b..57bb98b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`. diff --git a/README.md b/README.md index dde7b1b..7175da1 100644 --- a/README.md +++ b/README.md @@ -21,24 +21,19 @@ 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. + +### 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 diff --git a/actions/code.js b/actions/code.js index 737de3e..7435e4b 100644 --- a/actions/code.js +++ b/actions/code.js @@ -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 }, 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, { diff --git a/package-lock.json b/package-lock.json index 137a58c..a7e5a79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "eio-code-component", - "version": "1.1.0", + "version": "1.2.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 9827eff..17967aa 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "eio-code-component", - "version": "1.1.0", + "version": "1.2.0", "description": "elastic.io code component", "engines": { - "node": "^10" + "node": "10" }, "scripts": { "docker:build": "docker build -t elasticio/code-component:$(git log -n1 --format=%H) .", From 8554514ad564bf940ab1cbb9bf754df21fc15456 Mon Sep 17 00:00:00 2001 From: Jacob Horbulyk Date: Thu, 12 Dec 2019 17:37:49 +0100 Subject: [PATCH 5/6] Add Line to documentation. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7175da1..dc0d5c6 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ 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 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 From 48e46df7ce2416f5de59cb6b7e555b0e21d72f99 Mon Sep 17 00:00:00 2001 From: Jacob Horbulyk Date: Fri, 13 Dec 2019 11:18:58 +0100 Subject: [PATCH 6/6] Fix small logic bug. --- actions/code.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/actions/code.js b/actions/code.js index 7435e4b..ce80ce3 100644 --- a/actions/code.js +++ b/actions/code.js @@ -17,15 +17,16 @@ function wait(timeout) { // eslint-disable-next-line consistent-return,func-names exports.process = async function (msg, conf, snapshot) { + const vmExports = {}; const ctx = vm.createContext({ // Node Globals Buffer, clearInterval, clearTimeout, console, - exports: {}, + exports: vmExports, global: {}, - module: { exports }, + module: { exports: vmExports }, process, require, setInterval,