@@ -56,16 +56,15 @@ const landingPage = readFileSync(join(import.meta.dirname, 'index.html'), 'utf8'
5656) ;
5757
5858/**
59- *
60- * @param {import('http').IncomingMessage } req
59+ *
60+ * @param {import('http').IncomingMessage } req
6161 * @param {URL } u
6262 */
6363function isAllowed ( req , u ) {
6464 const isInfoRefs =
6565 u . pathname . endsWith ( '/info/refs' ) &&
6666 ( u . searchParams . get ( 'service' ) === 'git-upload-pack' || u . searchParams . get ( 'service' ) === 'git-receive-pack' ) ;
6767
68-
6968 switch ( req . method ) {
7069 case 'OPTIONS' :
7170 if ( isInfoRefs ) return true ;
@@ -88,10 +87,10 @@ function isAllowed(req, u) {
8887}
8988
9089/**
91- *
92- * @param {import('http').ClientRequest } req
93- * @param {import('http').ServerResponse } res
94- * @returns
90+ *
91+ * @param {import('http').IncomingMessage } req
92+ * @param {import('http').ServerResponse } res
93+ * @returns
9594 */
9695export default function handleRequest ( req , res ) {
9796 const u = new URL ( req . url , `https://0.0.0.0:${ req . socket . localPort } /` ) ;
@@ -135,47 +134,48 @@ export default function handleRequest(req, res) {
135134
136135 if ( process . env . DEBUG ) console . log ( req . method , req . url ) ;
137136
138- let headers = { } ;
137+ const headers = { } ;
139138 for ( let h of allowHeaders ) {
140139 if ( req . headers [ h ] ) {
141140 headers [ h ] = req . headers [ h ] ;
142141 }
143142 }
144143
145144 // GitHub uses user-agent sniffing for git/* and changes its behavior which is frustrating
146- if ( ! headers [ 'user-agent' ] || ! headers [ 'user-agent' ] . startsWith ( 'git/' ) ) {
145+ if ( ! headers [ 'user-agent' ] ? .startsWith ( 'git/' ) ) {
147146 headers [ 'user-agent' ] = 'git/@isomorphic-git/cors-proxy' ;
148147 }
149148
150- let p = u . pathname ;
151- let [ , pathdomain , remainingpath ] = p . match ( / \/ ( [ ^ \/ ] * ) \/ ( .* ) / ) ;
149+ let [ , pathdomain , remainingpath ] = u . pathname . match ( / \/ ( [ ^ \/ ] * ) \/ ( .* ) / ) ;
152150 let protocol = insecure_origins . includes ( pathdomain ) ? 'http' : 'https' ;
153151
154152 fetch ( `${ protocol } ://${ pathdomain } /${ remainingpath } ` , {
155153 method : req . method ,
156154 redirect : 'manual' ,
157155 headers,
158156 body : req . method !== 'GET' && req . method !== 'HEAD' ? req : undefined ,
159- } ) . then ( ( f ) => {
160- if ( f . headers . has ( 'location' ) ) {
161- // Modify the location so the client continues to use the proxy
162- let newUrl = f . headers . get ( 'location' ) . replace ( / ^ h t t p s ? : \/ / , '' ) ;
163- f . headers . set ( 'location' , newUrl ) ;
164- }
165- res . statusCode = f . status ;
166- for ( let h of exposeHeaders ) {
167- if ( h === 'content-length' ) continue ;
168- if ( f . headers . has ( h ) ) {
169- res . setHeader ( h , f . headers . get ( h ) ) ;
157+ } )
158+ . then ( ( f ) => {
159+ if ( f . headers . has ( 'location' ) ) {
160+ // Modify the location so the client continues to use the proxy
161+ let newUrl = f . headers . get ( 'location' ) . replace ( / ^ h t t p s ? : \/ / , '' ) ;
162+ f . headers . set ( 'location' , newUrl ) ;
170163 }
171- }
172- if ( f . redirected ) {
173- res . setHeader ( 'x-redirected-url' , f . url ) ;
174- }
175- return f . body . pipeTo ( Writable . toWeb ( res ) ) ;
176- } ) . catch ( e => {
177- res . statusCode = 502 ;
178- console . error ( e ) ;
179- res . end ( ) ;
180- } ) ;
164+ res . statusCode = f . status ;
165+ for ( let h of exposeHeaders ) {
166+ if ( h === 'content-length' ) continue ;
167+ if ( f . headers . has ( h ) ) {
168+ res . setHeader ( h , f . headers . get ( h ) ) ;
169+ }
170+ }
171+ if ( f . redirected ) {
172+ res . setHeader ( 'x-redirected-url' , f . url ) ;
173+ }
174+ return f . body . pipeTo ( Writable . toWeb ( res ) ) ;
175+ } )
176+ . catch ( ( e ) => {
177+ res . statusCode = 502 ;
178+ console . error ( e ) ;
179+ res . end ( ) ;
180+ } ) ;
181181}
0 commit comments