11diff --git a/lib/sax.js b/lib/sax.js
2- index 122ad8e5a478339d56fc86d00d21c5d142739f0e..2557c46482ff651026436cc6c7142ebfe68ddfc5 100644
2+ index d35adf1a319b7f064d7a7e43c46630ed5ef587ff..4c5a08844f3be4ddceb12285d3fd133213dcc7e0 100644
33--- a/lib/sax.js
44+++ b/lib/sax.js
5- @@ -1 ,8 +1 ,6 @@
6- ;(function (sax) { // wrapper for non-node envs
7- sax.parser = function (strict, opt) { return new SAXParser(strict, opt) }
5+ @@ -4 ,8 +4 ,6 @@
6+ return new SAXParser(strict, opt)
7+ }
88 sax.SAXParser = SAXParser
99- sax.SAXStream = SAXStream
1010- sax.createStream = createStream
1111
1212 // When we pass the MAX_BUFFER_LENGTH position, start checking for buffer overruns.
1313 // When we check, schedule the next check for MAX_BUFFER_LENGTH - (max(buffer lengths)),
14- @@ -164,111 +162 ,6 @@
15- flush: function () { flushBuffers(this) }
14+ @@ -191,123 +189 ,6 @@
15+ },
1616 }
1717
1818- var Stream
@@ -27,11 +27,11 @@ index 122ad8e5a478339d56fc86d00d21c5d142739f0e..2557c46482ff651026436cc6c7142ebf
2727- return ev !== 'error' && ev !== 'end'
2828- })
2929-
30- - function createStream (strict, opt) {
30+ - function createStream(strict, opt) {
3131- return new SAXStream(strict, opt)
3232- }
3333-
34- - function SAXStream (strict, opt) {
34+ - function SAXStream(strict, opt) {
3535- if (!(this instanceof SAXStream)) {
3636- return new SAXStream(strict, opt)
3737- }
@@ -72,26 +72,27 @@ index 122ad8e5a478339d56fc86d00d21c5d142739f0e..2557c46482ff651026436cc6c7142ebf
7272- me.on(ev, h)
7373- },
7474- enumerable: true,
75- - configurable: false
75+ - configurable: false,
7676- })
7777- })
7878- }
7979-
8080- SAXStream.prototype = Object.create(Stream.prototype, {
8181- constructor: {
82- - value: SAXStream
83- - }
82+ - value: SAXStream,
83+ - },
8484- })
8585-
8686- SAXStream.prototype.write = function (data) {
87- - if (typeof Buffer === 'function' &&
87+ - if (
88+ - typeof Buffer === 'function' &&
8889- typeof Buffer.isBuffer === 'function' &&
89- - Buffer.isBuffer(data)) {
90+ - Buffer.isBuffer(data)
91+ - ) {
9092- if (!this._decoder) {
91- - var SD = require('string_decoder').StringDecoder
92- - this._decoder = new SD('utf8')
93+ - this._decoder = new TextDecoder('utf8')
9394- }
94- - data = this._decoder.write (data)
95+ - data = this._decoder.decode (data, { stream: true } )
9596- }
9697-
9798- this._parser.write(data.toString())
@@ -103,6 +104,14 @@ index 122ad8e5a478339d56fc86d00d21c5d142739f0e..2557c46482ff651026436cc6c7142ebf
103104- if (chunk && chunk.length) {
104105- this.write(chunk)
105106- }
107+ - // Flush any remaining decoded data from the TextDecoder
108+ - if (this._decoder) {
109+ - var remaining = this._decoder.decode()
110+ - if (remaining) {
111+ - this._parser.write(remaining)
112+ - this.emit('data', remaining)
113+ - }
114+ - }
106115- this._parser.end()
107116- return true
108117- }
@@ -111,7 +120,10 @@ index 122ad8e5a478339d56fc86d00d21c5d142739f0e..2557c46482ff651026436cc6c7142ebf
111120- var me = this
112121- if (!me._parser['on' + ev] && streamWraps.indexOf(ev) !== -1) {
113122- me._parser['on' + ev] = function () {
114- - var args = arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments)
123+ - var args =
124+ - arguments.length === 1 ?
125+ - [arguments[0]]
126+ - : Array.apply(null, arguments)
115127- args.splice(0, 0, ev)
116128- me.emit.apply(me, args)
117129- }
0 commit comments