@@ -33,13 +33,12 @@ import { validateUri } from "../Utility/UriUtil";
3333import * as StreamUtil from "../Utility/StreamUtil" ;
3434import { closeHttpResponse } from "../Utility/HttpUtil" ;
3535import { PromiseStatusTracker } from "../Utility/PromiseUtil" ;
36- import * as http from "http" ;
37- import * as https from "https" ;
36+ import type * as http from "http" ;
37+ import type * as https from "https" ;
3838import { IBroadcast } from "./IBroadcast" ;
3939import { StringUtil } from "../Utility/StringUtil" ;
4040import { IRaftCommand } from "./IRaftCommand" ;
4141import AbortController from "abort-controller" ;
42- import { URL } from "url" ;
4342import { EventEmitter } from "events" ;
4443import {
4544 BeforeRequestEventArgs ,
@@ -142,7 +141,11 @@ export class NodeStatus implements IDisposable {
142141export class RequestExecutor implements IDisposable {
143142 private _emitter = new EventEmitter ( ) ;
144143
145- private static readonly GLOBAL_APPLICATION_IDENTIFIER = uuidv4 ( ) . toString ( ) ;
144+ /*
145+ we don't initialize this here due to issue with cloudflare
146+ see: https://github.com/cloudflare/miniflare/issues/292
147+ */
148+ private static GLOBAL_APPLICATION_IDENTIFIER : string = null ;
146149
147150 private static readonly INITIAL_TOPOLOGY_ETAG = - 2 ;
148151
@@ -183,9 +186,11 @@ export class RequestExecutor implements IDisposable {
183186
184187 private _httpAgent : http . Agent ;
185188
186- private static readonly KEEP_ALIVE_HTTP_AGENT = new http . Agent ( {
187- keepAlive : true
188- } ) ;
189+ /*
190+ we don't initialize this here due to issue with cloudflare
191+ see: https://github.com/cloudflare/miniflare/issues/292
192+ */
193+ private static KEEP_ALIVE_HTTP_AGENT : http . Agent = null ;
189194
190195 private static readonly HTTPS_AGENT_CACHE = new Map < string , https . Agent > ( ) ;
191196
@@ -330,6 +335,10 @@ export class RequestExecutor implements IDisposable {
330335 }
331336
332337 public getHttpAgent ( ) : http . Agent {
338+ if ( this . conventions . customFetch ) {
339+ return null ;
340+ }
341+
333342 if ( this . _httpAgent ) {
334343 return this . _httpAgent ;
335344 }
@@ -344,6 +353,9 @@ export class RequestExecutor implements IDisposable {
344353 if ( RequestExecutor . HTTPS_AGENT_CACHE . has ( cacheKey ) ) {
345354 return RequestExecutor . HTTPS_AGENT_CACHE . get ( cacheKey ) ;
346355 } else {
356+ // eslint-disable-next-line @typescript-eslint/no-var-requires
357+ const https = require ( "https" ) ;
358+
347359 const agent = new https . Agent ( {
348360 keepAlive : true ,
349361 ...agentOptions
@@ -353,10 +365,22 @@ export class RequestExecutor implements IDisposable {
353365 return agent ;
354366 }
355367 } else {
368+ RequestExecutor . assertKeepAliveAgent ( ) ;
356369 return RequestExecutor . KEEP_ALIVE_HTTP_AGENT ;
357370 }
358371 }
359372
373+ private static assertKeepAliveAgent ( ) {
374+ if ( ! RequestExecutor . KEEP_ALIVE_HTTP_AGENT ) {
375+ // eslint-disable-next-line @typescript-eslint/no-var-requires
376+ const http = require ( "http" ) ;
377+
378+ RequestExecutor . KEEP_ALIVE_HTTP_AGENT = new http . Agent ( {
379+ keepAlive : true
380+ } ) ;
381+ }
382+ }
383+
360384 public getTopologyNodes ( ) : ServerNode [ ] {
361385 const topology = this . getTopology ( ) ;
362386 return topology
@@ -399,14 +423,25 @@ export class RequestExecutor implements IDisposable {
399423 opts ?: IRequestExecutorOptions ) : RequestExecutor {
400424 const { authOptions, documentConventions } = opts || { } as IRequestExecutorOptions ;
401425 const executor = new RequestExecutor ( database , authOptions , documentConventions ) ;
402- executor . _firstTopologyUpdatePromise = executor . _firstTopologyUpdate ( initialUrls , this . GLOBAL_APPLICATION_IDENTIFIER ) ;
426+
427+ executor . _firstTopologyUpdatePromise = executor . _firstTopologyUpdate ( initialUrls , RequestExecutor . getGlobalApplicationIdentifier ( ) ) ;
403428
404429 // this is just to get rid of unhandled rejection, we're handling it later on
405430 executor . _firstTopologyUpdatePromise . catch ( TypeUtil . NOOP ) ;
406431
407432 return executor ;
408433 }
409434
435+ private static getGlobalApplicationIdentifier ( ) {
436+ // due to cloudflare constraints we can't init GLOBAL_APPLICATION_IDENTIFIER in static
437+
438+ if ( ! this . GLOBAL_APPLICATION_IDENTIFIER ) {
439+ this . GLOBAL_APPLICATION_IDENTIFIER = uuidv4 ( ) ;
440+ }
441+
442+ return this . GLOBAL_APPLICATION_IDENTIFIER ;
443+ }
444+
410445 public static createForSingleNodeWithConfigurationUpdates (
411446 url : string , database : string , opts : IRequestExecutorOptions ) : RequestExecutor {
412447 const executor =
@@ -1284,6 +1319,10 @@ export class RequestExecutor implements IDisposable {
12841319 return null ;
12851320 }
12861321
1322+ if ( this . conventions . customFetch ) {
1323+ request . fetcher = this . conventions . customFetch ;
1324+ }
1325+
12871326 const req = Object . assign ( request , this . _defaultRequestOptions ) ;
12881327 urlRef ( req . uri ) ;
12891328 req . headers = req . headers || { } ;
0 commit comments