[Pkg-javascript-commits] [node-jsonstream] 60/214: Reset stream.root after emitting it

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 12:58:40 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-jsonstream.

commit a844b893ccd36e67e9cf76f265d457db2cbaeb9b
Author: Santiago Gimeno <santiago.gimeno at quantion.es>
Date:   Sun Sep 9 01:22:57 2012 +0200

    Reset stream.root after emitting it
    
    - This was causing the emission of incorrect objects after an error.
---
 index.js                       | 11 +++++++----
 test/multiple_objects_error.js | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/index.js b/index.js
index 489fe0e..87201ad 100644
--- a/index.js
+++ b/index.js
@@ -55,10 +55,13 @@ exports.parse = function (path) {
   parser.onToken = function (token, value) {
     parser._onToken(token, value);
     if (this.stack.length === 0) {
-      if(!path)
-        stream.emit('data', stream.root)
-      stream.emit('root', stream.root, count)
-      count = 0;
+      if (stream.root) {
+        if(!path)
+          stream.emit('data', stream.root)
+        stream.emit('root', stream.root, count)
+        count = 0;
+        stream.root = null;
+      }
     }
   }
 
diff --git a/test/multiple_objects_error.js b/test/multiple_objects_error.js
new file mode 100644
index 0000000..980423b
--- /dev/null
+++ b/test/multiple_objects_error.js
@@ -0,0 +1,35 @@
+var fs = require ('fs');
+var net = require('net');
+var join = require('path').join;
+var file = join(__dirname, 'fixtures','all_npm.json');
+var it = require('it-is');
+var JSONStream = require('../');
+
+var str = fs.readFileSync(file);
+
+var server = net.createServer(function(client) {
+    var root_calls = 0;
+    var data_calls = 0;
+    var parser = JSONStream.parse();
+    parser.on('root', function(root, count) {
+        ++ root_calls;
+        it(root_calls).notEqual(2);
+    });
+
+    parser.on('error', function(err) {
+        console.log(err);
+        server.close();
+    });
+
+    parser.on('end', function() {
+        console.log('END');
+        server.close();
+    });
+    client.pipe(parser);
+});
+server.listen(9999);
+
+var client = net.connect({ port : 9999 }, function() {
+    var msgs = str + '}';
+    client.end(msgs);
+});

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jsonstream.git



More information about the Pkg-javascript-commits mailing list