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