packStream
packStream devuelve un ReadableStream<u8[]> con el archivo de un fichero o directorio a medida que se escribe, en el formato indicado: nada se escribe en disco, así que el archivo puede ir directo a una respuesta HTTP, a un stream de compresión o a un fichero. El stream guarda como máximo 64 KiB, y el archivo solo se produce a la velocidad en que se lee.
El ZIP se escribe con data descriptors después de cada entrada, ya que un stream no puede volver atrás para rellenar tamaños; toda herramienta actual lo lee.
packStream(source: string, format: ArchiveFormat, options?: PackOptions): ReadableStream<u8[]>Ejemplo mínimo
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);
}
}Todas las opciones
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 no tiene efecto, ya que no hay destino. Un fallo, desde un origen inexistente hasta el límite de tiempo, hace que el stream falle con el ArchiveError en lugar de terminarlo, así que un lector nunca confunde un archivo truncado con uno completo: read() y pipeTo() rechazan, y catch (error: ArchiveError) lo recibe.
