Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbbreuer committed Nov 14, 2024
1 parent 4e30d5d commit 1529f60
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,5 @@
],
"grammarly.files.exclude": [
"**/dictionary.txt"
],
]
}
7 changes: 5 additions & 2 deletions src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function loadSSLConfig(options: ReverseProxyOption): Promise<SSLConfig | n
if (!options.keyPath && !options.certPath)
return null

if (options.keyPath && !options.certPath || !options.keyPath && options.certPath) {
if ((options.keyPath && !options.certPath) || (!options.keyPath && options.certPath)) {
const missing = !options.keyPath ? 'keyPath' : 'certPath'
throw new Error(`SSL Configuration requires both keyPath and certPath. Missing: ${missing}`)
}
Expand Down Expand Up @@ -141,7 +141,10 @@ async function testConnection(hostname: string, port: number): Promise<void> {
})
}

export async function startServer(options: ReverseProxyOption): Promise<void> {
export async function startServer(options?: ReverseProxyOption): Promise<void> {
if (!options)
options = config

if (!options.from)
options.from = config.from
if (!options.to)
Expand Down
58 changes: 31 additions & 27 deletions test/reverse-proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { ReverseProxyOption } from '../src/types'
import { afterEach, beforeAll, beforeEach, describe, expect, it, mock } from 'bun:test'
import { setupReverseProxy, startHttpRedirectServer, startProxies, startProxy, startServer } from '../src/start'
import type { ReverseProxyOption } from '../src/types'

const mockLog = {
debug: mock(),
error: mock(),
info: mock(),
}

mock.module("node:fs/promises", () => ({
mock.module('node:fs/promises', () => ({
mkdir: mock(),
}));
}))

mock.module('@stacksjs/cli', () => ({
log: mockLog,
bold: mock((str) => str),
dim: mock((str) => str),
green: mock((str) => str),
bold: mock(str => str),
dim: mock(str => str),
green: mock(str => str),
}))

describe('@stacksjs/reverse-proxy', () => {
Expand All @@ -31,9 +31,9 @@ describe('@stacksjs/reverse-proxy', () => {
// Re-mock @stacksjs/cli after restoring all mocks
mock.module('@stacksjs/cli', () => ({
log: mockLog,
bold: mock((str) => str),
dim: mock((str) => str),
green: mock((str) => str),
bold: mock(str => str),
dim: mock(str => str),
green: mock(str => str),
}))
})

Expand Down Expand Up @@ -63,7 +63,8 @@ describe('@stacksjs/reverse-proxy', () => {
const mockConnect = mock(() => {
return {
on: (event: string, handler: (err: Error) => void) => {
if (event === 'error') handler(new Error('Connection failed'))
if (event === 'error')
handler(new Error('Connection failed'))
},
end: mock(),
}
Expand All @@ -88,13 +89,13 @@ describe('@stacksjs/reverse-proxy', () => {

const mockSetupReverseProxy = mock()
mock.module('../src/start', () => ({
...require('../src/start'),
...import('../src/start'),
setupReverseProxy: mockSetupReverseProxy,
}))

const subdomainOption: ReverseProxyOption = {
from: 'localhost:3000',
to: 'subdomain.example.com'
to: 'subdomain.example.com',
}
await startServer(subdomainOption)

Expand All @@ -120,9 +121,10 @@ describe('@stacksjs/reverse-proxy', () => {

const mockTestServer = {
once: mock((event, callback) => {
if (event === 'listening') callback()
if (event === 'listening')
callback()
}),
close: mock((callback) => callback()),
close: mock(callback => callback()),
listen: mock(),
}
mock.module('node:net', () => ({
Expand All @@ -137,7 +139,7 @@ describe('@stacksjs/reverse-proxy', () => {
})

it('handles port 80 already in use', () => {
const mockExit = mock((code: number) => {})
const mockExit = mock(() => {})
process.exit = mockExit as any

const mockTestServer = {
Expand All @@ -154,7 +156,10 @@ describe('@stacksjs/reverse-proxy', () => {
createServer: mock(() => mockTestServer),
}))

setupReverseProxy({ hostname: 'localhost', port: 3000, from: 'localhost:3000', to: 'example.com' })
setupReverseProxy({
from: 'localhost:3000',
to: 'example.com',
})

expect(mockLog.debug).toHaveBeenCalledWith('setupReverseProxy', expect.any(Object))
expect(mockTestServer.once).toHaveBeenCalledWith('error', expect.any(Function))
Expand All @@ -175,20 +180,19 @@ describe('@stacksjs/reverse-proxy', () => {

const mockTestServer = {
once: mock((event, callback) => {
if (event === 'listening') callback()
if (event === 'listening')
callback()
}),
close: mock((callback) => callback()),
close: mock(callback => callback()),
listen: mock(),
}
mock.module('node:net', () => ({
createServer: mock(() => mockTestServer),
}))

const subdomainOption: ReverseProxyOption = {
hostname: 'localhost',
port: 3000,
from: 'localhost:3000',
to: 'subdomain.example.com'
to: 'subdomain.example.com',
}
setupReverseProxy(subdomainOption)

Expand All @@ -203,21 +207,21 @@ describe('@stacksjs/reverse-proxy', () => {
const mockProxyReq = { on: mock(), end: mock() }

mock.module('node:http', () => ({
...require('node:http'),
...import('node:http'),
request: mock(() => mockProxyReq),
}))

createServerCallback(mockReq, mockRes)

expect(require('node:http').request).toHaveBeenCalledWith(
expect(import('node:http').request).toHaveBeenCalledWith(
expect.objectContaining({
hostname: 'localhost',
port: 3000,
headers: expect.objectContaining({
host: 'subdomain.example.com',
}),
}),
expect.any(Function)
expect.any(Function),
)
})
})
Expand All @@ -243,7 +247,7 @@ describe('@stacksjs/reverse-proxy', () => {
it('calls startServer with the provided option', async () => {
const mockStartServer = mock(() => Promise.resolve())
mock.module('../src/start', () => ({
...require('../src/start'),
...import('../src/start'),
startServer: mockStartServer,
}))

Expand All @@ -258,7 +262,7 @@ describe('@stacksjs/reverse-proxy', () => {
it('starts multiple proxies when given an array', async () => {
const mockStartServer = mock(() => Promise.resolve())
mock.module('../src/start', () => ({
...require('../src/start'),
...import('../src/start'),
startServer: mockStartServer,
}))

Expand All @@ -276,7 +280,7 @@ describe('@stacksjs/reverse-proxy', () => {
it('starts a single proxy when given a single option', async () => {
const mockStartServer = mock(() => Promise.resolve())
mock.module('../src/start', () => ({
...require('../src/start'),
...import('../src/start'),
startServer: mockStartServer,
}))

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"verbatimModuleSyntax": true,
"isolatedDeclarations": true,
"verbatimModuleSyntax": true,
"skipDefaultLibCheck": true,
"skipLibCheck": true
}
Expand Down

0 comments on commit 1529f60

Please sign in to comment.