Skip to content

Commit

Permalink
Always pipe SAXParser to the /dev/null stream to prevent hitting Read…
Browse files Browse the repository at this point in the history
…ableStream buffer limits if we don't have consumers (fixes #97)
  • Loading branch information
inikulin committed Jan 15, 2016
1 parent 2b97a93 commit 0fa927a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/07_version_history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Version history

## 2.1.2
* Fixed: SAX parser silently exits on big files (GH [#97](https://github.com/inikulin/parse5/issues/97))

## 2.1.1
* Fixed: location info not attached for empty attributes (GH [#96](https://github.com/inikulin/parse5/issues/96))
(by [@yyx990803](https://github.com/yyx990803)).
Expand Down
14 changes: 14 additions & 0 deletions lib/sax/dev_null_stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

var WritableStream = require('stream').Writable,
util = require('util');

var DevNullStream = module.exports = function () {
WritableStream.call(this);
};

util.inherits(DevNullStream, WritableStream);

DevNullStream.prototype._write = function (chunk, encoding, cb) {
cb();
};
6 changes: 6 additions & 0 deletions lib/sax/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var TransformStream = require('stream').Transform,
DevNullStream = require('./dev_null_stream'),
inherits = require('util').inherits,
Tokenizer = require('../tokenizer'),
ParserFeedbackSimulator = require('./parser_feedback_simulator'),
Expand Down Expand Up @@ -59,6 +60,11 @@ var SAXParser = module.exports = function (options) {

this.lastChunkWritten = false;
this.stopped = false;

// NOTE: always pipe stream to the /dev/null stream to avoid
// `highWaterMark` hit even if we don't have consumers.
// (see: https://github.com/inikulin/parse5/issues/97#issuecomment-171940774)
this.pipe(new DevNullStream());
};

inherits(SAXParser, TransformStream);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "parse5",
"description": "WHATWG HTML5 specification-compliant, fast and ready for production HTML parsing/serialization toolset for Node.js",
"version": "2.1.1",
"version": "2.1.2",
"author": "Ivan Nikulin <[email protected]> (https://github.com/inikulin)",
"contributors": [
"Alan Clarke (https://github.com/alanclarke)",
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/sax_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,14 @@ exports['SAX - Piping and .stop()'] = function (done) {
done();
});
};

exports['Regression-SAX-SAX parser silently exits on big files (GH-97)'] = function (done) {
var parser = new SAXParser();

fs
.createReadStream(path.join(__dirname, '../data/huge-page/huge-page.html'))
.pipe(parser);

//NOTE: This is a smoke test - in case of regression it will fail with timeout.
parser.once('finish', done);
};

0 comments on commit 0fa927a

Please sign in to comment.