-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/add javascript scripting (#114)
* Add javascript functionality and unit tests * Updated documentation * Bumped up the version and resolved audits * Add some more unit tests * Changed docker versions in docker-compose file
- Loading branch information
Showing
23 changed files
with
16,611 additions
and
16,104 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/***** | ||
License | ||
-------------- | ||
Copyright © 2017 Bill & Melinda Gates Foundation | ||
The Mojaloop files are made available by the Bill & Melinda Gates Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. | ||
Contributors | ||
-------------- | ||
This is the official list of the Mojaloop project contributors for this file. | ||
Names of the original copyright holders (individuals or organizations) | ||
should be listed with a '*' in the first column. People who have | ||
contributed from an organization can be listed under the organization | ||
that actually holds the copyright for their contributions (see the | ||
Gates Foundation organization for an example). Those individuals should have | ||
their names indented and be marked with a '-'. Email address can be added | ||
optionally within square brackets <email>. | ||
* Gates Foundation | ||
* ModusBox | ||
* Vijaya Kumar <[email protected]> (Original Author) | ||
-------------- | ||
******/ | ||
|
||
const Sandbox = require('vm') | ||
const axios = require('axios').default | ||
|
||
const consoleWrapperFn = (consoleOutObj) => { | ||
return { | ||
log: function () { | ||
consoleOutObj.stdOut.push(arguments) | ||
} | ||
} | ||
} | ||
|
||
const preScript = ` | ||
(async () => { | ||
consoleOutObj = { | ||
stdOut: [] | ||
} | ||
console = consoleWrapperFn(consoleOutObj) | ||
` | ||
|
||
const postScript = ` | ||
return true | ||
})() | ||
` | ||
|
||
const generateContextObj = async (environmentObj = {}) => { | ||
// const ctx = await createContextAsync({ timeout: 30000 }) | ||
// ctx.executeAsync = util.promisify(ctx.execute) | ||
// ctx.on('error', function (cursor, err) { | ||
// // log the error in postman sandbox | ||
// console.log(cursor, err) | ||
// }) | ||
const contextObj = { | ||
ctx: { | ||
dispose: () => {} | ||
}, | ||
environment: { ...environmentObj }, | ||
axios, | ||
consoleWrapperFn, | ||
executeAsync | ||
} | ||
return contextObj | ||
} | ||
|
||
const executeAsync = async (script, data, contextObj) => { | ||
const fullScript = preScript + script.join('\n') + postScript | ||
let consoleLog = [] | ||
|
||
if (data.response) { | ||
contextObj.response = data.response | ||
} | ||
|
||
try { | ||
await Sandbox.runInNewContext(fullScript, contextObj, { timeout: 30000, microtaskMode: 'afterEvaluate' }) | ||
for (let i = 0; i < contextObj.consoleOutObj.stdOut.length; i++) { | ||
consoleLog.push([{ execution: 0 }, 'log', ...contextObj.consoleOutObj.stdOut[i]]) | ||
} | ||
} catch (err) { | ||
// console.log(err) | ||
for (let i = 0; i < contextObj.consoleOutObj.stdOut.length; i++) { | ||
consoleLog.push([{ execution: 0 }, 'log', ...contextObj.consoleOutObj.stdOut[i]]) | ||
} | ||
consoleLog.push([{ execution: 0 }, 'executionError', err.toString()]) | ||
} | ||
|
||
const result = { | ||
consoleLog: consoleLog, | ||
environment: contextObj.environment | ||
} | ||
consoleLog = [] | ||
return result | ||
} | ||
|
||
module.exports = { | ||
generateContextObj, | ||
executeAsync | ||
} |
Oops, something went wrong.