Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'buff' of undefined #7

Open
DanielSWolf opened this issue Nov 6, 2021 · 0 comments
Open

TypeError: Cannot read property 'buff' of undefined #7

DanielSWolf opened this issue Nov 6, 2021 · 0 comments

Comments

@DanielSWolf
Copy link

I'm using fastfile to parse a large .tar file. At first, everything seems to work, but some way in, the following error is thrown:

TypeError: Cannot read property 'buff' of undefined
   at FastFile.readToBuffer (C:\dev\rhubarb-2-poc\node_modules\fastfile\build\main.cjs:316:58)       
   at async readTarFile (C:\dev\rhubarb-2-poc\src\utils\read-tar-file.ts:23:7)

Here's my code. Given that the error only occurs after quite some iterations, I don't have a better way of reproducing the problem.

import { readExisting } from 'fastfile';
import { utf8ArrayToString } from './utf8-array-to-string';

interface TarFileEntry {
  filePath: string;
  fileSize: number;
  getFileBytes(): Promise<Uint8Array>;
}

export async function* readTarFile(
  tarFilePath: string,
): AsyncIterableIterator<TarFileEntry> {
  const file = await readExisting(tarFilePath);

  try {
    const blockSize = 512;
    const headerBytes = new Uint8Array(blockSize);
    const filePathBytes = new Uint8Array(headerBytes.buffer, 0, 100);
    const fileSizeBytes = new Uint8Array(headerBytes.buffer, 124, 12);

    let zeroBlockCount = 0;
    while (true) {
      await file.readToBuffer(headerBytes, 0, blockSize); // THIS LINE THROWS AFTER SOME ITERATIONS
      if (headerBytes[0] === 0) {
        zeroBlockCount++;
        // Two consecutive zero blocks indicate end of file
        if (zeroBlockCount === 2) return;
      } else {
        zeroBlockCount = 0;
      }

      const filePath = utf8ArrayToString(filePathBytes);
      const fileSize = parseInt(utf8ArrayToString(fileSizeBytes), 8);
      if (fileSize === 0) continue; // directory

      const bufferSize = Math.ceil(fileSize / blockSize) * blockSize;
      const bufferPosition = file.pos;
      console.log(filePath);
      yield {
        filePath,
        fileSize,
        async getFileBytes() {
          if (file.pos !== bufferPosition) {
            throw new Error('Cannot read file once iteration has continued.');
          }

          const bufferBytes = new Uint8Array(bufferSize);
          await file.readToBuffer(bufferBytes, 0, bufferSize);
          return new Uint8Array(bufferBytes.buffer, 0, fileSize);
        },
      };

      file.pos = bufferPosition + bufferSize;
    }
  } finally {
    await file.close();
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant