-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.js
More file actions
79 lines (74 loc) · 1.59 KB
/
parse.js
File metadata and controls
79 lines (74 loc) · 1.59 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright (c) Maximilian Antoni <max@javascript.studio>
*
* @license MIT
*/
'use strict';
const Transform = require('stream').Transform;
module.exports = class extends Transform {
constructor(options) {
super({
readableObjectMode: true
});
this.str = '';
if (options) {
this.loose = options.loose || options.loose_out;
this.loose_out = options.loose_out;
} else {
this.loose = false;
}
}
_transform(data, encoding, callback) {
let str = this.str + data;
for (;;) {
const p = str.indexOf('\n');
if (p === -1) {
break;
}
if (this._line(str.substring(0, p), callback)) {
return;
}
str = str.substring(p + 1);
}
this.str = str;
callback();
}
_flush(callback) {
if (this.str && this._line(this.str, callback)) {
return;
}
callback();
}
_line(line, callback) {
if (this.loose) {
const s = line.indexOf('{');
if (s === -1) {
if (this.loose_out) {
this.loose_out.write(line);
this.loose_out.write('\n');
}
return false;
}
if (this.loose_out) {
this.loose_out.write(line.substring(0, s));
}
line = line.substring(s);
}
let parsed;
try {
parsed = JSON.parse(line);
} catch (e) {
if (this.loose_out) {
this.loose_out.write(line);
this.loose_out.write('\n');
return false;
}
e.code = 'ERR_JSON_PARSE';
e.line = line;
callback(e);
return true;
}
this.push(parsed);
return false;
}
};