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

split-file-stream

v2.0.0

Published

Partition your readable streams into multiple files.

Downloads

592

Readme

split-file-stream

npm version

Partition your readable streams into multiple files or combine files into one merged readable stream.

You should use this module if:

  • You have a readable stream and want to save to multiple files
    • Other solutions require you to supply a path as the source.
      • This means you'd have to write your readable stream first, before partitioning the data.
    • Faster solution as disk writes are slow
  • Vice Versa: You want to pipe the merge output as a stream
    • Pipe the merge output to the response of a web request
    • Pipe encrypted partitioned files merge output to a decryption stream

Installation

npm install --save split-file-stream

Usage

To split a read stream into multiple files:

var splitFileStream = require("split-file-stream");
let maxFileSize = 1024; // 1024 bytes per file
let outputPath = __dirname + "/outputFiles"; // file path partition's prefix

splitFileStream.split(readStream, maxFileSize, outputPath, (error, filePaths) => {
	/* If an error occured, filePaths will still contain all files that were written */
	if (error) throw error; // Alternatively you could just log the error instead of throwing: if (error) console.error(error)

	console.log("This is an array of my new files:", filePaths);
	/* stream will be saved to files in the path ∈ { ./outputFiles.split-x | x ∈ N } */
});

To merge a set of files together into one output stream:

var splitFileStream = require("split-file-stream");
let filePaths = filePaths; // take this filePaths array from the output of the split method

splitFileStream.mergeFilesToStream(filePaths, (outStream) => {
	outStream.on("data", (chunk) => {
		console.log(`Received chunk of ${chunk.length} bytes`);
	});

	outStream.on("end", () => {
		console.log("Out stream closed. All files have been merged")
	});
});

To merge a set of files to write to disk:

// Note: You can also do this with the mergeFilesToStream method and piping the stream to a fs writeStream.
var splitFileStream = require("split-file-stream");
let filePaths = filePaths; // take this filePaths array from the output of the split method
let outputPath = __dirname + "/outputFile";

splitFileStream.mergeFilesToDisk(filePaths, outputPath, () => {
    console.log("Finished merging files");
});

Example usage of the mergeFilesToDisk method using the mergeFilesToStream method:

var fs = require("fs");
var splitFileStream = require("split-file-stream");
let filePaths = filePaths; // take this filePaths array from the output of the split method
let outputPath = __dirname + "/outputFile";

splitFileStream.mergeFilesToStream(filePaths, (outStream) => {
	let writeStream = fs.createWriteStream(outputPath);
	outStream.pipe(writeStream);
});

To split a read stream with a custom function that determines the file name:

var splitFileStream = require("split-file-stream");
let maxFileSize = 1024; // 1024 bytes per file
let outputPath = __dirname + "/outputFiles"; // file path partition's prefix
var customSplit = splitFileStream.getSplitWithGenFilePath((n) => `${outputPath}-${(n + 1)}`)

customSplit(readStream, maxFileSize, (error, filePaths) => {
	/* If an error occured, filePaths will still contain all files that were written */
	if (error) throw error; // Alternatively you could just log the error instead of throwing: if (error) console.error(error)

	console.log("This is an array of my new files:", filePaths);
});

Alternatively, if you'd like a lower level API for splitting a stream, you can use _splitToStream. This function will split your readable stream into multiple streams. This function is what is used to implement the split function.

var stream = require("stream");
var splitFileStream = require("split-file-stream");
let partitionStreamSize = 1024; // 1024 bytes per partition
const outStreamCreate = (partitionNum) => {
	return stream.passThrough();
};

splitFileStream._splitToStream(outStreamCreate, readStream, partitionStreamSize, (error, outStreams) => {
	/* If an error occured, filePaths will still contain all files that were written */
	if (error) throw error; // Alternatively you could just log the error instead of throwing: if (error) console.error(error)

	console.log("This is an array of the created output streams:", outStreams);
});