17
17
import { expect } from 'chai' ;
18
18
import {
19
19
BundleReader ,
20
- SizedBundleElement ,
21
- toReadableStream
20
+ SizedBundleElement
22
21
} from '../../../src/util/bundle_reader' ;
23
- import { isNode } from '../../util/test_platform' ;
24
22
import { BundleElement } from '../../../src/protos/firestore_bundle_proto' ;
23
+ import { isNode } from '../../util/test_platform' ;
24
+ import {
25
+ PlatformSupport ,
26
+ toByteStreamReader
27
+ } from '../../../src/platform/platform' ;
25
28
26
- function readableStreamFromString (
29
+ /**
30
+ * Create a `ReadableStream` from a string.
31
+ *
32
+ * @param content: Bundle in string.
33
+ * @param bytesPerRead: How many bytes to read from the underlying buffer from
34
+ * each read through the stream.
35
+ */
36
+ export function readableStreamFromString (
27
37
content : string ,
28
- bytesPerRead : number
29
- ) : ReadableStream {
30
- return toReadableStream ( new TextEncoder ( ) . encode ( content ) , bytesPerRead ) ;
38
+ bytesPerRead = 10240
39
+ ) : ReadableStream < Uint8Array | ArrayBuffer > {
40
+ let readFrom = 0 ;
41
+ const data = new TextEncoder ( ) . encode ( content ) ;
42
+ return new ReadableStream ( {
43
+ start ( controller ) { } ,
44
+ async pull ( controller ) : Promise < void > {
45
+ controller . enqueue ( data . slice ( readFrom , readFrom + bytesPerRead ) ) ;
46
+ readFrom += bytesPerRead ;
47
+ if ( readFrom >= data . byteLength ) {
48
+ controller . close ( ) ;
49
+ }
50
+ }
51
+ } ) ;
31
52
}
32
53
33
54
function lengthPrefixedString ( o : { } ) : string {
@@ -60,14 +81,30 @@ describe('readableStreamFromString()', () => {
60
81
} ) ;
61
82
} ) ;
62
83
63
- describe ( 'Bundle ' , ( ) => {
84
+ describe . only ( 'Bundle ' , ( ) => {
64
85
genericBundleReadingTests ( 1 ) ;
65
86
genericBundleReadingTests ( 4 ) ;
66
87
genericBundleReadingTests ( 64 ) ;
67
88
genericBundleReadingTests ( 1024 ) ;
68
89
} ) ;
69
90
70
91
function genericBundleReadingTests ( bytesPerRead : number ) : void {
92
+ if ( isNode ( ) ) {
93
+ const platform = PlatformSupport . getPlatform ( ) ;
94
+ platform . toByteStreamReader = source =>
95
+ toByteStreamReader ( source as Uint8Array , bytesPerRead ) ;
96
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
97
+ ( PlatformSupport as any ) . _forceSetPlatform ( platform ) ;
98
+ }
99
+
100
+ function bundleFromString ( s : string ) : BundleReader {
101
+ if ( isNode ( ) ) {
102
+ return new BundleReader ( encoder . encode ( s ) ) ;
103
+ } else {
104
+ return new BundleReader ( readableStreamFromString ( s , bytesPerRead ) ) ;
105
+ }
106
+ }
107
+
71
108
const encoder = new TextEncoder ( ) ;
72
109
// Setting up test data.
73
110
const meta : BundleElement = {
@@ -197,8 +234,7 @@ function genericBundleReadingTests(bytesPerRead: number): void {
197
234
bytesPerRead : number ,
198
235
validMeta = false
199
236
) : Promise < void > {
200
- const bundleStream = readableStreamFromString ( bundleString , bytesPerRead ) ;
201
- const bundle = new BundleReader ( bundleStream ) ;
237
+ const bundle = bundleFromString ( bundleString ) ;
202
238
203
239
if ( ! validMeta ) {
204
240
await expect ( await bundle . getMetadata ( ) ) . should . be . rejected ;
@@ -210,15 +246,13 @@ function genericBundleReadingTests(bytesPerRead: number): void {
210
246
}
211
247
212
248
it ( 'reads with query and doc with bytesPerRead ' + bytesPerRead , async ( ) => {
213
- const bundleStream = readableStreamFromString (
249
+ const bundle = bundleFromString (
214
250
metaString +
215
251
limitQueryString +
216
252
limitToLastQueryString +
217
253
doc1MetaString +
218
- doc1String ,
219
- bytesPerRead
254
+ doc1String
220
255
) ;
221
- const bundle = new BundleReader ( bundleStream ) ;
222
256
223
257
expect ( await bundle . getMetadata ( ) ) . to . deep . equal ( meta . metadata ) ;
224
258
@@ -233,16 +267,14 @@ function genericBundleReadingTests(bytesPerRead: number): void {
233
267
it (
234
268
'reads with unexpected orders with bytesPerRead ' + bytesPerRead ,
235
269
async ( ) => {
236
- const bundleStream = readableStreamFromString (
270
+ const bundle = bundleFromString (
237
271
metaString +
238
272
doc1MetaString +
239
273
doc1String +
240
274
limitQueryString +
241
275
doc2MetaString +
242
- doc2String ,
243
- bytesPerRead
276
+ doc2String
244
277
) ;
245
- const bundle = new BundleReader ( bundleStream ) ;
246
278
247
279
const actual = await getAllElements ( bundle ) ;
248
280
expect ( actual . length ) . to . equal ( 5 ) ;
@@ -260,11 +292,7 @@ function genericBundleReadingTests(bytesPerRead: number): void {
260
292
it (
261
293
'reads without named query with bytesPerRead ' + bytesPerRead ,
262
294
async ( ) => {
263
- const bundleStream = readableStreamFromString (
264
- metaString + doc1MetaString + doc1String ,
265
- bytesPerRead
266
- ) ;
267
- const bundle = new BundleReader ( bundleStream ) ;
295
+ const bundle = bundleFromString ( metaString + doc1MetaString + doc1String ) ;
268
296
269
297
expect ( await bundle . getMetadata ( ) ) . to . deep . equal ( meta . metadata ) ;
270
298
@@ -276,11 +304,9 @@ function genericBundleReadingTests(bytesPerRead: number): void {
276
304
) ;
277
305
278
306
it ( 'reads with deleted doc with bytesPerRead ' + bytesPerRead , async ( ) => {
279
- const bundleStream = readableStreamFromString (
280
- metaString + noDocMetaString + doc1MetaString + doc1String ,
281
- bytesPerRead
307
+ const bundle = bundleFromString (
308
+ metaString + noDocMetaString + doc1MetaString + doc1String
282
309
) ;
283
- const bundle = new BundleReader ( bundleStream ) ;
284
310
285
311
expect ( await bundle . getMetadata ( ) ) . to . deep . equal ( meta . metadata ) ;
286
312
@@ -294,8 +320,7 @@ function genericBundleReadingTests(bytesPerRead: number): void {
294
320
it (
295
321
'reads without documents or query with bytesPerRead ' + bytesPerRead ,
296
322
async ( ) => {
297
- const bundleStream = readableStreamFromString ( metaString , bytesPerRead ) ;
298
- const bundle = new BundleReader ( bundleStream ) ;
323
+ const bundle = bundleFromString ( metaString ) ;
299
324
300
325
expect ( await bundle . getMetadata ( ) ) . to . deep . equal ( meta . metadata ) ;
301
326
0 commit comments