Skip to content

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

@DanielSWolf

Description

@DanielSWolf

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();
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions