bare-http1 3.5.1 → 3.6.0

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/README.md CHANGED
@@ -6,29 +6,23 @@ HTTP/1 library for JavaScript.
6
6
  npm i bare-http1
7
7
  ```
8
8
 
9
- `http.Server` supports most HTTP features, e.g. keep-alive, chunked encoding, and streaming responses.
10
-
11
- Basic `http.ClientRequest` is implemented, temporarily it does NOT support keep-alive and protocol negotiation.
12
-
13
9
  ## Usage
14
10
 
15
- ``` js
11
+ ```js
16
12
  const http = require('bare-http1')
17
13
 
18
- // Same API as Node.js
19
-
20
- const server = http.createServer(function (req, res) {
14
+ const server = http.createServer((req, res) => {
21
15
  res.statusCode = 200
22
16
  res.setHeader('Content-Length', 10)
23
17
  res.write('hello world!')
24
18
  res.end()
25
19
  })
26
20
 
27
- server.listen(0, function () {
21
+ server.listen(0, () => {
28
22
  const { port } = server.address()
29
23
  console.log('server is bound on', port)
30
24
 
31
- const client = http.request({ port }, res => {
25
+ const client = http.request({ port }, (res) => {
32
26
  res.on('data', (data) => console.log(data.toString()))
33
27
  })
34
28
  client.end()
package/index.js CHANGED
@@ -1,6 +1,9 @@
1
1
  exports.IncomingMessage = require('./lib/incoming-message')
2
2
  exports.OutgoingMessage = require('./lib/outgoing-message')
3
3
 
4
+ const Agent = exports.Agent = require('./lib/agent')
5
+ exports.globalAgent = Agent.global
6
+
4
7
  const Server = exports.Server = require('./lib/server')
5
8
  exports.ServerResponse = require('./lib/server-response')
6
9
  exports.ServerConnection = require('./lib/server-connection')
package/lib/agent.js ADDED
@@ -0,0 +1,34 @@
1
+ const tcp = require('bare-tcp')
2
+
3
+ module.exports = class HTTPAgent {
4
+ constructor (opts = {}) {
5
+ const {
6
+ keepAlive = false,
7
+ keepAliveMsecs = 1000,
8
+ maxSockets = Infinity,
9
+ maxTotalSockets = Infinity,
10
+ maxFreeSockets = 256,
11
+ timeout = -1
12
+ } = opts
13
+
14
+ this._keepAlive = typeof keepAlive === 'number' ? keepAlive : keepAlive ? keepAliveMsecs : -1
15
+ this._maxSockets = maxSockets
16
+ this._maxTotalSockets = maxTotalSockets
17
+ this._maxFreeSockets = maxFreeSockets
18
+ this._timeout = timeout
19
+ }
20
+
21
+ createConnection (opts) {
22
+ return tcp.createConnection(opts)
23
+ }
24
+
25
+ reuseSocket (socket, req) {
26
+ socket.ref()
27
+ }
28
+
29
+ keepSocketAlive (socket) {
30
+ return false
31
+ }
32
+
33
+ static global = new this({ keepAlive: 1000, timeout: 5000 })
34
+ }
@@ -1,4 +1,4 @@
1
- const tcp = require('bare-tcp')
1
+ const HTTPAgent = require('./agent')
2
2
  const HTTPOutgoingMessage = require('./outgoing-message')
3
3
  const HTTPClientConnection = require('./client-connection')
4
4
 
@@ -9,8 +9,10 @@ module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
9
9
  opts = {}
10
10
  }
11
11
 
12
+ const agent = opts.agent === false ? new HTTPAgent() : opts.agent || HTTPAgent.global
13
+
12
14
  const {
13
- connection = new HTTPClientConnection(tcp.createConnection(opts))
15
+ connection = new HTTPClientConnection(agent.createConnection(opts), opts)
14
16
  } = opts
15
17
 
16
18
  super(connection.socket)
@@ -56,7 +58,7 @@ module.exports = class HTTPClientRequest extends HTTPOutgoingMessage {
56
58
  return h
57
59
  }
58
60
 
59
- _write (data, cb) {
61
+ _write (data, encoding, cb) {
60
62
  if (this.headersSent === false) this.flushHeaders()
61
63
 
62
64
  if (this._chunked) {
@@ -1,20 +1,20 @@
1
1
  const { Readable } = require('bare-stream')
2
2
 
3
3
  module.exports = class HTTPIncomingMessage extends Readable {
4
- constructor (socket, headers, args = {}) {
4
+ constructor (socket = null, headers = {}, opts = {}) {
5
5
  super()
6
6
 
7
7
  this.socket = socket
8
8
  this.headers = headers
9
9
  this.upgrade = false
10
10
 
11
- // Server arguments
12
- this.method = args.method || ''
13
- this.url = args.url || ''
11
+ // Server options
12
+ this.method = opts.method || ''
13
+ this.url = opts.url || ''
14
14
 
15
- // Client arguments
16
- this.statusCode = args.statusCode || 0
17
- this.statusMessage = args.statusMessage || ''
15
+ // Client options
16
+ this.statusCode = opts.statusCode || 0
17
+ this.statusMessage = opts.statusMessage || ''
18
18
  }
19
19
 
20
20
  get httpVersion () {
@@ -34,6 +34,6 @@ module.exports = class HTTPIncomingMessage extends Readable {
34
34
  }
35
35
 
36
36
  _predestroy () {
37
- if (this.upgrade === false) this.socket.destroy()
37
+ if (this.upgrade === false && this.socket !== null) this.socket.destroy()
38
38
  }
39
39
  }
@@ -2,8 +2,8 @@ const { Writable } = require('bare-stream')
2
2
  const errors = require('./errors')
3
3
 
4
4
  module.exports = class HTTPOutgoingMessage extends Writable {
5
- constructor (socket) {
6
- super({ mapWritable })
5
+ constructor (socket = null) {
6
+ super()
7
7
 
8
8
  this.socket = socket
9
9
  this.headers = {}
@@ -28,7 +28,7 @@ module.exports = class HTTPOutgoingMessage extends Writable {
28
28
  }
29
29
 
30
30
  flushHeaders () {
31
- if (this.headersSent === true) return
31
+ if (this.headersSent === true || this.socket === null) return
32
32
 
33
33
  this.socket.write(Buffer.from(this._header()))
34
34
  this.headersSent = true
@@ -39,10 +39,6 @@ module.exports = class HTTPOutgoingMessage extends Writable {
39
39
  }
40
40
 
41
41
  _predestroy () {
42
- if (this.upgrade === false) this.socket.destroy()
42
+ if (this.upgrade === false && this.socket !== null) this.socket.destroy()
43
43
  }
44
44
  }
45
-
46
- function mapWritable (data) {
47
- return typeof data === 'string' ? Buffer.from(data) : data
48
- }
@@ -54,7 +54,7 @@ module.exports = class HTTPServerResponse extends HTTPOutgoingMessage {
54
54
  return h
55
55
  }
56
56
 
57
- _write (data, cb) {
57
+ _write (data, encoding, cb) {
58
58
  if (this.headersSent === false) {
59
59
  if (this._finishing) {
60
60
  this.setHeader('Content-Length', (data.byteLength + this._writableState.buffered).toString())
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "3.5.1",
3
+ "version": "3.6.0",
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": "^1.0.0",
29
+ "bare-stream": "^2.0.0",
30
30
  "bare-tcp": "^1.1.2"
31
31
  },
32
32
  "devDependencies": {