Skip to content

Commit 3c4f8c0

Browse files
committed
Rewrite to ESM, remove broken deps
1 parent 65e7d34 commit 3c4f8c0

File tree

8 files changed

+8175
-7427
lines changed

8 files changed

+8175
-7427
lines changed

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"useTabs": true,
3+
"tabWidth": 2,
4+
"printWidth": 120,
5+
"singleQuote": true
6+
}

allow-request.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

bin.js

Lines changed: 88 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,91 @@
11
#!/usr/bin/env node
2-
const fs = require('fs')
3-
const path = require('path')
4-
const {spawn} = require('child_process')
5-
const kill = require('tree-kill')
6-
const minimisted = require('minimisted')
7-
8-
async function main({_: [cmd], p, d}) {
9-
switch (cmd) {
10-
case 'start': {
11-
if (d) require('daemonize-process')()
12-
const cmd = require.resolve('micro/bin/micro.js')
13-
const args = [
14-
cmd,
15-
`--listen=tcp://0.0.0.0:${p || 9999}`
16-
]
17-
let server = spawn(
18-
'node', args,
19-
{
20-
stdio: 'inherit',
21-
windowsHide: true,
22-
cwd: __dirname
23-
}
24-
)
25-
fs.writeFileSync(
26-
path.join(process.cwd(), 'cors-proxy.pid'),
27-
String(process.pid),
28-
'utf8'
29-
)
30-
process.on('exit', server.kill)
31-
return
32-
}
33-
case 'stop': {
34-
let pid
35-
try {
36-
pid = fs.readFileSync(
37-
path.join(process.cwd(), 'cors-proxy.pid'),
38-
'utf8'
39-
);
40-
} catch (err) {
41-
console.log('No cors-proxy.pid file')
42-
return
43-
}
44-
pid = parseInt(pid)
45-
console.log('killing', pid)
46-
kill(pid, (err) => {
47-
if (err) {
48-
console.log(err)
49-
} else {
50-
fs.unlinkSync(path.join(process.cwd(), 'cors-proxy.pid'))
51-
}
52-
})
53-
}
54-
}
2+
3+
import { spawn } from 'node:child_process';
4+
import { existsSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs';
5+
import { tmpdir } from 'node:os';
6+
import { join } from 'node:path';
7+
import { parseArgs } from 'node:util';
8+
import handleRequest from './index.js';
9+
10+
const temp = tmpdir();
11+
12+
const {
13+
positionals: [cmd],
14+
values: { port, help, pid: pidFile },
15+
} = parseArgs({
16+
options: {
17+
help: { type: 'boolean', short: 'h', default: false },
18+
port: { type: 'string', short: 'p', default: '9999' },
19+
pid: { type: 'string', default: join(temp, 'cors-proxy.pid') },
20+
},
21+
allowPositionals: true,
22+
});
23+
24+
if (cmd == 'help' || help || !cmd) {
25+
console.log(`Usage: ${process.argv0} [...options] <command>
26+
27+
Commands:
28+
run Run the CORS proxy server in the foreground
29+
start Start the CORS proxy server daemon
30+
stop Stop the CORS proxy server daemon
31+
32+
Options:
33+
-h, --help Show this help message
34+
-p, --port <port> Port to listen on (default: 9999)
35+
--pid <path> Path to PID file (default: ${join(temp, 'cors-proxy.pid')}
36+
`);
5537
}
5638

57-
minimisted(main)
39+
switch (cmd) {
40+
case 'run': {
41+
const server = createServer(handleRequest);
42+
server.listen(port, () => console.log('Listening on port', port));
43+
process.on('exit', () => {
44+
console.log('Shutting down server');
45+
server.close();
46+
});
47+
break;
48+
}
49+
50+
case 'start': {
51+
if (existsSync(pidFile)) {
52+
let pid;
53+
try {
54+
pid = parseInt(readFileSync(pidFile, 'utf8'));
55+
} catch (e) {
56+
console.error('Found existing PID file but could not read it');
57+
process.exit(13);
58+
}
59+
60+
try {
61+
process.kill(pid, 0);
62+
} catch {
63+
console.error(`Server is already running (pid is ${pid})`);
64+
process.exit(16);
65+
}
66+
67+
console.error('Removing stale PID file');
68+
unlinkSync(pidFile);
69+
}
70+
71+
const daemon = spawn(
72+
process.execPath,
73+
['run', port && `--port=${port}`].filter((a) => a),
74+
{
75+
stdio: 'ignore',
76+
detached: true,
77+
},
78+
);
79+
daemon.unref();
80+
console.log('Started CORS proxy server with PID', daemon.pid);
81+
writeFileSync(pidFile, daemon.pid.toString());
82+
process.exit(0);
83+
}
84+
case 'stop':
85+
try {
86+
const pid = parseInt(readFileSync(pidFile, 'utf8'));
87+
process.kill(pid);
88+
} catch (e) {
89+
console.error('Invalid or missing PID file');
90+
}
91+
}

index.html

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<!doctype html>
2+
<html>
3+
<title>@isomorphic-git/cors-proxy</title>
4+
<h1>@isomorphic-git/cors-proxy</h1>
5+
<p>
6+
This is the server software that runs on
7+
<a href="https://cors.isomorphic-git.org"
8+
>https://cors.isomorphic-git.org</a
9+
>
10+
&ndash; a free service (generously sponsored by
11+
<a
12+
href="https://www.clever-cloud.com/?utm_source=ref&utm_medium=link&utm_campaign=isomorphic-git"
13+
>Clever Cloud</a
14+
>) for users of
15+
<a href="https://isomorphic-git.org">isomorphic-git</a> that enables
16+
cloning and pushing repos in the browser.
17+
</p>
18+
<p>
19+
The source code is hosted on Github at
20+
<a href="https://github.com/isomorphic-git/cors-proxy"
21+
>https://github.com/isomorphic-git/cors-proxy</a
22+
>
23+
</p>
24+
<p>
25+
It can also be installed from npm with
26+
<code
27+
>npm install
28+
<a href="https://npmjs.org/package/@isomorphic-git/cors-proxy">
29+
@isomorphic-git/cors-proxy
30+
</a></code
31+
>
32+
</p>
33+
34+
<h2>Terms of Use</h2>
35+
<p>
36+
<b
37+
>This free service is provided to you AS IS with no guarantees. By
38+
using this free service, you promise not to use excessive amounts of
39+
bandwidth.
40+
</b>
41+
</p>
42+
43+
<p>
44+
<b
45+
>If you are cloning or pushing large amounts of data your IP address
46+
may be banned. Please run your own instance of the software if you
47+
need to make heavy use this service.</b
48+
>
49+
</p>
50+
51+
<h2>Allowed Origins</h2>
52+
This proxy allows git clone / fetch / push / getRemoteInfo requests from
53+
these domains:
54+
<code>%allowed_origins%</code>
55+
</html>

0 commit comments

Comments
 (0)