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

node-memcache-parser-obvfork

v0.1.1

Published

memcached binary protocol parser

Downloads

7

Readme

node-memcache-parser

A low level binary protocol parser for memcache.

About

Options/Properties

chunked

true or false. Default is true.

If true, body of messages will be made available through the onBody callback and will not be allocated on the message object received in the onMessage event. This allows the callee complete control over the bodies of message and keeps allocations at a minimum.

If false, the body will be allocated on the message object as it is parsed. It will be a string if encoding is set to UTF8 or ASCII. it will be an array of Buffer objects if encoding is set to BINARY

encoding

"encodings": {
	"BINARY": 0,
	"ASCII": 1,
	"UTF8": 2
}

Default is BINARY.

Only applies if chunked is false. When chunked is true, only raw buffers will be made available through the onData event. When chunked is false, encoding will determine whether the body is stored as a utf8/ascii encoded string on the message object or as an array of binary buffers.

Methods

new Parser(options)

options

You can set encoding and chunked by passing the constructor an options object as follows:

{
	encoding: 0|1|2,
	chunked: true|false
}

execute(buffer, start, end)

Parse a buffer

buffer

a node.js Buffer object

start

where to start parsing in the buffer

end

where to finish parsing in the buffer

reset()

Reset the state of the parser

Callbacks

onHeader(message)

fired when a header (first 24 bytes of message) has been parsed. more info on header formats here.

At this stage, only the header on the message object will be populated

onMessage(message)

fired when a message has been completely parsed. if the message has a body and chunked = false, you will now have the body, extras and key available on the message

onBody(buffer, start, end)

fired for each chunk of data being sent to the parser for the current body. this callback will only be fired if chunked = true.

onError(err)

Message format

{
	[m]header: {
		magic: int,
		opcode: int,
		keylen: int16,
		exlen: int,
		datatype: int,
		status/reserved: int16,
		totlen: int32,
		opaque: int32,
		cashi: int32,
		caslo: int32,
		bodylen: int32
	},
	[o]key: string,
	[o]extra: {},
	[o]body: string
}

Example

var memc = require("../lib/parser");

...

	var current = null;
	
	var parser = new memc.parser({
		"chunked": true,
		"encoding": memc.constants.encodings.BINARY
	});
	
	parser.onMessage = function(message) {
		// Will fire when a message has completed fully (i.e. body has been fully parsed), 
		// even if the message is only a header with no body. This means for a message 
		// that is only a header, you will get it in th onHeader and the onMessage callbacks
		sys.puts("message\n" + JSON.stringify(message, null, "\t"));
		switch(message.header.opcode) {
			case memc.constants.opcodes.SET:
				break;
			case memc.constants.opcodes.GET:
				break;
			case memc.constants.opcodes.QUIT:
				break;
		}
	};

	parser.onHeader = function(message) {
		// Will fire after the header (first 24 bytes) of a message has been parsed.
		sys.puts("header\n" + JSON.stringify(message, null, "\t"));
		switch(message.header.opcode) {
			case memc.constants.opcodes.SET:
				break;
			case memc.constants.opcodes.GET:
				break;
			case memc.constants.opcodes.QUIT:
				break;
		}
		if(message.header.bodylen > 0) {
			if(parser.chunked) {
				// save a pointer to the message in current so we can access it the onBody callback 
				// as we will not get anything in the body when onMessage fires while in chunked mode
				current = message;
				current.body = [];
			}
			else {
				// we will get the body on the message returned in the onMessage callback
			}
		}
	};

	parser.onBody = function(buffer, start, end) {
		// this will only fire if chunked is set to true. the parser will not set the body of 
		// the message and will just forward on the chunks of the body in this callback. 
		// NOTE: the parser does not use Buffer.slice(), it is giving you the actual buffer which was passed into it.
		sys.puts("chunk: " + (end-start));
		current.body.push(buffer.slice(start, end));
	};

	parser.onError = function(err) {
		sys.puts("error\n" + JSON.stringify(err, null, "\t"));
	};
	
...

	stream.ondata = function (buffer, start, end) {
		parser.execute(buffer, start, end);
	};

Dependencies

For the test script in examples/test.js you need to copy binary.node from this project:

to the lib directory so that the binary messages for sending to memcached can be created.