Skip to content

Commit

Permalink
Replace the built-in Node HTTP parser with the http-parser-js package.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Jul 30, 2017
1 parent b76f4e2 commit 751c77a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
3 changes: 3 additions & 0 deletions lib/websocket/driver/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ var instance = {
this.statusCode = this._http.statusCode;
this.headers = this._http.headers;

if (this._http.error)
return this._failHandshake(this._http.error.message);

if (this._http.statusCode !== 101)
return this._failHandshake('Unexpected response code: ' + this._http.statusCode);

Expand Down
13 changes: 10 additions & 3 deletions lib/websocket/http_parser.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var NodeHTTPParser = process.binding('http_parser').HTTPParser;
var NodeHTTPParser = require('http-parser-js').HTTPParser;

var VERSION = process.version.match(/[0-9]+/g).map(function(n) { return parseInt(n, 10) });

Expand Down Expand Up @@ -115,8 +115,15 @@ HttpParser.prototype.isComplete = function() {
};

HttpParser.prototype.parse = function(chunk) {
var offset = (VERSION[0] === 0 && VERSION[1] < 6) ? 1 : 0,
consumed = this._parser.execute(chunk, 0, chunk.length) + offset;
var consumed = this._parser.execute(chunk, 0, chunk.length);

if (typeof consumed !== 'number') {
this.error = consumed;
this._complete = true;
return;
}

if (VERSION[0] === 0 && VERSION[1] < 6) consumed += 1;

if (this._complete)
this.body = (consumed < chunk.length)
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
, "version" : "0.6.5"
, "engines" : {"node": ">=0.6.0"}
, "main" : "./lib/websocket/driver"
, "dependencies" : {"websocket-extensions": ">=0.1.1"}
, "devDependencies" : {"jstest": "", "permessage-deflate": ""}

, "dependencies" : { "http-parser-js": ">=0.4.0"
, "websocket-extensions": ">=0.1.1"
}
, "devDependencies" : { "jstest": ""
, "permessage-deflate": ""
}

, "scripts" : {"test": "jstest spec/runner.js"}

Expand Down
4 changes: 2 additions & 2 deletions spec/websocket/driver/client_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ test.describe("Client", function() { with(this) {

it("changes the state to closed", function() { with(this) {
assertEqual( false, open )
assertEqual( "Error during WebSocket handshake: Unexpected response code: 4", error.message )
assertEqual( [1002, "Error during WebSocket handshake: Unexpected response code: 4"], close )
assertEqual( "Error during WebSocket handshake: Parse Error", error.message )
assertEqual( [1002, "Error during WebSocket handshake: Parse Error"], close )
assertEqual( "closed", driver().getState() )
}})
}})
Expand Down

2 comments on commit 751c77a

@benjamn
Copy link

@benjamn benjamn commented on 751c77a Aug 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you npm publish a new version that includes this change? Thanks!

@jcoglan
Copy link
Collaborator Author

@jcoglan jcoglan commented on 751c77a Aug 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjamn Please see my comment here: #21 (comment)

Please sign in to comment.