Skip to content

Commit e77f060

Browse files
authored
feat: add 'cors-proxy' CLI that can launch proxy in the background (#3)
1 parent 6e84faa commit e77f060

File tree

4 files changed

+198
-111
lines changed

4 files changed

+198
-111
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,38 @@ Namely, it blocks requests that don't look like valid git requests.
1313
npm install @isomorphic-git/cors-proxy
1414
```
1515

16+
## CLI usage
17+
18+
Start proxy on default port 9999:
19+
20+
```sh
21+
cors-proxy start
22+
```
23+
24+
Start proxy on a custom port:
25+
26+
```sh
27+
cors-proxy start -p 9889
28+
```
29+
30+
Start proxy in daemon mode. It will write the PID of the daemon process to `$PWD/cors-proxy.pid`:
31+
32+
```sh
33+
cors-proxy start -d
34+
```
35+
36+
Kill the process with the PID specified in `$PWD/cors-proxy.pid`:
37+
38+
```sh
39+
cors-proxy stop
40+
```
41+
1642
## Configuration
1743

1844
Environment variables:
19-
- `PORT` the port to listen to
45+
- `PORT` the port to listen to (if run with `npm start`)
2046
- `ALLOW_ORIGIN` the value for the 'Access-Control-Allow-Origin' CORS header
2147

22-
2348
## License
2449

2550
This work is released under [The MIT License](https://opensource.org/licenses/MIT)

bin.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/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 = path.join(
13+
process.cwd(),
14+
'node_modules',
15+
'.bin',
16+
process.platform === 'win32' ? 'micro.cmd' : 'micro'
17+
)
18+
const args = [
19+
`--listen=tcp://0.0.0.0:${p || 9999}`
20+
]
21+
let server = spawn(
22+
cmd, args,
23+
{
24+
stdio: 'inherit',
25+
windowsHide: true
26+
}
27+
)
28+
fs.writeFileSync(
29+
path.join(process.cwd(), 'cors-proxy.pid'),
30+
String(process.pid),
31+
'utf8'
32+
)
33+
process.on('exit', server.kill)
34+
return
35+
}
36+
case 'stop': {
37+
let pid
38+
try {
39+
pid = fs.readFileSync(
40+
path.join(process.cwd(), 'cors-proxy.pid'),
41+
'utf8'
42+
);
43+
} catch (err) {
44+
console.log('No cors-proxy.pid file')
45+
return
46+
}
47+
pid = parseInt(pid)
48+
console.log('killing', pid)
49+
kill(pid, (err) => {
50+
if (err) {
51+
console.log(err)
52+
} else {
53+
fs.unlinkSync(path.join(process.cwd(), 'cors-proxy.pid'))
54+
}
55+
})
56+
}
57+
}
58+
}
59+
60+
minimisted(main)

0 commit comments

Comments
 (0)