bare-http1 4.3.0 → 4.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +1 -1
- package/lib/agent.js +39 -9
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -118,7 +118,7 @@ export interface HTTPAgent {
|
|
|
118
118
|
class HTTPAgent {
|
|
119
119
|
constructor(opts?: HTTPAgentOptions & TCPSocketOptions & TCPSocketConnectOptions)
|
|
120
120
|
|
|
121
|
-
static global: HTTPAgent
|
|
121
|
+
static readonly global: HTTPAgent
|
|
122
122
|
}
|
|
123
123
|
|
|
124
124
|
export const globalAgent: HTTPAgent
|
package/lib/agent.js
CHANGED
|
@@ -5,11 +5,18 @@ const HTTPClientConnection = require('./client-connection')
|
|
|
5
5
|
class HTTPSocketSet {
|
|
6
6
|
constructor() {
|
|
7
7
|
this._sockets = new Map()
|
|
8
|
+
this._size = 0
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get size() {
|
|
12
|
+
return this._size
|
|
8
13
|
}
|
|
9
14
|
|
|
10
15
|
add(name, socket) {
|
|
11
16
|
const sockets = this._sockets.get(name)
|
|
12
17
|
|
|
18
|
+
this._size++
|
|
19
|
+
|
|
13
20
|
if (sockets === undefined) this._sockets.set(name, [socket])
|
|
14
21
|
else sockets.push(socket)
|
|
15
22
|
}
|
|
@@ -18,6 +25,8 @@ class HTTPSocketSet {
|
|
|
18
25
|
const sockets = this._sockets.get(name)
|
|
19
26
|
if (sockets === undefined || sockets.length === 0) return null
|
|
20
27
|
|
|
28
|
+
this._size--
|
|
29
|
+
|
|
21
30
|
const last = sockets.pop()
|
|
22
31
|
|
|
23
32
|
if (sockets.length === 0) this._sockets.delete(name)
|
|
@@ -32,6 +41,8 @@ class HTTPSocketSet {
|
|
|
32
41
|
const i = sockets.indexOf(socket)
|
|
33
42
|
if (i === -1) return
|
|
34
43
|
|
|
44
|
+
this._size--
|
|
45
|
+
|
|
35
46
|
const last = sockets.pop()
|
|
36
47
|
if (last !== socket) sockets[i] = last
|
|
37
48
|
|
|
@@ -51,7 +62,7 @@ class HTTPSocketSet {
|
|
|
51
62
|
}
|
|
52
63
|
}
|
|
53
64
|
|
|
54
|
-
|
|
65
|
+
class HTTPAgent extends EventEmitter {
|
|
55
66
|
constructor(opts = {}) {
|
|
56
67
|
super()
|
|
57
68
|
|
|
@@ -109,10 +120,10 @@ module.exports = class HTTPAgent extends EventEmitter {
|
|
|
109
120
|
|
|
110
121
|
socket
|
|
111
122
|
.on('free', onfree)
|
|
123
|
+
.on('timeout', ontimeout)
|
|
112
124
|
.on('end', onremove)
|
|
113
125
|
.on('finish', onremove)
|
|
114
126
|
.on('close', onremove)
|
|
115
|
-
.on('timeout', ontimeout)
|
|
116
127
|
|
|
117
128
|
function onfree() {
|
|
118
129
|
if (socket.destroyed) return
|
|
@@ -126,18 +137,24 @@ module.exports = class HTTPAgent extends EventEmitter {
|
|
|
126
137
|
agent.emit('free', socket)
|
|
127
138
|
}
|
|
128
139
|
|
|
140
|
+
function ontimeout() {
|
|
141
|
+
socket.destroy()
|
|
142
|
+
|
|
143
|
+
agent._freeSockets.delete(name, socket)
|
|
144
|
+
}
|
|
145
|
+
|
|
129
146
|
function onremove() {
|
|
130
147
|
socket.off('free', onfree)
|
|
131
148
|
|
|
132
149
|
agent._sockets.delete(name, socket)
|
|
133
150
|
agent._freeSockets.delete(name, socket)
|
|
134
|
-
}
|
|
135
151
|
|
|
136
|
-
|
|
137
|
-
agent._freeSockets.delete(name, socket)
|
|
152
|
+
if (agent._sockets.size === 0) HTTPAgent._agents.delete(agent)
|
|
138
153
|
}
|
|
139
154
|
}
|
|
140
155
|
|
|
156
|
+
if (this._sockets.size === 0) HTTPAgent._agents.add(this)
|
|
157
|
+
|
|
141
158
|
this._sockets.add(name, socket)
|
|
142
159
|
|
|
143
160
|
req.socket = socket
|
|
@@ -148,10 +165,23 @@ module.exports = class HTTPAgent extends EventEmitter {
|
|
|
148
165
|
}
|
|
149
166
|
|
|
150
167
|
destroy() {
|
|
151
|
-
for (const
|
|
152
|
-
|
|
153
|
-
|
|
168
|
+
for (const socket of this._sockets.sockets()) socket.destroy()
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
static _global = new this({ keepAlive: 1000, timeout: 5000 })
|
|
172
|
+
static _agents = new Set()
|
|
173
|
+
|
|
174
|
+
static get global() {
|
|
175
|
+
return this._global
|
|
154
176
|
}
|
|
155
177
|
|
|
156
|
-
static
|
|
178
|
+
static _onidle() {
|
|
179
|
+
for (const agent of this._agents) {
|
|
180
|
+
for (const socket of agent._sockets.sockets()) socket.destroy()
|
|
181
|
+
}
|
|
182
|
+
}
|
|
157
183
|
}
|
|
184
|
+
|
|
185
|
+
module.exports = HTTPAgent
|
|
186
|
+
|
|
187
|
+
Bare.on('idle', HTTPAgent._onidle.bind(HTTPAgent))
|