bare-http1 2.0.5 → 3.1.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.
@@ -0,0 +1,80 @@
1
+ const HTTPOutgoingMessage = require('./outgoing-message')
2
+
3
+ module.exports = class HTTPServerResponse extends HTTPOutgoingMessage {
4
+ constructor (socket, req, close) {
5
+ super(socket)
6
+
7
+ this.req = req
8
+
9
+ this._chunked = true
10
+ this._close = close
11
+ this._finishing = false
12
+ this._onlyHeaders = req.method === 'HEAD'
13
+
14
+ this._pendingWrite = null
15
+ }
16
+
17
+ end (data) {
18
+ this._finishing = true
19
+ return super.end(data)
20
+ }
21
+
22
+ writeHead (statusCode, statusMessage = null, headers = {}) {
23
+ if (typeof statusMessage !== 'string') {
24
+ headers = statusMessage
25
+ statusMessage = {}
26
+ }
27
+
28
+ this.statusCode = statusCode
29
+ this.statusMessage = statusMessage || null
30
+ this.headers = headers || {}
31
+ }
32
+
33
+ _write (data, cb) {
34
+ if (this.headersSent === false) {
35
+ if (this._finishing) {
36
+ this.setHeader('Content-Length', (data.byteLength + this._writableState.buffered).toString())
37
+ }
38
+
39
+ this.flushHeaders()
40
+ }
41
+
42
+ if (this._onlyHeaders === true) return cb(null)
43
+
44
+ if (this._chunked) {
45
+ data = Buffer.concat([
46
+ Buffer.from('' + data.byteLength.toString(16) + '\r\n'),
47
+ data,
48
+ Buffer.from('\r\n')
49
+ ])
50
+ }
51
+
52
+ if (this.socket.write(data)) cb(null)
53
+ else this._pendingWrite = cb
54
+ }
55
+
56
+ _final (cb) {
57
+ if (this.headersSent === false) {
58
+ this.setHeader('Content-Length', '0')
59
+ this.flushHeaders()
60
+ }
61
+
62
+ if (this._chunked && this._onlyHeaders === false) this.socket.write(Buffer.from('0\r\n\r\n'))
63
+ if (this._close) this.socket.end()
64
+
65
+ cb(null)
66
+ }
67
+
68
+ _predestroy () {
69
+ super._predestroy()
70
+ this.req.destroy()
71
+ this._continueWrite()
72
+ }
73
+
74
+ _continueWrite () {
75
+ if (this._pendingWrite === null) return
76
+ const cb = this._pendingWrite
77
+ this._pendingWrite = null
78
+ cb(null)
79
+ }
80
+ }
package/lib/server.js ADDED
@@ -0,0 +1,17 @@
1
+ const TCPServer = require('bare-tcp').Server
2
+ const HTTPServerConnection = require('./server-connection')
3
+
4
+ module.exports = class HTTPServer extends TCPServer {
5
+ constructor (opts = {}, onrequest) {
6
+ if (typeof opts === 'function') {
7
+ onrequest = opts
8
+ opts = {}
9
+ }
10
+
11
+ super({ allowHalfOpen: false })
12
+
13
+ this.on('connection', (socket) => new HTTPServerConnection(this, socket, opts))
14
+
15
+ if (onrequest) this.on('request', onrequest)
16
+ }
17
+ }
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "bare-http1",
3
- "version": "2.0.5",
3
+ "version": "3.1.0",
4
4
  "description": "Native HTTP/1 library for JavaScript",
5
- "main": "index.js",
5
+ "exports": {
6
+ ".": "./index.js",
7
+ "./package": "./package.json",
8
+ "./constants": "./lib/constants.js"
9
+ },
6
10
  "files": [
7
11
  "index.js",
8
- "binding.c",
9
- "binding.js",
10
- "CMakeLists.txt",
11
- "prebuilds"
12
+ "lib"
12
13
  ],
13
- "addon": true,
14
14
  "scripts": {
15
15
  "test": "standard && bare test.js"
16
16
  },
@@ -26,6 +26,7 @@
26
26
  "homepage": "https://github.com/holepunchto/bare-http1#readme",
27
27
  "dependencies": {
28
28
  "bare-events": "^2.0.0",
29
+ "bare-tcp": "^1.1.2",
29
30
  "streamx": "^2.13.0"
30
31
  },
31
32
  "devDependencies": {
package/CMakeLists.txt DELETED
@@ -1,21 +0,0 @@
1
- cmake_minimum_required(VERSION 3.25)
2
-
3
- project(bare_http1 C)
4
-
5
- include(bare)
6
-
7
- add_bare_module(bare_http1)
8
-
9
- target_sources(
10
- ${bare_http1}
11
- PRIVATE
12
- binding.c
13
- )
14
-
15
- if(WIN32)
16
- target_link_libraries(
17
- ${bare_http1}
18
- INTERFACE
19
- ws2_32
20
- )
21
- endif()