Skip to content

Commit 4313e51

Browse files
committed
Added more comments, and more tests for Node.
1 parent 4cbe608 commit 4313e51

File tree

6 files changed

+69
-10
lines changed

6 files changed

+69
-10
lines changed

packages/firestore/src/platform/platform.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export interface Platform {
5050
*/
5151
randomBytes(nBytes: number): Uint8Array;
5252

53+
/**
54+
* Builds a `ByteStreamReader` from a data source.
55+
*/
5356
toByteStreamReader(source: unknown): ByteStreamReader;
5457

5558
/** The Platform's 'window' implementation or null if not available. */
@@ -62,16 +65,31 @@ export interface Platform {
6265
readonly base64Available: boolean;
6366
}
6467

68+
/**
69+
* An interface compatible with Web's ReadableStream.getReader() return type.
70+
*
71+
* This can be used as an abstraction to mimic `ReadableStream` where it is not
72+
* available.
73+
*/
6574
export interface ByteStreamReader {
6675
read(): Promise<ByteStreamReadResult>;
6776
cancel(reason?: string): Promise<void>;
6877
}
6978

79+
/**
80+
* An interface compatible with ReadableStreamReadResult<UInt8Array>.
81+
*/
7082
export interface ByteStreamReadResult {
7183
done: boolean;
72-
value: Uint8Array;
84+
value?: Uint8Array;
7385
}
7486

87+
/**
88+
* Builds a `ByteStreamReader` from a UInt8Array.
89+
* @param source The data source to use.
90+
* @param bytesPerRead How many bytes each `read()` from the returned reader
91+
* will read.
92+
*/
7593
export function toByteStreamReader(
7694
source: Uint8Array,
7795
bytesPerRead = 10240
@@ -88,7 +106,7 @@ export function toByteStreamReader(
88106
return result;
89107
}
90108

91-
return { value: new Uint8Array(), done: true };
109+
return { value: undefined, done: true };
92110
}
93111

94112
async cancel(reason?: string): Promise<void> {}
@@ -109,6 +127,9 @@ export class PlatformSupport {
109127
PlatformSupport.platform = platform;
110128
}
111129

130+
/**
131+
* Forcing to set the platform instance, testing only!
132+
*/
112133
private static _forceSetPlatform(platform: Platform): void {
113134
PlatformSupport.platform = platform;
114135
}

packages/firestore/src/platform_browser/browser_platform.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ export class BrowserPlatform implements Platform {
9999
return bytes;
100100
}
101101

102-
toByteStreamReader(source: unknown): ByteStreamReader {
102+
/**
103+
* On web, a `ReadableStream` is wrapped around by a `ByteStreamReader`.
104+
*/
105+
toByteStreamReader(
106+
source: Uint8Array | ArrayBuffer | ReadableStream<Uint8Array>
107+
): ByteStreamReader {
103108
if (source instanceof Uint8Array) {
104109
return toByteStreamReader(source);
105110
}

packages/firestore/src/platform_node/node_platform.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ export class NodePlatform implements Platform {
8888
return randomBytes(nBytes);
8989
}
9090

91+
/**
92+
* On Node, only supported data source is a `Uint8Array` for now.
93+
*/
9194
toByteStreamReader(source: unknown): ByteStreamReader {
9295
if (source instanceof Uint8Array) {
9396
return toByteStreamReader(source);

packages/firestore/src/util/bundle_reader.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from '../protos/firestore_bundle_proto';
2222
import { Deferred } from './promise';
2323
import { ByteStreamReader, PlatformSupport } from '../platform/platform';
24+
import { debugAssert } from './assert';
2425

2526
/**
2627
* A complete element in the bundle stream, together with the byte length it
@@ -204,11 +205,12 @@ export class BundleReader {
204205
private async pullMoreDataToBuffer(): Promise<boolean> {
205206
const result = await this.reader.read();
206207
if (!result.done) {
208+
debugAssert(!!result.value, 'Read undefined when "done" is false.');
207209
const newBuffer = new Uint8Array(
208-
this.buffer.length + result.value.length
210+
this.buffer.length + result.value!.length
209211
);
210212
newBuffer.set(this.buffer);
211-
newBuffer.set(result.value, this.buffer.length);
213+
newBuffer.set(result.value!, this.buffer.length);
212214
this.buffer = newBuffer;
213215
}
214216
return result.done;

packages/firestore/test/unit/platform/platform.test.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,34 @@
1616
*/
1717

1818
import { expect } from 'chai';
19-
import { PlatformSupport } from '../../../src/platform/platform';
19+
import {
20+
PlatformSupport,
21+
toByteStreamReader
22+
} from '../../../src/platform/platform';
2023

2124
describe('Platform', () => {
2225
it('can load the platform at runtime', () => {
2326
expect(PlatformSupport.getPlatform()).to.exist;
2427
});
28+
29+
it('toByteStreamReader() steps underlying data', async () => {
30+
const encoder = new TextEncoder();
31+
const r = toByteStreamReader(encoder.encode('0123456789'), 4);
32+
33+
let result = await r.read();
34+
expect(result.value).to.deep.equal(encoder.encode('0123'));
35+
expect(result.done).to.be.false;
36+
37+
result = await r.read();
38+
expect(result.value).to.deep.equal(encoder.encode('4567'));
39+
expect(result.done).to.be.false;
40+
41+
result = await r.read();
42+
expect(result.value).to.deep.equal(encoder.encode('89'));
43+
expect(result.done).to.be.false;
44+
45+
result = await r.read();
46+
expect(result.value).to.be.undefined;
47+
expect(result.done).to.be.true;
48+
});
2549
});

packages/firestore/test/unit/util/bundle.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,14 @@ function lengthPrefixedString(o: {}): string {
5757
return `${l}${str}`;
5858
}
5959

60-
describe('readableStreamFromString()', () => {
60+
// Testing readableStreamFromString() is working as expected.
61+
// eslint-disable-next-line no-restricted-properties
62+
(isNode() ? describe.skip : describe)('readableStreamFromString()', () => {
6163
it('returns stepping readable stream', async () => {
6264
const encoder = new TextEncoder();
63-
const s = readableStreamFromString('0123456789', 4);
64-
const r = s.getReader();
65+
const r = PlatformSupport.getPlatform().toByteStreamReader(
66+
readableStreamFromString('0123456789', 4)
67+
);
6568

6669
let result = await r.read();
6770
expect(result.value).to.deep.equal(encoder.encode('0123'));
@@ -81,14 +84,15 @@ describe('readableStreamFromString()', () => {
8184
});
8285
});
8386

84-
describe.only('Bundle ', () => {
87+
describe('Bundle ', () => {
8588
genericBundleReadingTests(1);
8689
genericBundleReadingTests(4);
8790
genericBundleReadingTests(64);
8891
genericBundleReadingTests(1024);
8992
});
9093

9194
function genericBundleReadingTests(bytesPerRead: number): void {
95+
// On Node, we need to override `bytesPerRead` from it's platform's `toByteStreamReader` call.
9296
if (isNode()) {
9397
const platform = PlatformSupport.getPlatform();
9498
platform.toByteStreamReader = source =>

0 commit comments

Comments
 (0)