Giter VIP home page Giter VIP logo

node-concat-transform's Introduction

node-concat-transform

Concatenate data in a stream using a transform

Install

npm install @voliware/node-concat-transform

What is it?

In Node streams you may get data in small chunks. This is rare from a file, unless set explicitly, but will happen in a throttled network system. Most often you cannot operate on pieces of data beause you do not have the entire chunk of data. node-concat-transform is a Node Transform that you can use with the pipe() method to concatenate (link or bundle) data into one bigger chunk. Then, you can operate on that larger chunk if needed, and continue sending it downstream.

Why do I need it?

If you are using a readable Node stream that receives chunks and you are not 100% sure you will receive all the data at once, you will probbably need to bundle it first. You can easily accomplish this with node-concat-stream.

How do I use it?

Once you have a readable Node stream, such as a file stream, TCP stream, or any other type of Readable stream, you simply create a ConcatTransform and pass it into the pipe() method before you intend to do something with the stream data.

Example

Suppose you have a readable stream receiving chunks of data that is only 1 character in length. You can create a ConcatTransform to bundle the chunks into one big peice of data that can then be operated on. This would be essential for example when zipping stream data.

let readable = new Stream.Readable();
let concat_transform = new ConcatTransform();
let writable = new Stream.Writable();

// simulate a stream reading data 1 character/number at a time
for(let i = 0; i < data.length; i++){
    readable.read(data[i]);
}
// stream ends
readable.read(null);

// make use of the stream data
readable.pipe(concat_transform)
        .pipe(some_other_transform)
        .pipe(writable);

If you are receiving data that has a header, or a start delimiter and/or end delimiter, you can also set options for your ConcatTransform that will bundle the data based on these options. This is common in network based streams where an EOF set of characters, such as /r/n, indicate the end of a datagram, but you may actually get peices of the next or previous datagram in the same chunk. If your chunks have headers, than you will need to pass a custom function that parses the header and returns the total number of bytes expected in a fully formed chunk.

let concat_transform = new ConcatTransform({
    // strings or Buffers are accepted
    delimiter: {
        start: "<START>",
        end: "<END>"
    }
    // to parse a header, write a custom function
    // that will return the expected full chunk length
    header_parser: header_parsing_function
});

node-concat-transform's People

Contributors

tones31 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.