How to use the tar-stream/headers.encode function in tar-stream

To help you get started, we’ve selected a few tar-stream examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github digidem / indexed-tarball / single.js View on Github external
function appendMeta (fd, pos, meta, cb) {
  var data = Buffer.from(JSON.stringify(meta), 'utf8')

  var header = tarHeader.encode({
    name: '___index.json',
    type: 'file',
    mode: parseInt('644', 8),
    uid: 0,
    gid: 0,
    mtime: new Date(),
    size: data.length
  })

  // leftover bytes to reach 512 block boundary, plus another 512 * 2 = 1024 to mark the end-of-file
  var padding = Buffer.alloc(512 - (data.length % 512) + 512 + 512).fill(0)

  var buf = Buffer.concat([header, data, padding])

  fs.write(fd, buf, 0, buf.length, pos, cb)
}
github digidem / indexed-tarball / single.js View on Github external
function write (archive, start) {
      // 3. Prepare the tar archive for appending.
      var fsOpts = {
        flags: 'r+',
        start: start !== undefined ? start : 0
      }
      if (fsOpts.start < 0) fsOpts.start = 0
      var appendStream = fs.createWriteStream(self.filepath, fsOpts)

      // 4. Write tar header, without size info (yet).
      var header = tarHeader.encode({
        name: filepath,
        type: 'file',
        mode: parseInt('644', 8),
        uid: 0,
        gid: 0,
        mtime: new Date(),
        size: 0
      })
      appendStream.write(header)

      // 5. Write data.
      t.pipe(appendStream)
      t.on('end', function () {
        // 6. Pad the remaining bytes to fit a 512-byte block.
        var leftover = 512 - (size % 512)
        if (leftover === 512) leftover = 0
github digidem / indexed-tarball / lib / integrity.js View on Github external
function writeIndex (fd, offset, index, cb) {
  var meta = { index: index }
  try {
    var json = Buffer.from(JSON.stringify(meta), 'utf8')
    var header = tarHeader.encode({
      name: '___index.json',
      type: 'file',
      mode: parseInt('644', 8),
      uid: 0,
      gid: 0,
      mtime: new Date(),
      size: json.length
    })
    var leftover = json.length % 512 === 0 ? 0 : 512 - json.length % 512
    var padding = Buffer.alloc(leftover).fill(0)
    var finalBuf = Buffer.concat([header, json, padding])
    fs.write(fd, finalBuf, 0, finalBuf.length, offset, function (err, bytesWritten) {
      if (err) cb(err)
      else if (bytesWritten < finalBuf.length) cb(new Error('failed to fully write index'))
      else cb(null, bytesWritten)
    })

tar-stream

tar-stream is a streaming tar parser and generator and nothing else. It operates purely using streams which means you can easily extract/parse tarballs without ever hitting the file system.

MIT
Latest version published 4 months ago

Package Health Score

83 / 100
Full package analysis

Similar packages