Install @electron/asar
mainInstall the @electron/asar package using npm. This module requires Node.js version 22.12.0 or later.
npm install --engine-strict @electron/asarrepository·main·Indexed 25 days ago
https://github.com/electron/asarA simple, extensive archive format used by Electron that concatenates files without compression while providing random access support via a JSON header. It includes a CLI for packing, listing, and extracting archives, as well as a programmatic API for creating packages from directories, files, or streams, and managing archive entries through the Filesystem class.
Install the @electron/asar package using npm. This module requires Node.js version 22.12.0 or later.
npm install --engine-strict @electron/asarUse createPackage to create an asar archive from a source directory.
Note: There is currently no error handling provided for this function.
import { createPackage } from '@electron/asar';
const src = 'some/path/';
const dest = 'name.asar';
await createPackage(src, dest);
console.log('done.');Use createPackageWithOptions to apply a transformation to files as they are being packed into the .asar archive. The transform option accepts a function that returns either undefined or a stream.Transform instance. The transform stream will be applied to the files being written to the archive (useful for tasks like compression).
import { createPackageWithOptions } from '@electron/asar';
const src = 'some/path/';
const dest = 'name.asar';
function transform (filename) {
return new CustomTransformStream()
}
await createPackageWithOptions(src, dest, { transform: transform });
console.log('done.');The ASAR format is a flat structure that concatenates files without compression, supporting random access. It uses [Pickle][pickle] to serialize a header.
Structure:
| UInt32: header_size | String: header | Bytes: file1 | ... | Bytes: file42 |
Header Details:
header is a JSON string containing a files object.files object is a nested tree representing the directory structure.offset: A UINT64 (represented as a string) indicating the start of the file relative to the end of the header. To get the absolute offset, add the size of header_size and header to this value.size: A JavaScript Number representing the file size.executable: Boolean.integrity: An object containing:algorithm: Currently only SHA256 is supported.hash: Hex encoded hash of the entire file.blockSize: Integer size of each block in bytes.blocks: An array of hex encoded hashes for each block.When using the pack command, you can use the --unpack-dir option to exclude specific files or patterns from being packed into the archive (they will remain uncompressed/unpacked).
Examples:
asar pack app app.asar --unpack-dir "{x1,x2}"asar pack app app.asar --unpack-dir "**/{x1,x2}"asar pack app app.asar --unpack-dir "{**/x1,**/x2,z4/w1}"asar pack app app.asar --unpack-dir "{x1,x2}"The asar CLI allows you to pack directories into archives, list files, and extract files or entire archives.
Commands:
pack|p <dir> <output>: Create an asar archive from a directory.list|l <archive>: List files within an asar archive.extract-file|ef <archive> <filename>: Extract a single file from an archive.extract|e <archive> <dest>: Extract the entire archive to a destination.Options:
-h, --help: Output usage information.-V, --version: Output the version number.$ asar --help
Usage: asar [options] [command]
Commands:
pack|p <dir> <output>
create asar archive
list|l <archive>
list files of asar archive
extract-file|ef <archive> <filename>
extract one file from archive
extract|e <archive> <dest>
extract archive
Options:
-h, --help output usage information
-V, --version output the version numberUse getFileIntegrity to calculate the SHA256 hash and block-level hashes for a file provided as a NodeJS.ReadableStream. This is useful for verifying the integrity of files within an ASAR archive.
Returns a FileIntegrity object containing:
algorithm: Always 'SHA256'.hash: The full file SHA256 hash in hex.blockSize: The size of each block used (default is 4MB).blocks: An array of hex-encoded SHA256 hashes for each block.Use insertFile to add a file to the archive. This method requires a streamGenerator function that returns a NodeJS.ReadableStream of the file content.
Key constraints and behaviors:
UINT32_MAX).shouldUnpack is true or the parent directory is marked as unpacked, the file is treated as an unpacked entry.transform function in the options object. If provided, the file is piped through the transform stream and the resulting bytes are what get stored and hashed in the archive.insertLink to add a symlink. The method ensures that the link target does not point outside of the package. If the target is absolute or uses .. to escape the package root, an error is thrown.Filesystem class is the core interface for managing the internal structure of an asar archive. It allows you to manipulate the archive header, insert files, directories, and symlinks, and traverse the archive's contents. You initialize it with the path to the archive source.getRawHeader(archivePath) to retrieve the raw header data from an ASAR archive.insertDirectory to create a directory entry within the archive. You can specify if the directory should be marked as unpacked.