Skip to content

Commit

Permalink
chore: remove usage of http-errors in proxy example (#2753)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Feb 13, 2024
1 parent 5f02182 commit d076328
Showing 1 changed file with 65 additions and 3 deletions.
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
}
}

0 comments on commit d076328

Please sign in to comment.