0.5.5 • Published 3 months ago

@globocom/backstage-functions-sandbox v0.5.5

Weekly downloads
13
License
MIT
Repository
github
Last release
3 months 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).

$ npm install @globocom/functions-sandbox

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', `
  async function main(req, res) {
    const result = req.body.x * req.body.y;
    const name = Backstage.env.MY_VAR;
    // you could call await here
    return { 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

Pre-built exceptions

ClassDescription
NotModified()finalize the response sending 304 without body
BadRequest(msg)finalize the response sending 400 with error msg
NotFound(msg)finalize the response sending 404 with error msg
UnprocessableEntity(msg)finalize the response sending 422 with error msg
InternalServerError(msg)finalize the response sending 500 with error msg
0.5.5

3 months ago

0.6.0-next.0

1 year ago

0.5.3

4 years ago

0.5.2

5 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.6

7 years ago