MCPcopy Index your code
hub / github.com/electron/asar

github.com/electron/asar @v4.2.0 sqlite

repository ↗ · DeepWiki ↗ · release v4.2.0 ↗
143 symbols 498 edges 36 files 1 documented · 1% 9 cross-repo links
README

@electron/asar - Electron Archive

Test npm version API docs

ASAR is a simple extensive archive format. It concatenates all files together without compression (like tar) while having random access support.

Features

  • Support random access
  • Use JSON to store file information
  • Very easy to write a parser

CLI

Install

This module requires Node 22.12.0 or later.

npm install --engine-strict @electron/asar

Usage

$ 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 number

Excluding multiple resources from being packed

Given:

    app
(a) ├── x1
(b) ├── x2
(c) ├── y3
(d) │   ├── x1
(e) │   └── z1
(f) │       └── x2
(g) └── z4
(h)     └── w1

Exclude: a, b

asar pack app app.asar --unpack-dir "{x1,x2}"

Exclude: a, b, d, f

asar pack app app.asar --unpack-dir "**/{x1,x2}"

Exclude: a, b, d, f, h

asar pack app app.asar --unpack-dir "{**/x1,**/x2,z4/w1}"

Programmatic usage

For full API usage, see the API documentation.

Example

import { createPackage } from '@electron/asar';

const src = 'some/path/';
const dest = 'name.asar';

await createPackage(src, dest);
console.log('done.');

Please note that there is currently no error handling provided!

Transform

You can pass in a transform option, that is a function, which either returns nothing, or a stream.Transform. The latter will be used on files that will be in the .asar file to transform them (e.g. compress).

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.');

Format

Asar uses Pickle to safely serialize binary value to file.

The format of asar is very flat:

| UInt32: header_size | String: header | Bytes: file1 | ... | Bytes: file42 |

The header_size and header are serialized with Pickle class, and header_size's Pickle object is 8 bytes.

The header is a JSON string, and the header_size is the size of header's Pickle object.

Structure of header is something like this:

{
   "files": {
      "tmp": {
         "files": {}
      },
      "usr" : {
         "files": {
           "bin": {
             "files": {
               "ls": {
                 "offset": "0",
                 "size": 100,
                 "executable": true,
                 "integrity": {
                   "algorithm": "SHA256",
                   "hash": "...",
                   "blockSize": 1024,
                   "blocks": ["...", "..."]
                 }
               },
               "cd": {
                 "offset": "100",
                 "size": 100,
                 "executable": true,
                 "integrity": {
                   "algorithm": "SHA256",
                   "hash": "...",
                   "blockSize": 1024,
                   "blocks": ["...", "..."]
                 }
               }
             }
           }
         }
      },
      "etc": {
         "files": {
           "hosts": {
             "offset": "200",
             "size": 32,
             "integrity": {
                "algorithm": "SHA256",
                "hash": "...",
                "blockSize": 1024,
                "blocks": ["...", "..."]
              }
           }
         }
      }
   }
}

offset and size records the information to read the file from archive, the offset starts from 0 so you have to manually add the size of header_size and header to the offset to get the real offset of the file.

offset is a UINT64 number represented in string, because there is no way to precisely represent UINT64 in JavaScript Number. size is a JavaScript Number that is no larger than Number.MAX_SAFE_INTEGER, which has a value of 9007199254740991 and is about 8PB in size. We didn't store size in UINT64 because file size in Node.js is represented as Number and it is not safe to convert Number to UINT64.

integrity is an object consisting of a few keys:

  • A hashing algorithm, currently only SHA256 is supported.
  • A hex encoded hash value representing the hash of the entire file.
  • An array of hex encoded hashes for the blocks of the file (i.e. for a blockSize of 4KB, this array contains the hash of every block if you split the file into N 4KB blocks).
  • A integer value blockSize representing the size in bytes of each block in the blocks hashes above.

Extension points exported contracts — how you extend this code

FileRecord (Interface)
(no doc)
src/disk.ts
ListOptions (Interface)
(no doc)
src/asar.ts

Core symbols most depended-on inside this repo

createPackage
called by 59
src/asar.ts
extractFile
called by 56
src/asar.ts
createFixture
called by 54
test/util/tmpDir.ts
extractAll
called by 27
src/asar.ts
execAsar
called by 19
test/cli-spec.ts
compFiles
called by 19
test/util/compareFiles.ts
verifySmartUnpack
called by 18
test/util/verifySmartUnpack.ts
createPackageWithOptions
called by 18
src/asar.ts

Shape

Function 80
Method 51
Class 10
Interface 2

Languages

TypeScript100%

Modules by API surface

src/pickle.ts36 symbols
src/filesystem.ts18 symbols
src/disk.ts15 symbols
src/asar.ts15 symbols
benchmark/run.ts15 symbols
test/util/transformStream.ts6 symbols
benchmark/generate-fixtures.ts6 symbols
vitest.electron-reporter.ts4 symbols
test/util/verifySmartUnpack.ts4 symbols
src/integrity.ts4 symbols
test/util/tmpDir.ts3 symbols
benchmark/profile-memory.ts3 symbols

Dependencies from manifests, versioned

@tsconfig/node2222.0.1 · 1×
@types/node22.12.0 · 1×
commander13.1.0 · 1×
electron35.7.5 · 1×
glob13.0.2 · 1×
husky9.1.7 · 1×
lint-staged16.2.7 · 1×
minimatch10.0.1 · 1×
prettier3.8.1 · 1×
tsx4.21.0 · 1×
typedoc0.25.13 · 1×
typescript5.5.4 · 1×

For agents

$ claude mcp add asar \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact