bare-http1 3.8.0 → 3.8.2

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/lib/agent.js CHANGED
@@ -5,11 +5,7 @@ module.exports = class HTTPAgent {
5
5
  constructor (opts = {}) {
6
6
  const {
7
7
  keepAlive = false,
8
- keepAliveMsecs = 1000,
9
- maxSockets = Infinity,
10
- maxTotalSockets = Infinity,
11
- maxFreeSockets = 256,
12
- timeout = -1
8
+ keepAliveMsecs = 1000
13
9
  } = opts
14
10
 
15
11
  this._sockets = new Map()
@@ -18,10 +14,6 @@ module.exports = class HTTPAgent {
18
14
  this._keepAlive = typeof keepAlive === 'number' ? keepAlive : keepAlive ? keepAliveMsecs : -1
19
15
 
20
16
  this._opts = { ...opts }
21
- this._maxSockets = maxSockets
22
- this._maxTotalSockets = maxTotalSockets
23
- this._maxFreeSockets = maxFreeSockets
24
- this._timeout = timeout
25
17
  }
26
18
 
27
19
  createConnection (opts) {
@@ -65,6 +57,7 @@ module.exports = class HTTPAgent {
65
57
  socket
66
58
  .on('free', () => this._onfree(socket, name))
67
59
  .on('close', () => this._onremove(socket, name))
60
+ .on('timeout', () => this._ontimeout(socket, name))
68
61
  }
69
62
 
70
63
  let sockets = this._sockets.get(name)
@@ -116,5 +109,13 @@ module.exports = class HTTPAgent {
116
109
  }
117
110
  }
118
111
 
112
+ _ontimeout (socket, name) {
113
+ const sockets = this._freeSockets.get(name)
114
+ if (!sockets) return
115
+
116
+ if (sockets.delete(socket)) socket.destroy()
117
+ if (sockets.size === 0) this._freeSockets.delete(name)
118
+ }
119
+
119
120
  static global = new this({ keepAlive: 1000, timeout: 5000 })
120
121
  }
@@ -10,10 +10,7 @@ module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
10
10
 
11
11
  opts = opts ? { ...opts } : {}
12
12
 
13
- // TODO: Renable the default global agent when tests have been sorted
14
- // const agent = opts.agent === false ? new HTTPAgent() : opts.agent || HTTPAgent.global
15
-
16
- const agent = opts.agent || new HTTPAgent()
13
+ const agent = opts.agent === false ? new HTTPAgent() : opts.agent || HTTPAgent.global
17
14
  const method = opts.method || 'GET'
18
15
  const path = opts.path || '/'
19
16
  const host = opts.host = opts.host || 'localhost'
@@ -1,4 +1,5 @@
1
1
  const tcp = require('bare-tcp')
2
+ const { isEnded, isFinished, getStreamError } = require('bare-stream')
2
3
  const HTTPIncomingMessage = require('./incoming-message')
3
4
  const HTTPServerResponse = require('./server-response')
4
5
  const constants = require('./constants')
@@ -33,13 +34,14 @@ module.exports = class HTTPServerConnection {
33
34
  this._buffer = null
34
35
  this._idle = true
35
36
 
36
- this._onerror = this._onerror.bind(this)
37
+ this._onclose = this._onclose.bind(this)
37
38
  this._ondata = this._ondata.bind(this)
38
39
  this._ondrain = this._ondrain.bind(this)
39
40
  this._ontimeout = this._ontimeout.bind(this)
40
41
 
41
42
  socket
42
- .on('error', this._onerror)
43
+ .on('error', noop)
44
+ .on('close', this._onclose)
43
45
  .on('data', this._ondata)
44
46
  .on('drain', this._ondrain)
45
47
  .on('timeout', this._ontimeout)
@@ -53,8 +55,11 @@ module.exports = class HTTPServerConnection {
53
55
  return this._idle
54
56
  }
55
57
 
56
- _onerror (err) {
57
- this.socket.destroy(err)
58
+ _onclose () {
59
+ if (this.req && !isEnded(this.req)) this.req.destroy()
60
+ if (this.res && !isFinished(this.res)) this.res.destroy()
61
+ const err = getStreamError(this.socket)
62
+ if (err) this.socket.destroy(err)
58
63
  }
59
64
 
60
65
  _ondata (data) {
@@ -232,7 +237,8 @@ module.exports = class HTTPServerConnection {
232
237
 
233
238
  _ondetach () {
234
239
  this.socket
235
- .off('error', this._onerror)
240
+ .off('error', noop)
241
+ .off('close', this._onclose)
236
242
  .off('data', this._ondata)
237
243
  .off('drain', this._ondrain)
238
244
  .off('timeout', this._ontimeout)
@@ -240,3 +246,5 @@ module.exports = class HTTPServerConnection {
240
246
  HTTPServerConnection._connections.delete(this.socket)
241
247
  }
242
248
  }
249
+
250
+ function noop () {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "3.8.0",
3
+ "version": "3.8.2",
4
4
  "description": "Native HTTP/1 library for JavaScript",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -26,7 +26,7 @@
26
26
  "homepage": "https://github.com/holepunchto/bare-http1#readme",
27
27
  "dependencies": {
28
28
  "bare-events": "^2.0.0",
29
- "bare-stream": "^2.0.0",
29
+ "bare-stream": "^2.3.0",
30
30
  "bare-tcp": "^1.8.0"
31
31
  },
32
32
  "devDependencies": {