bare-http1 4.4.0 → 4.5.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/index.d.ts CHANGED
@@ -99,6 +99,8 @@ export interface HTTPAgentOptions {
99
99
  }
100
100
 
101
101
  export interface HTTPAgent {
102
+ readonly suspended: boolean
103
+ readonly resumed: Promise<void> | null
102
104
  readonly sockets: IterableIterator<TCPSocket>
103
105
  readonly freeSockets: IterableIterator<TCPSocket>
104
106
 
@@ -112,6 +114,8 @@ export interface HTTPAgent {
112
114
 
113
115
  addRequest(req: HTTPClientRequest, opts: TCPSocketOptions & TCPSocketConnectOptions): void
114
116
 
117
+ suspend(): void
118
+ resume(): void
115
119
  destroy(): void
116
120
  }
117
121
 
@@ -175,7 +179,7 @@ export interface HTTPServerConnectionOptions {
175
179
 
176
180
  export interface HTTPServerConnection {
177
181
  readonly server: HTTPServer
178
- readonly socket: TCPSocket
182
+ readonly socket: TCPSocket | null
179
183
 
180
184
  readonly req: HTTPIncomingMessage | null
181
185
  readonly res: HTTPServerResponse | null
@@ -224,7 +228,7 @@ export interface HTTPClientConnectionOptions {
224
228
  }
225
229
 
226
230
  export interface HTTPClientConnection {
227
- readonly socket: TCPSocket
231
+ readonly socket: TCPSocket | null
228
232
 
229
233
  readonly req: HTTPClientRequest | null
230
234
  readonly res: HTTPIncomingMessage | null
package/lib/agent.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const EventEmitter = require('bare-events')
2
2
  const tcp = require('bare-tcp')
3
3
  const HTTPClientConnection = require('./client-connection')
4
+ const errors = require('./errors')
4
5
 
5
6
  class HTTPSocketSet {
6
7
  constructor() {
@@ -68,6 +69,9 @@ class HTTPAgent extends EventEmitter {
68
69
 
69
70
  const { keepAlive = false, keepAliveMsecs = 1000, defaultPort = 80 } = opts
70
71
 
72
+ this._suspended = false
73
+ this._resuming = null
74
+
71
75
  this._sockets = new HTTPSocketSet()
72
76
  this._freeSockets = new HTTPSocketSet()
73
77
 
@@ -77,6 +81,14 @@ class HTTPAgent extends EventEmitter {
77
81
  this._opts = { ...opts }
78
82
  }
79
83
 
84
+ get suspended() {
85
+ return this._suspended
86
+ }
87
+
88
+ get resumed() {
89
+ return this._resuming ? this._resuming.promise : null
90
+ }
91
+
80
92
  get sockets() {
81
93
  return this._sockets.sockets()
82
94
  }
@@ -90,6 +102,8 @@ class HTTPAgent extends EventEmitter {
90
102
  }
91
103
 
92
104
  createConnection(opts) {
105
+ if (this._suspended) throw errors.AGENT_SUSPENDED()
106
+
93
107
  return tcp.createConnection(opts)
94
108
  }
95
109
 
@@ -169,6 +183,23 @@ class HTTPAgent extends EventEmitter {
169
183
  connection._req = req
170
184
  }
171
185
 
186
+ suspend() {
187
+ if (this._suspended) return
188
+
189
+ this._resuming = Promise.withResolvers()
190
+ this._suspended = true
191
+
192
+ this.destroy()
193
+ }
194
+
195
+ resume() {
196
+ if (this._resuming === null) return
197
+
198
+ this._resuming.resolve()
199
+ this._resuming = null
200
+ this._suspended = false
201
+ }
202
+
172
203
  destroy() {
173
204
  for (const socket of this._sockets.sockets()) socket.destroy()
174
205
  }
@@ -177,7 +208,7 @@ class HTTPAgent extends EventEmitter {
177
208
 
178
209
  static _onidle() {
179
210
  for (const agent of this._agents) {
180
- for (const socket of agent._sockets.sockets()) socket.destroy()
211
+ agent.destroy()
181
212
  }
182
213
  }
183
214
  }
@@ -71,7 +71,7 @@ module.exports = class HTTPClientConnection {
71
71
  }
72
72
 
73
73
  _onclose() {
74
- if (this._req) this._req._continueFinal()
74
+ if (this._req) this._req.destroy()
75
75
  }
76
76
 
77
77
  _onend() {
@@ -113,8 +113,15 @@ module.exports = class HTTPClientConnection {
113
113
  break
114
114
 
115
115
  case END:
116
- if (this._res) this._res.push(null)
117
- if (this._req) this._req._continueFinal()
116
+ if (this._res) {
117
+ this._res._socket = null
118
+ this._res.push(null)
119
+ }
120
+
121
+ if (this._req) {
122
+ this._req._socket = null
123
+ this._req.destroy()
124
+ }
118
125
  break
119
126
  }
120
127
  }
@@ -134,7 +141,7 @@ module.exports = class HTTPClientConnection {
134
141
  const upgraded = req.emit('upgrade', res, this._socket, data || EMPTY)
135
142
 
136
143
  res.push(null)
137
- req._continueFinal()
144
+ req.destroy()
138
145
 
139
146
  if (!upgraded) this._socket.destroy()
140
147
  }
package/lib/errors.d.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  declare class HTTPError extends Error {
2
- private constructor()
3
-
4
- static NOT_IMPLEMENTED(msg?: string): HTTPError
5
- static CONNECTION_LOST(msg?: string): HTTPError
2
+ readonly code: string
6
3
  }
7
4
 
8
5
  export = HTTPError
package/lib/errors.js CHANGED
@@ -19,4 +19,8 @@ module.exports = class HTTPError extends Error {
19
19
  static CONNECTION_LOST(msg = 'Socket hung up') {
20
20
  return new HTTPError(msg, HTTPError.CONNECTION_LOST)
21
21
  }
22
+
23
+ static AGENT_SUSPENDED(msg = 'Agent is suspended') {
24
+ return new HTTPError(msg, HTTPError.AGENT_SUSPENDED)
25
+ }
22
26
  }
@@ -71,6 +71,7 @@ module.exports = class HTTPServerConnection {
71
71
  _onclose() {
72
72
  if (this._req && !isEnded(this._req)) this._req.destroy()
73
73
  if (this._res && !isFinished(this._res)) this._res.destroy()
74
+
74
75
  const err = getStreamError(this._socket)
75
76
  if (err) this._socket.destroy(err)
76
77
  }
@@ -121,7 +122,10 @@ module.exports = class HTTPServerConnection {
121
122
  break
122
123
 
123
124
  case END:
124
- if (this._req) this._req.push(null)
125
+ if (this._req) {
126
+ this._req._socket = null
127
+ this._req.push(null)
128
+ }
125
129
  break
126
130
  }
127
131
  }
@@ -109,9 +109,7 @@ module.exports = class HTTPServerResponse extends HTTPOutgoingMessage {
109
109
  this.flushHeaders()
110
110
  }
111
111
 
112
- if (this._chunked && this._onlyHeaders === false) {
113
- this._socket.write(CHUNK_TERMINATOR)
114
- }
112
+ if (this._chunked && this._onlyHeaders === false) this._socket.write(CHUNK_TERMINATOR)
115
113
 
116
114
  if (this._close) this._socket.end()
117
115
 
@@ -122,6 +120,7 @@ module.exports = class HTTPServerResponse extends HTTPOutgoingMessage {
122
120
  super._predestroy()
123
121
 
124
122
  this._req.destroy()
123
+
125
124
  this._continueWrite()
126
125
  }
127
126
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "4.4.0",
3
+ "version": "4.5.2",
4
4
  "description": "Native HTTP/1 library for JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",
@@ -37,7 +37,7 @@
37
37
  "homepage": "https://github.com/holepunchto/bare-http1#readme",
38
38
  "dependencies": {
39
39
  "bare-events": "^2.6.0",
40
- "bare-http-parser": "^1.0.0",
40
+ "bare-http-parser": "^1.1.1",
41
41
  "bare-stream": "^2.3.0",
42
42
  "bare-tcp": "^2.2.0"
43
43
  },