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-async

v1.0.2

Published

Library for reading/streaming asar archives asynchronously

Downloads

9

Readme

async-asar

This library creates a class-based interface for reading and streaming asar files from any location. By default, it can read from the filesystem or HTTP/HTTPS using the built-in backends. However, it allows you to write your own backend using request functions, which only load parts of the file into memory. This module does not require any dependencies, and has been split out into different files of varying complexity to keep it lightweight and versatile. All of the code has been documented and commented, with typings provided.

File Structure

index (contains EasyAsar)

Defines the EasyAsar class that you will most likely use. This extends the streams implementation to allow the use of filesystem, HTTP, and HTTPS backends by default. Simply provide a file path or URL to the constructor.

streams (contains AsarStreams)

This defines the streaming implementation of the asar reader, extending upon the base to allow streaming a file from the archive.

base (contains AsarArchive)

This defines the base implementation of the asar reader, usable in the browser via a Buffer polyfill.

Constructors

EasyAsar examples

const ar = new EasyAsar("/home/user/testing.asar");
const ar = new EasyAsar("file:///home/user/testing.asar");
const ar = new EasyAsar("http://example.com/testing.asar");
const ar = new EasyAsar("https://example.com/testing.asar");
const ar = new EasyAsar(new URL("file:///home/user/testing.asar"));
const ar = new EasyAsar(new URL("http://example.com/testing.asar"));
const ar = new EasyAsar(new URL("https://example.com/testing.asar"));

This constructor will also accept AsarStreams getter functions

AsarStreams

const ar = new AsarStreams((offset, length) => {
	return fs.createReadStream("/path/to/file.asar", {
		start: offset,
		end: offset + length - 1,
	});
});

The getter function can also return a promise resolving the stream This constructor will also accept AsarArchive getter functions.

AsarArchive

const ar = AsarArchive((offset, length) => new Promise((res, rej) => {
	fs.open("/path/to/file.asar", (fd) => {
		fs.read(fd, {
			buffer: Buffer.allocUnsafe(length),
			position: offset,
			length,
		}, (err, bytesRead, buf) => {
			if(err) rej(err);
			if(bytesRead !== length) rej(new Error("Incomplete data"));
			res(buf);
		});
	});
}));

The getter can also return the Buffer directly.

Methods

AsarArchive

fetchIndex

await ar.fetchIndex();

Fetches the asar archive's file index, caching it for later use. This function must be run before anything else can be done.

getFileIndex

const index: AsarIndex | AsarFile = ar.getFileIndex("/path/to/file");

Gets the metadata for a file/folder

isFolder

const isFolder: boolean = ar.isFolder("/path/to/file/or/folder");

Determines whether a path points to a file or folder

readdir

const children: string[] = ar.readdir("/path/to/folder");

Lists the names of file/folders within the requested folder

readFile

const contents: Buffer = await ar.readFile("/path/to/file");

Reads the contents of a file asynchronously

AsarStreams extends AsarArchive

createReadStream

const fileStream: Readable = await ar.createReadStream("/path/to/file");
fileStream.pipe(process.stdout);
// or
fileStream.on("data", (chunk) => process.stdout.write(chunk));
fileStream.on("end", () => console.log("Data finished"));

Creates a readable stream through which you can retrieve the file's contents.

EasyAsar extends AsarStreams

EasyAsar only provides built-in backends and thus does not add any new methods