@fastly/js-compute
Advanced tools
Changelog
0.5.9 (2022-11-29)
Changelog
0.5.5 (2022-11-23)
Changelog
0.5.4 (2022-09-28)
Note: This feature is disabled by default for Fastly Services. Please contact Fastly Support to request the feature be enabled on the Fastly Services which require Dynamic Backends.
This feature makes it possible to use the standard fetch
within JavaScript applications.
Dynamic Backends is a new feature which enables JavaScript applications to dynamically create new backend server definitions without having to deploy a new version of their Fastly Service. These backends function exactly the same as existing backends, and can be configured in all the same ways as existing backends can via the Fastly Service configuration.
By default, Dynamic Backends are disabled within a JavaScript application as it can be a potential avenue for third-party JavaScript code to send requests, potentially including sensitive/secret data, off to destinations that the JavaScript project was not intending, which could be a security issue. To enable Dynamic Backends the application will need to set fastly.allowDynamicBackends
is to true
.
There are two ways to make use of Dynamic Backends within JavaScript projects:
The first way is by omitting the backend
property definition on the Request instance. The JavaScript Runtime will then create a Dynamic Backend definition using default configuration options. This approach is useful for JavaScript applications as it means that a standard fetch
call will now be possible, which means libraries that use the standard fetch
will begin to work for applications deployed to Fastly.
Below is as an example JavaScript application using the default Dynamic Backend option:
// Enable dynamic backends -- warning, this is potentially dangerous as third-party dependencies could make requests to their own backends, potentially including your sensitive/secret data
fastly.allowDynamicBackends = true;
// For any request, return the fastly homepage -- without defining a backend!
addEventListener("fetch", event => {
event.respondWith(fetch('https://www.fastly.com/'));
});
The second way is by creating a new Dynamic Backend using the new Backend
class. This approach is useful for JavaScript applications that want to have full control over the configuration of the new backend defintion, such as only allowing TLS 1.3 and disallowing older versions of TLS for requests made to the new backend.
E.G.
// Enable dynamic backends -- warning, this is potentially dangerous as third-party dependencies could make requests to their own backends, potentially including your sensitive/secret data
fastly.allowDynamicBackends = true;
// For any request, return the fastly homepage -- without defining a backend!
addEventListener("fetch", event => {
// We are not defining all the possible fields here, the ones which are not defined will use their default value instead.
const backend = new Backend({
name: 'fastly',
target: 'fastly.com',
hostOverride: "www.fastly.com",
sslMinVersion: 1.3,
sslMaxVersion: 1.3,
sniHostname: "www.fastly.com",
});
event.respondWith(fetch('https://www.fastly.com/', {
backend // Here we are configuring this request to use the newly defined backend from above.
}));
});
We have renamed the Dictionary
class to ConfigStore
, the old name Dictionary
still exists but is now deprecated. We recommend replacing Dictionary
with ConfigStore
in your code to avoid having to migrate in the future when Dictionary
is fully removed.
Below is an example application using the ConfigStore
class:
async function app(event) {
const store = new ConfigStore('example')
// Retrieve the contents of the 'hello' key
const hello = await store.get('hello')
return new Response(hello)
}
addEventListener("fetch", event => {
event.respondWith(app(event))
})
Changelog
0.5.3 (2022-09-16)
CVE-2022-39218:
Fixed Math.random
and crypto.getRandomValues
methods to always use sufficiently random values. The previous versions would use a PRNG (pseudorandom number generator) which we would seed with a random value however due to our use of Wizer, the initial value to seed the PRNG was baked-in to the final WebAssembly module meaning the sequence of numbers generated was predictable for that specific WebAssembly module. The new implementations of both Math.random
and crypto.getRandomValues
do not use a PRNG and instead pull random values from WASI (WebAssembly System Interface) libc’s random_get
function, which is always a sufficiently random value.
An attacker with access to the same WebAssembly module that calls the affected methods could use the fixed seed to predict random numbers generated by these functions. This information could be used to bypass cryptographic security controls, for example to disclose sensitive data encrypted by functions that use these generators.
Developers should update affected modules after applying this patch. Any secrets generated using affected versions should be rotated. Any sensitive ciphertext generated using affected versions should be considered unsafe, e.g. and be deleted or re-generated.
Updated the Typescript definitions for the console
methods to indicate that they now accept any number of objects. (#258)
Store the Object-Store key string into a native object to avoid it becoming garbage collected before being used within ObjectStore.prototype.get
or ObjectStore.prototype.put
((381242)
Changelog
0.5.1 (2022-08-31)
type: "module"
from the @fastly/js-compute package.json file as the package still uses require
Changelog
0.5.0 (2022-08-30)
This release adds support for Fastly Object-store, which is globally consistent key-value storage accessible across the Fastly Network. This makes it possible for your Fastly Compute application to read and write from Object-stores.
We've added two classes, ObjectStore
, and ObjectStoreEntry
. ObjectStore
is used to interact with a particular Object-store and ObjectStoreEntry
is a particular value within an Object-store. We've made ObjectStoreEntry
have a similar API as Response
to make it simpler to read and write from Object-stores. I.e. ObjectStoreEntry
has a body
property which is a ReadableStream
and has arrayBuffer
/json
/text
methods - just like Response
.
The way to use these classes is best shown with an example:
async function app(event) {
// Create a connection the the Object-store named 'example-store'
const store = new ObjectStore('example-store')
// Create or update the 'hello' key with the contents 'world'
await store.put('hello', 'world')
// Retrieve the contents of the 'hello' key
// Note: Object-stores are eventually consistent, this means that the updated contents associated may not be available to read from all
// Fastly edge locations immediately and some edge locations may continue returning the previous contents associated with the key.
const hello = await store.get('hello')
// Read the contents of the `hello` key into a string
const hellotext = await hello.text()
return new Response(hellotext)
}
addEventListener("fetch", event => {
event.respondWith(app(event))
})
btoa
and atob
global functionsThese two functions enable you to encode to (btoa) and decode from (atob) Base64 strings. They follow the same specification as the atob
and btoa
functions that exist in web-browsers.
addEventListener("fetch", event => {
event.respondWith(new Response(atob(btoa('hello from fastly'))))
})
Previously our console methods only supported a single argument and would convert the argument to a string via String(argument)
, this unfortunately made it difficult to log out complex objects such as Request objects or similar.
We've updated our console methods and they now support any number of arguments. As well as supporting any number of arguments, we've also changed the implementation to have better support for logging out complex objects.
This is a before and after example of what happens when logging a Request with our console methods.
Before:
const request = new Request('https://www.fastly.com', {body:'I am the body', method: 'POST'});
console.log(request); // outputs `[object Object]`.
After:
const request = new Request('https://www.fastly.com', {body:'I am the body', method: 'POST'});
console.log(request); // outputs `Request: {method: POST, url: https://www.fastly.com/, version: 2, headers: {}, body: null, bodyUsed: false}`.