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

fifo-buffer

v1.0.0

Published

A fast FIFO implementation purely based on Node Buffers

Downloads

39

Readme

fifo.js

A simple fifo buffer for node-buffers. Excellent for handling encapsulated binary data in a TCP/IP stream. I encountered many situations where I needed to decode an embedded packet structure from a socket stream.

Installation

As npm for Node.js:

$ npm install fifo-buffer

Example

Some console interaction:

> FIFO=require('./fifo.js')
[Function: FIFOBuffer]
> b = new FIFO()
FIFOBuffer {
  buffer: <Buffer 00 00 00 00 00 00 00 00 00 ... >,
  high: 1048576,
  tail: 0,
  head: 0 }
> b.enq(Buffer.from([1,2,3,4]))
true
> b.enq(Buffer.from([5,6,7,8]))
true
> b.deq(2)
<Buffer 01 02>
> b.deq(2)
<Buffer 03 04>
> b.deq(2)
<Buffer 05 06>
> b.deq(2)
<Buffer 07 08>
> b.deq(2)
null
> b.enq(Buffer.from([1,2,3,4]))
true
> b.peek(2)
<Buffer 01 02>
> b.peek(2)
<Buffer 01 02>
> b.peek(5)
null
> b.size
4
>

A sample interface to extract a packet within a TCP/IP stream. Assuming socket is configured to provide a buffer on "data". The encapsulated packet simply begins with a 4 byte header containing its length.

var FIFO = require('fifo-buffer');
var fifo = new FIFO();

// ... assuming socket setup and connected:
socket.on("data", (data) => {
   if (!fifo.enq(data))
      throw "No more space in FIFO!";
   let temp_buf;
   while (temp_buf = fifo.peek(4)) { // as long as there are 4 bytes in fifo (our header)
      let psize = temp_buf.readUInt32BE(); // we only deq complete packets so the first 4 bytes must be the length of the incoming packet.
      if (fifo.size < psize + 4)
         break;   // incomplete packet, try again next time we receive data.

      // EITHER remove header and give remaining packet to some handler:
      fifo.deq(4);   // trash 4 byte Header
      handleCompleteEncapsulatedPacket(fifo.deq(psize));
      // OR leave header in place:
      handleCompleteEncapsulatedPacket(fifo.deq(psize + 4));
   }
});

API

FIFOBuffer()

Initializes a new empty FIFOBuffer with the default capacity (1MByte).

FIFOBuffer(capacity)

Initializes a new empty FIFOBuffer with the given capacity.

FIFOBuffer#deq(n)

Dequeues the oldest n bytes of the FIFO buffer and returns a copy. Returns null when the buffer has less bytes available.

FIFOBuffer#enq(buffer)

Enqueues the buffer at the end and returns true when there was enough space. When the buffer is full the method returns false.

FIFOBuffer#peek(n)

Copies the oldest n bytes of the FIFO buffer. Does not change the state. Returns null when the buffer has less bytes available.

FIFOBuffer#size

Returns the current size of the FIFO.

Testing

As npm package:

$ npm test

Licence

ISC