JSPM

streaming-tarball

1.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 8
  • Score
    100M100P100Q47253F
  • License MIT

Streaming interface for decoding tarballs on modern JavaScript runtimes

Package Exports

  • streaming-tarball
  • streaming-tarball/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (streaming-tarball) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

streaming-tarball

Streaming interface for decoding tarballs on modern JavaScript runtimes.

Usage

Use the extract function to get a readable stream of tar objects. Each object contains a header and a body (if it's a file).

import { extract } from 'streaming-tarball';

const response = await fetch('https://github.com/shaunpersad/streaming-tarball/archive/refs/heads/main.tar.gz');
const stream = response.body.pipeThrough(new DecompressionStream('gzip'));

for await (const obj of extract(stream)) {
  console.log('name:', obj.header.name, 'type:', obj.header.type, 'size:', obj.header.size);
  console.log('text body:', await obj.text());
}

The file bodies are streams themselves, so we could've rewritten the above example like this:

import { extract } from 'streaming-tarball';

const response = await fetch('https://github.com/shaunpersad/streaming-tarball/archive/refs/heads/main.tar.gz');
const stream = response.body.pipeThrough(new DecompressionStream('gzip'));

for await (const { header, body } of extract(stream)) {
  console.log('name:', header.name, 'type:', header.type, 'size:', header.size);
  if (body) {
    const subStream = body.pipeThrough(new TextDecoderStream());
    let str = '';
    for await (const chunk of subStream) {
      str += chunk;
    }
    console.log('text body:', str);
  }
}

Because file bodies are sub-streams of the parent stream, you must consume them all in order for the parent stream to make progress. There's a discard helper function on the tar object to help you do so:

import { extract, TAR_OBJECT_TYPE_FILE } from 'streaming-tarball';

const response = await fetch('https://github.com/shaunpersad/streaming-tarball/archive/refs/heads/main.tar.gz');
const stream = response.body.pipeThrough(new DecompressionStream('gzip'));

for await (const obj of extract(stream)) {
  if (obj.header.type === TAR_OBJECT_TYPE_FILE && obj.header.size < 100_000) {
    console.log('file found:', obj.header.name, 'body:', await obj.text());
  } else {
    await discard();
  }
}