Skip to content

Commit 9a9d98e

Browse files
author
Jake Champion
committed
docs: improve documentation for fastly:backend module
1 parent 699e18a commit 9a9d98e

File tree

2 files changed

+106
-16
lines changed

2 files changed

+106
-16
lines changed

test-d/fastly:backend.test-d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {expectError, expectType} from 'tsd';
1717
expectType<Backend>(new Backend({name: 'eu', target: 'www.example.com', tlsMinVersion: 1.2,}))
1818
expectType<Backend>(new Backend({name: 'eu', target: 'www.example.com', tlsMaxVersion: 1.2,}))
1919
expectType<Backend>(new Backend({name: 'eu', target: 'www.example.com', certificateHostname: 'example.com',}))
20-
expectType<Backend>(new Backend({name: 'eu', target: 'www.example.com', checkCertificate: false,}))
2120
expectType<Backend>(new Backend({name: 'eu', target: 'www.example.com', caCertificate: '',}))
2221
expectType<Backend>(new Backend({name: 'eu', target: 'www.example.com', ciphers: 'DEFAULT',}))
2322
expectType<Backend>(new Backend({name: 'eu', target: 'www.example.com', sniHostname: 'example.com',}))

types/fastly:backend.d.ts

Lines changed: 106 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,56 @@ declare module 'fastly:backend' {
22
interface BackendConfiguration {
33
/**
44
* The name of the backend.
5+
* The name has to be between 1 and 254 characters inclusive.
6+
* The name can be whatever you would like, as long as it does not match the name of any of the static service backends nor match any other dynamic backends built during a single execution of the application.
7+
*
8+
* @throws {TypeError} Throws a TypeError if the value is not valid. I.E. The value is null, undefined, an empty string or a string with more than 254 characters.
59
*/
610
name: string;
711
/**
812
* A hostname, IPv4, or IPv6 address for the backend as well as an optional port.
9-
* E.G. "hostname", "ip address", "hostname:port", or "ip address:port"
13+
* If set, the target has to be at-least 1 character.
14+
*
15+
* @example hostname
16+
* "example.com"
17+
* @example hostname and port
18+
* "example.com:443"
19+
* @example ip address
20+
* "1.2.3.4"
21+
* @example ip address and port
22+
* "1.2.3.4:8080"
23+
*
24+
* @throws {TypeError} Throws a TypeError if the value is not valid. I.E. Is null, undefined, an empty string, not a valid IP address or host, or is the string `::`
1025
*/
1126
target: string;
1227
/**
1328
* If set, will force the HTTP Host header on connections to this backend to be the supplied value.
29+
*
30+
* @example
31+
* "example.com:443"
32+
*
33+
* @throws {TypeError} Throws a TypeError if the value is an empty string.
1434
*/
1535
hostOverride?: string;
1636
/**
1737
* Maximum duration in milliseconds to wait for a connection to this backend to be established.
1838
* If exceeded, the connection is aborted and a 503 response will be presented instead.
19-
* Defaults to 1,000 milliseconds.
39+
* @defaultValue 1_000
40+
* @throws {RangeError} Throws a RangeError if the value is negative or greater than or equal to 2^32
2041
*/
2142
connectTimeout?: number;
2243
/**
2344
* Maximum duration in milliseconds to wait for the server response to begin after a TCP connection is established and the request has been sent.
2445
* If exceeded, the connection is aborted and a 503 response will be presented instead.
25-
* Defaults to 15,000 milliseconds.
46+
* @defaultValue 15_000
47+
* @throws {RangeError} Throws a RangeError if the value is negative or greater than or equal to 2^32
2648
*/
2749
firstByteTimeout?: number;
2850
/**
2951
* Maximum duration in milliseconds that Fastly will wait while receiving no data on a download from a backend.
3052
* If exceeded, the response received so far will be considered complete and the fetch will end.
31-
* Defaults to 10,000 milliseconds.
53+
* @defaultValue 10_000
54+
* @throws {RangeError} Throws a RangeError if the value is negative or greater than or equal to 2^32
3255
*/
3356
betweenBytesTimeout?: number;
3457
/**
@@ -38,51 +61,119 @@ declare module 'fastly:backend' {
3861
/**
3962
* Minimum allowed TLS version on SSL connections to this backend.
4063
* If the backend server is not able to negotiate a connection meeting this constraint, a 503 response will be presented instead.
64+
*
65+
* @throws {RangeError} Throws a RangeError if the value is not 1, 1.1, 1.2, or 1.3
4166
*/
42-
tlsMinVersion?: number;
67+
tlsMinVersion?: 1 | 1.1 | 1.2 | 1.3;
4368
/**
4469
* Maximum allowed TLS version on SSL connections to this backend.
4570
* If the backend server is not able to negotiate a connection meeting this constraint, a 503 response will be presented instead.
71+
*
72+
* @throws {RangeError} Throws a RangeError if the value is not 1, 1.1, 1.2, or 1.3
4673
*/
47-
tlsMaxVersion?: number;
74+
tlsMaxVersion?: 1 | 1.1 | 1.2 | 1.3;
4875
/**
4976
* Define the hostname that the server certificate should declare.
77+
*
78+
* @throws {TypeError} Throws a TypeError if the value is an empty string.
5079
*/
5180
certificateHostname?: string;
52-
/**
53-
* Whether or not to validate the server certificate.
54-
* Highly recommended to enable this if `useSSL` is set to `true`.
55-
*/
56-
checkCertificate?: boolean;
5781
/**
5882
* The CA certificate to use when checking the validity of the backend.
83+
*
84+
* @throws {TypeError} Throws a TypeError if the value is an empty string.
5985
*/
6086
caCertificate?: string;
6187
/**
6288
* List of OpenSSL ciphers to support for connections to this origin.
6389
* If the backend server is not able to negotiate a connection meeting this constraint, a 503 response will be presented instead.
90+
*
91+
* [List of ciphers supported by Fastly](https://developer.fastly.com/learning/concepts/routing-traffic-to-fastly/#use-a-tls-configuration).
92+
*
93+
* @throws {TypeError} Throws a TypeError if the value is an empty string.
6494
*/
6595
ciphers?: string;
6696
/**
6797
* The SNI hostname to use on connections to this backend.
98+
*
99+
* @throws {TypeError} Throws a TypeError if the value is an empty string.
68100
*/
69101
sniHostname?: string;
70102
}
71103

72104
/**
73-
* Class for creating new [Fastly Backends](https://developer.fastly.com/reference/api/services/backend/).
105+
* Class for dynamically creating new [Fastly Backends](https://developer.fastly.com/reference/api/services/backend/).
106+
*
107+
* @note
108+
* This feature is in disabled by default for Fastly Services. Please contact [Fastly Support](https://support.fastly.com/hc/en-us/requests/new?ticket_form_id=360000269711) to request the feature be enabled on the Fastly Services which require Dynamic Backends.
74109
*
110+
* By default, Dynamic Backends are disabled within a JavaScript application as it can be a potential
111+
* avenue for third-party JavaScript code to send requests, potentially including sensitive/secret data,
112+
* off to destinations that the JavaScript project was not intending, which could be a security issue.
113+
*
114+
* To enable Dynamic Backends the application will need to explicitly allow Dynamic Backends via:
115+
* ```js
116+
* import { allowDynamicBackends } from "fastly:experimental";
117+
* allowDynamicBackends(true);
118+
* ```
119+
*
75120
* **Note**: Can only be used when processing requests, not during build-time initialization.
121+
*
122+
* @example
123+
* In this example an implicit Dynamic Backend is created when making the fetch request to https://www.fastly.com/ and the response is then returned to the client.
124+
*
125+
* View this example on [Fiddle](https://fiddle.fastly.dev/fiddle/e5b6fa0e).
126+
*
127+
* ```js
128+
* /// <reference types="@fastly/js-compute" />
129+
* import { allowDynamicBackends } from "fastly:experimental";
130+
* allowDynamicBackends(true);
131+
* async function app() {
132+
* // For any request, return the fastly homepage -- without defining a backend!
133+
* return fetch('https://www.fastly.com/');
134+
* }
135+
* addEventListener("fetch", event => event.respondWith(app(event)));
136+
* ```
76137
*
77138
* @example
78-
* Construct a new backend with all properties set to their default values:
139+
* In this example an explicit Dynamic Backend is created and supplied to the fetch request, the response is then returned to the client.
140+
*
141+
* View this example on [Fiddle](https://fiddle.fastly.dev/fiddle/e0c26a33).
142+
*
79143
* ```js
80-
* const myBackend = new Backend({ name: 'fastly', target: 'www.fastly.com'});
144+
* /// <reference types="@fastly/js-compute" />
145+
* import { allowDynamicBackends } from "fastly:experimental";
146+
* import { Backend } from "fastly:backend";
147+
* allowDynamicBackends(true);
148+
* async function app() {
149+
* // For any request, return the fastly homepage -- without defining a backend!
150+
* const backend = new Backend({
151+
* name: 'fastly',
152+
* target: 'fastly.com',
153+
* hostOverride: "www.fastly.com",
154+
* connectTimeout: 1000,
155+
* firstByteTimeout: 15000,
156+
* betweenBytesTimeout: 10000,
157+
* useSSL: true,
158+
* sslMinVersion: 1.3,
159+
* sslMaxVersion: 1.3,
160+
* });
161+
* return fetch('https://www.fastly.com/', {
162+
* backend // Here we are configuring this request to use the backend from above.
163+
* });
164+
* }
165+
* addEventListener("fetch", event => event.respondWith(app(event)));
81166
* ```
82167
*/
83168
class Backend {
84169
/**
85-
* Creates a new Backend object
170+
* Creates a new Backend instance
171+
*
172+
* @example
173+
* Construct a new backend with all properties set to their default values:
174+
* ```js
175+
* const myBackend = new Backend({ name: 'fastly', target: 'www.fastly.com'});
176+
* ```
86177
*/
87178
constructor(configuration: BackendConfiguration);
88179
/**

0 commit comments

Comments
 (0)