-
-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use StringDecoder for Buffers in WritableStream
- Loading branch information
Showing
2 changed files
with
27 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
module.exports = Stream; | ||
|
||
var Parser = require("./Parser.js"), | ||
WritableStream = require("stream").Writable || require("readable-stream").Writable; | ||
WritableStream = require("stream").Writable || require("readable-stream").Writable, | ||
StringDecoder = require("string_decoder").StringDecoder, | ||
Buffer = require("buffer").Buffer; | ||
|
||
function Stream(cbs, options){ | ||
var parser = this._parser = new Parser(cbs, options); | ||
var decoder = this._decoder = new StringDecoder(); | ||
|
||
WritableStream.call(this, {decodeStrings: false}); | ||
|
||
this.once("finish", function(){ | ||
parser.end(); | ||
parser.end(decoder.end()); | ||
}); | ||
} | ||
|
||
require("inherits")(Stream, WritableStream); | ||
|
||
WritableStream.prototype._write = function(chunk, encoding, cb){ | ||
if(chunk instanceof Buffer) chunk = this._decoder.write(chunk); | ||
this._parser.write(chunk); | ||
cb(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var htmlparser2 = require(".."), | ||
assert = require("assert"); | ||
|
||
describe("WritableStream", function(){ | ||
|
||
it("should decode fragmented unicode characters", function(){ | ||
var processed = false; | ||
var stream = new htmlparser2.WritableStream({ | ||
ontext: function(text){ | ||
assert.equal(text, "€"); | ||
processed = true; | ||
} | ||
}); | ||
|
||
stream.write(new Buffer([0xE2, 0x82])); | ||
stream.write(new Buffer([0xAC])); | ||
stream.end(); | ||
|
||
assert(processed); | ||
}); | ||
}); |