Skip to content

Commit

Permalink
[node] Improve edge-handler-template error handling (#9697)
Browse files Browse the repository at this point in the history
  • Loading branch information
EndangeredMassa committed Mar 22, 2023
1 parent ce25dec commit 0fb0601
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/cli/test/dev/integration-1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ test('[vercel dev] throws an error when an edge function has no response', async
expect(await res.status).toBe(500);
expect(await res.text()).toMatch('FUNCTION_INVOCATION_FAILED');
expect(stdout).toMatch(
/Error from API Route \/api\/edge-no-response: Edge Function "api\/edge-no-response.js" did not return a response./g
/Error from API Route \/api\/edge-no-response: Edge Function did not return a response./g
);
} finally {
await dev.kill();
Expand Down
31 changes: 19 additions & 12 deletions packages/node/src/edge-functions/edge-handler-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
/* global addEventListener */

function buildUrl(requestDetails) {
let proto = requestDetails.headers['x-forwarded-proto'].split(/\b/).shift(); // handling multi-protocol like https,http://...
let host = requestDetails.headers['x-forwarded-host'];
let path = requestDetails.url;
const host = requestDetails.headers['x-forwarded-host'] || '127.0.0.1';
const path = requestDetails.url || '/';

const allProtocols = requestDetails.headers['x-forwarded-proto'];
let proto;
if (allProtocols) {
// handle multi-protocol like: https,http://...
proto = allProtocols.split(/\b/).shift();
} else {
proto = 'http';
}

return `${proto}://${host}${path}`;
}

Expand All @@ -16,7 +25,7 @@ async function respond(
dependencies
) {
const { Request, Response } = dependencies;
const { isMiddleware, entrypointLabel } = options;
const { isMiddleware } = options;

let body;

Expand All @@ -26,7 +35,7 @@ async function respond(
}
}

let request = new Request(buildUrl(requestDetails), {
const request = new Request(buildUrl(requestDetails), {
headers: requestDetails.headers,
method: requestDetails.method,
body: body,
Expand All @@ -45,9 +54,7 @@ async function respond(
},
});
} else {
throw new Error(
`Edge Function "${entrypointLabel}" did not return a response.`
);
throw new Error(`Edge Function did not return a response.`);
}
}
return response;
Expand All @@ -68,8 +75,8 @@ function toResponseError(error, Response) {
}

async function parseRequestEvent(event) {
let serializedRequest = await event.request.text();
let requestDetails = JSON.parse(serializedRequest);
const serializedRequest = await event.request.text();
const requestDetails = JSON.parse(serializedRequest);
return requestDetails;
}

Expand All @@ -78,8 +85,8 @@ async function parseRequestEvent(event) {
function registerFetchListener(userEdgeHandler, options, dependencies) {
addEventListener('fetch', async event => {
try {
let requestDetails = await parseRequestEvent(event);
let response = await respond(
const requestDetails = await parseRequestEvent(event);
const response = await respond(
userEdgeHandler,
requestDetails,
event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ describe('edge-handler-template', () => {
});
expect(url).toBe('https://somewhere.com/api/add');
});

test('url falls back to `/`', async () => {
const url = buildUrl({
// missing url
headers: {
'x-forwarded-proto': 'https',
'x-forwarded-host': 'somewhere.com',
},
});
expect(url).toBe('https://somewhere.com/');
});

test('host header falls back to `127.0.0.1`', async () => {
const url = buildUrl({
url: '/api/add',
headers: {
'x-forwarded-proto': 'https',
// missing 'x-forwarded-host'
},
});
expect(url).toBe('https://127.0.0.1/api/add');
});

test('proto header falls back to `http`', async () => {
const url = buildUrl({
url: '/api/add',
headers: {
// missing 'x-forwarded-proto'
'x-forwarded-host': 'somewhere.com',
},
});
expect(url).toBe('http://somewhere.com/api/add');
});
});

describe('respond()', () => {
Expand Down

0 comments on commit 0fb0601

Please sign in to comment.