Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove usage of http-errors in proxy example #2753

Merged
merged 1 commit into from
Feb 13, 2024
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
}
}
Loading