bare-http1 4.0.4 → 4.1.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 CHANGED
@@ -24,6 +24,7 @@ export {
24
24
  HTTPError as errors
25
25
  }
26
26
 
27
+ export const METHODS: HTTPMethod[]
27
28
  export const STATUS_CODES: typeof constants.status
28
29
 
29
30
  export interface HTTPIncomingMessageEvents extends ReadableEvents {
package/index.js CHANGED
@@ -14,6 +14,7 @@ exports.ClientConnection = require('./lib/client-connection')
14
14
  exports.constants = require('./lib/constants')
15
15
  exports.errors = require('./lib/errors')
16
16
 
17
+ exports.METHODS = Object.values(exports.constants.method) // For Node.js compatibility
17
18
  exports.STATUS_CODES = exports.constants.status // For Node.js compatibility
18
19
 
19
20
  exports.createServer = function createServer(opts, onrequest) {
@@ -1,6 +1,9 @@
1
1
  const HTTPAgent = require('./agent')
2
2
  const HTTPOutgoingMessage = require('./outgoing-message')
3
3
 
4
+ const CHUNK_DELIMITER = Buffer.from('\r\n')
5
+ const CHUNK_TERMINATOR = Buffer.from('0\r\n\r\n')
6
+
4
7
  module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
5
8
  constructor(opts = {}, onresponse = null) {
6
9
  if (typeof opts === 'function') {
@@ -32,6 +35,11 @@ module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
32
35
  if (onresponse) this.once('response', onresponse)
33
36
  }
34
37
 
38
+ // For Node.js compatibility
39
+ abort() {
40
+ return this.destroy()
41
+ }
42
+
35
43
  _header() {
36
44
  let h = `${this.method} ${this.path} HTTP/1.1\r\n`
37
45
 
@@ -61,21 +69,22 @@ module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
61
69
  if (this.headersSent === false) this.flushHeaders()
62
70
 
63
71
  if (this._chunked) {
64
- data = Buffer.concat([
65
- Buffer.from('' + data.byteLength.toString(16) + '\r\n'),
66
- data,
67
- Buffer.from('\r\n')
68
- ])
72
+ this.socket.write(Buffer.from(data.byteLength.toString(16)))
73
+ this.socket.write(CHUNK_DELIMITER)
69
74
  }
70
75
 
71
- if (this.socket.write(data)) cb(null)
76
+ let flushed = this.socket.write(data)
77
+
78
+ if (this._chunked) flushed = this.socket.write(CHUNK_DELIMITER)
79
+
80
+ if (flushed) cb(null)
72
81
  else this._pendingWrite = cb
73
82
  }
74
83
 
75
84
  _final(cb) {
76
85
  if (this.headersSent === false) this.flushHeaders()
77
86
 
78
- if (this._chunked) this.socket.write(Buffer.from('0\r\n\r\n'))
87
+ if (this._chunked) this.socket.write(CHUNK_TERMINATOR)
79
88
 
80
89
  this._pendingFinal = cb
81
90
  }
@@ -1,6 +1,9 @@
1
1
  const HTTPOutgoingMessage = require('./outgoing-message')
2
2
  const constants = require('./constants')
3
3
 
4
+ const CHUNK_DELIMITER = Buffer.from('\r\n')
5
+ const CHUNK_TERMINATOR = Buffer.from('0\r\n\r\n')
6
+
4
7
  module.exports = class HTTPServerResponse extends HTTPOutgoingMessage {
5
8
  constructor(socket, req, close) {
6
9
  super(socket)
@@ -77,14 +80,15 @@ module.exports = class HTTPServerResponse extends HTTPOutgoingMessage {
77
80
  if (this._onlyHeaders === true) return cb(null)
78
81
 
79
82
  if (this._chunked) {
80
- data = Buffer.concat([
81
- Buffer.from('' + data.byteLength.toString(16) + '\r\n'),
82
- data,
83
- Buffer.from('\r\n')
84
- ])
83
+ this.socket.write(Buffer.from(data.byteLength.toString(16)))
84
+ this.socket.write(CHUNK_DELIMITER)
85
85
  }
86
86
 
87
- if (this.socket.write(data)) cb(null)
87
+ let flushed = this.socket.write(data)
88
+
89
+ if (this._chunked) flushed = this.socket.write(CHUNK_DELIMITER)
90
+
91
+ if (flushed) cb(null)
88
92
  else this._pendingWrite = cb
89
93
  }
90
94
 
@@ -94,8 +98,10 @@ module.exports = class HTTPServerResponse extends HTTPOutgoingMessage {
94
98
  this.flushHeaders()
95
99
  }
96
100
 
97
- if (this._chunked && this._onlyHeaders === false)
98
- this.socket.write(Buffer.from('0\r\n\r\n'))
101
+ if (this._chunked && this._onlyHeaders === false) {
102
+ this.socket.write(CHUNK_TERMINATOR)
103
+ }
104
+
99
105
  if (this._close) this.socket.end()
100
106
 
101
107
  cb(null)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "4.0.4",
3
+ "version": "4.1.1",
4
4
  "description": "Native HTTP/1 library for JavaScript",
5
5
  "exports": {
6
6
  "./package": "./package.json",