Shield

Classin shielding@fastly/js-compute 4.0.0since 3.37.0View source

Represents a Fastly shield location, used to route requests through a designated shield POP.

Note: Can only be used when processing requests, not during build-time initialization.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/// <reference types="@fastly/js-compute" />
import { Shield } from "fastly:shielding";
async function app(event) {
const shield = new Shield('wsi-australia-au');
// If running on the shield POP, fetch from the origin directly
if (shield.runningOn()) {
return await fetch('https://my-site.com/anything', { backend: 'my-backend' });
}
// Otherwise, route the request through the shield using an encrypted connection
return await fetch(event.request, { backend: shield.encryptedBackend() });
}
addEventListener("fetch", (event) => event.respondWith(app(event)))

Constructor

new Shield(name: string)

Load information about the given shield.

Shield names are shield codes as listed in the shield locations documentation (e.g. "pdx-or-us" for Portland, OR, USA or "paris-fr" for Paris, France).

Parameters

name: string

The shield code identifying the shield POP

Throws

  • Throws an Error if no shield exists with the provided name

Methods

encryptedBackend

View source
encryptedBackend(configuration?: ShieldBackendConfiguration): Backend

Return a Backend representing an encrypted connection to this shield POP. This is almost always the backend you want to use.

Parameters

configuration: ShieldBackendConfigurationoptional

Optional backend configuration

Returns

runningOn

View source
runningOn(): boolean

Returns whether the code is currently running on (or effectively on) the given shield POP.

This may also return true when Fastly is routing traffic from the target shield POP to the current POP for load balancing or performance reasons, since the result would be approximately identical to running on the target shield itself.

Returns

boolean

unencryptedBackend

View source
unencryptedBackend(configuration?: ShieldBackendConfiguration): Backend

Return a Backend representing an unencrypted connection to this shield POP. Prefer encryptedBackend unless the data is already encrypted, as data sent over this backend travels unencrypted over the open internet.

Parameters

configuration: ShieldBackendConfigurationoptional

Optional backend configuration

Returns