Skip to content

Commit

Permalink
perf: use node native parser
Browse files Browse the repository at this point in the history
Fixes: #22
  • Loading branch information
ronag committed Jun 26, 2020
1 parent e5ec7ea commit b41e63e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
24 changes: 21 additions & 3 deletions lib/client-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const { URL } = require('url')
const net = require('net')
const tls = require('tls')
const { HTTPParser } = require('http-parser-js')
const EventEmitter = require('events')
const Request = require('./request')
const assert = require('assert')
Expand Down Expand Up @@ -39,6 +38,8 @@ const {
kEnqueue,
kClient
} = require('./symbols')
// TODO: This is not really allowed by Node but it works for now.
const HTTPParser = process.binding('http_parser').HTTPParser // eslint-disable-line

const CRLF = Buffer.from('\r\n', 'ascii')
const TE_CHUNKED = Buffer.from('transfer-encoding: chunked\r\n', 'ascii')
Expand Down Expand Up @@ -285,23 +286,40 @@ class ClientBase extends EventEmitter {
}
}

class HTTPServerAsyncResource {
constructor (type, socket) {
this.type = type
this.socket = socket
}
}

class Parser extends HTTPParser {
constructor (client, socket) {
super(HTTPParser.RESPONSE)
super()

this.client = client
this.socket = socket
this.resumeSocket = () => socket.resume()
this.read = 0
this.body = null

this.initialize(
HTTPParser.RESPONSE,
// TODO: What to do with these options?
new HTTPServerAsyncResource('HTTPINCOMINGMESSAGE', socket),
0,
false,
0
)
}

/* istanbul ignore next: we don't support trailers yet */
[HTTPParser.kOnHeaders] () {
// TODO: Handle trailers.
}

[HTTPParser.kOnHeadersComplete] ({ statusCode, headers }) {
[HTTPParser.kOnHeadersComplete] (versionMajor, versionMinor, headers, method,
url, statusCode, statusMessage, upgrade, shouldKeepAlive) {
const { client, resumeSocket } = this
const request = client[kQueue][client[kRunningIdx]]
const { signal, opaque } = request
Expand Down
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
"standard": "^14.0.0",
"tap": "^14.0.0"
},
"dependencies": {
"http-parser-js": "^0.5.2"
},
"pre-commit": [
"coverage"
]
Expand Down

0 comments on commit b41e63e

Please sign in to comment.