Skip to content

Commit 4f085d2

Browse files
committed
Fixes, use beforeExit
1 parent 8c578a8 commit 4f085d2

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

bin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ switch (cmd) {
6464
case 'run': {
6565
const server = createServer(handleRequest);
6666
server.listen(port, () => console.log('Listening on port', port));
67-
process.on('exit', () => {
67+
process.on('beforeExit', () => {
6868
console.log('Shutting down server');
6969
server.close();
7070
unlinkSync(pidFile);

index.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readFileSync } from 'fs';
22
import { join } from 'path';
3-
import { parse } from 'url';
3+
import { Writable } from 'stream';
44

55
const origin = process.env.ALLOW_ORIGIN || '*';
66
const 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+
*/
5863
function 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+
*/
8496
export 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

Comments
 (0)