Skip to content
VaelJS Blog

vael:archive

Hardened archive creation, listing, reading, and extraction (tar, tar.gz, tar.zst, zip).

Available in the vjs (vaeljs) runtime with both synchronous and async worker-thread APIs (create, extract, list, readEntry); vjs-lite (vaeljs-lite) exposes the synchronous APIs only. Never installed on globalThis — import explicitly from "vael:archive".

Enforces path-traversal rejection, symlink/hardlink/special-entry blocking, bounded streaming decompression, and compression-bomb ratio detection (VAEL_ARCHIVE_ZIP_BOMB).

import ... from "vael:archive"
Since v0.0.0-alpha.2 vjs (V8) vjs-lite (QuickJS)
Cargo feature: vael-archive (enabled by default in vaeljs-core and vaeljs-lite-core)
Worker-thread async APIs (create, extract, list, readEntry) are V8-only (vjs); vjs-lite exposes the 8 synchronous exports (detectFormat, packBytesSync, unpackBytesSync, listSync, readEntrySync, extractSync, createSync, ArchiveError).
import { detectFormat, packBytesSync, unpackBytesSync } from "vael:archive";
const bytes = packBytesSync(
[{ path: "pkg/hello.txt", data: "hello archive" }],
{ format: "tar.gz" },
);
console.log(detectFormat(bytes)); // "tar.gz"
const entries = unpackBytesSync(bytes);
console.log(entries[0].path, entries[0].size);
API Symbols
Functions (11)
detectFormatv0.0.0-alpha.2
Detect an ArchiveFormat from leading archive magic bytes or a filename extension (.tar, .tar.gz, .tgz, .tar.zst, .tzst, .zip).
packBytesSyncv0.0.0-alpha.2
Synchronously pack in-memory entries into a tar, tar.gz, tar.zst, or zip byte buffer.
unpackBytesSyncv0.0.0-alpha.2
Synchronously unpack an in-memory archive buffer into validated UnpackedEntry items.
listvjs onlyv0.0.0-alpha.2
Asynchronously list validated entry metadata in an archive file on disk without retaining entry payloads.
listSyncv0.0.0-alpha.2
Synchronously list validated entry metadata in an archive file on disk without retaining entry payloads.
readEntryvjs onlyv0.0.0-alpha.2
Asynchronously read and verify a single regular file entry from an archive file on disk.
readEntrySyncv0.0.0-alpha.2
Synchronously read and verify a single regular file entry from an archive file on disk.
extractvjs onlyv0.0.0-alpha.2
Asynchronously extract an archive file into destDir with symlink guards, canonical containment checks, and atomic overwrite policy.
extractSyncv0.0.0-alpha.2
Synchronously extract an archive file into destDir with symlink guards, canonical containment checks, and atomic overwrite policy.
createvjs onlyv0.0.0-alpha.2
Asynchronously create an archive file at destPath from host files or directories without following symlinks, writing via a sibling temp...
createSyncv0.0.0-alpha.2
Synchronously create an archive file at destPath from host files or directories without following symlinks, writing via a sibling temp...
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function detectFormat(input: Uint8Array | string): ArchiveFormat | null;

Detect an ArchiveFormat from leading archive magic bytes or a filename extension (.tar, .tar.gz, .tgz, .tar.zst, .tzst, .zip).

Parameters
NameTypeDescription
inputUint8Array | stringRaw leading archive bytes or a file path/name string.
Returns
ArchiveFormat | null — Detected canonical ArchiveFormat, or null if unrecognized.
Exceptions
  • TypeError — when input is neither a Uint8Array nor a string.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function packBytesSync(entries: readonly PackEntry[], options: PackOptions): Uint8Array;

Synchronously pack in-memory entries into a tar, tar.gz, tar.zst, or zip byte buffer.

Validates all entry paths, rejects traversal segments and duplicate paths, and enforces default archive limits.

Parameters
NameTypeDescription
entriesreadonly PackEntry[]Entries to pack into the archive.
optionsPackOptionsFormat and optional compression level.
Returns
Uint8Array — Encoded archive bytes.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on invalid format, unsafe path, duplicate entry, or limit exceeded.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function unpackBytesSync(
  archiveBytes: Uint8Array,
  options?: UnpackOptions,
): UnpackedEntry[];

Synchronously unpack an in-memory archive buffer into validated UnpackedEntry items.

Enforces path-traversal, forbidden-entry, duplicate-entry, size/count, and compression-bomb (VAEL_ARCHIVE_ZIP_BOMB) protections.

Parameters
NameTypeDescription
archiveBytesUint8ArrayRaw archive container bytes.
options optionalUnpackOptionsOptional format override and resource limits.
Returns
UnpackedEntry[] — Array of unpacked entries with payload bytes.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on invalid format, corrupt archive, unsafe path, limit exceeded, or zip bomb.
functionSince v0.0.0-alpha.2vjs only
export function list(path: string, options?: UnpackOptions): Promise<ArchiveEntryInfo[]>;

Asynchronously list validated entry metadata in an archive file on disk without retaining entry payloads.

Available in vjs (vaeljs) only; use listSync in vjs-lite.

Parameters
NameTypeDescription
pathstringPath to the archive file on disk.
options optionalUnpackOptionsOptional format override and resource limits.
Returns
Promise<ArchiveEntryInfo[]> — Promise resolving to an array of ArchiveEntryInfo records.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on invalid format, corrupt archive, unsafe path, limit exceeded, or I/O error.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function listSync(path: string, options?: UnpackOptions): ArchiveEntryInfo[];

Synchronously list validated entry metadata in an archive file on disk without retaining entry payloads.

Parameters
NameTypeDescription
pathstringPath to the archive file on disk.
options optionalUnpackOptionsOptional format override and resource limits.
Returns
ArchiveEntryInfo[] — Array of ArchiveEntryInfo records.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on invalid format, corrupt archive, unsafe path, limit exceeded, or I/O error.
functionSince v0.0.0-alpha.2vjs only
export function readEntry(
  path: string,
  entryPath: string,
  options?: UnpackOptions,
): Promise<Uint8Array>;

Asynchronously read and verify a single regular file entry from an archive file on disk.

Available in vjs (vaeljs) only; use readEntrySync in vjs-lite.

Parameters
NameTypeDescription
pathstringPath to the archive file on disk.
entryPathstringNormalized archive-relative path of the regular file entry.
options optionalUnpackOptionsOptional format override and resource limits.
Returns
Promise<Uint8Array> — Promise resolving to the uncompressed file bytes.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on missing entry, directory entry, unsafe path, limit exceeded, zip bomb, or I/O error.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function readEntrySync(
  path: string,
  entryPath: string,
  options?: UnpackOptions,
): Uint8Array;

Synchronously read and verify a single regular file entry from an archive file on disk.

Parameters
NameTypeDescription
pathstringPath to the archive file on disk.
entryPathstringNormalized archive-relative path of the regular file entry.
options optionalUnpackOptionsOptional format override and resource limits.
Returns
Uint8Array — Uncompressed file bytes.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on missing entry, directory entry, unsafe path, limit exceeded, zip bomb, or I/O error.
functionSince v0.0.0-alpha.2vjs only
export function extract(
  archivePath: string,
  destDir: string,
  options?: ExtractOptions,
): Promise<ExtractResult>;

Asynchronously extract an archive file into destDir with symlink guards, canonical containment checks, and atomic overwrite policy.

Available in vjs (vaeljs) only; use extractSync in vjs-lite.

Parameters
NameTypeDescription
archivePathstringPath to the archive file on disk.
destDirstringDestination directory on disk.
options optionalExtractOptionsOptional format, overwrite policy, stripComponents, and limits.
Returns
Promise<ExtractResult> — Promise resolving to ExtractResult summary counts.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on unsafe path, existing target (overwrite: false), limit exceeded, zip bomb, or I/O error.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function extractSync(
  archivePath: string,
  destDir: string,
  options?: ExtractOptions,
): ExtractResult;

Synchronously extract an archive file into destDir with symlink guards, canonical containment checks, and atomic overwrite policy.

Parameters
NameTypeDescription
archivePathstringPath to the archive file on disk.
destDirstringDestination directory on disk.
options optionalExtractOptionsOptional format, overwrite policy, stripComponents, and limits.
Returns
ExtractResult — ExtractResult summary counts.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on unsafe path, existing target (overwrite: false), limit exceeded, zip bomb, or I/O error.
functionSince v0.0.0-alpha.2vjs only
export function create(
  destPath: string,
  sources: readonly (string | CreateSource)[],
  options?: CreateOptions,
): Promise<CreateResult>;

Asynchronously create an archive file at destPath from host files or directories without following symlinks, writing via a sibling temp file and atomic commit.

Available in vjs (vaeljs) only; use createSync in vjs-lite.

Parameters
NameTypeDescription
destPathstringDestination archive file path.
sourcesreadonly (string | CreateSource)[]Host file/directory paths or CreateSource mappings.
options optionalCreateOptionsFormat, optional compression level, and overwrite flag.
Returns
Promise<CreateResult> — Promise resolving to CreateResult summary.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on existing destination (overwrite: false), unsafe path, or I/O error.
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function createSync(
  destPath: string,
  sources: readonly (string | CreateSource)[],
  options?: CreateOptions,
): CreateResult;

Synchronously create an archive file at destPath from host files or directories without following symlinks, writing via a sibling temp file and atomic commit.

Parameters
NameTypeDescription
destPathstringDestination archive file path.
sourcesreadonly (string | CreateSource)[]Host file/directory paths or CreateSource mappings.
options optionalCreateOptionsOptional format, compression level, overwrite, symlink, and limit options.
Returns
CreateResult — CreateResult summary.
Exceptions
  • TypeError — on invalid argument types.
  • ArchiveError — on existing destination (overwrite: false), unsafe path, or I/O error.
classSince v0.0.0-alpha.2vjs & vjs-lite
export class ArchiveError extends Error {
  override name: "ArchiveError";
  code: ArchiveErrorCode;
  constructor(code: ArchiveErrorCode, message: string);
}

Error thrown by vael:archive operations carrying a stable VAEL_ARCHIVE_* error code.

Constructor
constructor(code: ArchiveErrorCode, message: string);

Construct an ArchiveError.

Parameters
NameTypeDescription
codeArchiveErrorCodeMachine-readable ArchiveErrorCode.
messagestringHuman-readable error message.
Properties
PropertyTypeDescriptionSince
name "ArchiveError"Error name ("ArchiveError").v0.0.0-alpha.2
code ArchiveErrorCodeMachine-readable VAEL_ARCHIVE_* error classification.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface ArchiveLimits {
  maxEntries?: number;
  maxFileBytes?: number;
  maxTotalBytes?: number;
  maxCompressionRatio?: number;
  maxPathLen?: number;
  maxDepth?: number;
  allowDuplicateEntries?: boolean;
}

Security and resource bounds enforced during archive reading, listing, unpacking, and extraction.

Properties
PropertyTypeDescriptionSince
maxEntries optionalnumberMaximum number of entries permitted in a single archive (default 10_000, hard max 100_000).v0.0.0-alpha.2
maxFileBytes optionalnumberMaximum uncompressed bytes for any single entry (default 64 MiB, hard max 1 GiB).v0.0.0-alpha.2
maxTotalBytes optionalnumberMaximum total uncompressed bytes across all entries (default 256 MiB, hard max 4 GiB).v0.0.0-alpha.2
maxCompressionRatio optionalnumberMaximum allowed decompressed / compressed ratio once output exceeds 1 MiB (default 100).v0.0.0-alpha.2
maxPathLen optionalnumberMaximum normalized archive entry path length in bytes (default 1024).v0.0.0-alpha.2
maxDepth optionalnumberMaximum path segment depth (default 64).v0.0.0-alpha.2
allowDuplicateEntries optionalbooleanWhether duplicate normalized entry paths are permitted (default false).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface PackEntry {
  path: string;
  data?: Uint8Array | string;
  isDir?: boolean;
  mode?: number;
  mtime?: number;
}

Input entry passed to packBytesSync().

Properties
PropertyTypeDescriptionSince
path stringArchive-relative entry path using POSIX / separators.v0.0.0-alpha.2
data optionalUint8Array | stringFile contents as bytes or UTF-8 string (must be empty or omitted when isDir is true).v0.0.0-alpha.2
isDir optionalbooleanWhether this entry is a directory (true) rather than a regular file (false, default).v0.0.0-alpha.2
mode optionalnumberOptional Unix permission bits (masked to 0o777 on write).v0.0.0-alpha.2
mtime optionalnumberOptional modification time in Unix epoch seconds.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface PackOptions {
  format: ArchiveFormat | "tgz" | "tzst";
  level?: number;
}

Options accepted by packBytesSync().

Properties
PropertyTypeDescriptionSince
format ArchiveFormat | "tgz" | "tzst"Target archive format ("tar", "tar.gz", "tar.zst", or "zip", plus "tgz" / "tzst" aliases).v0.0.0-alpha.2
level optionalnumberOptional compression level (0..=9 for gzip/zip, 0..=22 for zstd).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface UnpackOptions {
  format?: ArchiveFormat | "tgz" | "tzst";
  limits?: ArchiveLimits;
}

Options accepted by unpackBytesSync(), list(), listSync(), readEntry(), and readEntrySync().

Properties
PropertyTypeDescriptionSince
format optionalArchiveFormat | "tgz" | "tzst"Explicit archive format override; auto-detected from magic bytes or filename extension when omitted.v0.0.0-alpha.2
limits optionalArchiveLimitsResource and security bounds applied during reading or unpacking.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface UnpackedEntry {
  readonly path: string;
  readonly size: number;
  readonly isDir: boolean;
  readonly mode: number | null;
  readonly mtime: number | null;
  readonly data: Uint8Array;
}

Materialized entry returned by unpackBytesSync().

Properties
PropertyTypeDescriptionSince
path readonlystringValidated, normalized archive-relative path.v0.0.0-alpha.2
size readonlynumberUncompressed entry size in bytes.v0.0.0-alpha.2
isDir readonlybooleantrue when the entry is a directory, false for a regular file.v0.0.0-alpha.2
mode readonlynumber | nullUnix mode bits reported by the archive header, or null if absent.v0.0.0-alpha.2
mtime readonlynumber | nullModification time in Unix epoch seconds, or null if absent.v0.0.0-alpha.2
data readonlyUint8ArrayUncompressed payload bytes (empty for directories).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface ArchiveEntryInfo {
  readonly path: string;
  readonly size: number;
  readonly compressedSize: number | null;
  readonly isDir: boolean;
  readonly mode: number | null;
  readonly mtime: number | null;
}

Entry metadata summary returned by list() and listSync().

Properties
PropertyTypeDescriptionSince
path readonlystringValidated, normalized archive-relative path.v0.0.0-alpha.2
size readonlynumberUncompressed entry size in bytes.v0.0.0-alpha.2
compressedSize readonlynumber | nullCompressed entry size in bytes (present for ZIP entries, null for TAR).v0.0.0-alpha.2
isDir readonlybooleantrue when the entry is a directory, false for a regular file.v0.0.0-alpha.2
mode readonlynumber | nullUnix mode bits reported by the archive header, or null if absent.v0.0.0-alpha.2
mtime readonlynumber | nullModification time in Unix epoch seconds, or null if absent.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface ExtractOptions {
  format?: ArchiveFormat | "tgz" | "tzst";
  overwrite?: boolean;
  stripComponents?: number;
  allowDuplicateEntries?: boolean;
  limits?: ArchiveLimits;
}

Options controlling extract() and extractSync().

Properties
PropertyTypeDescriptionSince
format optionalArchiveFormat | "tgz" | "tzst"Explicit archive format override; auto-detected from bytes/path when omitted.v0.0.0-alpha.2
overwrite optionalbooleanWhether existing regular files at the destination may be overwritten (default false, which fails with "VAEL_ARCHIVE_ALREADY_EXISTS").v0.0.0-alpha.2
stripComponents optionalnumberNumber of leading path components to strip from extracted entry paths (default 0).v0.0.0-alpha.2
allowDuplicateEntries optionalbooleanWhether duplicate normalized entry paths are permitted (default false).v0.0.0-alpha.2
limits optionalArchiveLimitsResource and security bounds applied during extraction.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface ExtractResult {
  readonly filesExtracted: number;
  readonly dirsCreated: number;
  readonly totalBytes: number;
}

Summary returned by extract() and extractSync().

Properties
PropertyTypeDescriptionSince
filesExtracted readonlynumberNumber of regular files written to disk.v0.0.0-alpha.2
dirsCreated readonlynumberNumber of directories created or ensured on disk.v0.0.0-alpha.2
totalBytes readonlynumberTotal regular file payload bytes written to disk.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface CreateSource {
  path: string;
  archivePath?: string;
}

Host file or directory source specification accepted by create() and createSync().

Properties
PropertyTypeDescriptionSince
path stringHost filesystem path to a regular file or directory.v0.0.0-alpha.2
archivePath optionalstringOptional archive-relative prefix or target path (defaults to path's basename).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface CreateOptions {
  format?: ArchiveFormat | "tgz" | "tzst";
  level?: number;
  overwrite?: boolean;
  followSymlinks?: boolean;
  limits?: ArchiveLimits;
}

Options controlling create() and createSync().

Properties
PropertyTypeDescriptionSince
format optionalArchiveFormat | "tgz" | "tzst"Target archive format ("tar", "tar.gz", "tar.zst", or "zip", plus "tgz" / "tzst" aliases). When omitted, inferred from the destPath file extension.v0.0.0-alpha.2
level optionalnumberOptional compression level (0..=9 for tar/tar.gz/zip, 0..=22 for tar.zst).v0.0.0-alpha.2
overwrite optionalbooleanWhether an existing file at destPath may be atomically replaced (default false, which fails with "VAEL_ARCHIVE_ALREADY_EXISTS").v0.0.0-alpha.2
followSymlinks optionalbooleanWhether to follow symbolic links in host source paths (default false, skipping symlinks inside directories and refusing direct symlink sources).v0.0.0-alpha.2
limits optionalArchiveLimitsOptional entry count, byte size, path length, depth, and duplicate-entry bounds enforced while collecting host sources.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs & vjs-lite
export interface CreateResult {
  readonly path: string;
  readonly entriesWritten: number;
  readonly archiveBytes: number;
}

Summary returned by create() and createSync().

Properties
PropertyTypeDescriptionSince
path readonlystringDestination archive path written atomically.v0.0.0-alpha.2
entriesWritten readonlynumberTotal archive entries written (files and directories).v0.0.0-alpha.2
archiveBytes readonlynumberTotal compressed container bytes written to path.v0.0.0-alpha.2
typeSince v0.0.0-alpha.2vjs & vjs-lite
export type ArchiveFormat = "tar" | "tar.gz" | "tar.zst" | "zip";

Canonical archive container and compression format identifiers.

typeSince v0.0.0-alpha.2vjs & vjs-lite
export type ArchiveErrorCode =
  | "VAEL_ARCHIVE_INVALID_FORMAT"
  | "VAEL_ARCHIVE_INVALID_ARCHIVE"
  | "VAEL_ARCHIVE_UNSAFE_PATH"
  | "VAEL_ARCHIVE_UNSUPPORTED_ENTRY_TYPE"
  | "VAEL_ARCHIVE_DUPLICATE_ENTRY"
  | "VAEL_ARCHIVE_LIMIT_EXCEEDED"
  | "VAEL_ARCHIVE_ZIP_BOMB"
  | "VAEL_ARCHIVE_ENTRY_NOT_FOUND"
  | "VAEL_ARCHIVE_ALREADY_EXISTS"
  | "VAEL_ARCHIVE_IO";

Stable machine-readable VAEL_ARCHIVE_* error codes exposed on ArchiveError.code.