Skip to content

Commit 04ab36b

Browse files
committed
don't do dev server cookie passthrough for now
1 parent 4b9a751 commit 04ab36b

File tree

1 file changed

+9
-48
lines changed

1 file changed

+9
-48
lines changed

dotcom-rendering/src/server/lib/get-content-from-url.ts

Lines changed: 9 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,17 @@ import type { Handler } from 'express';
55
const isStringTuple = (_: [string, unknown]): _ is [string, string] =>
66
typeof _[1] === 'string';
77

8-
const allowedOrigins = [
9-
'https://www.theguardian.com',
10-
'https://m.code.dev-theguardian.com',
11-
'https://m.thegulocal.com',
12-
'http://localhost:9000',
13-
];
14-
158
/**
169
* Get DCR content from a `theguardian.com` URL.
1710
* Takes in optional `X-Gu-*` headers to send.
18-
* Returns the parsed JSON config and any cookies set by the request.
1911
*/
2012
async function getContentFromURL(
2113
url: URL,
2214
_headers: IncomingHttpHeaders,
23-
): Promise<{ config: unknown; cookies: string[] }> {
15+
): Promise<unknown> {
2416
// searchParams will only work for the first set of query params because 'url' is already a query param itself
2517
const searchparams = url.searchParams.toString();
2618

27-
// Prevent requests to unknown origins
28-
if (!allowedOrigins.includes(url.origin)) {
29-
throw new Error(
30-
`Origin ${
31-
url.origin
32-
} is not allowed. Allowed origins are: ${allowedOrigins.join(
33-
', ',
34-
)}`,
35-
);
36-
}
37-
3819
// Reconstruct the parsed url adding .json?dcr which we need to force dcr to return json
3920
const jsonUrl = `${url.origin}${url.pathname}.json?dcr=true&${searchparams}`;
4021

@@ -49,13 +30,10 @@ async function getContentFromURL(
4930
.filter(isStringTuple),
5031
);
5132

52-
const { cookies, json } = await fetch(jsonUrl, { headers })
53-
.then(async (response) => {
54-
return {
55-
json: (await response.json()) as unknown,
56-
cookies: response.headers.getSetCookie(),
57-
};
58-
})
33+
// pick all the keys from the JSON except `html`
34+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- we don't want `html` in the config
35+
const { html, ...config } = await fetch(jsonUrl, { headers })
36+
.then((response) => response.json())
5937
.catch((error) => {
6038
if (isObject(error) && error.type === 'invalid-json') {
6139
throw new Error(
@@ -65,11 +43,7 @@ async function getContentFromURL(
6543
throw error;
6644
});
6745

68-
// pick all the keys from the JSON except `html`
69-
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- we don't want `html` in the config
70-
const { html, ...config } = json as Record<string, unknown>;
71-
72-
return { config, cookies };
46+
return config;
7347
}
7448

7549
/**
@@ -91,14 +65,8 @@ export const getContentFromURLMiddleware: Handler = async (req, res, next) => {
9165
const url = new URL(
9266
'https://www.theguardian.com/crosswords/digital-edition',
9367
);
94-
const { config, cookies } = await getContentFromURL(
95-
url,
96-
req.headers,
97-
);
98-
req.body = config;
99-
for (const cookie of cookies) {
100-
res.append('Set-Cookie', cookie);
101-
}
68+
const content = await getContentFromURL(url, req.headers);
69+
req.body = content;
10270
next();
10371
} catch (error) {
10472
console.error(error);
@@ -120,14 +88,7 @@ export const getContentFromURLMiddleware: Handler = async (req, res, next) => {
12088
}
12189

12290
try {
123-
const { config, cookies } = await getContentFromURL(
124-
sourceURL,
125-
req.headers,
126-
);
127-
req.body = config;
128-
for (const cookie of cookies) {
129-
res.append('Set-Cookie', cookie);
130-
}
91+
req.body = await getContentFromURL(sourceURL, req.headers);
13192
} catch (error) {
13293
console.error(error);
13394
next(error);

0 commit comments

Comments
 (0)