Shield
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.
123456789101112131415/// <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
Errorif no shield exists with the provided name
Methods
encryptedBackend
View sourceReturn 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 sourcerunningOn(): booleanReturns 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
unencryptedBackend
View sourceReturn 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