1515 */
1616import http from 'http' ;
1717import https from 'https' ;
18- import url from 'url' ;
1918import { AbortableRequest , Headers , RequestHandler , Response } from './http' ;
2019import decompressResponse from 'decompress-response' ;
2120import { LoggerFacade } from '../../logging/logger' ;
2221import { REQUEST_TIMEOUT_MS } from '../enums' ;
23- import { NO_STATUS_CODE_IN_RESPONSE , REQUEST_ERROR , REQUEST_TIMEOUT , UNSUPPORTED_PROTOCOL } from 'error_message' ;
22+ import { INVALID_REQUEST_URL , NO_STATUS_CODE_IN_RESPONSE , REQUEST_ERROR , REQUEST_TIMEOUT , UNSUPPORTED_PROTOCOL } from 'error_message' ;
2423import { OptimizelyError } from '../../error/optimizly_error' ;
2524import { Platform } from '../../platform_support' ;
2625/**
@@ -46,7 +45,15 @@ export class NodeRequestHandler implements RequestHandler {
4645 * @returns AbortableRequest contains both the response Promise and capability to abort()
4746 */
4847 makeRequest ( requestUrl : string , headers : Headers , method : string , data ?: string ) : AbortableRequest {
49- const parsedUrl = url . parse ( requestUrl ) ;
48+ let parsedUrl : URL ;
49+ try {
50+ parsedUrl = new URL ( requestUrl ) ;
51+ } catch {
52+ return {
53+ responsePromise : Promise . reject ( new OptimizelyError ( INVALID_REQUEST_URL , requestUrl ) ) ,
54+ abort : ( ) => { } ,
55+ } ;
56+ }
5057
5158 if ( parsedUrl . protocol !== 'https:' && parsedUrl . protocol !== 'http:' ) {
5259 return {
@@ -79,11 +86,11 @@ export class NodeRequestHandler implements RequestHandler {
7986 * @private
8087 * @returns http.RequestOptions Standard request options dictionary compatible with both http and https
8188 */
82- private getRequestOptionsFromUrl ( url : url . UrlWithStringQuery ) : http . RequestOptions {
89+ private getRequestOptionsFromUrl ( url : URL ) : http . RequestOptions {
8390 return {
8491 hostname : url . hostname ,
85- path : url . path ,
86- port : url . port ,
92+ path : url . pathname + url . search ,
93+ port : url . port || null ,
8794 protocol : url . protocol ,
8895 } ;
8996 }
0 commit comments