Skip to content

Commit d85f62b

Browse files
committed
bit more documentation
1 parent d0ff103 commit d85f62b

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/lazyFile.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type LazyFileConfig = {
1919
maxReadHeads?: number;
2020
/** max read speed for sequential access. default: 5 MiB */
2121
maxReadSpeed?: number;
22+
/** if true, log all read pages into the `readPages` field for debugging */
2223
logPageReads?: boolean;
2324
};
2425
export type PageReadLog = {
@@ -30,7 +31,6 @@ export type PageReadLog = {
3031
};
3132

3233
type ReadHead = { startChunk: number; speed: number };
33-
// Lazy chunked Uint8Array (implements get and length from Uint8Array)
3434
export class LazyUint8Array {
3535
private serverChecked = false;
3636
private readonly chunks: Uint8Array[] = []; // Loaded chunks. Index is the chunk number
@@ -39,7 +39,7 @@ export class LazyUint8Array {
3939
readPages: PageReadLog[] = [];
4040
private _length?: number;
4141

42-
// LRU list of read heds, max length = READ_HEADS. first is most recently used
42+
// LRU list of read heds, max length = maxReadHeads. first is most recently used
4343
private readonly readHeads: ReadHead[] = [];
4444
private readonly _chunkSize: number;
4545
private readonly rangeMapper: RangeMapper;
@@ -59,14 +59,19 @@ export class LazyUint8Array {
5959
this._length = config.fileLength;
6060
}
6161
}
62+
/**
63+
* efficiently copy the range [start, start + length) from the http file into the
64+
* output buffer at position [outOffset, outOffest + length)
65+
* reads from cache or synchronously fetches via HTTP if needed
66+
*/
6267
copyInto(
6368
buffer: Uint8Array,
6469
outOffset: number,
65-
_length: number,
70+
length: number,
6671
start: number
6772
): number {
6873
if (start >= this.length) return 0;
69-
const length = Math.min(this.length - start, _length);
74+
length = Math.min(this.length - start, length);
7075
const end = start + length;
7176
let i = 0;
7277
while (i < length) {
@@ -85,15 +90,7 @@ export class LazyUint8Array {
8590
return length;
8691
}
8792

88-
get(idx: number) {
89-
if (idx > this.length - 1 || idx < 0) {
90-
return undefined;
91-
}
92-
var chunkOffset = idx % this.chunkSize;
93-
var chunkNum = (idx / this.chunkSize) | 0;
94-
return this.getChunk(chunkNum)[chunkOffset];
95-
}
96-
lastGet = -1;
93+
private lastGet = -1;
9794
/* find the best matching existing read head to get the given chunk or create a new one */
9895
private moveReadHead(wantedChunkNum: number): ReadHead {
9996
for (const [i, head] of this.readHeads.entries()) {
@@ -164,7 +161,7 @@ export class LazyUint8Array {
164161
return this.chunks[wantedChunkNum];
165162
}
166163
/** verify the server supports range requests and find out file length */
167-
checkServer() {
164+
private checkServer() {
168165
var xhr = new XMLHttpRequest();
169166
const url = this.rangeMapper(0, 0).url;
170167
xhr.open("HEAD", url, false);
@@ -206,7 +203,9 @@ export class LazyUint8Array {
206203
}
207204
private doXHR(absoluteFrom: number, absoluteTo: number) {
208205
console.log(
209-
`[xhr of size ${(absoluteTo + 1 - absoluteFrom) / 1024} KiB @ ${absoluteFrom / 1024} KiB]`
206+
`[xhr of size ${(absoluteTo + 1 - absoluteFrom) / 1024} KiB @ ${
207+
absoluteFrom / 1024
208+
} KiB]`
210209
);
211210
this.totalFetchedBytes += absoluteTo - absoluteFrom;
212211
this.totalRequests++;

0 commit comments

Comments
 (0)