|
2 | 2 |
|
3 | 3 | const path = require('path'); |
4 | 4 | const net = require('net'); |
| 5 | +const readline = require('readline'); |
5 | 6 | const _ = require('lodash'); |
6 | 7 | const { EventEmitter } = require('events'); |
7 | 8 |
|
@@ -44,48 +45,12 @@ class LightningClient extends EventEmitter { |
44 | 45 | }); |
45 | 46 | }); |
46 | 47 |
|
47 | | - this.client.on('data', data => { |
48 | | - _.each(LightningClient.splitJSON(data.toString()), str => { |
49 | | - let dataObject = {}; |
50 | | - try { |
51 | | - dataObject = JSON.parse(str); |
52 | | - } catch (err) { |
53 | | - return; |
54 | | - } |
| 48 | + readline.createInterface({ input: this.client }).on('line', str => { |
| 49 | + try { var data = JSON.parse(''+str); } |
| 50 | + catch (err) { return _self.emit('error', 'Invalid JSON: '+str); } |
55 | 51 |
|
56 | | - _self.emit('res:'+dataObject.id, dataObject) |
57 | | - }); |
58 | | - }); |
59 | | - } |
60 | | - |
61 | | - static splitJSON(str) { |
62 | | - const parts = []; |
63 | | - |
64 | | - let openCount = 0; |
65 | | - let lastSplit = 0; |
66 | | - |
67 | | - for (let i = 0; i < str.length; i++) { |
68 | | - if (i > 0 && str.charCodeAt(i - 1) === 115) { // 115 => backslash, ignore this character |
69 | | - continue; |
70 | | - } |
71 | | - |
72 | | - if (str[i] === '{') { |
73 | | - openCount++; |
74 | | - } else if (str[i] === '}') { |
75 | | - openCount--; |
76 | | - |
77 | | - if (openCount === 0) { |
78 | | - const start = lastSplit; |
79 | | - const end = i + 1 === str.length ? undefined : i + 1; |
80 | | - |
81 | | - parts.push(str.slice(start, end)); |
82 | | - |
83 | | - lastSplit = end; |
84 | | - } |
85 | | - } |
86 | | - } |
87 | | - |
88 | | - return parts.length === 0 ? [str] : parts; |
| 52 | + _self.emit('res:'+data.id, data); |
| 53 | + }) |
89 | 54 | } |
90 | 55 |
|
91 | 56 | increaseWaitTime() { |
|
0 commit comments