notes.dt.in.th

Here’s how I read a tar file in-memory with modern Node.js APIs:

import tar from 'tar'
import stream from 'stream/promises'

await stream.pipeline(
  // Assuming data arrives by stdin
  process.stdin,

  new tar.Parse({
    // Only process file entries, skip non-files (e.g. directories).
    filter: (path, entry) => entry.type === 'File',

    onentry: async (entry) => {
      let size = 0
      // Consume the file.
      // Note that the onentry callback **must** consume the file. Otherwise,
      // `tar.Parse` will not proceed to the next file and execution will stop.
      // If you don’t want to consume the file, skip it by returning `false`
      // in the `filter` callback.
      for await (const data of entry) {
        size += data.length
      }
      console.log(entry.path, size)
    },
  })
)