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

@mediafish/buffer-operator

v0.1.2

Published

A library to read/write byte arrays

Downloads

84

Readme

Build Status Coverage Status Dependency Status Development Dependency Status Known Vulnerabilities npm Downloads XO code style

buffer-operator

Provides JS functions to read/write a byte buffer (both node's Buffer and Uint8Array)

Install

NPM

Usage

const {reader, writer} = require('@mediafish/buffer-operator');

const buffer = Buffer.from([0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63]);
// Or use Uint8Array
const buffer = Uint8Array.from([0xFF, 0xFF, 0xFF, 0x61, 0x62, 0x63]);

let offset, value;
[offset, value] = reader.readNumber(buffer, 0, 1); // Specify offset and length
// offset => 1
// value => 255
[offset, value] = reader.readNumber(buffer, offset, 2, true); // signed = true
// offset => 3
// value => -1
[offset, value] = reader.readString(buffer, offset, 3);
// offset => 6
// value => 'abc'

const dest = Buffer.alloc(6);
// Or use Uint8Array
const dest = new Uint8Array(6);

offset = writer.writeNumber(255, dest, 0, 1);
// offset => 1
offset = writer.writeNumber(-1, dest, offset, 2);
// offset => 3
offset = writer.writeString('abc', dest, offset, 3);
// offset => 6
dest.equals(buffer); // => true

API

reader.readNumber(buffer, offset[, length, signed])

Read an integer from the buffer

params

| Name | Type | Required | Default | Description | | -------- | ------- | -------- | ------- | ------------- | | buffer | Buffer or Uint8Array | Yes | N/A | The buffer from which the data is read | | offset | number | Yes | N/A | An integer to specify the position within the buffer | | length | number | No | 4 | An integer to specify how many bytes to read | | signed | boolean | No | false | Set true to read a negative number |

return value

An array containing the following pair of values | Index | Type | Description | | ----- | ------ | ------------ | | [0] | number | An integer to indicate the position from which the next data should be read | | [1] | number | The read value |

reader.readString(buffer, offset[, length, nullTerminated])

Read a string from the buffer

params

| Name | Type | Required | Default | Description | | -------- | ------- | -------- | ------- | ------------- | | buffer | Buffer or Uint8Array | Yes | N/A | The buffer from which the data is read | | offset | number | Yes | N/A | An integer to specify the position within the buffer | | length | number | No | buffer.length - offset | An integer to specify how many bytes to read | | nullTerminated | boolean | No | false | Set true to stop reading when encountering zero |

return value

An array containing the following pair of values | Index | Type | Description | | ----- | ------ | ------------ | | [0] | number | An integer to indicate the position from which the next data should be read | | [1] | string | The read value |

reader.subBuffer(buffer, offset[, length])

Create a sub buffer from the original one.

params

| Name | Type | Required | Default | Description | | -------- | ------- | -------- | ------- | ------------- | | buffer | Buffer or Uint8Array | Yes | N/A | The buffer from which the sub buffer is extracted | | offset | number | Yes | N/A | An integer to specify the position within the original buffer | | length | number | No | buffer.length - offset | An integer to specify how many bytes to extract |

return value

The created sub buffer

reader.setOptions(obj)

Updates the option values

params

| Name | Type | Required | Default | Description | | ------- | ------ | -------- | ------- | ------------- | | obj | Object | Yes | {} | An object holding option values which will be used to overwrite the internal option values. |

supported options

| Name | Type | Default | Description | | ---------- | ------- | ------- | ------------- | | strictMode | boolean | false | If true, the function throws an error when the method invocations failed. If false, the function just logs the error and continues to run.|

reader.getOptions()

Retrieves the current option values

return value

A cloned object containing the current option values

writer.writeNumber(value, buffer, offset[, length])

Write an integer to the buffer

params

| Name | Type | Required | Default | Description | | -------- | ------- | -------- | ------- | ------------- | | value | number | Yes | N/A | The value to be written to the buffer | | buffer | Buffer or Uint8Array | Yes | N/A | The buffer to which the data is written | | offset | number | Yes | N/A | An integer to specify the position within the buffer | | length | number | No | 4 | An integer to specify how many bytes to write |

return value

An integer to indicate the position to which the next data should be written

writer.writeString(value, buffer, offset[, length])

Write a string to the buffer

params

| Name | Type | Required | Default | Description | | -------- | ------- | -------- | ------- | ------------- | | value | string | Yes | N/A | The value to be written to the buffer | | buffer | Buffer or Uint8Array | Yes | N/A | The buffer to which the data is written | | offset | number | Yes | N/A | An integer to specify the position within the buffer | | length | number | No | undefined | An integer to specify how many bytes to write. If not specified, the data is written until the end of the buffer |

return value

An integer to indicate the position to which the next data should be written

writer.copyBuffer(src, srcOffset, dst, dstOffset[, length])

Copy data from the src buffer to dst buffer

params

| Name | Type | Required | Default | Description | | ----------- | ------- | -------- | ------- | ------------- | | src | Buffer or Uint8Array | Yes | N/A | The source buffer from which the data is copied | | srcOffset | number | Yes | N/A | An integer to specify the position within the source buffer | | dst | Buffer or Uint8Array | Yes | N/A | The destination buffer to which the data is copied | | dstOffset | number | Yes | N/A | An integer to specify the position within the destination buffer | | length | number | No | src.length - srcOffset | An integer to specify how many bytes to copy. |

return value

An integer to indicate the position within the dst buffer to which the next data should be written