Skip to content

Commit

Permalink
chore: housekeeping
Browse files Browse the repository at this point in the history
chore(deps): update dependency vitepress to ^1.5.0 (#9)

Co-Authored-By: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
chrisbbreuer and renovate[bot] committed Nov 12, 2024
1 parent 34a5834 commit 1cdf696
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 80 deletions.
2 changes: 2 additions & 0 deletions .vscode/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ davidanson
degit
deps
destructurable
dtsx
entrypoints
heroicons
lockb
Expand All @@ -26,6 +27,7 @@ Postcardware
postcompile
prefetch
preinstall
proxied
socio
Solana
stacksjs
Expand Down
68 changes: 25 additions & 43 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,32 @@
import type { ReverseProxyOption } from '../src/types'
import os from 'node:os'
import { log, CAC } from '@stacksjs/cli'
import { CAC, log } from '@stacksjs/cli'
import { readFileSync, writeFileSync } from '@stacksjs/storage'
import { version } from '../package.json'
import { config } from '../src/config'
import { startProxy } from '../src/start'
import type { ReverseProxyOption } from '../src/types'

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

cli
.command('start', 'Start the Reverse Proxy Server')
.option('--from <from>', 'The URL to proxy from')
.option('--to <to>', 'The URL to proxy to')
.option('--keyPath <path>', 'Absolute path to the SSL key')
.option('--certPath <path>', 'Absolute path to the SSL certificate')
.option('--verbose', 'Enable verbose logging', { default: false })
.example('reverse-proxy start --from localhost:3000 --to my-project.localhost')
.example('reverse-proxy start --from localhost:3001 --to my-project.localhost/api')
.option('--from <from>', 'The URL to proxy from', { default: config.from })
.option('--to <to>', 'The URL to proxy to', { default: config.to })
.option('--key-path <path>', 'Absolute path to the SSL key', { default: config.keyPath })
.option('--cert-path <path>', 'Absolute path to the SSL certificate', { default: config.certPath })
.option('--verbose', 'Enable verbose logging', { default: config.verbose })
.example('reverse-proxy start --from localhost:5173 --to my-project.localhost')
.example('reverse-proxy start --from localhost:3000 --to my-project.localhost/api')
.example('reverse-proxy start --from localhost:3000 --to localhost:3001')
.example(
'reverse-proxy start --from localhost:3000 --to my-project.test --keyPath /absolute/path/to/key --certPath /absolute/path/to/cert',
)
.example('reverse-proxy start --from localhost:5173 --to my-project.test --key-path /absolute/path/to/key --cert-path /absolute/path/to/cert')
.action(async (options?: ReverseProxyOption) => {
if (options?.from || options?.to) {
startProxy({
from: options?.from ?? 'localhost:3000',
to: options?.to ?? 'stacks.localhost',
keyPath: options?.keyPath,
certPath: options?.certPath,
})

return
}

// loop over the config and start all the proxies
if (config) {
for (const [from, to] of Object.entries(config)) {
startProxy({
from,
to,
keyPath: options?.keyPath,
certPath: options?.certPath,
})
}
} else {
// eslint-disable-next-line no-console
console.log('No proxies found in the config')
}
startProxy({
from: options?.from,
to: options?.to,
keyPath: options?.keyPath,
certPath: options?.certPath,
verbose: options?.verbose,
})
})

cli
Expand Down Expand Up @@ -82,30 +62,32 @@ cli
// If not, append it
currentHostsContent += `\n${entry}`
updated = true
} else {
}
else {
log.info(`Entry for ${host} already exists in the hosts file.`)
}
}

if (updated) {
writeFileSync(hostsFilePath, currentHostsContent, 'utf8')
log.success('Hosts file updated with latest proxy domains.')
} else {
}
else {
log.info('No new entries were added to the hosts file.')
}
} catch (error: unknown) {
}
catch (error: unknown) {
if ((error as NodeJS.ErrnoException).code === 'EACCES')
console.error('Permission denied. Please run this command with administrative privileges.')
else console.error(`An error occurred: ${(error as NodeJS.ErrnoException).message}`)
}
} else {
// eslint-disable-next-line no-console
}
else {
console.log('No proxies found. Is your config configured properly?')
}
})

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

Expand Down
4 changes: 2 additions & 2 deletions build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log } from '@stacksjs/cli'
import { $ } from 'bun'
import dts from 'bun-plugin-dts-auto'
import { log } from '@stacksjs/cli'
import { dts } from 'bun-plugin-dtsx'

log.info('Building...')

Expand Down
Binary file modified bun.lockb
Binary file not shown.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@stacksjs/reverse-proxy",
"type": "module",
"version": "0.1.0",
"description": "A modern reverse proxy, powered by Bun.",
"description": "A modern reverse proxy.",
"author": "Chris Breuer <chris@stacksjs.org>",
"license": "MIT",
"homepage": "https://github.com/stacksjs/reverse-proxy#readme",
Expand Down Expand Up @@ -62,18 +62,18 @@
"release": "bun run changelog && bunx bumpp package.json --all",
"test": "bun test",
"typecheck": "bunx tsc --noEmit",
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
"dev:docs": "vitepress dev docs",
"build:docs": "vitepress build docs",
"preview:docs": "vitepress preview docs"
},
"devDependencies": {
"@stacksjs/cli": "^0.68.2",
"@stacksjs/eslint-config": "^3.8.1-beta.2",
"@stacksjs/development": "^0.68.2",
"@stacksjs/storage": "^0.68.2",
"@types/bun": "^1.1.13",
"bun-config": "^0.2.0",
"bun-plugin-dtsx": "^0.21.8",
"typescript": "^5.6.3",
"vitepress": "^1.5.0"
},
"simple-git-hooks": {
Expand Down
22 changes: 16 additions & 6 deletions reverse-proxy.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
export default {
'localhost:5173': 'stacks.localhost',
// 'localhost:3333': 'docs.stacks.localhost',
// 'localhost:8080': 'api.stacks.localhost',
// 'localhost:1234': 'example.test',
}
import type { ReverseProxyOptions } from './src/types'

const config: ReverseProxyOptions = [
{
from: 'localhost:5173',
to: 'stacks.localhost',
// key: 'content of the key',
keyPath: '/absolute/path/to/the/key',
// cert: 'content of the cert',
certPath: '/absolute/path/to/the/cert',
httpsRedirect: true,
verbose: false,
},
]

export default config
10 changes: 8 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import type { ReverseProxyConfig } from './types'
import { loadConfig } from 'bun-config'

// Get loaded config
// eslint-disable-next-line antfu/no-top-level-await
const config: ReverseProxyConfig = await loadConfig({
name: 'reverse-proxy',
defaultConfig: {
'localhost:5173': 'stacks.localhost',
from: 'localhost:5173',
to: 'stacks.localhost',
key: undefined,
keyPath: undefined,
cert: undefined,
certPath: undefined,
httpsRedirect: false,
verbose: true,
},
})

Expand Down
42 changes: 26 additions & 16 deletions src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,38 @@ import * as net from 'node:net'
import process from 'node:process'
import { bold, dim, green, log } from '@stacksjs/cli'
import { version } from '../package.json'
import { config } from './config'

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

if (!options.from)

Check failure on line 12 in src/start.ts

View workflow job for this annotation

GitHub Actions / test

TypeError: undefined is not an object (evaluating 'options.from')

at /home/runner/work/reverse-proxy/reverse-proxy/src/start.ts:12:8 at startServer (/home/runner/work/reverse-proxy/reverse-proxy/src/start.ts:9:35) at /home/runner/work/reverse-proxy/reverse-proxy/test/reverse-proxy.test.ts:57:13 at /home/runner/work/reverse-proxy/reverse-proxy/test/reverse-proxy.test.ts:46:50
options.from = 'localhost:5173'
if (!options.to)
options.to = 'stacks.localhost'

// Ensure the from URL has a valid protocol
if (!options.from.startsWith('http://') && !options.from.startsWith('https://')) {
options.from = `http://${options.from}` // Default to http if no protocol is specified
}

// Parse the option.from URL to dynamically set hostname and port
const fromUrl = new URL(
options.from
? options.from.startsWith('http')
? options.from
: `http://${options.from}`
: 'http://localhost:3000',
)
const fromUrl = new URL(options.from)
console.log('fromUrl', fromUrl)
const hostname = fromUrl.hostname
const port = Number.parseInt(fromUrl.port) || (fromUrl.protocol === 'https:' ? 443 : 80)
const port = Number.parseInt(fromUrl.port) || (fromUrl.protocol.includes('https:') ? 443 : 80)
console.log('hostname', hostname)
console.log('port', port)

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

const to = `${hostname}:${port}`

// Proceed with setting up the reverse proxy after successful connection
setupReverseProxy({ ...options, hostname, port })
setupReverseProxy({ ...options, to })
})

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

const { hostname, port, from, to } = options
const { from, to } = options

// Check if port 80 is in use
const testServer = net.createServer()
Expand All @@ -57,11 +65,10 @@ export function setupReverseProxy(options: ReverseProxyOption): void {
const httpServer = http.createServer((req, res) => {
// Define the target server's options
const options = {
hostname,
port,
to,
path: req.url,
method: req.method,
headers: { ...req.headers, host: `${hostname}:${port}` }, // Update the host header
headers: { ...req.headers, host: to },
}

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

export function startProxy(option?: ReverseProxyOption): void {
if (!option)
option = config

startServer(option)
}

Expand Down
10 changes: 4 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
export interface ReverseProxyConfig {
hostname: string
port: number
from: string // domain to proxy from, defaults to localhost:3000
to: string // domain to proxy to, defaults to stacks.localhost
key: string // content of the key
keyPath: string // absolute path to the key
cert: string // content of the cert
certPath: string // absolute path to the cert
key?: string // content of the key
keyPath?: string // absolute path to the key
cert?: string // content of the cert
certPath?: string // absolute path to the cert
httpsRedirect: boolean // redirect http to https, defaults to true
verbose: boolean
}
Expand Down

0 comments on commit 1cdf696

Please sign in to comment.