0.2.6 • Published 7 years ago

backstage-functions-sandbox v0.2.6

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Build Status Coverage Status

Backstage functions-sandbox

functions-sandbox is the engine behind Backstage Functions and executes code in isolation (a sandbox). It could be used for both running code in production as well as testing the deployed functions (before they are deployed, hopefully).

Example of usage

const Sandbox = require('backstage-functions-sandbox');

const mySandbox = new Sandbox({
  env: {
     MY_VAR: 'TRUE', // environment variable will be available on Backstage.env.MY_VAR
  },
  globalModules: [ 'path' ], // put all available modules that will allow to import
  asyncTimeout: 10000,
  syncTimeout: 300,
});

const myCode = mySandbox.compileCode('test.js', `
  function main(req, res) {
    const result = req.body.x * req.body.y;
    const name = Backstage.env.MY_VAR;
    res.send({ name, result })
  }
`);

// express.Request compatible
const req = {
  headers: {},
  query: {},
  body: { x: 10, y: 10}
};

mySandbox.runScript(myCode, req).then(({status, body}) => {
  console.info('Result:', status, body);
}, (err) => {
  console.error('Error:', err);
});

Configuration

NameDescriptionExample
envEnvironment variables used by deployed functions{ MY_VAR: 'Some value' }
syncTimeoutTimeout when executing synchronous functionssyncTimeout: 300
asyncTimeoutTimeout when executing asynchronous functionsasyncTimeout: 1000
globalModulesModules that will be available to all functionsglobalModules: [ 'path/to/module' ]

Objects

req -> Request

PropertyTypeDescription
headerspropertyHTTP Headers received from this request
querypropertyHTTP parsed querystring
bodypropertyHTTP body decoded from json

res -> Response

PropertyTypeDescription
set(header, value)methodset a HTTP Header value
status(statusCode)methodchange status code of this response, default: 200
send(body)methodfinalize the response sending body to the client
notModified()methodfinalize the response sending 304 without body
badRequest(msg)methodfinalize the response sending 400 with error msg
notFound(msg)methodfinalize the response sending 404 with error msg
unprocessableEntity(msg)methodfinalize the response sending 422 with error msg
internalServerError(msg)methodfinalize the response sending 500 with error msg
0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.5

7 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago