Skip to content

Commit d076328

Browse files
authored
chore: remove usage of http-errors in proxy example (#2753)
1 parent 5f02182 commit d076328

File tree

1 file changed

+65
-3
lines changed

1 file changed

+65
-3
lines changed

examples/proxy/proxy.js

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
'use strict'
2+
13
const net = require('node:net')
24
const { pipeline } = require('node:stream')
3-
const createError = require('http-errors')
5+
const { STATUS_CODES } = require('node:http')
46

57
module.exports = async function proxy (ctx, client) {
68
const { req, socket, proxyName } = ctx
@@ -214,13 +216,13 @@ function getHeaders ({
214216
].join(';'))
215217
} else if (forwarded) {
216218
// The forwarded header should not be included in response.
217-
throw new createError.BadGateway()
219+
throw new BadGateway()
218220
}
219221

220222
if (proxyName) {
221223
if (via) {
222224
if (via.split(',').some(name => name.endsWith(proxyName))) {
223-
throw new createError.LoopDetected()
225+
throw new LoopDetected()
224226
}
225227
via += ', '
226228
}
@@ -254,3 +256,63 @@ function printIp (address, port) {
254256
}
255257
return str
256258
}
259+
260+
class BadGateway extends Error {
261+
constructor (message = STATUS_CODES[502]) {
262+
super(message)
263+
}
264+
265+
toString () {
266+
return `BadGatewayError: ${this.message}`
267+
}
268+
269+
get name () {
270+
return 'BadGatewayError'
271+
}
272+
273+
get status () {
274+
return 502
275+
}
276+
277+
get statusCode () {
278+
return 502
279+
}
280+
281+
get expose () {
282+
return false
283+
}
284+
285+
get headers () {
286+
return undefined
287+
}
288+
}
289+
290+
class LoopDetected extends Error {
291+
constructor (message = STATUS_CODES[508]) {
292+
super(message)
293+
}
294+
295+
toString () {
296+
return `LoopDetectedError: ${this.message}`
297+
}
298+
299+
get name () {
300+
return 'LoopDetectedError'
301+
}
302+
303+
get status () {
304+
return 508
305+
}
306+
307+
get statusCode () {
308+
return 508
309+
}
310+
311+
get expose () {
312+
return false
313+
}
314+
315+
get headers () {
316+
return undefined
317+
}
318+
}

0 commit comments

Comments
 (0)