Skip to content

Commit a2baaf5

Browse files
build(deps-dev): bump proxy from 1.0.2 to 2.1.1 (#2137)
1 parent 95bd929 commit a2baaf5

File tree

4 files changed

+65
-29
lines changed

4 files changed

+65
-29
lines changed

docs/docs/best-practices/proxy.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ If you proxy requires basic authentication, you can send it via the `proxy-autho
1717
```js
1818
import { Client } from 'undici'
1919
import { createServer } from 'http'
20-
import proxy from 'proxy'
20+
import { createProxy } from 'proxy'
2121

2222
const server = await buildServer()
2323
const proxyServer = await buildProxy()
@@ -59,7 +59,7 @@ function buildServer () {
5959

6060
function buildProxy () {
6161
return new Promise((resolve, reject) => {
62-
const server = proxy(createServer())
62+
const server = createProxy(createServer())
6363
server.listen(0, () => resolve(server))
6464
})
6565
}
@@ -70,16 +70,16 @@ function buildProxy () {
7070
```js
7171
import { Client } from 'undici'
7272
import { createServer } from 'http'
73-
import proxy from 'proxy'
73+
import { createProxy } from 'proxy'
7474

7575
const server = await buildServer()
7676
const proxyServer = await buildProxy()
7777

7878
const serverUrl = `http://localhost:${server.address().port}`
7979
const proxyUrl = `http://localhost:${proxyServer.address().port}`
8080

81-
proxyServer.authenticate = function (req, fn) {
82-
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
81+
proxyServer.authenticate = function (req) {
82+
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`
8383
}
8484

8585
server.on('request', (req, res) => {
@@ -119,7 +119,7 @@ function buildServer () {
119119

120120
function buildProxy () {
121121
return new Promise((resolve, reject) => {
122-
const server = proxy(createServer())
122+
const server = createProxy(createServer())
123123
server.listen(0, () => resolve(server))
124124
})
125125
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
"jsdom": "^24.0.0",
104104
"jsfuzz": "^1.0.15",
105105
"pre-commit": "^1.2.2",
106-
"proxy": "^1.0.2",
106+
"proxy": "^2.1.1",
107107
"proxyquire": "^2.1.3",
108108
"snazzy": "^9.0.0",
109109
"standard": "^17.0.0",

test/proxy-agent.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const ProxyAgent = require('../lib/dispatcher/proxy-agent')
1010
const Pool = require('../lib/dispatcher/pool')
1111
const { createServer } = require('node:http')
1212
const https = require('node:https')
13-
const proxy = require('proxy')
13+
const { createProxy } = require('proxy')
1414

1515
test('should throw error when no uri is provided', (t) => {
1616
t = tspl(t, { plan: 2 })
@@ -81,16 +81,16 @@ test('use proxy agent to connect through proxy using Pool', async (t) => {
8181
let resolveFirstConnect
8282
let connectCount = 0
8383

84-
proxy.authenticate = async function (req, fn) {
84+
proxy.authenticate = async function (req) {
8585
if (++connectCount === 2) {
8686
t.ok(true, 'second connect should arrive while first is still inflight')
8787
resolveFirstConnect()
88-
fn(null, true)
88+
return true
8989
} else {
9090
await new Promise((resolve) => {
9191
resolveFirstConnect = resolve
9292
})
93-
fn(null, true)
93+
return true
9494
}
9595
}
9696

@@ -161,7 +161,7 @@ test('use proxy-agent to connect through proxy with basic auth in URL', async (t
161161

162162
proxy.authenticate = function (req, fn) {
163163
t.ok(true, 'authentication should be called')
164-
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
164+
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`
165165
}
166166
proxy.on('connect', () => {
167167
t.ok(true, 'proxy should be called')
@@ -203,9 +203,9 @@ test('use proxy-agent with auth', async (t) => {
203203
})
204204
const parsedOrigin = new URL(serverUrl)
205205

206-
proxy.authenticate = function (req, fn) {
206+
proxy.authenticate = function (req) {
207207
t.ok(true, 'authentication should be called')
208-
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
208+
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`
209209
}
210210
proxy.on('connect', () => {
211211
t.ok(true, 'proxy should be called')
@@ -247,9 +247,9 @@ test('use proxy-agent with token', async (t) => {
247247
})
248248
const parsedOrigin = new URL(serverUrl)
249249

250-
proxy.authenticate = function (req, fn) {
250+
proxy.authenticate = function (req) {
251251
t.ok(true, 'authentication should be called')
252-
fn(null, req.headers['proxy-authorization'] === `Bearer ${Buffer.from('user:pass').toString('base64')}`)
252+
return req.headers['proxy-authorization'] === `Bearer ${Buffer.from('user:pass').toString('base64')}`
253253
}
254254
proxy.on('connect', () => {
255255
t.ok(true, 'proxy should be called')
@@ -460,16 +460,17 @@ test('ProxyAgent correctly sends headers when using fetch - #1355, #1623', async
460460
})
461461

462462
test('should throw when proxy does not return 200', async (t) => {
463-
t = tspl(t, { plan: 2 })
463+
t = tspl(t, { plan: 3 })
464464

465465
const server = await buildServer()
466466
const proxy = await buildProxy()
467467

468468
const serverUrl = `http://localhost:${server.address().port}`
469469
const proxyUrl = `http://localhost:${proxy.address().port}`
470470

471-
proxy.authenticate = function (req, fn) {
472-
fn(null, false)
471+
proxy.authenticate = function (_req) {
472+
t.ok(true, 'should call authenticate')
473+
return false
473474
}
474475

475476
const proxyAgent = new ProxyAgent(proxyUrl)
@@ -488,15 +489,16 @@ test('should throw when proxy does not return 200', async (t) => {
488489
})
489490

490491
test('pass ProxyAgent proxy status code error when using fetch - #2161', async (t) => {
491-
t = tspl(t, { plan: 1 })
492+
t = tspl(t, { plan: 2 })
492493
const server = await buildServer()
493494
const proxy = await buildProxy()
494495

495496
const serverUrl = `http://localhost:${server.address().port}`
496497
const proxyUrl = `http://localhost:${proxy.address().port}`
497498

498-
proxy.authenticate = function (req, fn) {
499-
fn(null, false)
499+
proxy.authenticate = function (_req) {
500+
t.ok(true, 'should call authenticate')
501+
return false
500502
}
501503

502504
const proxyAgent = new ProxyAgent(proxyUrl)
@@ -742,8 +744,8 @@ function buildSSLServer () {
742744
function buildProxy (listener) {
743745
return new Promise((resolve) => {
744746
const server = listener
745-
? proxy(createServer(listener))
746-
: proxy(createServer())
747+
? createProxy(createServer(listener))
748+
: createProxy(createServer())
747749
server.listen(0, () => resolve(server))
748750
})
749751
}
@@ -758,7 +760,7 @@ function buildSSLProxy () {
758760
}
759761

760762
return new Promise((resolve) => {
761-
const server = proxy(https.createServer(serverOptions))
763+
const server = createProxy(https.createServer(serverOptions))
762764
server.listen(0, () => resolve(server))
763765
})
764766
}

test/proxy.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { tspl } = require('@matteo.collina/tspl')
44
const { test } = require('node:test')
55
const { Client, Pool } = require('..')
66
const { createServer } = require('node:http')
7-
const proxy = require('proxy')
7+
const { createProxy } = require('proxy')
88

99
test('connect through proxy', async (t) => {
1010
t = tspl(t, { plan: 3 })
@@ -50,8 +50,8 @@ test('connect through proxy with auth', async (t) => {
5050
const serverUrl = `http://localhost:${server.address().port}`
5151
const proxyUrl = `http://localhost:${proxy.address().port}`
5252

53-
proxy.authenticate = function (req, fn) {
54-
fn(null, req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`)
53+
proxy.authenticate = function (req) {
54+
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`
5555
}
5656

5757
server.on('request', (req, res) => {
@@ -83,6 +83,40 @@ test('connect through proxy with auth', async (t) => {
8383
client.close()
8484
})
8585

86+
test('connect through proxy with auth but invalid credentials', async (t) => {
87+
t = tspl(t, { plan: 1 })
88+
89+
const server = await buildServer()
90+
const proxy = await buildProxy()
91+
92+
const serverUrl = `http://localhost:${server.address().port}`
93+
const proxyUrl = `http://localhost:${proxy.address().port}`
94+
95+
proxy.authenticate = function (req) {
96+
return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:no-pass').toString('base64')}`
97+
}
98+
99+
server.on('request', (req, res) => {
100+
t.fail('should not be called')
101+
})
102+
103+
const client = new Client(proxyUrl)
104+
105+
const response = await client.request({
106+
method: 'GET',
107+
path: serverUrl + '/hello?foo=bar',
108+
headers: {
109+
'proxy-authorization': `Basic ${Buffer.from('user:pass').toString('base64')}`
110+
}
111+
})
112+
113+
t.strictEqual(response.statusCode, 407)
114+
115+
server.close()
116+
proxy.close()
117+
client.close()
118+
})
119+
86120
test('connect through proxy (with pool)', async (t) => {
87121
t = tspl(t, { plan: 3 })
88122

@@ -127,7 +161,7 @@ function buildServer () {
127161

128162
function buildProxy () {
129163
return new Promise((resolve, reject) => {
130-
const server = proxy(createServer())
164+
const server = createProxy(createServer())
131165
server.listen(0, () => resolve(server))
132166
})
133167
}

0 commit comments

Comments
 (0)