Skip to content

Commit c9db870

Browse files
authored
fix(api-request): replace deprecated url.parse() with WHATWG URL (#3124)
url.parse() is deprecated (DEP0169) and emits warnings on every cold start in serverless runtimes. Migrate the two call sites in BaseRequestConfigImpl.buildUrl() to the WHATWG URL constructor, matching the pattern from #3061. The protected buildUrl() return type changes from url.UrlWithStringQuery to URL. Both call sites are inside the same file. parsed.path is replaced with ${parsed.pathname}${parsed.search} so the request path on the wire stays byte-for-byte identical, and parsed.port is now string (always); the existing if (!port) falsy check still catches the empty case. Fixes #3118.
1 parent 1b0a0d0 commit c9db870

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

src/utils/api-request.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import * as validator from './validator';
2323
import http = require('http');
2424
import https = require('https');
2525
import http2 = require('http2');
26-
import url = require('url');
2726
import { EventEmitter } from 'events';
2827
import { Readable } from 'stream';
2928
import * as zlibmod from 'zlib';
@@ -991,23 +990,23 @@ class BaseRequestConfigImpl implements BaseRequestConfig {
991990
return data;
992991
}
993992

994-
protected buildUrl(): url.UrlWithStringQuery {
993+
protected buildUrl(): URL {
995994
const fullUrl: string = this.urlWithProtocol();
995+
const parsedUrl = new URL(fullUrl);
996996
if (!this.hasEntity() || this.isEntityEnclosingRequest()) {
997-
return url.parse(fullUrl);
997+
return parsedUrl;
998998
}
999999
if (!validator.isObject(this.data)) {
10001000
throw new Error(`${this.method} requests cannot have a body`);
10011001
}
1002-
// Parse URL and append data to query string.
1003-
const parsedUrl = new url.URL(fullUrl);
1004-
const dataObj = this.data as { [key: string]: string; };
1002+
// Append data to query string.
1003+
const dataObj = this.data as {[key: string]: string};
10051004
for (const key in dataObj) {
10061005
if (Object.prototype.hasOwnProperty.call(dataObj, key)) {
10071006
parsedUrl.searchParams.append(key, dataObj[key]);
10081007
}
10091008
}
1010-
return url.parse(parsedUrl.toString());
1009+
return parsedUrl;
10111010
}
10121011

10131012
protected urlWithProtocol(): string {
@@ -1044,7 +1043,7 @@ class HttpRequestConfigImpl extends BaseRequestConfigImpl implements HttpRequest
10441043
public buildRequestOptions(): https.RequestOptions {
10451044
const parsed = this.buildUrl();
10461045
const protocol = parsed.protocol;
1047-
let port: string | null = parsed.port;
1046+
let port: string = parsed.port;
10481047
if (!port) {
10491048
const isHttps = protocol === 'https:';
10501049
port = isHttps ? '443' : '80';
@@ -1054,7 +1053,7 @@ class HttpRequestConfigImpl extends BaseRequestConfigImpl implements HttpRequest
10541053
protocol,
10551054
hostname: parsed.hostname,
10561055
port,
1057-
path: parsed.path,
1056+
path: `${parsed.pathname}${parsed.search}`,
10581057
method: this.method,
10591058
agent: this.httpAgent,
10601059
headers: Object.assign({}, this.headers),
@@ -1082,7 +1081,7 @@ class Http2RequestConfigImpl extends BaseRequestConfigImpl implements Http2Reque
10821081

10831082
return {
10841083
protocol,
1085-
path: parsed.path,
1084+
path: `${parsed.pathname}${parsed.search}`,
10861085
method: this.method,
10871086
headers: Object.assign({}, this.headers),
10881087
};

0 commit comments

Comments
 (0)