Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@globocom/backstage-functions-sandbox

Package Overview
Dependencies
Maintainers
11
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@globocom/backstage-functions-sandbox

Sandbox for Backstage functions

  • 0.5.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
11
Created
Source

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

Keywords

FAQs

Package last updated on 08 Feb 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc