[Pkg-javascript-commits] [node-jsonstream] 57/214: Support multiple JSON objects in a stream

Bastien Roucariès rouca at moszumanska.debian.org
Fri Dec 1 12:58:39 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 3134b61ccc694aac9ea4c5a3c250b0325258eaf2
Author: Santiago Gimeno <santiago.gimeno at quantion.es>
Date:   Fri Sep 7 13:48:33 2012 +0200

    Support multiple JSON objects in a stream
    
    - At the moment only one per stream was supported.
    - Added test case.
---
 index.js                 | 14 +++++++++++---
 test/multiple_objects.js | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+), 3 deletions(-)

diff --git a/index.js b/index.js
index 80a536f..489fe0e 100644
--- a/index.js
+++ b/index.js
@@ -50,6 +50,17 @@ exports.parse = function (path) {
   stream.emit('data', this.value[this.key])
   }
 
+  parser._onToken = parser.onToken;
+
+  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;
+    }
+  }
 
   parser.onError = function (err) {
     stream.emit('error', err)
@@ -71,9 +82,6 @@ exports.parse = function (path) {
   stream.end = function (data) {
     if(data)
       stream.write(data)
-    if(!path)
-      stream.emit('data', stream.root)
-    stream.emit('root', stream.root, count)
     stream.emit('end')
   }
 
diff --git a/test/multiple_objects.js b/test/multiple_objects.js
new file mode 100644
index 0000000..e7b8500
--- /dev/null
+++ b/test/multiple_objects.js
@@ -0,0 +1,37 @@
+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 expected = JSON.parse(str);
+
+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;
+    });
+
+    parser.on('data', function(data) {
+        ++ data_calls;
+        it(data).deepEqual(expected)
+    });
+
+    parser.on('end', function() {
+        console.error('END');
+        it(root_calls).equal(3);
+        it(data_calls).equal(3);
+        server.close();
+    });
+    client.pipe(parser);
+});
+server.listen(9999);
+
+var client = net.connect({ port : 9999 }, function() {
+    var msgs = [str, str, str].join('');
+    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