Update code to use Express v5 async handler

This is to update the code after v5 upgrade on commit 16d3f51
This commit is contained in:
Noah Halstead 2025-03-12 15:01:18 -04:00
parent 1c6b4be721
commit 75e99700d3

View File

@ -80,14 +80,6 @@ app.use(
// DO Apps doesn't send a X-Forwarded-Proto header, so we simply need to make a blanket trust // DO Apps doesn't send a X-Forwarded-Proto header, so we simply need to make a blanket trust
app.set("trust proxy", true); app.set("trust proxy", true);
function asyncHandler(fn: any) {
return (req: express.Request, res: express.Response, next: express.NextFunction) => {
return Promise.resolve(fn(req, res, next)).catch(next);
};
}
const asyncAuthGuard = asyncHandler(authenticated);
app.get("/", (req, res) => { app.get("/", (req, res) => {
return res.status(200).send("OK"); return res.status(200).send("OK");
}); });
@ -101,8 +93,8 @@ app.get("/healthz", (req, res) => {
app.get( app.get(
"/me", "/me",
asyncAuthGuard, authenticated,
asyncHandler(async (req: express.Request, res: express.Response) => { async (req: express.Request, res: express.Response) => {
const idToken = req.session?.id_token; const idToken = req.session?.id_token;
const { sub, iss, exp, aud, iat, jti, nbf } = jose.decodeJwt(idToken); const { sub, iss, exp, aud, iat, jti, nbf } = jose.decodeJwt(idToken);
@ -115,32 +107,32 @@ app.get(
} }
return res.json({ ...user, sub }); return res.json({ ...user, sub });
}), },
); );
app.get("/releases", asyncHandler(Releases.Retrieve)); app.get("/releases", Releases.Retrieve);
app.get( app.get(
"/releases/system_recovery/latest", "/releases/system_recovery/latest",
asyncHandler(Releases.RetrieveLatestSystemRecovery), Releases.RetrieveLatestSystemRecovery,
); );
app.get("/releases/app/latest", asyncHandler(Releases.RetrieveLatestApp)); app.get("/releases/app/latest", Releases.RetrieveLatestApp);
app.get("/devices", asyncAuthGuard, asyncHandler(Devices.List)); app.get("/devices", authenticated, Devices.List);
app.get("/devices/:id", asyncAuthGuard, asyncHandler(Devices.Retrieve)); app.get("/devices/:id", authenticated, Devices.Retrieve);
app.post("/devices/token", asyncHandler(Devices.Token)); app.post("/devices/token", Devices.Token);
app.put("/devices/:id", asyncAuthGuard, asyncHandler(Devices.Update)); app.put("/devices/:id", authenticated, Devices.Update);
app.delete("/devices/:id", asyncHandler(Devices.Delete)); app.delete("/devices/:id", Devices.Delete);
app.post("/webrtc/session", asyncAuthGuard, asyncHandler(Webrtc.CreateSession)); app.post("/webrtc/session", authenticated, Webrtc.CreateSession);
app.post("/webrtc/ice_config", asyncAuthGuard, asyncHandler(Webrtc.CreateIceCredentials)); app.post("/webrtc/ice_config", authenticated, Webrtc.CreateIceCredentials);
app.post( app.post(
"/webrtc/turn_activity", "/webrtc/turn_activity",
asyncAuthGuard, authenticated,
asyncHandler(Webrtc.CreateTurnActivity), Webrtc.CreateTurnActivity,
); );
app.post("/oidc/google", asyncHandler(OIDC.Google)); app.post("/oidc/google", OIDC.Google);
app.get("/oidc/callback_o", asyncHandler(OIDC.Callback)); app.get("/oidc/callback_o", OIDC.Callback);
app.get("/oidc/callback", (req, res) => { app.get("/oidc/callback", (req, res) => {
/* /*
* We set the session cookie in the /oidc/google route as a part of 302 redirect to the OIDC login page * We set the session cookie in the /oidc/google route as a part of 302 redirect to the OIDC login page
@ -187,10 +179,10 @@ app.get("/oidc/callback", (req, res) => {
app.post( app.post(
"/logout", "/logout",
asyncHandler((req: express.Request, res: express.Response) => { (req: express.Request, res: express.Response) => {
req.session = null; req.session = null;
return res.json({ message: "Logged out" }); return res.json({ message: "Logged out" });
}), }
); );
// Error-handling middleware // Error-handling middleware
@ -200,7 +192,7 @@ app.use(
req: express.Request, req: express.Request,
res: express.Response, res: express.Response,
next: express.NextFunction, next: express.NextFunction,
) => { ): void => {
const isProduction = process.env.NODE_ENV === "production"; const isProduction = process.env.NODE_ENV === "production";
const statusCode = err instanceof HttpError ? err.status : 500; const statusCode = err instanceof HttpError ? err.status : 500;