Skip to content

Commit

Permalink
fix(formats): Fixing BSP header reading from blob
Browse files Browse the repository at this point in the history
  • Loading branch information
GordiNoki authored and tsa96 committed Jan 11, 2024
1 parent 689b67a commit 2b23ab3
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions libs/formats/bsp/src/bsp-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,17 @@ export class BspHeader {

// Node/browsers will almost certainly read the 1036 bytes we need in one
// go, but just to be safe, this can read multiple.
await reader.read().then(function readHeader({ done, value }) {
if (done && value.length < headerIndex) {
while (headerIndex < headerBytes.length) {
const { done, value } = await reader.read();
const leftToRead = headerBytes.length - headerIndex;

if (!value || (done && value.length < leftToRead)) {
throw new BspReadError('Invalid BSP file');
}

const leftToRead = headerBytes.length - headerIndex;
headerBytes.set(value.slice(0, leftToRead), headerIndex);

headerIndex = value.length;
if (headerIndex < headerBytes.length) {
reader.read().then(readHeader);
}
});
}

// Don't need stream anymore once header is read
void reader.cancel();
Expand Down

0 comments on commit 2b23ab3

Please sign in to comment.