Skip to content

Commit

Permalink
feat: add 'cors-proxy' CLI that can launch proxy in the background (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose authored Sep 11, 2018
1 parent 6e84faa commit e77f060
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 111 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,38 @@ Namely, it blocks requests that don't look like valid git requests.
npm install @isomorphic-git/cors-proxy
```

## CLI usage

Start proxy on default port 9999:

```sh
cors-proxy start
```

Start proxy on a custom port:

```sh
cors-proxy start -p 9889
```

Start proxy in daemon mode. It will write the PID of the daemon process to `$PWD/cors-proxy.pid`:

```sh
cors-proxy start -d
```

Kill the process with the PID specified in `$PWD/cors-proxy.pid`:

```sh
cors-proxy stop
```

## Configuration

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


## License

This work is released under [The MIT License](https://opensource.org/licenses/MIT)
60 changes: 60 additions & 0 deletions bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const {spawn} = require('child_process')
const kill = require('tree-kill')
const minimisted = require('minimisted')

async function main({_: [cmd], p, d}) {
switch (cmd) {
case 'start': {
if (d) require('daemonize-process')()
const cmd = path.join(
process.cwd(),
'node_modules',
'.bin',
process.platform === 'win32' ? 'micro.cmd' : 'micro'
)
const args = [
`--listen=tcp://0.0.0.0:${p || 9999}`
]
let server = spawn(
cmd, args,
{
stdio: 'inherit',
windowsHide: true
}
)
fs.writeFileSync(
path.join(process.cwd(), 'cors-proxy.pid'),
String(process.pid),
'utf8'
)
process.on('exit', server.kill)
return
}
case 'stop': {
let pid
try {
pid = fs.readFileSync(
path.join(process.cwd(), 'cors-proxy.pid'),
'utf8'
);
} catch (err) {
console.log('No cors-proxy.pid file')
return
}
pid = parseInt(pid)
console.log('killing', pid)
kill(pid, (err) => {
if (err) {
console.log(err)
} else {
fs.unlinkSync(path.join(process.cwd(), 'cors-proxy.pid'))
}
})
}
}
}

minimisted(main)
Loading

0 comments on commit e77f060

Please sign in to comment.