node-memcache-parser

memcached binary protocol parser and encoder

  • 所有者: billywhizz/node-memcache-parser
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

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.

主要指標

概覽
名稱與所有者billywhizz/node-memcache-parser
主編程語言JavaScript
編程語言Perl (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2010-10-15 07:24:27
推送於2013-08-13 01:45:07
最后一次提交2012-05-29 04:24:31
發布數8
最新版本名稱v0.1.1 (發布於 )
第一版名稱v0.0.1 (發布於 2010-10-16 05:56:47)
用户参与
星數22
關注者數3
派生數6
提交數24
已啟用問題?
問題數1
打開的問題數0
拉請求數0
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?