{"library":"stream-parser","title":"Stream Parser Mixin for Node.js Streams","type":"library","description":"This package, `stream-parser` (version 0.3.1), provides a generic \"parser\" mixin designed for Node.js `Writable` and `Transform` stream instances and subclasses. It offers a convenient API for byte-level parsing, allowing developers to implement streaming parsers for various binary file formats or network protocols. Key methods include `_bytes(n, cb)` to buffer a specified number of bytes, `_skipBytes(n, cb)` to discard bytes, and `_passthrough(n, cb)` to pass bytes through directly to the readable side of a `Transform` stream. The library was last updated over a decade ago in June 2015, indicating it is no longer actively maintained. Its primary differentiator was providing a low-level, interruptible parsing mechanism by taking control over stream's `_write` or `_transform` callbacks, which was a common pattern in older Node.js stream implementations.","language":"javascript","status":"abandoned","last_verified":"Sun Apr 19","install":{"commands":["npm install stream-parser"],"cli":null},"imports":["const Parser = require('stream-parser');"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/TooTallNate/node-stream-parser","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/stream-parser","openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"const Parser = require('stream-parser');\nconst inherits = require('util').inherits;\nconst { Transform } = require('stream');\n\n// Create a Transform stream subclass that utilizes stream-parser\nfunction MyParser () {\n  Transform.call(this);\n\n  // Buffer the first 8 bytes written, then call onheader\n  this._bytes(8, this.onheader);\n}\ninherits(MyParser, Transform);\n\n// Mixin stream-parser into MyParser's `prototype`\nParser(MyParser.prototype);\n\n// Invoked when the first 8 bytes have been received\nMyParser.prototype.onheader = function (buffer) {\n  // Parse the 'buffer' into a useful 'header' object\n  const header = {};\n  // Assuming a theoretical 4-byte type and 4-byte UTF-8 name\n  header.type = buffer.readUInt32LE(0);\n  header.name = buffer.toString('utf8', 4, 8);\n  this.emit('header', header);\n\n  // After parsing the header, pass through the rest of the data indefinitely\n  this._passthrough(Infinity);\n};\n\n// Now we can *use* it!\nconst parser = new MyParser();\nparser.on('header', (header) => {\n  console.log('Got header:', header);\n});\n\n// Example usage: pipe some data to the parser\nconst data = Buffer.from('01000000TESTHEADER' + 'This is the rest of the stream content.', 'latin1');\nconst input = new Transform();\ninput._read = () => {}; // Implement a dummy _read method for the input stream\ninput.push(data);\ninput.push(null); // End the input stream\n\ninput.pipe(parser).pipe(process.stdout);\n// Expected output: 'Got header: { type: 1, name: 'TEST' }' (to console.log) and 'HEADERThis is the rest of the stream content.' (to stdout)","lang":"javascript","description":"Demonstrates creating a custom Transform stream subclass, mixing in `stream-parser`, parsing a fixed-size header with `_bytes`, emitting an event, and then passing through the remaining stream content with `_passthrough`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}