dominictarr / JSONStream

rawStream.pipe(JSONStream.parse()).pipe(streamOfObjects)
Other
1.92k stars 165 forks source link

Header is emitted after first data event #122

Closed stephenlacy closed 7 years ago

stephenlacy commented 7 years ago

The header event is emitted after the first row is parsed.

const parse = JSONStream.parse('items.rows.*')
let header
parse.on('header', (data) => header = data)

const tail = through2.obj((row, _, cb) => {
  if (header) row.header = header
  cb(null, row)
})

parse.pipe(tail)
const stream = duplex.obj(parse, tail)

stream.on('data', (row) => {
  console.log(typeof row.header)
}

output:

undefined
object
object

I fixed it by doing this:


diff --git a/index.js b/index.js
index c164100..6d68a19 100755
--- a/index.js
+++ b/index.js
@@ -94,6 +94,12 @@ exports.parse = function (path, map) {
       }

     }
+
+    // emit header
+    if (header) {
+      stream.emit('header', header);
+      header = false;
+    }
     if (j !== this.stack.length) return

     count ++
@@ -115,12 +121,6 @@ exports.parse = function (path, map) {
     for(var k in this.stack)
       if (!Object.isFrozen(this.stack[k]))
         this.stack[k].value = null
-
-    // emit header
-    if (header) {
-      stream.emit('header', header);
-      header = false;
-    }
   }
   parser._onToken = parser.onToken;

output:

object
object
object

@dominictarr Would you be to accept a PR with tests passing for this change?

dominictarr commented 7 years ago

I don't quite understand the difference here - was the header event emitted after the first data event? It should certainly be before, I would be happy to merge a PR for that.

stephenlacy commented 7 years ago

Yes, the header event was emitted after the first event:

https://github.com/dominictarr/JSONStream/blob/master/index.js#L112, while the header emitter is on line 120

I'll open a PR, thanks