Skip to content

Commit 4e3b93d

Browse files
author
Guy Bedford
authored
feat: support backend property on both Request and Response, as a Backend instance (#1019)
1 parent 616e898 commit 4e3b93d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1187
-724
lines changed

integration-tests/js-compute/fixtures/app/src/assertions.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ export function pass(message = '') {
3636
return new Response(message);
3737
}
3838

39-
export function fail(message = '') {
40-
throw new Response(message, { status: 500 });
41-
}
42-
4339
function prettyPrintSymbol(a) {
4440
if (typeof a === 'symbol') {
4541
return String(a);
@@ -48,7 +44,7 @@ function prettyPrintSymbol(a) {
4844
}
4945
export function assert(actual, expected, code) {
5046
if (!deepEqual(actual, expected)) {
51-
fail(
47+
throw new Error(
5248
`Expected \`${code}\` to equal \`${JSON.stringify(prettyPrintSymbol(expected))}\` - Found \`${JSON.stringify(prettyPrintSymbol(actual))}\``,
5349
);
5450
}
@@ -58,7 +54,7 @@ export { assert as strictEqual };
5854

5955
export function ok(truthy, code) {
6056
if (!truthy) {
61-
fail(
57+
throw new Error(
6258
`Expected ${code ? ' ' + code : ''}to be truthy - Found \`${JSON.stringify(prettyPrintSymbol(truthy))}\``,
6359
);
6460
}
@@ -68,7 +64,7 @@ export async function assertResolves(func) {
6864
try {
6965
await func();
7066
} catch (error) {
71-
fail(
67+
throw new Error(
7268
`Expected \`${func.toString()}\` to resolve - Found it rejected: ${error.name}: ${error.message}`,
7369
);
7470
}
@@ -80,23 +76,25 @@ export async function assertRejects(func, errorClass, errorMessage) {
8076
} catch (error) {
8177
if (errorClass) {
8278
if (error instanceof errorClass === false) {
83-
fail(
79+
throw new Error(
8480
`Expected \`${func.toString()}\` to reject instance of \`${errorClass.name}\` - Found instance of \`${error.name}\``,
8581
);
8682
}
8783
}
8884

8985
if (errorMessage) {
9086
if (error.message !== errorMessage) {
91-
fail(
87+
throw new Error(
9288
`Expected \`${func.toString()}\` to reject error message of \`${errorMessage}\` - Found \`${error.message}\``,
9389
);
9490
}
9591
}
9692

9793
return;
9894
}
99-
fail(`Expected \`${func.toString()}\` to reject - Found it did not reject`);
95+
throw new Error(
96+
`Expected \`${func.toString()}\` to reject - Found it did not reject`,
97+
);
10098
}
10199

102100
export function assertThrows(func, errorClass, errorMessage) {
@@ -105,30 +103,32 @@ export function assertThrows(func, errorClass, errorMessage) {
105103
} catch (error) {
106104
if (errorClass) {
107105
if (error instanceof errorClass === false) {
108-
fail(
106+
throw new Error(
109107
`Expected \`${func.toString()}\` to throw instance of \`${errorClass.name}\` - Found instance of \`${error.name}\`: ${error.message}\n${error.stack}`,
110108
);
111109
}
112110
}
113111

114112
if (errorMessage) {
115113
if (error.message !== errorMessage) {
116-
fail(
114+
throw new Error(
117115
`Expected \`${func.toString()}\` to throw error message of \`${errorMessage}\` - Found \`${error.message}\``,
118116
);
119117
}
120118
}
121119

122120
return;
123121
}
124-
fail(`Expected \`${func.toString()}\` to throw - Found it did not throw`);
122+
throw new Error(
123+
`Expected \`${func.toString()}\` to throw - Found it did not throw`,
124+
);
125125
}
126126

127127
export function assertDoesNotThrow(func) {
128128
try {
129129
func();
130130
} catch (error) {
131-
fail(
131+
throw new Error(
132132
`Expected \`${func.toString()}\` to not throw - Found it did throw: ${error.name}: ${error.message}`,
133133
);
134134
}

integration-tests/js-compute/fixtures/app/src/dynamic-backend.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,24 @@ routes.set('/backend/timeout', async () => {
5555
await assertRejects(() => fetch('https://http-me.glitch.me/headers'));
5656
},
5757
);
58-
routes.set('/implicit-dynamic-backend/dynamic-backends-enabled', async () => {
59-
allowDynamicBackends(true);
60-
await assertResolves(async () => {
61-
await fetch('https://http-me.glitch.me/headers');
62-
const backend = Backend.fromName('http-me.glitch.me');
63-
strictEqual(backend.isSSL, true);
64-
});
65-
await assertResolves(() => fetch('https://www.fastly.com'));
66-
enforceExplicitBackends();
67-
await assertRejects(() => fetch('https://www.fastly.com'));
68-
enforceExplicitBackends('TheOrigin');
69-
await assertResolves(() => fetch('https://www.fastly.com'));
70-
});
58+
routes.set(
59+
'/implicit-dynamic-backend/dynamic-backends-enabled',
60+
async (evt) => {
61+
allowDynamicBackends(true);
62+
strictEqual(evt.request.backend, undefined);
63+
strictEqual(new Response('test').backend, undefined);
64+
await assertResolves(async () => {
65+
const res = await fetch('https://http-me.glitch.me/headers');
66+
strictEqual(res.backend.name, 'http-me.glitch.me');
67+
strictEqual(res.backend.isSSL, true);
68+
});
69+
await assertResolves(() => fetch('https://www.fastly.com'));
70+
enforceExplicitBackends();
71+
await assertRejects(() => fetch('https://www.fastly.com'));
72+
enforceExplicitBackends('TheOrigin');
73+
await assertResolves(() => fetch('https://www.fastly.com'));
74+
},
75+
);
7176
routes.set(
7277
'/implicit-dynamic-backend/dynamic-backends-enabled-called-twice',
7378
async () => {

integration-tests/js-compute/fixtures/app/src/index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import { routes } from './routes.js';
55
import { env } from 'fastly:env';
6-
import { fail } from './assertions.js';
76

87
import './async-select.js';
98
import './btoa.js';
@@ -27,7 +26,6 @@ import './fastly-global.js';
2726
import './fetch-errors.js';
2827
import './geoip.js';
2928
import './headers.js';
30-
import './hono.js';
3129
import './include-bytes.js';
3230
import './kv-store.js';
3331
import './logger.js';
@@ -72,7 +70,9 @@ async function app(event) {
7270
res = (await routeHandler(event)) || new Response('ok');
7371
} else {
7472
try {
75-
fail(`${path} endpoint does not exist`);
73+
return (res = new Response(`${path} endpoint does not exist`, {
74+
status: 500,
75+
}));
7676
} catch (errRes) {
7777
res = errRes;
7878
}
@@ -82,11 +82,12 @@ async function app(event) {
8282
res = error;
8383
} else {
8484
try {
85-
fail(
85+
return (res = new Response(
8686
`The routeHandler for ${path} threw a [${error.constructor?.name ?? error.name}] error: ${error.message || error}` +
8787
'\n' +
8888
error.stack,
89-
);
89+
{ status: 500 },
90+
));
9091
} catch (errRes) {
9192
res = errRes;
9293
}

integration-tests/js-compute/fixtures/app/tests.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,12 +1357,6 @@
13571357
]
13581358
}
13591359
},
1360-
"GET /hono": {
1361-
"downstream_response": {
1362-
"status": 200,
1363-
"body_prefix": "{\n \"args\": {},"
1364-
}
1365-
},
13661360
"POST /tee": {
13671361
"downstream_request": {
13681362
"method": "POST",

integration-tests/js-compute/fixtures/tla/src/assertions.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ export function pass(message = '') {
3636
return new Response(message);
3737
}
3838

39-
export function fail(message = '') {
40-
throw new Response(message, { status: 500 });
41-
}
42-
4339
function prettyPrintSymbol(a) {
4440
if (typeof a === 'symbol') {
4541
return String(a);
@@ -48,17 +44,27 @@ function prettyPrintSymbol(a) {
4844
}
4945
export function assert(actual, expected, code) {
5046
if (!deepEqual(actual, expected)) {
51-
fail(
47+
throw new Error(
5248
`Expected \`${code}\` to equal \`${JSON.stringify(prettyPrintSymbol(expected))}\` - Found \`${JSON.stringify(prettyPrintSymbol(actual))}\``,
5349
);
5450
}
5551
}
5652

53+
export { assert as strictEqual };
54+
55+
export function ok(truthy, code) {
56+
if (!truthy) {
57+
throw new Error(
58+
`Expected ${code ? ' ' + code : ''}to be truthy - Found \`${JSON.stringify(prettyPrintSymbol(truthy))}\``,
59+
);
60+
}
61+
}
62+
5763
export async function assertResolves(func) {
5864
try {
5965
await func();
6066
} catch (error) {
61-
fail(
67+
throw new Error(
6268
`Expected \`${func.toString()}\` to resolve - Found it rejected: ${error.name}: ${error.message}`,
6369
);
6470
}
@@ -70,23 +76,25 @@ export async function assertRejects(func, errorClass, errorMessage) {
7076
} catch (error) {
7177
if (errorClass) {
7278
if (error instanceof errorClass === false) {
73-
fail(
79+
throw new Error(
7480
`Expected \`${func.toString()}\` to reject instance of \`${errorClass.name}\` - Found instance of \`${error.name}\``,
7581
);
7682
}
7783
}
7884

7985
if (errorMessage) {
8086
if (error.message !== errorMessage) {
81-
fail(
87+
throw new Error(
8288
`Expected \`${func.toString()}\` to reject error message of \`${errorMessage}\` - Found \`${error.message}\``,
8389
);
8490
}
8591
}
8692

8793
return;
8894
}
89-
fail(`Expected \`${func.toString()}\` to reject - Found it did not reject`);
95+
throw new Error(
96+
`Expected \`${func.toString()}\` to reject - Found it did not reject`,
97+
);
9098
}
9199

92100
export function assertThrows(func, errorClass, errorMessage) {
@@ -95,35 +103,39 @@ export function assertThrows(func, errorClass, errorMessage) {
95103
} catch (error) {
96104
if (errorClass) {
97105
if (error instanceof errorClass === false) {
98-
fail(
106+
throw new Error(
99107
`Expected \`${func.toString()}\` to throw instance of \`${errorClass.name}\` - Found instance of \`${error.name}\`: ${error.message}\n${error.stack}`,
100108
);
101109
}
102110
}
103111

104112
if (errorMessage) {
105113
if (error.message !== errorMessage) {
106-
fail(
114+
throw new Error(
107115
`Expected \`${func.toString()}\` to throw error message of \`${errorMessage}\` - Found \`${error.message}\``,
108116
);
109117
}
110118
}
111119

112120
return;
113121
}
114-
fail(`Expected \`${func.toString()}\` to throw - Found it did not throw`);
122+
throw new Error(
123+
`Expected \`${func.toString()}\` to throw - Found it did not throw`,
124+
);
115125
}
116126

117127
export function assertDoesNotThrow(func) {
118128
try {
119129
func();
120130
} catch (error) {
121-
fail(
131+
throw new Error(
122132
`Expected \`${func.toString()}\` to not throw - Found it did throw: ${error.name}: ${error.message}`,
123133
);
124134
}
125135
}
126136

137+
export { deepEqual as deepStrictEqual };
138+
127139
export function deepEqual(a, b) {
128140
var aKeys;
129141
var bKeys;
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { routes } from './routes';
22

3-
routes.set('/hello-world', () => {
4-
return new Response('hello world');
3+
routes.set('/hello-world', (evt) => {
4+
console.log(evt.request);
5+
const res = new Response('hello world');
6+
return res;
57
});

integration-tests/js-compute/fixtures/app/src/hono.js renamed to integration-tests/js-compute/fixtures/tla/src/hono.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ routes.set('/hono', async (evt) => {
1313
'https://http-me.glitch.me/anything',
1414
c.req.raw,
1515
);
16-
console.log('newRequest: ' + newRequest.url);
1716
newRequest.headers.set('X-Connecting-IP', `${REALIP}`);
1817
newRequest.headers.set(`${REAL_HDR}`, `${REALIP}`);
1918
let cacheOverride = new CacheOverride('pass');
@@ -24,8 +23,8 @@ routes.set('/hono', async (evt) => {
2423
} else {
2524
newRequest.headers.set('X-KnownDevice', '0');
2625
}
26+
console.log('a', newRequest.backend);
2727
const res = await fetch(newRequest, { cacheOverride });
28-
console.log('res.status: ' + res.status);
2928
const newResponse = new Response(res.body, res);
3029
if (c.req.header('Fastly-Debug') === 'bw-fastly-debug') {
3130
newResponse.headers.set('X-Connecting-IP', `${REALIP}`);

integration-tests/js-compute/fixtures/tla/src/index.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33

44
import { routes } from './routes.js';
55
import { env } from 'fastly:env';
6-
import { fail } from './assertions.js';
7-
import './hello-world.js';
86

9-
// TLA
10-
await Promise.resolve();
7+
import './hello-world.js';
8+
import './hono.js';
119

1210
addEventListener('fetch', (event) => {
1311
event.respondWith(app(event));
@@ -22,23 +20,31 @@ async function app(event) {
2220
console.log(`FASTLY_SERVICE_VERSION: ${FASTLY_SERVICE_VERSION}`);
2321
const path = new URL(event.request.url).pathname;
2422
console.log(`path: ${path}`);
25-
let res = new Response('Internal Server Error', { status: 500 });
23+
let res;
2624
try {
2725
const routeHandler = routes.get(path);
2826
if (routeHandler) {
29-
res = await routeHandler(event);
27+
res = (await routeHandler(event)) || new Response('ok');
3028
} else {
31-
res = fail(`${path} endpoint does not exist`);
29+
return (res = new Response(`${path} endpoint does not exist`, {
30+
status: 500,
31+
}));
3232
}
3333
} catch (error) {
3434
if (error instanceof Response) {
3535
res = error;
3636
} else {
37-
res = fail(
38-
`The routeHandler for ${path} threw an error: ${error.message || error}` +
39-
'\n' +
40-
error.stack,
41-
);
37+
try {
38+
return (res = new Response(
39+
`The routeHandler for ${path} threw a [${error.constructor?.name ?? error.name}] error: ${error.message || error}` +
40+
'\n' +
41+
error.stack,
42+
{ status: 500 },
43+
));
44+
} catch (errRes) {
45+
console.error('err2', errRes);
46+
res = errRes;
47+
}
4248
}
4349
} finally {
4450
res.headers.set('fastly_service_version', FASTLY_SERVICE_VERSION);

0 commit comments

Comments
 (0)