• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visgl / loaders.gl / 20382848403

19 Dec 2025 09:20PM UTC coverage: 35.219% (+0.1%) from 35.095%
20382848403

push

github

web-flow
feat: Upgrade to handle ArrayBufferLike (#3271)

1190 of 2002 branches covered (59.44%)

Branch coverage included in aggregate %.

157 of 269 new or added lines in 41 files covered. (58.36%)

3 existing lines in 3 files now uncovered.

37536 of 107957 relevant lines covered (34.77%)

0.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.62
/modules/zip/src/parse-zip/readable-file-utils.ts
1
// loaders.gl
1✔
2
// SPDX-License-Identifier: MIT
1✔
3
// Copyright (c) vis.gl contributors
1✔
4

1✔
5
import {copyToArrayBuffer, type ReadableFile, type Stat} from '@loaders.gl/loader-utils';
1✔
6

1✔
7
function toBigInt(value: number | bigint): bigint {
×
8
  return typeof value === 'bigint' ? value : BigInt(value);
×
9
}
×
10

1✔
11
function toNumber(value: number | bigint): number {
×
12
  const numberValue = Number(value);
×
13
  if (!Number.isFinite(numberValue)) {
×
14
    throw new Error('Offset is out of bounds');
×
15
  }
×
16
  return numberValue;
×
17
}
×
18

1✔
NEW
19
function normalizeOffset(offset: number, size: number): number {
×
NEW
20
  if (offset < 0) {
×
NEW
21
    return Math.max(size + offset, 0);
×
NEW
22
  }
×
NEW
23
  return Math.min(offset, size);
×
NEW
24
}
×
25

1✔
26
/**
1✔
27
 * Read a byte range from a readable file.
1✔
28
 * @param file readable file handle
1✔
29
 * @param start inclusive start offset
1✔
30
 * @param end exclusive end offset
1✔
31
 * @returns requested slice
1✔
32
 */
1✔
33
export async function readRange(
×
34
  file: ReadableFile,
×
35
  start: number | bigint,
×
36
  end: number | bigint
×
37
): Promise<ArrayBuffer> {
×
38
  const startOffset = toBigInt(start);
×
39
  const endOffset = toBigInt(end);
×
40
  const length = endOffset - startOffset;
×
41
  if (length < 0) {
×
42
    throw new Error('Invalid range requested');
×
43
  }
×
44
  return await file.read(startOffset, toNumber(length));
×
45
}
×
46

1✔
47
export async function readDataView(
×
48
  file: ReadableFile,
×
49
  start: number | bigint,
×
50
  end: number | bigint
×
51
): Promise<DataView> {
×
52
  const arrayBuffer = await readRange(file, start, end);
×
53
  return new DataView(arrayBuffer);
×
54
}
×
55

1✔
56
export async function readUint8(file: ReadableFile, offset: number | bigint): Promise<number> {
×
57
  const dataView = await readDataView(file, offset, toBigInt(offset) + 1n);
×
58
  return dataView.getUint8(0);
×
59
}
×
60

1✔
61
export async function readUint16(file: ReadableFile, offset: number | bigint): Promise<number> {
×
62
  const dataView = await readDataView(file, offset, toBigInt(offset) + 2n);
×
63
  return dataView.getUint16(0, true);
×
64
}
×
65

1✔
66
export async function readUint32(file: ReadableFile, offset: number | bigint): Promise<number> {
×
67
  const dataView = await readDataView(file, offset, toBigInt(offset) + 4n);
×
68
  return dataView.getUint32(0, true);
×
69
}
×
70

1✔
71
export async function readBigUint64(file: ReadableFile, offset: number | bigint): Promise<bigint> {
×
72
  const dataView = await readDataView(file, offset, toBigInt(offset) + 8n);
×
73
  return dataView.getBigUint64(0, true);
×
74
}
×
75

1✔
76
/**
1✔
77
 * Resolve the size of a readable file.
1✔
78
 * @param file readable file handle
1✔
79
 * @returns file size as bigint
1✔
80
 */
1✔
81
export async function getReadableFileSize(file: ReadableFile): Promise<bigint> {
×
82
  if (file.bigsize > 0n) {
×
83
    return file.bigsize;
×
84
  }
×
85
  if (file.size > 0) {
×
86
    return BigInt(file.size);
×
87
  }
×
88
  if (file.stat) {
×
89
    const stats: Stat = await file.stat();
×
90
    if (stats?.bigsize !== undefined) {
×
91
      return stats.bigsize;
×
92
    }
×
93
    if (stats?.size !== undefined) {
×
94
      return BigInt(stats.size);
×
95
    }
×
96
  }
×
97
  return 0n;
×
98
}
×
99

1✔
100
/**
1✔
101
 * Minimal readable file backed by a DataView.
1✔
102
 */
1✔
103
export class DataViewReadableFile implements ReadableFile {
1✔
104
  readonly handle: DataView;
1!
105
  readonly size: number;
×
106
  readonly bigsize: bigint;
×
107
  readonly url: string;
×
108

1✔
109
  constructor(dataView: DataView, url: string = '') {
1✔
110
    this.handle = dataView;
×
111
    this.size = dataView.byteLength;
×
112
    this.bigsize = BigInt(dataView.byteLength);
×
113
    this.url = url;
×
114
  }
×
115

1✔
116
  async close(): Promise<void> {}
1✔
117

1✔
118
  async stat(): Promise<Stat> {
1✔
119
    return {size: this.size, bigsize: this.bigsize, isDirectory: false};
×
120
  }
×
121

1✔
122
  async read(start: number | bigint = 0, length?: number): Promise<ArrayBuffer> {
1✔
123
    const offset = toNumber(start);
×
124
    const end = length ? offset + length : this.size;
×
NEW
125
    const normalizedStart = normalizeOffset(offset, this.size);
×
NEW
126
    const normalizedEnd = normalizeOffset(end, this.size);
×
NEW
127
    const clampedEnd = Math.max(normalizedEnd, normalizedStart);
×
NEW
128
    const lengthToRead = clampedEnd - normalizedStart;
×
NEW
129
    if (lengthToRead <= 0) {
×
NEW
130
      return new ArrayBuffer(0);
×
NEW
131
    }
×
NEW
132
    return copyToArrayBuffer(this.handle.buffer, normalizedStart, lengthToRead);
×
UNCOV
133
  }
×
134
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc