npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

asar-pack

v1.0.2

Published

A library to create ASAR files for electron.

Downloads

10

Readme

ASAR pack

Programmatically create Electron's ASAR files in memory.

import * as fs from "fs";
import pack from "asar-pack";

const buffer = pack( [
    { name: 'src/main.js', data: fs.readFileSync( '../src/main.js' ) },
    { name: 'package.json', data: `{ "main": "src/main.js" }` }
]);

fs.writeFileSync( '../out/myfile.asar', buffer );

If your archive can't fit in memory, you'll need the "official" asar library. (But, if you can load all the files, try packv().)

There's no command line tool. Again, use the offcial one.

install

npm install asar-pack

It's a single ESM module lib/pack.mjs.

It can't be used from a browser because it depends on node's path, crypto and Buffer modules. (These limitations, could be side-stepped.)

usage

import pack from "asar-pack";
const buffer = pack( members );

also

import {pack,packv} from "asar-pack";

pack( members[, options] )

  • members: <Iterable> yielding <ArchiveMember>
  • options: <Object>
    • integrity: <boolean>
  • Returns: <Buffer> containing the asar.

Create a buffer containing the archive.

Members are placed in the archive in the order they are passed to pack().

Using pack( members, {integrity:false}) stops per-member sha256 hashes being added to the archive directory. Electron 18.2.1 doesn't seem to check them (relying, instead, on a single hash for the whole archive). YMMV.

N.B. This function can be imported explicitly as pack() and also via the (unnamed) default for the module. It's the same function, either way.

ArchiveMember

{
     name: 'member/name',
     data: bufferOrString,  
     // integritySource: stringOrData   
}

name (required)

  • type <string>

The "filename" within the archive; e.g. foo/bar.html will be available as /path/to/app.asar/foo/bar.html.

Names are case sensitive.

Duplicate names aren't permitted.

Forward slashes are supported on Windows.

The root, root drive, or opening . is stripped. So foo/bar, /foo/bar, ./foo/bar, and c:\foo\bar all reference archive.asar/foo/bar

No component can be . or .. (except for an initial .). So foo/./bar and foo/../bar are illegal (but ./foo/bar is permitted).

data (required)

The data to include in the archive for this file.

Strings are converted to UTF8 via TextEncoder.

Buffers and ArrayBufferViews don't have to start or stop at the beginning or end of the underlying buffer.

The data will be referenced exactly once. So this would work:

class FileSync {
    constructor(name) {
        this.name = name;
    }
    get data() {
        return fs.readFileSync(this.name);
    }
};
pack( [ new FileSync( "./main.js") ] );

integritySource (optional)

The stock asar library calculates the integrity from the original file, not its transformed value. If you want to replicate this behaviour (!) set the integritySource to the untransformed data. Otherwise omit it.

As per data, it's either the literal data or text to be utf8 encoded.

const orgText = fs.readFileSync('myfile.json'),
      json = JSON.parse(orgText);
     
json.main = "electron.js";
const newText = JSON.stringify(json);

const asar = pack( [{ name:'package.json', data: newText, integritySource: orgText }] );

packv( members[, options] )

  • members: <Iterable> yielding <ArchiveMember>
  • options: <Object>
    • integrity: <boolean>
  • Return: <Uint8Array[]>

Create the headers for the archive, and format data members as Uint8Arrays, but stop short of concatenating them into a single chunk of memory.

pack() itself is just the convenience function:

const pack = ( members, options ) => Buffer.concat( packv( members, options ) );

If memory is tight, try:

import {packv} from "asar-pack";
import * as fs "from "fs";

const buffers = packv( members );
const fh = fs.openSync( 'app.asar', 'w' );
fs.writevSync( fh, buffers );
fs.closeSync(fh);

As of 1.0.2, there is one buffer for every member, plus an opening buffer that contains the archive header.

File Format

The file format appears to be:

  • DWORD: 4
  • DWORD: directory.length + padding.length + 8
  • DWORD: directory.length + padding.length + 4
  • DWORD: directory.length
  • byte[]: directory (JSON data describing the files and their positions.)
  • byte[]: padding (0-3 null bytes used to DWORD align the data.)
  • byte[]: archive data

This analysis is based on hunches and empirical observation rather than a full deconstruction of the code. There aren't enough test cases covering the code to fully confirm this.

Header

Despite the lack of an explicit identifier (e.g. begining the file with the characters 'asar') the first 16 bytes are distinctive.

They appear to be two of Google's "Pickle" archives. The first contains the length of the directory pickle. The second is the directory pickle. (This was probably a misunderstanding of how pickle works.)

Byte order is little endian (not network order.)

Directory

The directory is JSON. It lists the size and position of archive members within the body of the archive, and their names. For the full schema, see the offical distribution.

The integrity field for individual members seems to be ignored by Electron 18.2.1 and the official library records incorrect values for transformed files.

There is no hash for the directory iself. (Or if there is, I've missed it.)

CHANGES

1.0.2

  • packv() is now guaranted to return the header in a single buffer.
  • Fixed a bug that caused headers to have incorrect alignment. Sorry.

1.0.1

  • Added packv()