
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
split-file-stream
Advanced tools
Partition your readable streams into multiple files or combine files into one merged readable stream.
npm install --save split-file-stream
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);
});
FAQs
Partition your readable streams into multiple files.
The npm package split-file-stream receives a total of 6 weekly downloads. As such, split-file-stream popularity was classified as not popular.
We found that split-file-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.