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-chunk-merge

v1.0.0

Published

split and merge chunk tool library

Downloads

80

Readme

Split-Chunk-Merge

  • Split

splitByParts splitBySizes

  • Merge

fsMerge bufferMerge streamMerge

Installation

You can install and save an entry to your package.json with the following command:

npm i split-chunk-merge --save

Usage

Split Chunk

1. splitByParts

  • Description

Splitting file with number of parts.

  • Parameter

    • file: Path to the file to split.
    • parts: Number of parts
  • Return

    • Promise<string[]>: Promise with results in an array of part names (full paths) of the splitted files.
  • Example

const path = require('path');
const { splitByParts } = require('split-chunk-merge')

const filePath = path.join(__dirname, "chunks/", "chunks.jpg")

splitByParts(filePath, 3).then(res => {
  // <file>-chunk-1
  // ...
  // <file>-chunk-3
  console.log(res)
})

2. splitBySizes

  • Description

Splitting file with maximum bytes per part.

  • Parameter

    • file: Path to the file to split.
    • maxSize: Max size of the splitted parts. (bytes)
  • Return

    • Promise<string[]>: Promise with results in an array of part names (full paths) of the splitted files.
  • Example

const path = require('path');
const { splitBySizes } = require('split-chunk-merge')

const filePath = path.join(__dirname, "chunks/", "chunks.jpg")

splitBySizes(filePath, 2 * 1024 * 1024).then(res => {
  // <file>-chunk-1
  // ...
  // <file>-chunk-3
  console.log(res)
})

Merge Chunk

1. fsMerge

  • Description

Append file merging refers to merging using fs.appendFile(). The function of fs.appendFile() is to append data to a file asynchronously, if the file does not exist, create the file, data can be a string or buffer.

  • Parameter

    • inputPathList: Path to the file list to merge.
    • outputPath: Path to the ouput file after merge
  • Return

    • Promise: Promise with results in part names of the merged files.
  • Example

const path = require('path');
const { fsMerge } = require('split-chunk-merge')

const inputPath = path.join(__dirname, "chunks/")
const outputPath = path.join(__dirname, "chunks/", "chunks.jpg")
const inputPathList = new Array(3).fill().map((item, index) => {
  return inputPath + "chunks.jpg-chunk-" + index
})

fsMerge(inputPathList, outputPath).then(res => {
  console.log(res) // .../chunks/chunks.jpg
})

2. bufferMerge

  • Description

Buffer mode merging is a common file merging method. The method is to read each fragmented file separately with fs.readFile(), and then merge it through Buffer.concat().

This method is simple and easy to understand, but it has the biggest drawback, that is, how big the file you read, how much memory the merging process takes up, because we are equivalent to loading all the contents of this large file at once. In the memory, this is very inefficient. At the same time, Node's default buffer size limit is 2GB. Once we upload a large file exceeding 2GB, this method will fail. Although this problem can be circumvented by modifying the upper limit of the buffer size, since this merge method is extremely memory-intensive, I do not recommend you to do this

  • Parameter

    • inputPathList: Path to the file list to merge.
    • outputPath: Path to the ouput file after merge
  • Return

    • Promise: Promise with results in part names of the merged files.
  • Example

const path = require('path');
const { bufferMerge } = require('split-chunk-merge')

const inputPath = path.join(__dirname, "chunks/")
const outputPath = path.join(__dirname, "chunks/", "chunks.jpg")
const inputPathList = new Array(3).fill().map((item, index) => {
  return inputPath + "chunks.jpg-chunk-" + index
})

bufferMerge(inputPathList, outputPath).then(res => {
  console.log(res) // .../chunks/chunks.jpg
})

3. streamMerge

  • Description

A stream is a collection of data - just like an array or string. The difference is that the data in the stream may not be available all at once, and you don't need to put all the data in memory at once. This makes streaming very useful when manipulating large amounts of data or sending data piece by piece from an external source.

In other words, when you use the buffer method to process a 2GB file, the memory occupied may be more than 2GB, and when you use the stream to process the file, it may only occupy dozens of M. This is why the stream is chosen.

All streams are instances of EventEmitter. They emit events that can be used to read or write data. However, we can use the pipe method to use the data in the stream in a simpler way.

In the following code, we first create a writable stream through fs.createWriteStream() to store the final merged file. Then use fs.createReadStream() to read each fragmented file separately, and then use pipe() to "pour" the read data into a writable stream like pouring water. After monitoring that a glass of water is poured, Immediately continue to pour a cup until it is all poured. At this point, all files have been merged.

  • Parameter

    • inputPathList: Path to the file list to merge.
    • outputPath: Path to the ouput file after merge
  • Return

    • Promise: Promise with results in part names of the merged files.
  • Example

const path = require('path');
const { streamMerge } = require('split-chunk-merge')

const inputPath = path.join(__dirname, "chunks/")
const outputPath = path.join(__dirname, "chunks/", "chunks.jpg")
const inputPathList = new Array(3).fill().map((item, index) => {
  return inputPath + "chunks.jpg-chunk-" + index
})

streamMerge(inputPathList, outputPath).then(res => {
  console.log(res) // .../chunks/chunks.jpg
})

License

License is ISC.