Skip to content

Commit

Permalink
Add timeout support (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
delvedor authored May 2, 2022
1 parent a9bf98d commit be60918
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 2 deletions.
14 changes: 12 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class HttpProxyAgent extends http.Agent {
path: `${options.host}:${options.port}`,
setHost: false,
headers: { connection: this.keepAlive ? 'keep-alive' : 'close', host: `${options.host}:${options.port}` },
agent: false
agent: false,
timeout: options.timeout || 0
}

if (this.proxy.username || this.proxy.password) {
Expand All @@ -44,6 +45,10 @@ class HttpProxyAgent extends http.Agent {
}
})

request.once('timeout', () => {
request.destroy(new Error('Proxy timeout'))
})

request.once('error', err => {
request.removeAllListeners()
callback(err, null)
Expand All @@ -70,7 +75,8 @@ class HttpsProxyAgent extends https.Agent {
path: `${options.host}:${options.port}`,
setHost: false,
headers: { connection: this.keepAlive ? 'keep-alive' : 'close', host: `${options.host}:${options.port}` },
agent: false
agent: false,
timeout: options.timeout || 0
}

if (this.proxy.username || this.proxy.password) {
Expand All @@ -95,6 +101,10 @@ class HttpsProxyAgent extends https.Agent {
}
})

request.once('timeout', () => {
request.destroy(new Error('Proxy timeout'))
})

request.once('error', err => {
request.removeAllListeners()
callback(err, null)
Expand Down
30 changes: 30 additions & 0 deletions test/http-http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,33 @@ test('Test Host Header', async t => {
server.close()
proxy.close()
})

test('Timeout', async t => {
const server = await createServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

try {
await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
timeout: 1,
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})
t.fail('Should throw')
} catch (err) {
t.is(err.message, 'Proxy timeout')
}

server.close()
proxy.close()
})
30 changes: 30 additions & 0 deletions test/http-https.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,33 @@ test('Configure the agent to NOT reuse sockets', async t => {
server.close()
proxy.close()
})

test('Timeout', async t => {
const server = await createSecureServer()
const proxy = await createProxy()
server.on('request', (req, res) => res.end('ok'))

try {
await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
timeout: 1,
agent: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})
t.fail('Should throw')
} catch (err) {
t.is(err.message, 'Proxy timeout')
}

server.close()
proxy.close()
})
30 changes: 30 additions & 0 deletions test/https-http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,33 @@ test('Configure the agent to NOT reuse sockets', async t => {
server.close()
proxy.close()
})

test('Timeout', async t => {
const server = await createServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

try {
await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
timeout: 1,
agent: new HttpProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})
t.fail('Should throw')
} catch (err) {
t.is(err.message, 'Proxy timeout')
}

server.close()
proxy.close()
})
30 changes: 30 additions & 0 deletions test/https-https.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,33 @@ test('Configure the agent to NOT reuse sockets', async t => {
server.close()
proxy.close()
})

test('Timeout', async t => {
const server = await createSecureServer()
const proxy = await createSecureProxy()
server.on('request', (req, res) => res.end('ok'))

try {
await request({
method: 'GET',
hostname: server.address().address,
port: server.address().port,
path: '/',
timeout: 1,
agent: new HttpsProxyAgent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 256,
maxFreeSockets: 256,
scheduling: 'lifo',
proxy: `http://${proxy.address().address}:${proxy.address().port}`
})
})
t.fail('Should throw')
} catch (err) {
t.is(err.message, 'Proxy timeout')
}

server.close()
proxy.close()
})

0 comments on commit be60918

Please sign in to comment.