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

gulp-asar-stream

v1.0.3

Published

Stream vinyl files into asar archives

Downloads

7

Readme

gulp-asar-stream

This module provides an interface for working with asar files in gulp. Currently, only the writing half has been implemented, but I expect to create a reader that extracts archives, pushing them out as Vinyl files.

GulpAsarWriter

This interface provides the writing end of the module. It pulls in any files piped into it, and adds them to an asar archive, emitting one file once the input stream has ended.

This module uses asar-flex to build the asar archive, which means it streams the files through. In order to support this though, the entire pipeline must support streaming. If it does not, the data will be buffered into RAM until the pipeline finishes. If this becomes a problem, files could be saved to disk intermediately and then streamed back through into the archive. More notes on this below.

Constructor

export class GulpAsarWriter extends Transform {
    constructor(filename: string, opts?: GulpAsarWriter.Options);
	// ...
}

The constructor requires one argument, which is the name of the resulting asar file. An optional options argument can be provided to enable warnings and debugging information.

Example

import gulp from "gulp";
import typescript from "gulp-typescript";
import {
	GulpAsarWriter,
} from "gulp-asar-stream";

gulp.task("archiveFiles", () => {
	return gulp.src("src/**/*.ts") // Pull in ts files
		.pipe(typescript()) // Transpile into js
		.pipe(new GulpAsarWriter("app.asar")) // Write them into an asar archive named app.asar
		.pipe(gulp.dest("dist")) // Save it in the dist folder
});

A note on memory usage

This plugin can either be much better, or the same as the offical implementation, depending on your use case. If you want to stream files from an online source directly into your archive, this will do that. If you want to eliminate the need to create a temporary dist folder before archiving your source files, this will do that. However, in order to prevent consuming mass amounts of ram, make sure that your data is streaming data before piping it through. Because of the nature of the asar format, the metadata of all files must be written first, meaning that we cannot write anything until we have seen everything. The memory implications of this are largely mitigated by collecting only the metadata in the Vinyl objects, and leaving the streams within them untouched, allowing flow control to do its work and keep them at bay until we're ready for the data. Unfortunately, many plugins output buffers, which could be problematic when dealing with large amounts of files. If this is the case, and you start to notice abnormally high memory usage, you should pipe files to an intermediate folder before archiving.

If you would like to know if the files are being buffered before they are passed along, you can pass a second, optional argument to the writer class, like so:

return gulp.src("**/*.ts")
	.pipe(typescript())
	.pipe(new GulpAsarWriter("app.asar", { warnBuffers: true }));
	.pipe(gulp.dest("dist"));

By default, gulp.src buffers files into memory before passing them along. To prevent this, you can pass an options argument, like so:

return gulp.src("**/*.png", {
	buffers: false,
})
	.pipe(new GulpAsarWriter("app.asar"))
	.pipe(gulp.dest("dist"));

Usage with multiple input streams

This library is most useful when you can merge all of your compile streams into one place. An easy way to accomplish this is to use merge2. With merge2, you can create all your separate pipelines, running them in parallel, and funnel them all into GulpAsarWriter. This will allow you to merge all of your files into the archive without creating any intermediary folders at all.

Here is a PoC example (untested) of how one-step bundling of an electron application could be done.

import gulp from "gulp";
import merge2 from "merge2";
import typescript from "gulp-typescript";
import { GulpAsarStream } from "gulp-asar-stream";
import source from "vinyl-source-stream";
import axios from "axios";
import unzip from "gulp-unzip";
import zip from "gulp-zip";

gulp.task("default", () => {
	return merge2(

		// Application scripts
		merge2(
			// Assets
			gulp.src([
				"**/*.png",
				"**/*.ico",
				"**/*.svg",
				"**/*.html",
			], { buffers: false }),
			// Code
			gulp.src("**/*.ts")
				.pipe(typescript())
		)
			// Send it all into the asar
			.pipe(new GulpAsarWriter("resources/app.asar")),

		// Download electron distributable and unzip it
		(await axios("https://github.com/electron/electron/releases/download/v11.4.5/electron-v11.4.5-linux-x64.zip", { responseType: stream })).data
			.pipe(source("electron-v11.4.5-linux-x64.zip"))
			.pipe(unzip())

	)
		// Zip files back up for distribution
		.pipe(zip("application.zip"))
		// Save zip in dist folder
		.pipe(gulp.dest("dist"));
});