Runtime

The Forge app runtime includes a set of APIs that provide additional functionality to the Forge platform. You can use these APIs to interact with REST endpoints and to store data.

The new native Node.js runtime is now in preview, and will eventually replace Forge’s current runtime environment. We strongly encourage you to test your apps on this new runtime for compatibility and performance.

This new runtime introduces several changes to the way security and egress controls work. Sandboxing and snapshots (including related settings and restrictions) are no longer relevant in the new runtime as well. These changes may result in additional developer responsibilities to help uphold Forge's security.

Before you begin

To access Forge APIs from your app, first install the latest API package by running:

1
2
npm install @forge/api

This package will enable the use of the following APIs:

JavaScript environment

When a Forge app is invoked, the JavaScript code is executed within the app sandbox. This environment differs from a traditional Node.js environment you might already be familiar with. Learn more about the app sandbox.

You'll need to write your functions in a subset of Node.js. Some globals are not exposed, for example:

  • process (except for process.env)
  • queueMicrotask

which means that some NPM packages that depend on these may not function correctly.

The following Node.js built-in modules are not supported:

async_hooks, child_process, cluster, constants, dgram, dns, domain, http2, module, net, perf_hooks, readline, repl, sys, tls, trace_events, tty, v8, vm, worker_threads.

Invocation context

The invocation context is a function that gets called on invocation time. Each module receives different request parameters based on the module type.

A code editor showing the invocation context

Limitations

Invocation time

The invocation context has a timeout. Your function will be stopped and will result in an error if processing exceeds the time limit. Learn more about platform limits

Snapshot context

Snapshot is the mechanism of evaluating your function's global scope at each deployment of the app, rather than at every invocation. Forge apps use snapshots by default, as this improves the response time for your app. You can disable snapshots, but this means your app needs to be fully evaluated on each invocation. For this reason, you should consider leaving snapshots enabled.

The following image highlights a sample snapshot context. Note that the invocation method is not run at this point in time.

A code editor showing the constants declared in the snapshot context

Reference

The snapshots flag is available under the runtime object with a default value of true.

1
2
app:
  runtime:
    snapshots: true # Boolean

Limitations

Environment variables

Environment variables are not available in the snapshot context.

Example

1
2
const myVariable = process.env.MY_VARIABLE;

export const run = () => {
  console.log(myVariable) // Value is undefined
};

Randomness

Random values created at snapshot time are not random on each function invocation.

Example

1
2
const snapshotContextRandom = Math.random();

export const run = () => {
  console.log(snapshotContextRandom); // Same value per invocation
};

Buffers

We discourage using Buffers in the snapshot context. Warnings will be presented at deploy time if this is detected.

1
2
const snapshotContextBuffer = new Buffer(); // May result in unpredictable behavior

export const run = () => {
  // ...
};

Standard globals

Process

The global process object is a partial implementation of the Node JS process.

Object signature

1
2
process

Fields

Rate this page: