Skip to content

Commit

Permalink
refactor(cloud-function): don't return value in handler
Browse files Browse the repository at this point in the history
  • Loading branch information
caugner committed Feb 3, 2025
1 parent e231366 commit c1fa7c0
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 40 deletions.
25 changes: 14 additions & 11 deletions cloud-function/src/handlers/handle-stripe-plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ interface Result {
plans: PlanResult;
}

export async function handleStripePlans(req: Request, res: Response) {
export async function handleStripePlans(
req: Request,
res: Response
): Promise<void> {
const lookupData =
ORIGIN_MAIN === "developer.mozilla.org" ? prodLookup : stageLookup;

Expand All @@ -24,7 +27,8 @@ export async function handleStripePlans(req: Request, res: Response) {
const supportedCurrency = lookupData.countryToCurrency[countryCode];

if (!supportedCurrency) {
return res.sendStatus(404).end();
res.sendStatus(404).end();
return;
}

const localeHeader = req.headers["accept-language"];
Expand All @@ -47,7 +51,8 @@ export async function handleStripePlans(req: Request, res: Response) {

const plans = lookupData.langCurrencyToPlans[key];
if (!plans) {
return res.sendStatus(500).end();
res.sendStatus(500).end();
return;
}

const planResult: PlanResult = {};
Expand All @@ -67,12 +72,10 @@ export async function handleStripePlans(req: Request, res: Response) {
plans: planResult,
} satisfies Result;

return (
res
.status(200)
// Google CDN cannot partition by country, so we can only cache in browser.
.setHeader("Cache-Control", "private, max-age=86400")
.setHeader("Content-Type", "application/json")
.end(JSON.stringify(result))
);
res
.status(200)
// Google CDN cannot partition by country, so we can only cache in browser.
.setHeader("Cache-Control", "private, max-age=86400")
.setHeader("Content-Type", "application/json")
.end(JSON.stringify(result));
}
31 changes: 20 additions & 11 deletions cloud-function/src/handlers/proxy-bsa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const handleViewed = createPong2ViewedHandler(coder);
const plusLookup =
ORIGIN_MAIN === "developer.mozilla.org" ? prodPlusLookup : stagePlusLookup;

export async function proxyBSA(req: Request, res: Response) {
export async function proxyBSA(req: Request, res: Response): Promise<void> {
const countryCode = getRequestCountry(req);

const plusAvailable = countryCode in plusLookup.countryToCurrency;
Expand All @@ -36,7 +36,8 @@ export async function proxyBSA(req: Request, res: Response) {

if (pathname === "/pong/get") {
if (req.method !== "POST") {
return res.sendStatus(405).end();
res.sendStatus(405).end();
return;
}

const { body } = req;
Expand All @@ -48,14 +49,16 @@ export async function proxyBSA(req: Request, res: Response) {

payload.plusAvailable = plusAvailable;

return res
res
.status(status)
.setHeader("cache-control", "no-store")
.setHeader("content-type", "application/json")
.end(JSON.stringify(payload));
return;
} else if (req.path === "/pong/click") {
if (req.method !== "GET") {
return res.sendStatus(405).end();
res.sendStatus(405).end();
return;
}
const params = new URLSearchParams(search);
try {
Expand All @@ -65,21 +68,25 @@ export async function proxyBSA(req: Request, res: Response) {
userAgent
);
if (location && (status === 301 || status === 302)) {
return res.redirect(location);
res.redirect(location);
return;
} else {
return res.sendStatus(502).end();
res.sendStatus(502).end();
return;
}
} catch (e) {
console.error(e);
}
} else if (pathname === "/pong/viewed") {
if (req.method !== "POST") {
return res.sendStatus(405).end();
res.sendStatus(405).end();
return;
}
const params = new URLSearchParams(search);
try {
await handleViewed(params, countryCode, userAgent);
return res.sendStatus(201).end();
res.sendStatus(201).end();
return;
} catch (e) {
console.error(e);
}
Expand All @@ -88,17 +95,19 @@ export async function proxyBSA(req: Request, res: Response) {
decodeURIComponent(pathname.substring("/pimg/".length))
);
if (!src) {
return res.sendStatus(400).end();
res.sendStatus(400).end();
return;
}
const { buf, contentType } = await fetchImage(src);
return res
res
.status(200)
.set({
"cache-control": "max-age=86400",
"content-type": contentType,
})
.end(Buffer.from(buf));
return;
}

return res.status(204).end();
res.status(204).end();
}
31 changes: 20 additions & 11 deletions cloud-function/src/handlers/proxy-kevel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const handleViewed = createPongViewedHandler(coder);
const plusLookup =
ORIGIN_MAIN === "developer.mozilla.org" ? prodPlusLookup : stagePlusLookup;

export async function proxyKevel(req: Request, res: Response) {
export async function proxyKevel(req: Request, res: Response): Promise<void> {
const countryCode = getRequestCountry(req);

const plusAvailable = countryCode in plusLookup.countryToCurrency;
Expand All @@ -41,7 +41,8 @@ export async function proxyKevel(req: Request, res: Response) {

if (pathname === "/pong/get") {
if (req.method !== "POST") {
return res.sendStatus(405).end();
res.sendStatus(405).end();
return;
}

const { body } = req;
Expand All @@ -53,34 +54,40 @@ export async function proxyKevel(req: Request, res: Response) {

payload.plusAvailable = plusAvailable;

return res
res
.status(status)
.setHeader("cache-control", "no-store")
.setHeader("content-type", "application/json")
.end(JSON.stringify(payload));
return;
} else if (req.path === "/pong/click") {
if (req.method !== "GET") {
return res.sendStatus(405).end();
res.sendStatus(405).end();
return;
}
const params = new URLSearchParams(search);
try {
const { status, location } = await handleClick(params);
if (location && (status === 301 || status === 302)) {
return res.redirect(location);
res.redirect(location);
return;
} else {
return res.sendStatus(502).end();
res.sendStatus(502).end();
return;
}
} catch (e) {
console.error(e);
}
} else if (pathname === "/pong/viewed") {
if (req.method !== "POST") {
return res.sendStatus(405).end();
res.sendStatus(405).end();
return;
}
const params = new URLSearchParams(search);
try {
await handleViewed(params);
return res.sendStatus(201).end();
res.sendStatus(201).end();
return;
} catch (e) {
console.error(e);
}
Expand All @@ -89,17 +96,19 @@ export async function proxyKevel(req: Request, res: Response) {
decodeURIComponent(pathname.substring("/pimg/".length))
);
if (!src) {
return res.sendStatus(400).end();
res.sendStatus(400).end();
return;
}
const { buf, contentType } = await fetchImage(src);
return res
res
.status(200)
.set({
"cache-control": "max-age=86400",
"content-type": contentType,
})
.end(Buffer.from(buf));
return;
}

return res.status(204).end();
res.status(204).end();
}
2 changes: 1 addition & 1 deletion cloud-function/src/handlers/proxy-pong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { proxyKevel } from "./proxy-kevel.js";

import type { Request, Response } from "express";

export async function proxyPong(req: Request, res: Response) {
export async function proxyPong(req: Request, res: Response): Promise<void> {
if (BSA_ENABLED) {
return proxyBSA(req, res);
}
Expand Down
10 changes: 7 additions & 3 deletions cloud-function/src/middlewares/require-origin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ import type { NextFunction, Request, Response } from "express";
import { Origin, getOriginFromRequest } from "../env.js";

export function requireOrigin(...expectedOrigins: Origin[]) {
return async (req: Request, res: Response, next: NextFunction) => {
return async (
req: Request,
res: Response,
next: NextFunction
): Promise<void> => {
const actualOrigin = getOriginFromRequest(req);

if (expectedOrigins.includes(actualOrigin)) {
return next();
next();
} else {
return res.sendStatus(404).end();
res.sendStatus(404).end();
}
};
}
8 changes: 5 additions & 3 deletions libs/play/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ function playSubdomain(hostname) {
/**
* @param {express.Request} req
* @param {express.Response} res
* @returns {Promise<void>}
*/
export async function handleRunner(req, res) {
const url = new URL(req.url, "https://example.com");
Expand All @@ -358,7 +359,8 @@ export async function handleRunner(req, res) {
!state ||
(!isLocalhost && !hasMatchingHash && !isIframeOnMDN)
) {
return res.status(404).end();
res.status(404).end();
return;
}

const json = JSON.parse(state);
Expand All @@ -367,7 +369,7 @@ export async function handleRunner(req, res) {
if (req.headers["sec-fetch-dest"] === "iframe" || codeParam === codeCookie) {
const html = renderHtml(json);
withRunnerResponseHeaders(res);
return res.status(200).send(html);
res.status(200).send(html);
} else {
const rand = crypto.randomUUID();
res.cookie("code", rand, {
Expand All @@ -380,7 +382,7 @@ export async function handleRunner(req, res) {
urlWithCode.search = "";
urlWithCode.searchParams.set("state", stateParam);
urlWithCode.searchParams.set("code", rand);
return res
res
.status(200)
.send(
renderWarning(
Expand Down

0 comments on commit c1fa7c0

Please sign in to comment.