@@ -13,6 +13,7 @@ import contentType from 'content-type'
1313import cookie from 'cookie'
1414import { get } from 'dot-prop'
1515import generateETag from 'etag'
16+ import getAvailablePort from 'get-port'
1617import httpProxy from 'http-proxy'
1718import { createProxyMiddleware } from 'http-proxy-middleware'
1819import jwtDecode from 'jwt-decode'
@@ -545,6 +546,7 @@ export const startProxy = async function ({
545546 siteInfo,
546547 state,
547548} ) {
549+ const secondaryServerPort = settings . https ? await getAvailablePort ( ) : null
548550 const functionsServer = settings . functionsPort ? `http://127.0.0.1:${ settings . functionsPort } ` : null
549551 const edgeFunctionsProxy = await initializeEdgeFunctionsProxy ( {
550552 config,
@@ -555,9 +557,10 @@ export const startProxy = async function ({
555557 geoCountry,
556558 getUpdatedConfig,
557559 inspectSettings,
560+ mainPort : settings . port ,
558561 offline,
562+ passthroughPort : secondaryServerPort || settings . port ,
559563 projectDir,
560- settings,
561564 siteInfo,
562565 state,
563566 } )
@@ -586,16 +589,32 @@ export const startProxy = async function ({
586589 functionsServer,
587590 edgeFunctionsProxy,
588591 } )
589- const server = settings . https
592+ const primaryServer = settings . https
590593 ? https . createServer ( { cert : settings . https . cert , key : settings . https . key } , onRequestWithOptions )
591594 : http . createServer ( onRequestWithOptions )
592-
593- server . on ( 'upgrade' , function onUpgrade ( req , socket , head ) {
595+ const onUpgrade = function onUpgrade ( req , socket , head ) {
594596 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+ }
596616
597- server . listen ( { port : settings . port } )
598- await once ( server , 'listening' )
617+ await Promise . all ( eventQueue )
599618
600619 const scheme = settings . https ? 'https' : 'http'
601620 return `${ scheme } ://localhost:${ settings . port } `
0 commit comments