11import { readFileSync } from 'fs' ;
22import { join } from 'path' ;
3- import { parse } from 'url ' ;
3+ import { Writable } from 'stream ' ;
44
55const origin = process . env . ALLOW_ORIGIN || '*' ;
66const insecure_origins = ( process . env . INSECURE_HTTP_ORIGINS || '' ) . split ( ',' ) ;
@@ -55,10 +55,16 @@ const landingPage = readFileSync(join(import.meta.dirname, 'index.html'), 'utf8'
5555 origin ,
5656) ;
5757
58+ /**
59+ *
60+ * @param {import('http').IncomingMessage } req
61+ * @param {URL } u
62+ */
5863function isAllowed ( req , u ) {
5964 const isInfoRefs =
6065 u . pathname . endsWith ( '/info/refs' ) &&
61- ( u . query . service === 'git-upload-pack' || u . query . service === 'git-receive-pack' ) ;
66+ ( u . searchParams . get ( 'service' ) === 'git-upload-pack' || u . searchParams . get ( 'service' ) === 'git-receive-pack' ) ;
67+
6268
6369 switch ( req . method ) {
6470 case 'OPTIONS' :
@@ -81,8 +87,14 @@ function isAllowed(req, u) {
8187 }
8288}
8389
90+ /**
91+ *
92+ * @param {import('http').ClientRequest } req
93+ * @param {import('http').ServerResponse } res
94+ * @returns
95+ */
8496export default function handleRequest ( req , res ) {
85- const u = parse ( req . url , true ) ;
97+ const u = new URL ( req . url , `https://0.0.0.0: ${ req . socket . localPort } /` ) ;
8698
8799 // CORS
88100
@@ -135,7 +147,7 @@ export default function handleRequest(req, res) {
135147 headers [ 'user-agent' ] = 'git/@isomorphic-git/cors-proxy' ;
136148 }
137149
138- let p = u . path ;
150+ let p = u . pathname ;
139151 let [ , pathdomain , remainingpath ] = p . match ( / \/ ( [ ^ \/ ] * ) \/ ( .* ) / ) ;
140152 let protocol = insecure_origins . includes ( pathdomain ) ? 'http' : 'https' ;
141153
@@ -160,6 +172,10 @@ export default function handleRequest(req, res) {
160172 if ( f . redirected ) {
161173 res . setHeader ( 'x-redirected-url' , f . url ) ;
162174 }
163- f . body . pipe ( res ) ;
175+ return f . body . pipeTo ( Writable . toWeb ( res ) ) ;
176+ } ) . catch ( e => {
177+ res . statusCode = 502 ;
178+ console . error ( e ) ;
179+ res . end ( ) ;
164180 } ) ;
165181}
0 commit comments