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

byte-matcher

v0.2.0

Published

Match bytes in a buffer returning and array of objects containing the start and end indices of the matches

Downloads

5

Readme

Byte-matcher

A simple module/function for finding binary matches in buffers. This function takes a buffer and a target buffer (or string converted to buffer) to match within the first buffer. Once all the bytes in the buffer have been interated an array of objects is returned containing a start index, end and/or a cursor value.

Install:

$ (sudo) npm install byte-matcher

Usage:

var byteMatcher = require('byte-matcher')
	, bufferToSearch = new Buffer('to find the thing')
	, bufferToMatch = new Buffer('target to match')

var matches = byteMatcher(buffertoSearch, bufferToMatch [, startingBufferIndex])

The following attributes will only be present in the object if they have been established.

matches[0].start {number|undefined}
matches[0].end {number|undefined}
matches[0].cursor {number|undefined}

At this point I must point out the end value is actually the next index after the last index of that match - there is a good reason for this Say that you would like to slice a buffer:

var b = new Buffer('hello world!')
// in order to get 'hello'
// you must slice with the end index at the next byte
var hello = b.slice(0, 5)
console.log(hello.toString()) // === 'hello' even tho
console.log(new Buffer([hello[4]]).toString()) // === 'o'

so if you want the actual last index matched byte you will need to do matches[0].end - 1.

So if a match has been found and fully matched then it will have a start and an end. If a match has begun and the buffer has run out (suggesting the match could be split over multiple buffers) then the last match object in the array will have a start index and cursor signifying the next index of the target buffer, is intended to be used to check in a subsequent buffer.

The third argument will take a starting index (or cursor value) which the matching buffer can continue from.

This will yield a match with no start index value, and either an end index if the match is complete or a cursor value for the next buffer match to continue from.

This makes it particularly easy for juggling matches over a buffer boundaries because we will know that we have have started the match in this buffer if the match object has a startIndex and that the match is either complete with an end index or in progress with a cursor value.

If we have a match at the end of one buffer and then it is not fulfilled in a continuation it is safe to say that this was never matched.

Managing these indices is a little tricky but give a lot of power, this is intented to have other more user friendly modules built on top of it, such as binary matching streams.