@@ -16,22 +16,27 @@ export interface JSONParserOpts {
16
16
trackBytesWritten ?: boolean ,
17
17
trackBytesRead ?: boolean ,
18
18
includeByteCount ?: boolean ,
19
- emitNonJSON ?: boolean
19
+ emitNonJSON ?: boolean ,
20
+ includeRawString ?: boolean ,
21
+ stringifyNonJSON ?: boolean
20
22
}
21
23
24
+ export const RawStringSymbol = Symbol ( 'raw.json.str' ) ;
22
25
export const RawJSONBytesSymbol = Symbol ( 'raw.json.bytes' ) ;
23
26
export const JSONBytesSymbol = Symbol ( 'json.bytes' ) ;
24
27
25
- export class JSONParser extends stream . Transform {
28
+ export class JSONParser < T = any > extends stream . Transform {
26
29
27
30
emitNonJSON = false ;
28
31
lastLineData = '' ;
29
32
debug = false ;
30
33
delimiter = '\n' ;
31
34
jpBytesWritten = 0 ;
35
+ stringifyNonJSON = false ;
32
36
jpBytesRead = 0 ;
33
37
isTrackBytesRead = false ;
34
38
isTrackBytesWritten = false ;
39
+ isIncludeRawString = false ;
35
40
isIncludeByteCount = false ;
36
41
37
42
constructor ( opts ?: JSONParserOpts ) {
@@ -41,6 +46,10 @@ export class JSONParser extends stream.Transform {
41
46
this . emitNonJSON = true ;
42
47
}
43
48
49
+ if ( opts && opts . includeRawString ) {
50
+ this . isIncludeRawString = true ;
51
+ }
52
+
44
53
if ( opts && opts . includeByteCount ) {
45
54
this . isIncludeByteCount = true ;
46
55
}
@@ -49,6 +58,10 @@ export class JSONParser extends stream.Transform {
49
58
this . isTrackBytesWritten = true ;
50
59
}
51
60
61
+ if ( opts && opts . stringifyNonJSON ) {
62
+ this . stringifyNonJSON = true ;
63
+ }
64
+
52
65
if ( opts && opts . trackBytesRead ) {
53
66
this . isTrackBytesRead = true ;
54
67
}
@@ -89,13 +102,21 @@ export class JSONParser extends stream.Transform {
89
102
this . emit ( 'string' , o ) ;
90
103
}
91
104
105
+ if ( this . stringifyNonJSON ) {
106
+ this . emit ( 'data' , JSON . stringify ( o ) ) ;
107
+ }
108
+
92
109
return ;
93
110
}
94
111
95
112
if ( this . isIncludeByteCount && json && typeof json === 'object' ) {
96
113
json [ RawJSONBytesSymbol ] = Buffer . byteLength ( o ) ;
97
114
}
98
115
116
+ if ( this . isIncludeRawString && json && typeof json === 'object' ) {
117
+ json [ RawStringSymbol ] = o ;
118
+ }
119
+
99
120
this . push ( json ) ;
100
121
101
122
if ( this . isTrackBytesWritten ) {
@@ -140,7 +161,7 @@ export class JSONParser extends stream.Transform {
140
161
this . handleJSON ( this . lastLineData ) ;
141
162
this . lastLineData = '' ;
142
163
}
143
-
164
+
144
165
cb ( ) ;
145
166
}
146
167
0 commit comments