Skip to content

Commit

Permalink
chore(cloud-function): migrate url.parse() to new URL() (#9655)
Browse files Browse the repository at this point in the history
`url.parse()` is legacy API, and the WHATWG API `new URL()` is recommended instead.

Co-authored-by: A1lo <yin199909@aliyun.com>
  • Loading branch information
caugner and yin1999 committed Jun 5, 2024
1 parent a814540 commit 051a599
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 17 deletions.
4 changes: 1 addition & 3 deletions cloud-function/src/handlers/proxy-bsa.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as url from "node:url";

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

import { Coder } from "../internal/pong/index.js";
Expand All @@ -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 ?? "";

Expand Down
4 changes: 1 addition & 3 deletions cloud-function/src/handlers/proxy-kevel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as url from "node:url";

import { Client } from "@adzerk/decision-sdk";
import type { Request, Response } from "express";

Expand Down Expand Up @@ -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 ?? "";

Expand Down
6 changes: 2 additions & 4 deletions cloud-function/src/middlewares/lowercase-pathname.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import * as url from "node:url";

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

export async function lowercasePathname(
req: Request,
_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;
Expand Down
5 changes: 2 additions & 3 deletions cloud-function/src/middlewares/resolve-index-html.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from "node:path";
import * as url from "node:url";

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

Expand All @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions cloud-function/src/middlewares/resolve-runner-html.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import * as url from "node:url";

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

export async function resolveRunnerHtml(
req: Request,
_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;
Expand Down

0 comments on commit 051a599

Please sign in to comment.