You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2020/03/18 13:54:35 UTC

[GitHub] [couchdb-nano] eridal commented on a change in pull request #208: Axios

eridal commented on a change in pull request #208: Axios
URL: https://github.com/apache/couchdb-nano/pull/208#discussion_r394362834
 
 

 ##########
 File path: lib/changesreader.js
 ##########
 @@ -0,0 +1,284 @@
+const EventEmitter = require('events').EventEmitter
+const stream = require('stream')
+const EVENT_BATCH = 'batch'
+const EVENT_CHANGE = 'change'
+const EVENT_SEQ = 'seq'
+const EVENT_ERROR = 'error'
+
+// streaming line breaker
+const liner = () => {
+  const liner = new stream.Transform({ objectMode: true })
+
+  liner._transform = function (chunk, encoding, done) {
+    let data = chunk.toString('utf8')
+    if (this._lastLineData) {
+      data = this._lastLineData + data
+      this._lastLineData = null
+    }
+
+    const lines = data.split(/\s*\n/)
+    this._lastLineData = lines.splice(lines.length - 1, 1)[0]
+    lines.forEach(this.push.bind(this))
+    done()
+  }
+
+  liner._flush = function (done) {
+    this.push(this._lastLineData)
+    this._lastLineData = null
+    done()
+  }
+
+  return liner
+}
+
+// streaming change processor
+const changeProcessor = (ee, batchSize) => {
+  const changeProcessor = new stream.Transform({ objectMode: true })
+  const buffer = []
+  changeProcessor.lastSeq = '0'
+
+  changeProcessor._transform = function (chunk, encoding, done) {
+    // remove last char from string
+    if (chunk[chunk.length - 1] === ',') {
+      chunk = chunk.slice(0, -1)
+    }
+
+    try {
+      const j = JSON.parse(chunk)
+      buffer.push(j)
+      if (buffer.length >= batchSize) {
+        ee.emit(EVENT_BATCH, buffer.splice(0, batchSize))
+      }
+      done()
+    } catch (e) {
+      // look for last_seq
+      const match = chunk.match(/"last_seq":"([^"]+)"/)
 
 Review comment:
   What happens if the server replies with a number? Like in ..
   
   ```json
   {
     "last_seq": 1234
   }
   ```
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services