Skip to content

Commit 6bb85ac

Browse files
committed
chore: wip
1 parent 2cfb363 commit 6bb85ac

File tree

4 files changed

+171
-54
lines changed

4 files changed

+171
-54
lines changed

README.md

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,64 @@
66
<!-- [![npm downloads][npm-downloads-src]][npm-downloads-href] -->
77
<!-- [![Codecov][codecov-src]][codecov-href] -->
88

9-
# bun-ts-starter
9+
# Reverse Proxy
1010

11-
This is an opinionated TypeScript Starter kit to help kick-start development of your next Bun package.
11+
A modern reverse proxy. Pretty dev URLs for your local projects, with simplicity and flexibility.
1212

13-
## Get Started
13+
## ☘️ Features
1414

15-
It's rather simple to get your package development started:
15+
- Reverse Proxy
16+
- SSL Support
17+
- Custom Domains
18+
- Auto HTTP to HTTPS Redirection
19+
- /etc/hosts Management
20+
- Dependency-free Binary
21+
22+
## 🤖 Usage
1623

1724
```bash
18-
# you may use this GitHub template or the following command:
19-
bunx degit stacksjs/bun-ts-starter my-pkg
20-
cd my-pkg
25+
bun install -d bun-reverse-proxy
26+
# brew install reverse-proxy
27+
# pkgx install reverse-proxy
28+
```
29+
30+
Now, you can use it in your project:
2131

22-
# if you don't have pnpm installed, run `npm i -g pnpm`
23-
bun i # install all deps
24-
bun run build # builds the library for production-ready use
32+
```js
33+
import { startProxy } from 'bun-reverse-proxy'
34+
35+
startProxy({
36+
from: 'localhost:3000',
37+
to: 'my-project.localhost'
38+
})
39+
```
2540

26-
# how to create a git commit?
27-
git add . # select the changes you want to commit
28-
bun run commit # then simply answer the questions
41+
### CLI
2942

30-
# after you have successfully committed, you may create a "release"
31-
bun run release # automates git commits, versioning, and changelog generations
43+
```bash
44+
reverse-proxy --from localhost:3000 --to my-project.localhost
45+
reverse-proxy --from localhost:3000 --to my-project.test --keyPath ./key.pem --certPath ./cert.pem
46+
reverse-proxy --help
47+
reverse-proxy --version
3248
```
3349

34-
_Check out the package.json scripts for more commands._
50+
### Configuration
51+
52+
You can also use a configuration file:
3553

36-
### Developer Experience (DX)
54+
```ts
55+
export default {
56+
'localhost:3000': 'stacks.localhost'
57+
}
58+
```
3759

38-
This Starter Kit comes pre-configured with the following:
60+
Then run:
3961

40-
- [Powerful Build Process](https://github.com/oven-sh/bun) - via Bun
41-
- [Fully Typed APIs](https://www.typescriptlang.org/) - via TypeScript 5.1
42-
- [Be a Good Commitizen](https://www.npmjs.com/package/git-cz) - pre-configured Commitizen & git-cz setup to simplify semantic git commits, versioning, and changelog generations
43-
- [Built With Testing In Mind](https://bun.sh/docs/cli/test) - pre-configured unit-testing powered by [Bun](https://bun.sh/docs/cli/test)
44-
- [Renovate](https://renovatebot.com/) - optimized & automated PR dependency updates
45-
- [GitHub Actions](https://github.com/features/actions) - runs your CI _(fixes code style issues, tags releases & creates its changelogs, runs the test suite, etc.)_
62+
```bash
63+
reverse-proxy start
64+
```
65+
66+
And your config will be loaded from `reverse-proxy.config.ts` _(or `reverse-proxy.config.js`)_. Learn more in the docs.
4667

4768
## Testing
4869

@@ -52,48 +73,24 @@ bun test
5273

5374
## Changelog
5475

55-
Please see our [releases](https://github.com/stackjs/bun-ts-starter/releases) page for more information on what has changed recently.
76+
Please see our [releases](https://github.com/stacksjs/stacks/releases) page for more information on what has changed recently.
5677

5778
## Contributing
5879

59-
Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.
80+
Please review the [Contributing Guide](https://github.com/stacksjs/contributing) for details.
6081

6182
## Community
6283

6384
For help, discussion about best practices, or any other conversation that would benefit from being searchable:
6485

65-
[Discussions on GitHub](https://github.com/stacksjs/bun-ts-starter/discussions)
86+
[Discussions on GitHub](https://github.com/stacksjs/stacks/discussions)
6687

6788
For casual chit-chat with others using this package:
6889

6990
[Join the Stacks Discord Server](https://discord.gg/stacksjs)
7091

71-
## Postcardware
72-
73-
You will always be free to use any of the Stacks OSS software. We would also love to see which parts of the world Stacks ends up in. _Receiving postcards makes us happy—andwe will publish them on our website._
74-
75-
Our address is: Stacks.js, 5710 Crescent Park #107, Playa Vista 90094, CA.
76-
77-
## Sponsors
78-
79-
We would like to extend our thanks to the following sponsors for funding Stacks development. If you are interested in becoming a sponsor, please reach out to us.
92+
## License
8093

81-
- [JetBrains](https://www.jetbrains.com/)
82-
- [The Solana Foundation](https://solana.com/)
83-
84-
## 📄 License
85-
86-
The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.
94+
The MIT License (MIT). Please see [LICENSE](https://github.com/stacksjs/stacks/tree/main/LICENSE.md) for more information.
8795

8896
Made with 💙
89-
90-
<!-- Badges -->
91-
[npm-version-src]: https://img.shields.io/npm/v/bun-ts-starter?style=flat-square
92-
[npm-version-href]: https://npmjs.com/package/bun-ts-starter
93-
[npm-downloads-src]: https://img.shields.io/npm/dm/bun-ts-starter?style=flat-square
94-
[npm-downloads-href]: https://npmjs.com/package/bun-ts-starter
95-
[github-actions-src]: https://img.shields.io/github/actions/workflow/status/stacksjs/bun-ts-starter/ci.yml?style=flat-square&branch=main
96-
[github-actions-href]: https://github.com/stacksjs/bun-ts-starter/actions?query=workflow%3Aci
97-
98-
<!-- [codecov-src]: https://img.shields.io/codecov/c/gh/stacksjs/bun-ts-starter/main?style=flat-square
99-
[codecov-href]: https://codecov.io/gh/stacksjs/bun-ts-starter -->

bin/cli.ts

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import os from 'node:os'
2+
import { cli as command, log } from '@stacksjs/cli'
3+
import { fs } from '@stacksjs/storage'
4+
import { startProxy } from '../src/start'
5+
import { config } from '../src/config'
6+
import { version } from '../package.json'
7+
8+
const cli = command('reverse-proxy')
9+
10+
interface Options {
11+
from?: string
12+
to?: string
13+
keyPath?: string
14+
certPath?: string
15+
}
16+
17+
cli
18+
.command('start', 'Start the Reverse Proxy Server')
19+
.option('--from <from>', 'The URL to proxy from')
20+
.option('--to <to>', 'The URL to proxy to')
21+
.option('--keyPath <path>', 'Absolute path to the SSL key')
22+
.option('--certPath <path>', 'Absolute path to the SSL certificate')
23+
.example('reverse-proxy start --from localhost:3000 --to my-project.localhost')
24+
.example('reverse-proxy start --from localhost:3000 --to localhost:3001')
25+
.example('reverse-proxy start --from localhost:3000 --to my-project.test --keyPath /absolute/path/to/key --certPath /absolute/path/to/cert')
26+
.action(async (options?: Options) => {
27+
if (options?.from || options?.to) {
28+
startProxy({
29+
from: options?.from ?? 'localhost:3000',
30+
to: options?.to ?? 'stacks.localhost',
31+
keyPath: options?.keyPath,
32+
certPath: options?.certPath,
33+
})
34+
35+
return
36+
}
37+
38+
// loop over the config and start all the proxies
39+
if (config) {
40+
for (const [from, to] of Object.entries(config)) {
41+
startProxy({
42+
from,
43+
to,
44+
keyPath: options?.keyPath,
45+
certPath: options?.certPath,
46+
})
47+
}
48+
}
49+
else {
50+
// eslint-disable-next-line no-console
51+
console.log('No proxies found in the config')
52+
}
53+
})
54+
55+
cli
56+
.command('update:etc-hosts', 'Update the /etc/hosts file with the proxy domains. Please note, this command requires sudo/admin permissions.')
57+
.alias('update-etc-hosts')
58+
.example('sudo reverse-proxy update:etc-hosts')
59+
.example('sudo reverse-proxy update-etc-hosts')
60+
.action(async () => {
61+
log.info('Ensuring /etc/hosts file covers the proxy domains...')
62+
const hostsFilePath = os.platform() === 'win32'
63+
? 'C:\\Windows\\System32\\drivers\\etc\\hosts'
64+
: '/etc/hosts'
65+
66+
if (config && typeof config === 'object') {
67+
const entriesToAdd = Object.entries(config).map(([from, to]) => `127.0.0.1 ${to} # reverse-proxy mapping for ${from}`)
68+
try {
69+
let currentHostsContent = fs.readFileSync(hostsFilePath, 'utf8')
70+
let updated = false
71+
72+
for (const entry of entriesToAdd) {
73+
const to = entry.split(' ')[1]
74+
// Check if the entry (domain) is already in the file
75+
if (!currentHostsContent.includes(to)) {
76+
// If not, append it
77+
currentHostsContent += `\n${entry}`
78+
updated = true
79+
}
80+
else {
81+
log.info(`Entry for ${to} already exists in the hosts file.`)
82+
}
83+
}
84+
85+
if (updated) {
86+
fs.writeFileSync(hostsFilePath, currentHostsContent, 'utf8')
87+
88+
log.success('Hosts file updated with latest proxy domains.')
89+
}
90+
else {
91+
log.info('No new entries were added to the hosts file.')
92+
}
93+
}
94+
catch (error) {
95+
if (error.code === 'EACCES')
96+
console.error('Permission denied. Please run this command with administrative privileges.')
97+
else
98+
console.error(`An error occurred: ${error.message}`)
99+
}
100+
}
101+
else {
102+
// eslint-disable-next-line no-console
103+
console.log('No proxies found. Is your config configured properly?')
104+
}
105+
})
106+
107+
cli
108+
.command('version', 'Show the version of the Reverse Proxy CLI')
109+
.action(() => {
110+
// eslint-disable-next-line no-console
111+
console.log(version)
112+
})
113+
114+
cli.version(version)
115+
cli.help()
116+
cli.parse()

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"exports": {
2424
".": {
2525
"bun": "./src/index.ts",
26-
"types": "./dist/index.d.ts",
2726
"import": "./dist/index.js"
2827
},
2928
"./*": {
@@ -33,6 +32,9 @@
3332
},
3433
"module": "./dist/index.js",
3534
"types": "./dist/index.d.ts",
35+
"bin": {
36+
"reverse-proxy": "./dist/cli.js"
37+
},
3638
"files": [
3739
"dist",
3840
"src"

reverse-proxy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env bun
2+
import('./bin/cli')

0 commit comments

Comments
 (0)