Registry / database / memcache-parser

memcache-parser

JSON →
library1.0.1jsnpmunverified

memcache-parser is a highly efficient Node.js library specifically designed to parse the ASCII protocol used by Memcached. It leverages Node.js Buffer APIs extensively for optimized performance in parsing incoming data streams. The current stable version is 1.0.1, which was published approximately six years ago, indicating that the package is likely unmaintained. This parser is intended to be extended by developers implementing a Memcached client, requiring them to define how specific commands (like `VALUE` for data retrieval) are handled and how the parsed results are received. Its key differentiator is its low-level Buffer-based parsing for maximum efficiency, but its age means potential compatibility issues with modern Node.js versions or lack of security updates.

npm install memcache-parser
INSTALL
IMPORT
SIG · MEMCACHE-PARSER
M
memcache-parser
databasejavascriptv1.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MemcacheParser
const MemcacheParser = require('memcache-parser');
Primary usage pattern for Node.js versions targeted by this package (>=4).
MemcacheParser
import MemcacheParser from 'memcache-parser';
import { MemcacheParser } from 'memcache-parser';
This package likely uses `module.exports = MemcacheParser;` for CommonJS, which translates to a default import in ESM. Direct ESM support is not guaranteed and may require bundler configuration or Node.js `--experimental-modules` flags for older Node.js versions.
MemcacheParser
import type { MemcacheParser } from 'memcache-parser';
For TypeScript projects, import the type definition for extending or instantiating the parser. The package ships with its own `.d.ts` files.

This example demonstrates how to create a custom Memcached client by extending `MemcacheParser`. It shows the essential `processCmd` method for initiating data block parsing for 'VALUE' commands and the `receiveResult` method for handling fully parsed responses, simulating data flow from a mock socket.

const MemcacheParser = require('memcache-parser'); const { Readable } = require('stream'); // MockSocket simulates a network socket emitting data buffers. class MockSocket extends Readable { _read() {} send(data) { this.emit('data', Buffer.from(data)); } } class CustomMemcacheClient extends MemcacheParser { constructor(socket) { super(); this.socket = socket; this.parsedResults = []; socket.on('data', this.onData.bind(this)); socket.on('error', (err) => console.error('Socket error:', err)); } // processCmd is called by the parser when a command line is received. // If it returns true, it signifies that this command has a pending data block // which the user's receiveResult method will handle after the parser consumes it. // If false, the parser treats it as a simple line response. processCmd(cmdTokens) { const command = cmdTokens[0]; // Handle 'VALUE' which has a data block followed by 'END' if (command === 'VALUE') { const bytes = parseInt(cmdTokens[3], 10); this.initiatePending(cmdTokens, bytes); return true; // We expect a data block } else if (command === 'VERSION' || command === 'STAT' || command === 'ERROR' || command === 'END') { // These commands are typically simple line responses without separate data blocks. return false; // Let the parser handle as a simple line } else { console.warn('Unhandled Memcached command:', command, cmdTokens); return false; } } // receiveResult is called by the parser when a complete command result (and its data block, if any) // has been successfully parsed. receiveResult(result) { // result: { data: Buffer|string, cmd: string, cmdTokens: string[] } this.parsedResults.push(result); console.log(`Received: Cmd='${result.cmd}', Tokens=[${result.cmdTokens.join(', ')}], Data='${result.data ? result.data.toString().trim() : ''}'`); } } // --- Example Usage --- const mockSocket = new MockSocket(); const client = new CustomMemcacheClient(mockSocket); // Simulate Memcached responses mockSocket.send('VALUE mykey 0 11\r\nHello World\r\nEND\r\n'); mockSocket.send('VERSION 1.6.9\r\n'); mockSocket.send('STAT items:0:number 0\r\nSTAT active_slabs 0\r\nEND\r\n'); mockSocket.send('ERROR\r\n'); // Wait a moment for async parsing and log collected results setTimeout(() => { console.log('\n--- All Parsed Results ---'); client.parsedResults.forEach(r => { console.log(`Cmd: ${r.cmd}, Tokens: [${r.cmdTokens.join(', ')}], Data: ${r.data ? r.data.toString().trim() : 'N/A'}`); }); }, 100);
Debug
Known issues
breakingThe package is explicitly designed for Node.js >=4. Using it with significantly newer Node.js versions (e.g., Node.js 14+) might expose subtle incompatibilities with Buffer API changes, stream behavior, or internal Node.js mechanisms not present or different in legacy versions.
fix
Thoroughly test with your target Node.js version. Consider alternatives if you encounter unexpected behavior or performance regressions on modern Node.js environments.
affects: All versions
gotchaThis parser is designed exclusively for the Memcached ASCII protocol. It will not function correctly with the Memcached Binary protocol, which uses a different message structure and data encoding.
fix
Ensure your Memcached server or client is configured to use the ASCII protocol, or choose a different parser/library if you require binary protocol support.
affects: All versions
gotchaThe package appears to be unmaintained, with its last release over six years ago. This means there will be no future bug fixes, performance improvements, or security updates. Relying on it for new projects in production environments introduces significant risk.
fix
Evaluate newer, actively maintained Memcached client libraries or protocol parsers. If using this package, be prepared to fork and maintain it yourself for critical bug fixes or security patches.
affects: All versions
gotchaThe `processCmd` method is critical for correct parsing. If a command expects a data block (like `VALUE`), you must call `this.initiatePending(cmdTokens, byteCount)` and return `true`. Failing to do so will lead to incorrect parsing or data corruption.
fix
Carefully implement `processCmd` to correctly identify commands with data blocks and initiate pending data parsing. Refer to the Memcached ASCII protocol specification for command details.
affects: All versions
Errors
Common errors & fixes
TypeError: this.onData is not a function
The `onData` method from `MemcacheParser` is expected to be bound to the instance where `data` events are received. If `socket.on("data", this.onData)` is called without `bind(this)`, `onData` loses its `this` context.
fix
Ensure `this.onData` is correctly bound to the instance, e.g., `socket.on("data", this.onData.bind(this));` or use an arrow function: `socket.on("data", (chunk) => this.onData(chunk));`.
Error: Cannot find module 'memcache-parser'
The package has not been installed or is not resolvable in your project's `node_modules`.
fix
Run `npm install memcache-parser` or `yarn add memcache-parser` in your project directory.
No data received in receiveResult for commands like 'VALUE'
The `processCmd` method for the `VALUE` command did not correctly call `this.initiatePending(cmdTokens, byteCount)` or returned `false`, causing the parser to not expect the subsequent data block.
fix
Verify that your `processCmd` implementation for commands like `VALUE` accurately extracts the byte count and correctly calls `this.initiatePending(cmdTokens, byteCount)` before returning `true`.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
memcache-parser — npm install memcache-parser · libregistry