diff --git a/.vscode/dictionary.txt b/.vscode/dictionary.txt index 8dc4d7f..30013fc 100644 --- a/.vscode/dictionary.txt +++ b/.vscode/dictionary.txt @@ -14,6 +14,7 @@ destructurable entrypoints heroicons lockb +mkcert openweb outdir pausable diff --git a/README.md b/README.md index b9c6a32..0aaacbd 100644 --- a/README.md +++ b/README.md @@ -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' @@ -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) @@ -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 @@ -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. diff --git a/bin/cli.ts b/bin/cli.ts index 8ff6455..fb8ed56 100644 --- a/bin/cli.ts +++ b/bin/cli.ts @@ -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 diff --git a/bun.lockb b/bun.lockb index 4139502..1ce8ca2 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 5b1b40c..db0c6d8 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ }, "keywords": [ "reverse proxy", + "ssl", "development", "environment", "proxy", @@ -36,6 +37,7 @@ "module": "./dist/index.js", "types": "./dist/index.d.ts", "bin": { + "bun-reverse-proxy": "./dist/cli.js", "reverse-proxy": "./dist/cli.js" }, "files": [ @@ -43,7 +45,7 @@ "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", @@ -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", diff --git a/pkgx.yaml b/pkgx.yaml index 9b325ec..d9710e2 100644 --- a/pkgx.yaml +++ b/pkgx.yaml @@ -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 diff --git a/src/start.ts b/src/start.ts index 80d4351..06afeef 100644 --- a/src/start.ts +++ b/src/start.ts @@ -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 @@ -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