{"library":"memcache-parser","title":"Memcache ASCII Protocol Parser","description":"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.","language":"javascript","status":"abandoned","last_verified":"Sun Apr 19","install":{"commands":["npm install memcache-parser"],"cli":null},"imports":["const MemcacheParser = require('memcache-parser');","import MemcacheParser from 'memcache-parser';","import type { MemcacheParser } from 'memcache-parser';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const MemcacheParser = require('memcache-parser');\nconst { Readable } = require('stream');\n\n// MockSocket simulates a network socket emitting data buffers.\nclass MockSocket extends Readable {\n  _read() {}\n  send(data) {\n    this.emit('data', Buffer.from(data));\n  }\n}\n\nclass CustomMemcacheClient extends MemcacheParser {\n  constructor(socket) {\n    super();\n    this.socket = socket;\n    this.parsedResults = [];\n    socket.on('data', this.onData.bind(this));\n    socket.on('error', (err) => console.error('Socket error:', err));\n  }\n\n  // processCmd is called by the parser when a command line is received.\n  // If it returns true, it signifies that this command has a pending data block\n  // which the user's receiveResult method will handle after the parser consumes it.\n  // If false, the parser treats it as a simple line response.\n  processCmd(cmdTokens) {\n    const command = cmdTokens[0];\n    // Handle 'VALUE' which has a data block followed by 'END'\n    if (command === 'VALUE') {\n      const bytes = parseInt(cmdTokens[3], 10);\n      this.initiatePending(cmdTokens, bytes);\n      return true; // We expect a data block\n    } else if (command === 'VERSION' || command === 'STAT' || command === 'ERROR' || command === 'END') {\n      // These commands are typically simple line responses without separate data blocks.\n      return false; // Let the parser handle as a simple line\n    } else {\n      console.warn('Unhandled Memcached command:', command, cmdTokens);\n      return false;\n    }\n  }\n\n  // receiveResult is called by the parser when a complete command result (and its data block, if any)\n  // has been successfully parsed.\n  receiveResult(result) {\n    // result: { data: Buffer|string, cmd: string, cmdTokens: string[] }\n    this.parsedResults.push(result);\n    console.log(`Received: Cmd='${result.cmd}', Tokens=[${result.cmdTokens.join(', ')}], Data='${result.data ? result.data.toString().trim() : ''}'`);\n  }\n}\n\n// --- Example Usage ---\nconst mockSocket = new MockSocket();\nconst client = new CustomMemcacheClient(mockSocket);\n\n// Simulate Memcached responses\nmockSocket.send('VALUE mykey 0 11\\r\\nHello World\\r\\nEND\\r\\n');\nmockSocket.send('VERSION 1.6.9\\r\\n');\nmockSocket.send('STAT items:0:number 0\\r\\nSTAT active_slabs 0\\r\\nEND\\r\\n');\nmockSocket.send('ERROR\\r\\n');\n\n// Wait a moment for async parsing and log collected results\nsetTimeout(() => {\n  console.log('\\n--- All Parsed Results ---');\n  client.parsedResults.forEach(r => {\n    console.log(`Cmd: ${r.cmd}, Tokens: [${r.cmdTokens.join(', ')}], Data: ${r.data ? r.data.toString().trim() : 'N/A'}`);\n  });\n}, 100);","lang":"javascript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}