Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 65 additions & 3 deletions examples/proxy/proxy.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict'

const net = require('node:net')
const { pipeline } = require('node:stream')
const createError = require('http-errors')
const { STATUS_CODES } = require('node:http')

module.exports = async function proxy (ctx, client) {
const { req, socket, proxyName } = ctx
Expand Down Expand Up @@ -214,13 +216,13 @@ function getHeaders ({
].join(';'))
} else if (forwarded) {
// The forwarded header should not be included in response.
throw new createError.BadGateway()
throw new BadGateway()
}

if (proxyName) {
if (via) {
if (via.split(',').some(name => name.endsWith(proxyName))) {
throw new createError.LoopDetected()
throw new LoopDetected()
}
via += ', '
}
Expand Down Expand Up @@ -254,3 +256,63 @@ function printIp (address, port) {
}
return str
}

class BadGateway extends Error {
constructor (message = STATUS_CODES[502]) {
super(message)
}

toString () {
return `BadGatewayError: ${this.message}`
}

get name () {
return 'BadGatewayError'
}

get status () {
return 502
}

get statusCode () {
return 502
}

get expose () {
return false
}

get headers () {
return undefined
}
}

class LoopDetected extends Error {
constructor (message = STATUS_CODES[508]) {
super(message)
}

toString () {
return `LoopDetectedError: ${this.message}`
}

get name () {
return 'LoopDetectedError'
}

get status () {
return 508
}

get statusCode () {
return 508
}

get expose () {
return false
}

get headers () {
return undefined
}
}