Skip to content

Commit 263276a

Browse files
committed
Implemented pause/resume
Streams starting with node 0.10 are expected to implement the stream.pause method See https://nodejs.org/api/stream.html#stream_compatibility_with_older_node_js_versions
1 parent 9f9814f commit 263276a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/sax.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
this._parser = new SAXParser(strict, opt)
184184
this.writable = true
185185
this.readable = true
186+
this.paused = false
186187

187188
var me = this
188189

@@ -238,7 +239,7 @@
238239

239240
this._parser.write(data.toString())
240241
this.emit('data', data)
241-
return true
242+
return !this.paused
242243
}
243244

244245
SAXStream.prototype.end = function (chunk) {
@@ -249,6 +250,17 @@
249250
return true
250251
}
251252

253+
SAXStream.prototype.pause = function () {
254+
this.paused = true
255+
}
256+
257+
SAXStream.prototype.resume = function () {
258+
if (this.paused) {
259+
this.paused = false
260+
this.emit('drain')
261+
}
262+
}
263+
252264
SAXStream.prototype.on = function (ev, handler) {
253265
var me = this
254266
if (!me._parser['on' + ev] && streamWraps.indexOf(ev) !== -1) {

test/pause_resume_stream.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var tap = require('tap')
2+
3+
// pausable stream that will understand and handle push returning false
4+
var Readable = require('stream').Readable
5+
var inputStream = new Readable()
6+
var saxStream = require('../lib/sax').createStream()
7+
8+
var paused = true
9+
saxStream.pause()
10+
11+
saxStream.on('data', function () {
12+
tap.equal(false, paused, 'Received data while paused')
13+
})
14+
15+
inputStream.pipe(saxStream)
16+
17+
inputStream.push('<test><a>')
18+
inputStream.push('</a><b>')
19+
inputStream.push('</b><c>')
20+
inputStream.push('</c>')
21+
inputStream.push('<d>')
22+
inputStream.push('</d></test>')
23+
inputStream.push(null)
24+
25+
paused = false
26+
saxStream.resume()

0 commit comments

Comments
 (0)