Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Emit remaining buffers on DONE.

Closes #2.
  • Loading branch information...
commit 928ad0622d1f49c3b88a5b143c6aaa648dab8927 1 parent e512949
@pgriess authored
Showing with 50 additions and 0 deletions.
  1. +14 −0 lib/strtok.js
  2. +36 −0 test/test-done.js
View
14 lib/strtok.js
@@ -341,6 +341,20 @@ var parse = function(s, cb) {
if (type === DONE) {
s.removeListener('data', dataListener);
+
+ // Pump all of the buffers that we already saw back through the
+ // stream; the protocol layer will have set up listeners for this
+ // event if it cares about the remaining data.
+ while (bufs.length > 0) {
+ var b = bufs.shift();
+
+ if (bufOffset > 0) {
+ b = b.slice(bufOffset, b.length);
+ bufOffset = 0;
+ }
+
+ s.emit('data', b);
+ }
}
};
View
36 test/test-done.js
@@ -1,6 +1,8 @@
// Verify that the DONE value is respected.
var assert = require('assert');
+var Buffer = require('buffer').Buffer;
+var EventEmitter = require('events').EventEmitter;
var util = require('./util');
var strtok = require('../lib/strtok');
@@ -13,3 +15,37 @@ util.runParseTests('\x1a\x1a\x1a\x1a\x1a\x1a', [
return strtok.DONE;
}
]);
+
+var s = new EventEmitter();
+strtok.parse(s, (function() {
+ var bufsSeen = [];
+
+ process.on('exit', function() {
+ assert.equal(bufsSeen.length, 2);
+ assert.equal(
+ bufsSeen[0].toString('binary'),
+ '\x11\x22'
+ );
+ assert.equal(
+ bufsSeen[1].toString('binary'),
+ 'abcdef\xff'
+ );
+ });
+
+ return function(v) {
+ if (v === undefined) {
+ return strtok.UINT8;
+ }
+
+ assert.equal(v, 0xff);
+
+ s.on('data', function(b) {
+ bufsSeen.push(b);
+ });
+
+ return strtok.DONE;
+ };
+})());
+
+s.emit('data', new Buffer('\xff\x11\x22', 'binary'));
+s.emit('data', new Buffer('abcdef\xff', 'binary'));
Please sign in to comment.
Something went wrong with that request. Please try again.