Skip to content

Commit 1cdf696

Browse files
chore: housekeeping
chore(deps): update dependency vitepress to ^1.5.0 (#9) Co-Authored-By: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent 34a5834 commit 1cdf696

File tree

9 files changed

+88
-80
lines changed

9 files changed

+88
-80
lines changed

.vscode/dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ davidanson
1313
degit
1414
deps
1515
destructurable
16+
dtsx
1617
entrypoints
1718
heroicons
1819
lockb
@@ -26,6 +27,7 @@ Postcardware
2627
postcompile
2728
prefetch
2829
preinstall
30+
proxied
2931
socio
3032
Solana
3133
stacksjs

bin/cli.ts

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,32 @@
1+
import type { ReverseProxyOption } from '../src/types'
12
import os from 'node:os'
2-
import { log, CAC } from '@stacksjs/cli'
3+
import { CAC, log } from '@stacksjs/cli'
34
import { readFileSync, writeFileSync } from '@stacksjs/storage'
45
import { version } from '../package.json'
56
import { config } from '../src/config'
67
import { startProxy } from '../src/start'
7-
import type { ReverseProxyOption } from '../src/types'
88

99
const cli = new CAC('reverse-proxy')
1010

1111
cli
1212
.command('start', 'Start the Reverse Proxy Server')
13-
.option('--from <from>', 'The URL to proxy from')
14-
.option('--to <to>', 'The URL to proxy to')
15-
.option('--keyPath <path>', 'Absolute path to the SSL key')
16-
.option('--certPath <path>', 'Absolute path to the SSL certificate')
17-
.option('--verbose', 'Enable verbose logging', { default: false })
18-
.example('reverse-proxy start --from localhost:3000 --to my-project.localhost')
19-
.example('reverse-proxy start --from localhost:3001 --to my-project.localhost/api')
13+
.option('--from <from>', 'The URL to proxy from', { default: config.from })
14+
.option('--to <to>', 'The URL to proxy to', { default: config.to })
15+
.option('--key-path <path>', 'Absolute path to the SSL key', { default: config.keyPath })
16+
.option('--cert-path <path>', 'Absolute path to the SSL certificate', { default: config.certPath })
17+
.option('--verbose', 'Enable verbose logging', { default: config.verbose })
18+
.example('reverse-proxy start --from localhost:5173 --to my-project.localhost')
19+
.example('reverse-proxy start --from localhost:3000 --to my-project.localhost/api')
2020
.example('reverse-proxy start --from localhost:3000 --to localhost:3001')
21-
.example(
22-
'reverse-proxy start --from localhost:3000 --to my-project.test --keyPath /absolute/path/to/key --certPath /absolute/path/to/cert',
23-
)
21+
.example('reverse-proxy start --from localhost:5173 --to my-project.test --key-path /absolute/path/to/key --cert-path /absolute/path/to/cert')
2422
.action(async (options?: ReverseProxyOption) => {
25-
if (options?.from || options?.to) {
26-
startProxy({
27-
from: options?.from ?? 'localhost:3000',
28-
to: options?.to ?? 'stacks.localhost',
29-
keyPath: options?.keyPath,
30-
certPath: options?.certPath,
31-
})
32-
33-
return
34-
}
35-
36-
// loop over the config and start all the proxies
37-
if (config) {
38-
for (const [from, to] of Object.entries(config)) {
39-
startProxy({
40-
from,
41-
to,
42-
keyPath: options?.keyPath,
43-
certPath: options?.certPath,
44-
})
45-
}
46-
} else {
47-
// eslint-disable-next-line no-console
48-
console.log('No proxies found in the config')
49-
}
23+
startProxy({
24+
from: options?.from,
25+
to: options?.to,
26+
keyPath: options?.keyPath,
27+
certPath: options?.certPath,
28+
verbose: options?.verbose,
29+
})
5030
})
5131

5232
cli
@@ -82,30 +62,32 @@ cli
8262
// If not, append it
8363
currentHostsContent += `\n${entry}`
8464
updated = true
85-
} else {
65+
}
66+
else {
8667
log.info(`Entry for ${host} already exists in the hosts file.`)
8768
}
8869
}
8970

9071
if (updated) {
9172
writeFileSync(hostsFilePath, currentHostsContent, 'utf8')
9273
log.success('Hosts file updated with latest proxy domains.')
93-
} else {
74+
}
75+
else {
9476
log.info('No new entries were added to the hosts file.')
9577
}
96-
} catch (error: unknown) {
78+
}
79+
catch (error: unknown) {
9780
if ((error as NodeJS.ErrnoException).code === 'EACCES')
9881
console.error('Permission denied. Please run this command with administrative privileges.')
9982
else console.error(`An error occurred: ${(error as NodeJS.ErrnoException).message}`)
10083
}
101-
} else {
102-
// eslint-disable-next-line no-console
84+
}
85+
else {
10386
console.log('No proxies found. Is your config configured properly?')
10487
}
10588
})
10689

10790
cli.command('version', 'Show the version of the Reverse Proxy CLI').action(() => {
108-
// eslint-disable-next-line no-console
10991
console.log(version)
11092
})
11193

build.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { log } from '@stacksjs/cli'
21
import { $ } from 'bun'
3-
import dts from 'bun-plugin-dts-auto'
2+
import { log } from '@stacksjs/cli'
3+
import { dts } from 'bun-plugin-dtsx'
44

55
log.info('Building...')
66

bun.lockb

-327 KB
Binary file not shown.

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@stacksjs/reverse-proxy",
33
"type": "module",
44
"version": "0.1.0",
5-
"description": "A modern reverse proxy, powered by Bun.",
5+
"description": "A modern reverse proxy.",
66
"author": "Chris Breuer <chris@stacksjs.org>",
77
"license": "MIT",
88
"homepage": "https://github.com/stacksjs/reverse-proxy#readme",
@@ -62,18 +62,18 @@
6262
"release": "bun run changelog && bunx bumpp package.json --all",
6363
"test": "bun test",
6464
"typecheck": "bunx tsc --noEmit",
65-
"docs:dev": "vitepress dev docs",
66-
"docs:build": "vitepress build docs",
67-
"docs:preview": "vitepress preview docs"
65+
"dev:docs": "vitepress dev docs",
66+
"build:docs": "vitepress build docs",
67+
"preview:docs": "vitepress preview docs"
6868
},
6969
"devDependencies": {
7070
"@stacksjs/cli": "^0.68.2",
7171
"@stacksjs/eslint-config": "^3.8.1-beta.2",
72-
"@stacksjs/development": "^0.68.2",
7372
"@stacksjs/storage": "^0.68.2",
7473
"@types/bun": "^1.1.13",
7574
"bun-config": "^0.2.0",
7675
"bun-plugin-dtsx": "^0.21.8",
76+
"typescript": "^5.6.3",
7777
"vitepress": "^1.5.0"
7878
},
7979
"simple-git-hooks": {

reverse-proxy.config.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
export default {
2-
'localhost:5173': 'stacks.localhost',
3-
// 'localhost:3333': 'docs.stacks.localhost',
4-
// 'localhost:8080': 'api.stacks.localhost',
5-
// 'localhost:1234': 'example.test',
6-
}
1+
import type { ReverseProxyOptions } from './src/types'
2+
3+
const config: ReverseProxyOptions = [
4+
{
5+
from: 'localhost:5173',
6+
to: 'stacks.localhost',
7+
// key: 'content of the key',
8+
keyPath: '/absolute/path/to/the/key',
9+
// cert: 'content of the cert',
10+
certPath: '/absolute/path/to/the/cert',
11+
httpsRedirect: true,
12+
verbose: false,
13+
},
14+
]
15+
16+
export default config

src/config.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import type { ReverseProxyConfig } from './types'
22
import { loadConfig } from 'bun-config'
33

4-
// Get loaded config
54
// eslint-disable-next-line antfu/no-top-level-await
65
const config: ReverseProxyConfig = await loadConfig({
76
name: 'reverse-proxy',
87
defaultConfig: {
9-
'localhost:5173': 'stacks.localhost',
8+
from: 'localhost:5173',
9+
to: 'stacks.localhost',
10+
key: undefined,
11+
keyPath: undefined,
12+
cert: undefined,
13+
certPath: undefined,
14+
httpsRedirect: false,
15+
verbose: true,
1016
},
1117
})
1218

src/start.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,38 @@ import * as net from 'node:net'
44
import process from 'node:process'
55
import { bold, dim, green, log } from '@stacksjs/cli'
66
import { version } from '../package.json'
7+
import { config } from './config'
78

8-
export async function startServer(
9-
options: ReverseProxyOption = { from: 'localhost:3000', to: 'stacks.localhost' },
10-
): Promise<void> {
9+
export async function startServer(options: ReverseProxyOption): Promise<void> {
1110
log.debug('Starting Reverse Proxy Server', options)
1211

12+
if (!options.from)
13+
options.from = 'localhost:5173'
14+
if (!options.to)
15+
options.to = 'stacks.localhost'
16+
17+
// Ensure the from URL has a valid protocol
18+
if (!options.from.startsWith('http://') && !options.from.startsWith('https://')) {
19+
options.from = `http://${options.from}` // Default to http if no protocol is specified
20+
}
21+
1322
// Parse the option.from URL to dynamically set hostname and port
14-
const fromUrl = new URL(
15-
options.from
16-
? options.from.startsWith('http')
17-
? options.from
18-
: `http://${options.from}`
19-
: 'http://localhost:3000',
20-
)
23+
const fromUrl = new URL(options.from)
24+
console.log('fromUrl', fromUrl)
2125
const hostname = fromUrl.hostname
22-
const port = Number.parseInt(fromUrl.port) || (fromUrl.protocol === 'https:' ? 443 : 80)
26+
const port = Number.parseInt(fromUrl.port) || (fromUrl.protocol.includes('https:') ? 443 : 80)
27+
console.log('hostname', hostname)
28+
console.log('port', port)
2329

2430
// Attempt to connect to the specified host and port
2531
const socket = net.connect(port, hostname, () => {
2632
log.debug(`Successfully connected to ${options.from}`)
2733
socket.end()
2834

35+
const to = `${hostname}:${port}`
36+
2937
// Proceed with setting up the reverse proxy after successful connection
30-
setupReverseProxy({ ...options, hostname, port })
38+
setupReverseProxy({ ...options, to })
3139
})
3240

3341
socket.on('error', (err) => {
@@ -39,7 +47,7 @@ export async function startServer(
3947
export function setupReverseProxy(options: ReverseProxyOption): void {
4048
log.debug('setupReverseProxy', options)
4149

42-
const { hostname, port, from, to } = options
50+
const { from, to } = options
4351

4452
// Check if port 80 is in use
4553
const testServer = net.createServer()
@@ -57,11 +65,10 @@ export function setupReverseProxy(options: ReverseProxyOption): void {
5765
const httpServer = http.createServer((req, res) => {
5866
// Define the target server's options
5967
const options = {
60-
hostname,
61-
port,
68+
to,
6269
path: req.url,
6370
method: req.method,
64-
headers: { ...req.headers, host: `${hostname}:${port}` }, // Update the host header
71+
headers: { ...req.headers, host: to },
6572
}
6673

6774
// Create a request to the target server
@@ -109,6 +116,9 @@ export function startHttpRedirectServer(): void {
109116
}
110117

111118
export function startProxy(option?: ReverseProxyOption): void {
119+
if (!option)
120+
option = config
121+
112122
startServer(option)
113123
}
114124

src/types.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
export interface ReverseProxyConfig {
2-
hostname: string
3-
port: number
42
from: string // domain to proxy from, defaults to localhost:3000
53
to: string // domain to proxy to, defaults to stacks.localhost
6-
key: string // content of the key
7-
keyPath: string // absolute path to the key
8-
cert: string // content of the cert
9-
certPath: string // absolute path to the cert
4+
key?: string // content of the key
5+
keyPath?: string // absolute path to the key
6+
cert?: string // content of the cert
7+
certPath?: string // absolute path to the cert
108
httpsRedirect: boolean // redirect http to https, defaults to true
119
verbose: boolean
1210
}

0 commit comments

Comments
 (0)