Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Mar 6, 2024
1 parent c0a8bad commit f28b118
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions .vscode/dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ destructurable
entrypoints
heroicons
lockb
mkcert
openweb
outdir
pausable
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ pkgx install reverse-proxy # wip

## Get Started

Given you installed the npm package, you can use it in your project:
There are two ways of using this reverse proxy: _as a library or as a CLI._

### Library

Given the npm package is installed:

```js
import { startProxy } from 'bun-reverse-proxy'
Expand All @@ -55,9 +59,9 @@ reverse-proxy --help
reverse-proxy --version
```

### Configuration
## Configuration

You can also use a configuration file:
The Reverse Proxy can be configured using a `reverse-proxy.config.ts` _(or `reverse-proxy.config.js`)_ file and it will be automatically loaded when running the `reverse-proxy` command.

```ts
// reverse-proxy.config.ts (or reverse-proxy.config.js)
Expand All @@ -72,8 +76,6 @@ _Then run:_
reverse-proxy start
```

Your config will be loaded from `reverse-proxy.config.ts` _(or `reverse-proxy.config.js`)_.

To learn more, head over to the [documentation](https://reverse-proxy.sh/).

## Testing
Expand Down Expand Up @@ -102,7 +104,7 @@ For casual chit-chat with others using this package:

## Postcardware

Two things are true: Stacks OSS will always stay open-source, and we do/would love to receive postcards from wherever Stacks is used! 🌍 _And we also publish them on our website. -Thank you, Spatie_
Two things are true: Stacks OSS will always stay open-source, and we do love to receive postcards from wherever Stacks is used! 🌍 _We also publish them on our website. And thank you, Spatie_

Our address: Stacks.js, 5710 Crescent Park #107, Playa Vista 90094, CA.

Expand Down
7 changes: 4 additions & 3 deletions bin/cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os from 'node:os'
import { cli as command, log } from '@stacksjs/cli'
import { readFileSync, writeFileSync } from '@stacksjs/storage'
import { CAC } from 'cac'
import { readFileSync, writeFileSync } from 'fs-extra'
import { log } from '@stacksjs/logging'
import { startProxy } from '../src/start'
import { config } from '../src/config'
import { version } from '../package.json'

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

interface Options {
from?: string
Expand Down
Binary file modified bun.lockb
Binary file not shown.
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"keywords": [
"reverse proxy",
"ssl",
"development",
"environment",
"proxy",
Expand All @@ -36,14 +37,15 @@
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": {
"bun-reverse-proxy": "./dist/cli.js",
"reverse-proxy": "./dist/cli.js"
},
"files": [
"dist",
"src"
],
"scripts": {
"build": "bun build.ts && bun compile.ts",
"build": "bun build.ts && bun run compile",
"compile": "bun build ./bin/cli.ts --compile --minify --sourcemap --outfile dist/reverse-proxy",
"lint": "eslint .",
"lint:fix": "bunx eslint . --fix",
Expand All @@ -59,13 +61,13 @@
"docs:preview": "vitepress preview docs"
},
"dependencies": {
"@stacksjs/cli": "^0.59.4",
"@stacksjs/path": "^0.59.4",
"@stacksjs/storage": "^0.59.4",
"@stacksjs/cli": "^0.59.6",
"@stacksjs/path": "^0.59.6",
"@stacksjs/storage": "^0.59.6",
"c12": "^1.9.0"
},
"devDependencies": {
"@stacksjs/development": "^0.59.4",
"@stacksjs/development": "^0.59.6",
"@types/bun": "^1.0.8",
"@types/node": "^20.11.24",
"bun-plugin-dts-auto": "^0.10.0",
Expand Down
2 changes: 2 additions & 0 deletions pkgx.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
dependencies:
bun.sh: ^1.0.30
mkcert.dev: ^1.4.4
mozilla.org/nss: ^3.92.0 # if you test locally using Firefox
23 changes: 20 additions & 3 deletions src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,26 @@ type Options = Option | Option[]
export function startServer(option: Option = { from: 'localhost:3000', to: 'stacks.localhost' }): void {
log.debug('Starting Reverse Proxy Server')

const key = fs.readFileSync(option.keyPath ?? path.projectStoragePath(`keys/localhost-key.pem`))
const cert = fs.readFileSync(option.certPath ?? path.projectStoragePath(`keys/localhost-cert.pem`))
let key: Buffer | undefined
let cert: Buffer | undefined

const keyPath = option.keyPath ?? path.projectStoragePath(`keys/localhost-key.pem`)
if (fs.existsSync(keyPath))
key = fs.readFileSync(option.keyPath ?? path.projectStoragePath(`keys/localhost-key.pem`))
else
log.debug('No SSL key found')


const certPath = option.certPath ?? path.projectStoragePath(`certs/localhost.pem`)
if (fs.existsSync(certPath))
cert = fs.readFileSync(option.certPath ?? path.projectStoragePath(`certs/localhost.pem`))
else
log.debug('No SSL certificate found')

if (!fs.existsSync(keyPath) || fs.existsSync(certPath)) {
log.info('Because no valid SSL key or certificate was found, creating a self-signed certificate')
// wip using mkcert
}
// Parse the option.from URL to dynamically set hostname and port
const fromUrl = new URL(option.from ? (option.from.startsWith('http') ? option.from : `http://${option.from}`) : 'http://localhost:3000')
const hostname = fromUrl.hostname
Expand All @@ -43,7 +60,7 @@ export function startServer(option: Option = { from: 'localhost:3000', to: 'stac
})
}

function setupReverseProxy({ key, cert, hostname, port, option }: { key: Buffer, cert: Buffer, hostname: string, port: number, option: Option }): void {
function setupReverseProxy({ key, cert, hostname, port, option }: { key?: Buffer, cert?: Buffer, hostname: string, port: number, option: Option }): void {
// This server will act as a reverse proxy
const httpsServer = https.createServer({ key, cert }, (req, res) => {
// Define the target server's options
Expand Down

0 comments on commit f28b118

Please sign in to comment.