From 051a5993f7bdb3d5b7a987e7f10b3b4d15926ed4 Mon Sep 17 00:00:00 2001 From: Claas Augner <495429+caugner@users.noreply.github.com> Date: Wed, 5 Jun 2024 13:59:38 +0200 Subject: [PATCH] chore(cloud-function): migrate `url.parse()` to `new URL()` (#9655) `url.parse()` is legacy API, and the WHATWG API `new URL()` is recommended instead. Co-authored-by: A1lo --- cloud-function/src/handlers/proxy-bsa.ts | 4 +--- cloud-function/src/handlers/proxy-kevel.ts | 4 +--- cloud-function/src/middlewares/lowercase-pathname.ts | 6 ++---- cloud-function/src/middlewares/resolve-index-html.ts | 5 ++--- cloud-function/src/middlewares/resolve-runner-html.ts | 6 ++---- 5 files changed, 8 insertions(+), 17 deletions(-) diff --git a/cloud-function/src/handlers/proxy-bsa.ts b/cloud-function/src/handlers/proxy-bsa.ts index f929eae23fb9..38be31ca2341 100644 --- a/cloud-function/src/handlers/proxy-bsa.ts +++ b/cloud-function/src/handlers/proxy-bsa.ts @@ -1,5 +1,3 @@ -import * as url from "node:url"; - import type { Request, Response } from "express"; import { Coder } from "../internal/pong/index.js"; @@ -26,7 +24,7 @@ export async function proxyBSA(req: Request, res: Response) { const userAgent = req.headers["user-agent"] ?? ""; - const parsedUrl = url.parse(req.url); + const parsedUrl = new URL(req.url, `${req.protocol}://${req.headers.host}/`); const pathname = parsedUrl.pathname ?? ""; const search = parsedUrl.search ?? ""; diff --git a/cloud-function/src/handlers/proxy-kevel.ts b/cloud-function/src/handlers/proxy-kevel.ts index e8d241fc6f1e..a176b15e4628 100644 --- a/cloud-function/src/handlers/proxy-kevel.ts +++ b/cloud-function/src/handlers/proxy-kevel.ts @@ -1,5 +1,3 @@ -import * as url from "node:url"; - import { Client } from "@adzerk/decision-sdk"; import type { Request, Response } from "express"; @@ -31,7 +29,7 @@ export async function proxyKevel(req: Request, res: Response) { const userAgent = req.headers["user-agent"] ?? ""; - const parsedUrl = url.parse(req.url); + const parsedUrl = new URL(req.url, `${req.protocol}://${req.headers.host}`); const pathname = parsedUrl.pathname ?? ""; const search = parsedUrl.search ?? ""; diff --git a/cloud-function/src/middlewares/lowercase-pathname.ts b/cloud-function/src/middlewares/lowercase-pathname.ts index 3e87a55589d9..0d311b36f0e9 100644 --- a/cloud-function/src/middlewares/lowercase-pathname.ts +++ b/cloud-function/src/middlewares/lowercase-pathname.ts @@ -1,5 +1,3 @@ -import * as url from "node:url"; - import { NextFunction, Request, Response } from "express"; export async function lowercasePathname( @@ -7,10 +5,10 @@ export async function lowercasePathname( _res: Response, next: NextFunction ) { - const urlParsed = url.parse(req.url); + const urlParsed = new URL(req.url, `${req.protocol}://${req.headers.host}`); if (urlParsed.pathname) { urlParsed.pathname = urlParsed.pathname.toLowerCase(); - req.url = url.format(urlParsed); + req.url = urlParsed.toString(); // Workaround for http-proxy-middleware v2 using `req.originalUrl`. // See: https://github.com/chimurai/http-proxy-middleware/pull/731 req.originalUrl = req.url; diff --git a/cloud-function/src/middlewares/resolve-index-html.ts b/cloud-function/src/middlewares/resolve-index-html.ts index fa2589f81d44..57ca7ba3ae82 100644 --- a/cloud-function/src/middlewares/resolve-index-html.ts +++ b/cloud-function/src/middlewares/resolve-index-html.ts @@ -1,5 +1,4 @@ import * as path from "node:path"; -import * as url from "node:url"; import { NextFunction, Request, Response } from "express"; @@ -11,14 +10,14 @@ export async function resolveIndexHTML( _res: Response, next: NextFunction ) { - const urlParsed = url.parse(req.url); + const urlParsed = new URL(req.url, `${req.protocol}://${req.headers.host}`); if (urlParsed.pathname) { let pathname = slugToFolder(urlParsed.pathname); if (!isAsset(pathname)) { pathname = path.join(pathname, "index.html"); } urlParsed.pathname = pathname; - req.url = url.format(urlParsed); + req.url = urlParsed.toString(); // Workaround for http-proxy-middleware v2 using `req.originalUrl`. // See: https://github.com/chimurai/http-proxy-middleware/pull/731 req.originalUrl = req.url; diff --git a/cloud-function/src/middlewares/resolve-runner-html.ts b/cloud-function/src/middlewares/resolve-runner-html.ts index 5f7986ecfa32..7d2e71a17e9b 100644 --- a/cloud-function/src/middlewares/resolve-runner-html.ts +++ b/cloud-function/src/middlewares/resolve-runner-html.ts @@ -1,5 +1,3 @@ -import * as url from "node:url"; - import { NextFunction, Request, Response } from "express"; export async function resolveRunnerHtml( @@ -7,10 +5,10 @@ export async function resolveRunnerHtml( _res: Response, next: NextFunction ) { - const urlParsed = url.parse(req.url); + const urlParsed = new URL(req.url, `${req.protocol}://${req.headers.host}`); if (urlParsed.pathname && urlParsed.pathname.endsWith("/runner.html")) { urlParsed.pathname = "/runner.html"; - req.url = url.format(urlParsed); + req.url = urlParsed.toString(); // Workaround for http-proxy-middleware v2 using `req.originalUrl`. // See: https://github.com/chimurai/http-proxy-middleware/pull/731 req.originalUrl = req.url;