
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@fastly/compute-js-context
Advanced tools
NOTE:
@fastly/compute-js-contextis provided as a Fastly Labs product. Visit the Fastly Labs site for terms of use.
@fastly/compute-js-context exposes Fastly Compute resources to your Compute JavaScript application. It provides it as one typed, immutable context object, or as a custom, strongly-typed object mapping your service's specific resource names to their handles.
Context ObjectThe core export is createContext, which returns a single, immutable Context object.
type Context = Readonly<{
ACLS: Acls;
BACKENDS: Backends;
CONFIG_STORES: ConfigStores;
ENV: Env;
KV_STORES: KVStores;
LOGGERS: Loggers;
SECRET_STORES: SecretStores;
}>;
Each top-level field is a Proxy. Accessing a property lazily looks up and caches the corresponding runtime object. Unknown names return undefined.
ctx.*undefined for optional resourcesContext is immutablebuildContextProxy to create a bespoke, typed binding object for your appRequires a Fastly Compute JavaScript project (
@fastly/js-compute).
npm install @fastly/compute-js-context
This example shows basic usage of the main Context object.
/// <reference types="@fastly/js-compute" />
import { createContext } from '@fastly/compute-js-context';
addEventListener('fetch', (event) => event.respondWith(handler(event)));
async function handler(event) {
const ctx = createContext();
// Environment - simple strings (or empty string if not present)
console.log('FASTLY_SERVICE_VERSION', ctx.ENV.FASTLY_SERVICE_VERSION);
// Secret Store - property is the SecretStore object, or undefined if not configured
const awsSecrets = ctx.SECRET_STORES.AWS_CREDENTIALS;
const keyId = await awsSecrets?.get('AWS_ACCESS_KEY_ID');
console.log('key id', keyId?.plaintext());
// Backend - pass to fetch() options, or learn about the backend
const origin = ctx.BACKENDS.origin;
const res = await fetch("/", { backend: origin });
// Logger - send output to a named Fastly logging endpoint.
const myLogEndpoint = ctx.LOGGERS.my_log_endpoint;
myLogEndpoint?.log(`${event.request.url} ${event.client.address}`);
return new Response("ok");
}
buildContextProxyFor an even better developer experience, buildContextProxy creates a typed object that maps your application's specific binding names to the underlying Fastly resources. This gives you shorter names and better autocompletion.
First, define your bindings. The key is the name you want to use, and the value is a string identifying the resource type and (optionally) the resource name if it differs from the key.
Then, pass these definitions to buildContextProxy().
/// <reference types="@fastly/js-compute" />
import { createContext, buildContextProxy, type BindingsDefs } from '@fastly/compute-js-context';
// Define your application's bindings
const bindingsDefs = {
// Simple mapping: key 'assets' maps to KVStore named 'assets'
assets: 'KVStore',
// Remapping: key 'origin' maps to Backend named 'origin-s3'
origin: 'Backend:origin-s3',
// Explicit mapping for a logger
auditLog: 'Logger:audit_log',
} satisfies BindingsDefs; // <-- for full type safety
// This is the generated type for your bindings object
type Bindings = BuildBindings<typeof bindingsDefs>;
addEventListener('fetch', (event) => event.respondWith(handler(event)));
async function handler(event: FetchEvent): Promise<Response> {
const ctx = createContext();
// Create the typed environment
const bindings: Bindings = buildContextProxy(ctx, bindingsDefs);
// Now use your custom bindings!
const asset = await bindings.assets?.get('/index.html');
const res = await fetch("/", { backend: bindings.origin });
bindings.auditLog?.log('Request received');
return new Response(asset);
}
createContext(): ContextCreates the main immutable Context. Each sub-object is a Proxy that:
undefined for names that don’t exist (except for ENV, which returns '')Object.keys)buildContextProxy<T>(context: Context, bindingsDefs: T): BuildBindings<T>Creates a custom, strongly-typed proxy object based on your definitions.
context: An instance of the main Context objectbindingsDefs: A const object defining your desired bindings
contextProxy object'ResourceType' or 'ResourceType:actual-name'contextProxy with your custom bindings. Accessing a property on this object looks up the resource from the main ContextThese are the raw shapes available on the main
Contextobject.
ENV: Readonly<Record<string, string>>SECRET_STORES: Readonly<Record<string, SecretStore | undefined>>CONFIG_STORES: Readonly<Record<string, ConfigStore | undefined>>KV_STORES: Readonly<Record<string, KVStore | undefined>>BACKENDS: Readonly<Record<string, Backend | undefined>>LOGGERS: Readonly<Record<string, Logger | undefined>>ACLS: Readonly<Record<string, Acl | undefined>>Readonlyundefined for missing resources and code accordingly (?./guard)If you encounter any non-security-related bug or unexpected behavior, please file an issue using the bug report template.
Please see our SECURITY.md for guidance on reporting security-related issues.
MIT.
FAQs
Surfaces Fastly Compute environment as a context object
We found that @fastly/compute-js-context demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 13 open source maintainers collaborating on the project.
Did you know?

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.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.