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

streambuf

v1.2.3

Published

Streamed Buffers - .NET's BinaryReader facsimile

Downloads

702

Readme

streambuf - Stream Buffers Node.js CI Codecov npm

Stream Buffers - .NET's BinaryReader facsimile for Node.js

This library wraps most of Buffer's methods. The difference with Buffer is that with streambuf you don't have to specify an offset each read/write operation, it uses an internal cursor.

streambuf offers a low-level API that similar to C++'s fstream/iostream/etc. or .NET's BinaryReader/BinaryWriter.

If you're looking for a library that is more high-level and built on top of streambuf, check out node-structor.

Install

$ npm install streambuf

Usage

const fs = require('fs');
const StreamBuffer = require('streambuf');

let buffer = StreamBuffer.from(fs.readFileSync('hiscore.dat'));

let nameLength = buffer.readUInt32LE();
let name = buffer.readString(nameLength);

buffer.skip(-nameLength); // go back to the beginning of the name
buffer.writeString(name.toUpperCase()); // overwrite the name in the buffer with something else

Refer to Buffer for a list of available read and write methods supported by StreamBuffer (omit the offset param).

API

StreamBuffer(Buffer)

Constructor: initialize with a Buffer object.

StreamBuffer(StreamBuffer)

Constructor: initialize with another StreamBuffer object's underlying Buffer.

StreamBuffer numeric methods

readInt8, readInt16LE, readInt16BE, readInt32LE, readInt32BE, readUInt8, readUInt16LE, readUInt16BE, readUInt32LE, readUInt32BE, readFloatLE, readFloatBE, readDoubleLE, readDoubleBE writeInt8, writeInt16LE, writeInt16BE, writeInt32LE, writeInt32BE, writeUInt8, writeUInt16LE, writeUInt16BE, writeUInt32LE, writeUInt32BE, writeFloatLE, writeFloatBE, writeDoubleLE, writeDoubleBE

StreamBuffer BigInt methods

readBigInt64LE, readBigInt64BE, readBigUInt64LE, readBigUInt64BE, writeBigInt64LE, writeBigInt64BE, writeBigUInt64LE, writeBigUInt64BE

.buffer

Provides raw access to the underlying Buffer object (read-only)

.read(numBytes)

Returns a new StreamBuffer that references the same Buffer as the original, but cropped by offset and offset + numBytes.

.write(buffer: Buffer)

Writes the contents of another Buffer.

.readByte()

Alias for .readUInt8()

.readSByte()

Alias for .readInt8()

.writeByte()

Alias for .writeUInt8()

.writeSByte()

Alias for .writeInt8()

.read7BitInt()

Reads a 7 bit encoded integer, like those used by .NET

.write7BitInt()

Writes a 7 bit encoded integer, like those used by .NET

.readChar([encoding])

Reads a single character from the buffer according to the specified character encoding. 'encoding' defaults to utf8.

.writeChar([encoding])

Writes a single character to the buffer according to the specified character encoding. Multi-byte characters are not written - use writeString for that instead. 'encoding' defaults to utf8.

.readString([length, [encoding]])

Decodes to a string according to the specified character encoding in encoding and length. 'encoding' defaults to utf8. 'length' is optional. If left undefined, it will use the first occuring zero (0) byte as the end of the string.

.peekString([length, [encoding]])

Functions the same way as .readString(), but does not update the offset.

.writeString(str, [encoding])

Writes a string to the underlying buffer with the specified encoding. 'encoding' defaults to utf8.

.readString7([encoding])

A specialized version of readString(), it first reads a 7 bit encoded integer and uses that as the length of the to be read string. Can be used to read strings written by .NET's BinaryWriter.

.writeString7([encoding])

A specialized version of writeString(), it first writes a 7 bit encoded integer representing the length of the string, followed by the string.

.skip(numBytes)

Skips the specified number of bytes. A negative number can be used to rewind.

.setPos(pos) / .seek(pos)

Moves the offset to the specified pos.

.getPos() / .tell()

Returns the current offset.

.rewind()

Moves the offset back to 0.

.isEOF()

Returns true if the end of the buffer is reached. (offset >= buffer.length)