@@ -4,27 +4,14 @@ import { Meta } from "../../meta";
44import { normalizeColumn , normalizeRow } from "../normalizeResponse" ;
55import { hydrateRow } from "../hydrateResponse" ;
66import { RowStream } from "./rowStream" ;
7+ import { JSONParser } from "./parser" ;
78
89export class JSONStream {
10+ jsonParser : JSONParser ;
911 options ?: StreamOptions ;
1012 executeQueryOptions : ExecuteQueryOptions ;
1113 emitter : RowStream ;
1214 rowParser : RowParser ;
13- state :
14- | "meta"
15- | "meta-array"
16- | "rootKeys"
17- | "data"
18- | "data-array"
19- | "query"
20- | "query-object"
21- | null ;
22-
23- columns : Meta [ ] ;
24- rows : unknown [ ] ;
25- rest : string ;
26-
27- objBuffer ?: string ;
2815
2916 constructor ( {
3017 emitter,
@@ -35,151 +22,50 @@ export class JSONStream {
3522 options ?: StreamOptions ;
3623 executeQueryOptions : ExecuteQueryOptions ;
3724 } ) {
38- this . state = null ;
3925 this . emitter = emitter ;
4026 this . options = options ;
4127 this . executeQueryOptions = executeQueryOptions ;
42-
4328 this . rowParser = this . options ?. rowParser || this . defaultRowParser ;
44- this . columns = [ ] ;
45- this . rows = [ ] ;
46- this . rest = "{" ;
29+
30+ this . jsonParser = new JSONParser ( {
31+ onMetadataParsed : columns => {
32+ this . emitter . emit ( "metadata" , columns ) ;
33+ } ,
34+ hydrateRow : this . rowParser ,
35+ hydrateColumn : ( columnStr : string ) => {
36+ const column = JSONbig . parse ( columnStr ) ;
37+ return normalizeColumn ( column ) ;
38+ }
39+ } ) ;
4740 }
4841
49- defaultRowParser ( row : string , isLastRow : boolean ) {
42+ defaultRowParser = ( row : string , isLastRow : boolean ) => {
5043 const normalizeData = this . executeQueryOptions . response ?. normalizeData ;
5144 const parsed = JSONbig . parse ( row ) ;
5245 const hydrate = this . executeQueryOptions . response ?. hydrateRow || hydrateRow ;
53- const hydratedRow = hydrate ( parsed , this . columns , this . executeQueryOptions ) ;
46+ const result = this . getResult ( 0 ) ;
47+ const columns = result . columns ;
48+ const hydratedRow = hydrate (
49+ parsed ,
50+ columns as Meta [ ] ,
51+ this . executeQueryOptions
52+ ) ;
5453 if ( normalizeData ) {
5554 const normalizedRow = normalizeRow (
5655 hydratedRow ,
57- this . columns ,
56+ columns as Meta [ ] ,
5857 this . executeQueryOptions
5958 ) ;
6059 return normalizedRow ;
6160 }
6261 return hydratedRow ;
63- }
64-
65- parseRest ( ) {
66- const parsed = JSONbig . parse ( this . rest ) ;
67- return parsed ;
68- }
69-
70- handleRoot ( line : string ) {
71- if ( line === "{" ) {
72- this . state = "rootKeys" ;
73- }
74- }
75-
76- handleRootKeys ( line : string ) {
77- if ( line === "query" ) {
78- this . state = "query" ;
79- } else if ( line === '"query": {' ) {
80- this . state = "query-object" ;
81- } else if ( line === '"meta":' ) {
82- this . state = "meta" ;
83- } else if ( line === '"data":' ) {
84- this . state = "data" ;
85- } else if ( line === '"meta": [' ) {
86- this . state = "meta-array" ;
87- } else if ( line === '"data": [' ) {
88- this . state = "data-array" ;
89- } else {
90- this . rest += line ;
91- }
92- }
93-
94- handleMeta ( line : string ) {
95- if ( line === "[" ) {
96- this . state = "meta-array" ;
97- }
98- }
99-
100- handleMetaArray ( line : string ) {
101- if ( line . match ( / ^ } , ? $ / ) ) {
102- const columnStr = this . objBuffer + "}" ;
103- const column = JSONbig . parse ( columnStr ) ;
104- const normalizedColumn = normalizeColumn ( column ) ;
105- this . columns . push ( normalizedColumn ) ;
106- this . objBuffer = undefined ;
107- } else if ( line === "{" ) {
108- this . objBuffer = line ;
109- } else if ( line . match ( / ^ ] ,? $ / ) ) {
110- this . emitter . emit ( "metadata" , this . columns ) ;
111- this . state = "rootKeys" ;
112- } else {
113- this . objBuffer += line ;
114- }
115- }
116-
117- handleDataArray ( line : string ) {
118- if ( line . match ( / ^ [ \] } ] , ? $ / ) && this . objBuffer ) {
119- const rowStr = this . objBuffer + line [ 0 ] ;
120- const row = this . rowParser ( rowStr , false ) ;
121- this . rows . push ( row ) ;
122- this . objBuffer = undefined ;
123- } else if ( line === "{" || line === "[" ) {
124- this . objBuffer = line ;
125- } else if ( line . match ( / ^ ] ,? $ / ) ) {
126- this . state = "rootKeys" ;
127- } else if ( this . objBuffer === undefined ) {
128- const isLastRow = line [ line . length - 1 ] !== "," ;
129- const rowStr = isLastRow ? line : line . substr ( 0 , line . length - 1 ) ;
130- const row = this . rowParser ( rowStr , isLastRow ) ;
131- this . rows . push ( row ) ;
132- } else {
133- this . objBuffer += line ;
134- }
135- }
136-
137- handleData ( line : string ) {
138- if ( line === "[" ) {
139- this . state = "data-array" ;
140- }
141- }
142-
143- handleQuery ( line : string ) {
144- if ( line === "{" ) {
145- this . state = "query-object" ;
146- }
147- }
148-
149- handleQueryObject ( line : string ) {
150- if ( line . match ( / ^ } , ? $ / ) ) {
151- const queryStr = this . objBuffer + "}" ;
152- const query = JSONbig . parse ( queryStr ) ;
153- this . objBuffer = undefined ;
154- this . state = "rootKeys" ;
155- } else {
156- this . objBuffer += line ;
157- }
158- }
62+ } ;
15963
16064 processLine ( line : string ) {
161- line = line . trim ( ) ;
162-
163- if ( ! line . length ) {
164- return ;
165- }
65+ this . jsonParser . processLine ( line ) ;
66+ }
16667
167- if ( this . state === null ) {
168- this . handleRoot ( line ) ;
169- } else if ( this . state === "rootKeys" ) {
170- this . handleRootKeys ( line ) ;
171- } else if ( this . state === "meta" ) {
172- this . handleMeta ( line ) ;
173- } else if ( this . state === "data" ) {
174- this . handleData ( line ) ;
175- } else if ( this . state === "meta-array" ) {
176- this . handleMetaArray ( line ) ;
177- } else if ( this . state === "data-array" ) {
178- this . handleDataArray ( line ) ;
179- } else if ( this . state === "query" ) {
180- this . handleQuery ( line ) ;
181- } else if ( this . state === "query-object" ) {
182- this . handleQueryObject ( line ) ;
183- }
68+ getResult ( index : number ) {
69+ return this . jsonParser . results [ index ] ;
18470 }
18571}
0 commit comments