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
muslnode 18–226 runs
build_error
glibcnode 18–226 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);
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.
fixEnsure `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`.
fixRun `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.
fixVerify that your `processCmd` implementation for commands like `VALUE` accurately extracts the byte count and correctly calls `this.initiatePending(cmdTokens, byteCount)` before returning `true`.
Audit
Dependencies
No dependency data recorded yet.