-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDataHandler.js
44 lines (38 loc) · 1.43 KB
/
DataHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const { EventEmitter } = require("events");
module.exports = class DataHandler extends EventEmitter {
constructor() {
super();
this.streamContent = "";
}
handle(data) {
this.streamContent += data;
if (this.streamContent.charAt(0) != "{") {
this.emit("error", new Error(`Bad Data: "${this.streamContent}"`));
this.streamContent = "";
}
let counter = 0;
// This breaks when "{" or "}" is part of a string inside the json string
for (let i = 0; i < this.streamContent.length; i++) {
const char = this.streamContent[i];
if (char == "{") counter++;
if (char == "}") {
counter--;
if (counter == 0) {
const jsonContent = this.streamContent.substring(0, i + 1);
try {
const parsed = JSON.parse(jsonContent);
this.emit("data", parsed);
this.streamContent = this.streamContent.substring(i + 1);
// Restart for next potential object in string
counter = 0;
i = -1;
} catch(err) { // JSON corrupted
this.streamContent = "";
this.emit("error", err);
break;
}
}
}
}
}
}