@@ -12,8 +12,8 @@ import {
1212} from 'https://deno.land/std@0.133.0/testing/asserts.ts'
1313const { test } = Deno ;
1414
15- import { jsonStringifyGenerator } from '../json-stringify.ts'
16- import { JSONParseStream , JSONParseWritable } from '../json-parse-stream.ts'
15+ import { jsonStringifyGenerator , jsonStringifyStream } from '../json-stringify.ts'
16+ import { JSONParseStream , JSONParseNexus } from '../json-parse-stream.ts'
1717
1818async function consume ( stream : ReadableStream ) {
1919 const reader = stream . getReader ( ) ;
@@ -75,65 +75,102 @@ const aJoin = async (iter: AsyncIterable<string>, separator = '') => {
7575}
7676
7777test ( 'promise value' , async ( ) => {
78- const parseStream = new JSONParseWritable ( )
78+ const parseStream = new JSONParseNexus ( )
7979 const actual = {
80- type : parseStream . promise ( '$.type' ) ,
81- data : parseStream . iterable ( '$.data .*' )
80+ type : parseStream . lazy ( '$.type' ) ,
81+ items : parseStream . iterable ( '$.items .*' )
8282 }
83- const expected = JSON . stringify ( { type : 'foo' , data : [ { a : 1 } , { b : 2 } , { c : 3 } ] } )
84- const done = new Response ( expected ) . body !
85- . pipeTo ( parseStream )
83+ const expected = JSON . stringify ( { type : 'foo' , items : [ { a : 1 } , { a : 2 } , { a : 3 } ] } )
84+ new Response ( expected ) . body !
85+ . pipeThrough ( parseStream )
8686
8787 const actualString = await aJoin ( jsonStringifyGenerator ( actual ) )
8888 assertEquals ( actualString , expected )
89- await done ;
9089} )
9190
9291test ( 'promise value II' , async ( ) => {
93- const parseStream = new JSONParseWritable ( )
92+ const parseStream = new JSONParseNexus ( )
9493 const actual = {
95- type : parseStream . promise ( '$.type' ) ,
96- data : parseStream . stream ( '$.data .*' )
94+ type : parseStream . lazy ( '$.type' ) ,
95+ items : parseStream . stream ( '$.items .*' )
9796 }
98- const expected = JSON . stringify ( { type : 'foo' , data : [ { a : 1 } , { b : 2 } , { c : 3 } ] } )
99- const done = new Response ( expected ) . body !
100- . pipeTo ( parseStream )
97+ const expected = JSON . stringify ( { type : 'foo' , items : [ { a : 1 } , { a : 2 } , { a : 3 } ] } )
98+ new Response ( expected ) . body !
99+ . pipeThrough ( parseStream )
101100
102101 const actualString = await aJoin ( jsonStringifyGenerator ( actual ) )
103102 assertEquals ( actualString , expected )
104- await done ;
105103} )
106104
107105test ( 'promise value III' , async ( ) => {
108- const parseStream = new JSONParseWritable ( )
106+ const parseStream = new JSONParseNexus ( )
109107 const actual = {
110- type : parseStream . promise ( '$.type' ) ,
111- data : parseStream . iterable ( '$.data .*' )
108+ type : parseStream . lazy ( '$.type' ) ,
109+ items : parseStream . iterable ( '$.items .*' )
112110 }
113- const expected = JSON . stringify ( { type : 'foo' , data : [ { a : 1 } , { b : 2 } , { c : 3 } ] } )
114- new Response ( expected ) . body ! . pipeTo ( parseStream )
111+ const expected = JSON . stringify ( { type : 'foo' , items : [ { a : 1 } , { a : 2 } , { a : 3 } ] } )
112+ new Response ( expected ) . body ! . pipeThrough ( parseStream )
115113
116114 const actualString = await aJoin ( jsonStringifyGenerator ( actual ) )
117115 assertEquals ( actualString , expected )
118116} )
119117
120118test ( 'promise value IV' , async ( ) => {
121- const parseStream = new JSONParseWritable ( )
119+ const parseStream = new JSONParseNexus ( )
122120 const actual = {
123- type : parseStream . promise ( '$.type' ) ,
124- data : parseStream . stream ( '$.data .*' )
121+ type : parseStream . lazy ( '$.type' ) ,
122+ items : parseStream . stream ( '$.items .*' )
125123 }
126- const expected = JSON . stringify ( { type : 'foo' , data : [ { a : 1 } , { b : 2 } , { c : 3 } ] } )
127- new Response ( expected ) . body ! . pipeTo ( parseStream )
124+ const expected = JSON . stringify ( { type : 'foo' , items : [ { a : 1 } , { a : 2 } , { a : 3 } ] } )
125+ new Response ( expected ) . body ! . pipeThrough ( parseStream )
128126
129127 const actualString = await aJoin ( jsonStringifyGenerator ( actual ) )
130128 assertEquals ( actualString , expected )
131129} )
132130
133- test ( 'read only until first value' , async ( ) => {
134- const parseStream = new JSONParseWritable ( )
135- const type = parseStream . promise ( '$.type' ) ;
136- const expected = JSON . stringify ( { type : 'foo' , data : [ { a : 1 } , { b : 2 } , { c : 3 } ] } )
137- new Response ( expected ) . body ! . pipeTo ( parseStream )
131+ async function * asyncGen < T > ( xs : T [ ] ) {
132+ for ( const x of xs ) yield x
133+ }
134+
135+ const json1 = { filler : asyncGen ( [ '__' , '__' , '__' ] ) , type : 'foo' , items : asyncGen ( [ { a : 1 } , { a : 2 } , { a : 3 } , { a : 4 } , { a : 5 } ] ) }
136+ test ( 'read only until first value eager' , async ( ) => {
137+ const parseStream = new JSONParseNexus ( )
138+ const type = parseStream . eager < string > ( '$.type' ) ;
139+ jsonStringifyStream ( json1 ) . pipeThrough ( parseStream )
140+
138141 assertEquals ( await type , 'foo' )
142+ } )
143+ const timeout = ( n ?: number ) => new Promise ( r => setTimeout ( r , n ) )
144+
145+ test ( 'read only until first value lazy' , async ( ) => {
146+ const parseStream = new JSONParseNexus ( )
147+ const type = parseStream . promise < string > ( '$.type' ) ;
148+ jsonStringifyStream ( json1 ) . pipeThrough ( parseStream )
149+
150+ assertEquals ( await Promise . race ( [ type , timeout ( 10 ) . then ( ( ) => 'x' ) ] ) , 'x' )
151+ } )
152+
153+ test ( 'read only until first value lazy II' , async ( ) => {
154+ const parseStream = new JSONParseNexus ( )
155+ const type = parseStream . promise < string > ( '$.type' ) ;
156+ const _items = parseStream . stream ( '$.items.*' )
157+ jsonStringifyStream ( json1 ) . pipeThrough ( parseStream )
158+
159+ assertEquals ( await Promise . race ( [ type , timeout ( 10 ) . then ( ( ) => 'x' ) ] ) , 'x' )
160+ } )
161+
162+ test ( 'read only until first value lazy+pull' , async ( ) => {
163+ const parseStream = new JSONParseNexus ( )
164+ const type = parseStream . promise < string > ( '$.type' ) ;
165+ jsonStringifyStream ( json1 ) . pipeThrough ( parseStream )
166+
167+ assertEquals ( await type . pull ( ) , 'foo' )
168+ } )
169+
170+ test ( 'writable locked?' , async ( ) => {
171+ const parseStream = new JSONParseNexus ( )
172+ const filler = parseStream . stream < string > ( '$.filler.*' ) ;
173+ const items = parseStream . stream < string > ( '$.items.*' ) ;
174+ // jsonStringifyStream(json1).pipeThrough(parseStream)
175+ // assertEquals(await type.pull(), 'foo')
139176} )
0 commit comments