@@ -13,6 +13,7 @@ import contentType from 'content-type'
13
13
import cookie from 'cookie'
14
14
import { get } from 'dot-prop'
15
15
import generateETag from 'etag'
16
+ import getAvailablePort from 'get-port'
16
17
import httpProxy from 'http-proxy'
17
18
import { createProxyMiddleware } from 'http-proxy-middleware'
18
19
import jwtDecode from 'jwt-decode'
@@ -545,6 +546,7 @@ export const startProxy = async function ({
545
546
siteInfo,
546
547
state,
547
548
} ) {
549
+ const secondaryServerPort = settings . https ? await getAvailablePort ( ) : null
548
550
const functionsServer = settings . functionsPort ? `http://127.0.0.1:${ settings . functionsPort } ` : null
549
551
const edgeFunctionsProxy = await initializeEdgeFunctionsProxy ( {
550
552
config,
@@ -555,9 +557,10 @@ export const startProxy = async function ({
555
557
geoCountry,
556
558
getUpdatedConfig,
557
559
inspectSettings,
560
+ mainPort : settings . port ,
558
561
offline,
562
+ passthroughPort : secondaryServerPort || settings . port ,
559
563
projectDir,
560
- settings,
561
564
siteInfo,
562
565
state,
563
566
} )
@@ -586,16 +589,32 @@ export const startProxy = async function ({
586
589
functionsServer,
587
590
edgeFunctionsProxy,
588
591
} )
589
- const server = settings . https
592
+ const primaryServer = settings . https
590
593
? https . createServer ( { cert : settings . https . cert , key : settings . https . key } , onRequestWithOptions )
591
594
: http . createServer ( onRequestWithOptions )
592
-
593
- server . on ( 'upgrade' , function onUpgrade ( req , socket , head ) {
595
+ const onUpgrade = function onUpgrade ( req , socket , head ) {
594
596
proxy . ws ( req , socket , head )
595
- } )
597
+ }
598
+
599
+ primaryServer . on ( 'upgrade' , onUpgrade )
600
+ primaryServer . listen ( { port : settings . port } )
601
+
602
+ const eventQueue = [ once ( primaryServer , 'listening' ) ]
603
+
604
+ // If we're running the main server on HTTPS, we need to start a secondary
605
+ // server on HTTP for receiving passthrough requests from edge functions.
606
+ // This lets us run the Deno server on HTTP and avoid the complications of
607
+ // Deno talking to Node on HTTPS with potentially untrusted certificates.
608
+ if ( secondaryServerPort ) {
609
+ const secondaryServer = http . createServer ( onRequestWithOptions )
610
+
611
+ secondaryServer . on ( 'upgrade' , onUpgrade )
612
+ secondaryServer . listen ( { port : secondaryServerPort } )
613
+
614
+ eventQueue . push ( once ( secondaryServer , 'listening' ) )
615
+ }
596
616
597
- server . listen ( { port : settings . port } )
598
- await once ( server , 'listening' )
617
+ await Promise . all ( eventQueue )
599
618
600
619
const scheme = settings . https ? 'https' : 'http'
601
620
return `${ scheme } ://localhost:${ settings . port } `
0 commit comments