Skip to content

packStream ​

packStream returns a ReadableStream<u8[]> carrying the archive of a file or directory as it is written, in the format you name: nothing is written to disk, so the archive can go straight into an HTTP response, a compression stream, or a file. The stream holds at most 64 KiB, and the archive is produced only as fast as it is read.

ZIP is written with data descriptors after each entry, since a stream cannot go back to fill sizes in; every current tool reads it.

SignaturepackStream(source: string, format: ArchiveFormat, options?: PackOptions): ReadableStream<u8[]>

Minimal example ​

tyrus
import { ArchiveError, ArchiveFormat, packStream } from "tyrus:archive";
import { FsError, writeFileStream } from "tyrus:fs";
import { ReadableStream, WritableStream } from "tyrus:stream";

async function backup(): Promise<void> {
    try {
        const archive: ReadableStream<u8[]> = packStream("./site", ArchiveFormat.TarGz);
        const file: WritableStream<u8[]> = writeFileStream("./site.tgz");
        await archive.pipeTo(file);
        print("packed"); // packed
    } catch (error: ArchiveError | FsError) {
        printError(error.code);
    }
}

Every option ​

tyrus
import { ArchiveError, ArchiveFormat, PackOptions, packStream } from "tyrus:archive";
import { ReadableStream } from "tyrus:stream";

async function firstChunk(): Promise<void> {
    const archive: ReadableStream<u8[]> = packStream("./site", ArchiveFormat.Zip, {
        level: 9, // 0 (store only) to 9 (smallest output); default 6; not allowed for Tar
        exclude: ["**/*.map"], // glob patterns relative to the source to leave out; default none
        prefix: "site-1.0.0", // directory every entry is placed under; default none
        reproducible: true, // fixed timestamps and normalized permissions; default false
        timeoutMs: 60_000, // fail the stream after this many milliseconds; default 600000 (10 minutes)
    });
    try {
        const first: u8[]? = await archive.read();
        print((first ?? []).length > 0); // true
    } catch (error: ArchiveError) {
        printError(error.code);
    }
}

overwrite has no effect, since there is no destination. A failure, from a missing source to the time limit, fails the stream with the ArchiveError instead of ending it, so a reader never mistakes a truncated archive for a complete one: read() and pipeTo() reject, and catch (error: ArchiveError) receives it.