Gathering detailed insights and metrics for memcached-stream
Gathering detailed insights and metrics for memcached-stream
Gathering detailed insights and metrics for memcached-stream
Gathering detailed insights and metrics for memcached-stream
High performance streaming memcached protocol parser for Node.js. Code name: flaming-octo-bear
npm install memcached-stream
Typescript
Module System
NPM Version
JavaScript (99.83%)
Shell (0.17%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
6 Stars
78 Commits
3 Forks
4 Watchers
4 Branches
2 Contributors
Updated on Aug 28, 2017
Latest Version
0.0.2
Package Id
memcached-stream@0.0.2
Size
20.09 kB
NPM Version
1.2.14
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Memcached-stream is a streaming Memcached ASCII protocol parser for Node.js. The module is build with performance in mind and features an extensive micro benchmark suite that was used to research the best way to parse the stream of incoming data. My work on an Memcached ASCII parser originally started out for my node-memcached module. I decided to extract it out so it can be reused by different servers that are now using the Memcached protocol to communicate such as:
The parser has been build on top of the Node.js stream interface so it can take
advantage of the Stream#pipe
method to work it's parsing magic. The parser
assumes that you have set the encoding of the connection to UTF-8 using the
Stream#setEncoding
method, this ensures that we will not destroy multi-byte
strings and that your data is intact.
The reason that I have chosen to support the ASCII protocol is that its easier to debug. This might sound silly to you, but being able to see what is actually being send over the network in a human readable format is priceless when you have to debug something in production.
Install the module using the Node Package Manager (NPM):
npm install memcached-stream --save
The --save
flag tells NPM to automatically add the package to your
package.json
.
All the examples assume the following boot strapped code:
1var Parser = require('memcached-stream');
And we assume that the stream
variable is a valid TCP connection that already
had setEncoding('utf-8')
applied to it so it will not terminate UTF-8 chars.
The parser inherits from the Node.js Stream interface. This allows you to easily
attach the parser to a connection by using the Stream#pipe
method which is
available on every Stream interface in node. The best thing about using the pipe
method is that it takes care of all the flow control for us.
1var parser = new Parser(); 2 3stream.pipe(parser);
Memcached allows you to store 16 / 32 bits unsigned integers as flags when you store your response. This is mostly used to indicate what kind of data is actually stored.
The flag function takes 2 arguments:
Please note that this is a sync call.
1// a JSON parser for when the 1 flag is used 2parser.flag(1, function parse(str, buffer) { 3 return JSON.parse(str); 4});
The parser emit's a couple of events that you should be listening on:
The first 2 arguments of this are the most important. The first argument
command
is the response command that was returned from the server. It would be
VALUE, END, OK, NOT_STORED etc. The second argument is the value of the
response. This is the same for every response. Most responses will be a
Boolean
value. This will indicate if the command indicates success or failure.
Non boolean
responses should probably be queued until you receive an END
command. This only applies for VALUE, STAT and KEY. These commands also receive
a couple of extra arguments.
The KEY response here is the odd ball, where it's value is key. Please note that the KEY response isn't offically support by memcached.
1parser.on('response', function response(command, ..args) { 2 // command is the response type, VALUE, END, STORED etc. 3});
The error response is still a response from the server, this is a sepeare event
as it will recieve an Error argument. This error argument you can easily pass to
your callback functions. The error:response
is only called for known error
responses from a memcached server such as ERROR, CLIENT_ERROR and SERVER_ERROR.
1parser.on('error:response', function error(err) { 2 // err.code is the actual response type 3});
In addition to these events, we also have an error
event that gets emitted
when we receive an unknown response. When this happens the parser is destroyed
immediately as we have no idea in what state our parser is in.
1parser.on('error', function error(err) { 2 // optionally you can check the cause of the error, if it's due to a parser 3 // failure it will have a `code` property 4 parser.destroy(); 5 6 // rebuild the parser and pipe it the connection again 7});
To make it possible to re-use parsers, theres a reset method that will reset the internals back to the same state as it as when it was freshly initialized. Please note that the reset method does not remove the assigned event listeners.
1// reset internals 2parser.reset(); 3 4// also nuke the eventListeners 5parser.removeAllListeners();
Once you are done with parsing you can terminate it by calling:
1parser.end();
Or you can completely destroy the parser by calling:
1parser.destroy();
Please see the CONTRIBUTING.md
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 3/29 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More